Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ===========================================
0004 Userspace block device driver (ublk driver)
0005 ===========================================
0006 
0007 Overview
0008 ========
0009 
0010 ublk is a generic framework for implementing block device logic from userspace.
0011 The motivation behind it is that moving virtual block drivers into userspace,
0012 such as loop, nbd and similar can be very helpful. It can help to implement
0013 new virtual block device such as ublk-qcow2 (there are several attempts of
0014 implementing qcow2 driver in kernel).
0015 
0016 Userspace block devices are attractive because:
0017 
0018 - They can be written many programming languages.
0019 - They can use libraries that are not available in the kernel.
0020 - They can be debugged with tools familiar to application developers.
0021 - Crashes do not kernel panic the machine.
0022 - Bugs are likely to have a lower security impact than bugs in kernel
0023   code.
0024 - They can be installed and updated independently of the kernel.
0025 - They can be used to simulate block device easily with user specified
0026   parameters/setting for test/debug purpose
0027 
0028 ublk block device (``/dev/ublkb*``) is added by ublk driver. Any IO request
0029 on the device will be forwarded to ublk userspace program. For convenience,
0030 in this document, ``ublk server`` refers to generic ublk userspace
0031 program. ``ublksrv`` [#userspace]_ is one of such implementation. It
0032 provides ``libublksrv`` [#userspace_lib]_ library for developing specific
0033 user block device conveniently, while also generic type block device is
0034 included, such as loop and null. Richard W.M. Jones wrote userspace nbd device
0035 ``nbdublk`` [#userspace_nbdublk]_  based on ``libublksrv`` [#userspace_lib]_.
0036 
0037 After the IO is handled by userspace, the result is committed back to the
0038 driver, thus completing the request cycle. This way, any specific IO handling
0039 logic is totally done by userspace, such as loop's IO handling, NBD's IO
0040 communication, or qcow2's IO mapping.
0041 
0042 ``/dev/ublkb*`` is driven by blk-mq request-based driver. Each request is
0043 assigned by one queue wide unique tag. ublk server assigns unique tag to each
0044 IO too, which is 1:1 mapped with IO of ``/dev/ublkb*``.
0045 
0046 Both the IO request forward and IO handling result committing are done via
0047 ``io_uring`` passthrough command; that is why ublk is also one io_uring based
0048 block driver. It has been observed that using io_uring passthrough command can
0049 give better IOPS than block IO; which is why ublk is one of high performance
0050 implementation of userspace block device: not only IO request communication is
0051 done by io_uring, but also the preferred IO handling in ublk server is io_uring
0052 based approach too.
0053 
0054 ublk provides control interface to set/get ublk block device parameters.
0055 The interface is extendable and kabi compatible: basically any ublk request
0056 queue's parameter or ublk generic feature parameters can be set/get via the
0057 interface. Thus, ublk is generic userspace block device framework.
0058 For example, it is easy to setup a ublk device with specified block
0059 parameters from userspace.
0060 
0061 Using ublk
0062 ==========
0063 
0064 ublk requires userspace ublk server to handle real block device logic.
0065 
0066 Below is example of using ``ublksrv`` to provide ublk-based loop device.
0067 
0068 - add a device::
0069 
0070      ublk add -t loop -f ublk-loop.img
0071 
0072 - format with xfs, then use it::
0073 
0074      mkfs.xfs /dev/ublkb0
0075      mount /dev/ublkb0 /mnt
0076      # do anything. all IOs are handled by io_uring
0077      ...
0078      umount /mnt
0079 
0080 - list the devices with their info::
0081 
0082      ublk list
0083 
0084 - delete the device::
0085 
0086      ublk del -a
0087      ublk del -n $ublk_dev_id
0088 
0089 See usage details in README of ``ublksrv`` [#userspace_readme]_.
0090 
0091 Design
0092 ======
0093 
0094 Control plane
0095 -------------
0096 
0097 ublk driver provides global misc device node (``/dev/ublk-control``) for
0098 managing and controlling ublk devices with help of several control commands:
0099 
0100 - ``UBLK_CMD_ADD_DEV``
0101 
0102   Add a ublk char device (``/dev/ublkc*``) which is talked with ublk server
0103   WRT IO command communication. Basic device info is sent together with this
0104   command. It sets UAPI structure of ``ublksrv_ctrl_dev_info``,
0105   such as ``nr_hw_queues``, ``queue_depth``, and max IO request buffer size,
0106   for which the info is negotiated with the driver and sent back to the server.
0107   When this command is completed, the basic device info is immutable.
0108 
0109 - ``UBLK_CMD_SET_PARAMS`` / ``UBLK_CMD_GET_PARAMS``
0110 
0111   Set or get parameters of the device, which can be either generic feature
0112   related, or request queue limit related, but can't be IO logic specific,
0113   because the driver does not handle any IO logic. This command has to be
0114   sent before sending ``UBLK_CMD_START_DEV``.
0115 
0116 - ``UBLK_CMD_START_DEV``
0117 
0118   After the server prepares userspace resources (such as creating per-queue
0119   pthread & io_uring for handling ublk IO), this command is sent to the
0120   driver for allocating & exposing ``/dev/ublkb*``. Parameters set via
0121   ``UBLK_CMD_SET_PARAMS`` are applied for creating the device.
0122 
0123 - ``UBLK_CMD_STOP_DEV``
0124 
0125   Halt IO on ``/dev/ublkb*`` and remove the device. When this command returns,
0126   ublk server will release resources (such as destroying per-queue pthread &
0127   io_uring).
0128 
0129 - ``UBLK_CMD_DEL_DEV``
0130 
0131   Remove ``/dev/ublkc*``. When this command returns, the allocated ublk device
0132   number can be reused.
0133 
0134 - ``UBLK_CMD_GET_QUEUE_AFFINITY``
0135 
0136   When ``/dev/ublkc`` is added, the driver creates block layer tagset, so
0137   that each queue's affinity info is available. The server sends
0138   ``UBLK_CMD_GET_QUEUE_AFFINITY`` to retrieve queue affinity info. It can
0139   set up the per-queue context efficiently, such as bind affine CPUs with IO
0140   pthread and try to allocate buffers in IO thread context.
0141 
0142 - ``UBLK_CMD_GET_DEV_INFO``
0143 
0144   For retrieving device info via ``ublksrv_ctrl_dev_info``. It is the server's
0145   responsibility to save IO target specific info in userspace.
0146 
0147 Data plane
0148 ----------
0149 
0150 ublk server needs to create per-queue IO pthread & io_uring for handling IO
0151 commands via io_uring passthrough. The per-queue IO pthread
0152 focuses on IO handling and shouldn't handle any control & management
0153 tasks.
0154 
0155 The's IO is assigned by a unique tag, which is 1:1 mapping with IO
0156 request of ``/dev/ublkb*``.
0157 
0158 UAPI structure of ``ublksrv_io_desc`` is defined for describing each IO from
0159 the driver. A fixed mmaped area (array) on ``/dev/ublkc*`` is provided for
0160 exporting IO info to the server; such as IO offset, length, OP/flags and
0161 buffer address. Each ``ublksrv_io_desc`` instance can be indexed via queue id
0162 and IO tag directly.
0163 
0164 The following IO commands are communicated via io_uring passthrough command,
0165 and each command is only for forwarding the IO and committing the result
0166 with specified IO tag in the command data:
0167 
0168 - ``UBLK_IO_FETCH_REQ``
0169 
0170   Sent from the server IO pthread for fetching future incoming IO requests
0171   destined to ``/dev/ublkb*``. This command is sent only once from the server
0172   IO pthread for ublk driver to setup IO forward environment.
0173 
0174 - ``UBLK_IO_COMMIT_AND_FETCH_REQ``
0175 
0176   When an IO request is destined to ``/dev/ublkb*``, the driver stores
0177   the IO's ``ublksrv_io_desc`` to the specified mapped area; then the
0178   previous received IO command of this IO tag (either ``UBLK_IO_FETCH_REQ``
0179   or ``UBLK_IO_COMMIT_AND_FETCH_REQ)`` is completed, so the server gets
0180   the IO notification via io_uring.
0181 
0182   After the server handles the IO, its result is committed back to the
0183   driver by sending ``UBLK_IO_COMMIT_AND_FETCH_REQ`` back. Once ublkdrv
0184   received this command, it parses the result and complete the request to
0185   ``/dev/ublkb*``. In the meantime setup environment for fetching future
0186   requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ``
0187   is reused for both fetching request and committing back IO result.
0188 
0189 - ``UBLK_IO_NEED_GET_DATA``
0190 
0191   With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly
0192   issued to ublk server without data copy. Then, IO backend of ublk server
0193   receives the request and it can allocate data buffer and embed its addr
0194   inside this new io command. After the kernel driver gets the command,
0195   data copy is done from request pages to this backend's buffer. Finally,
0196   backend receives the request again with data to be written and it can
0197   truly handle the request.
0198 
0199   ``UBLK_IO_NEED_GET_DATA`` adds one additional round-trip and one
0200   io_uring_enter() syscall. Any user thinks that it may lower performance
0201   should not enable UBLK_F_NEED_GET_DATA. ublk server pre-allocates IO
0202   buffer for each IO by default. Any new project should try to use this
0203   buffer to communicate with ublk driver. However, existing project may
0204   break or not able to consume the new buffer interface; that's why this
0205   command is added for backwards compatibility so that existing projects
0206   can still consume existing buffers.
0207 
0208 - data copy between ublk server IO buffer and ublk block IO request
0209 
0210   The driver needs to copy the block IO request pages into the server buffer
0211   (pages) first for WRITE before notifying the server of the coming IO, so
0212   that the server can handle WRITE request.
0213 
0214   When the server handles READ request and sends
0215   ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy
0216   the server buffer (pages) read to the IO request pages.
0217 
0218 Future development
0219 ==================
0220 
0221 Container-aware ublk deivice
0222 ----------------------------
0223 
0224 ublk driver doesn't handle any IO logic. Its function is well defined
0225 for now and very limited userspace interfaces are needed, which is also
0226 well defined too. It is possible to make ublk devices container-aware block
0227 devices in future as Stefan Hajnoczi suggested [#stefan]_, by removing
0228 ADMIN privilege.
0229 
0230 Zero copy
0231 ---------
0232 
0233 Zero copy is a generic requirement for nbd, fuse or similar drivers. A
0234 problem [#xiaoguang]_ Xiaoguang mentioned is that pages mapped to userspace
0235 can't be remapped any more in kernel with existing mm interfaces. This can
0236 occurs when destining direct IO to ``/dev/ublkb*``. Also, he reported that
0237 big requests (IO size >= 256 KB) may benefit a lot from zero copy.
0238 
0239 
0240 References
0241 ==========
0242 
0243 .. [#userspace] https://github.com/ming1/ubdsrv
0244 
0245 .. [#userspace_lib] https://github.com/ming1/ubdsrv/tree/master/lib
0246 
0247 .. [#userspace_nbdublk] https://gitlab.com/rwmjones/libnbd/-/tree/nbdublk
0248 
0249 .. [#userspace_readme] https://github.com/ming1/ubdsrv/blob/master/README
0250 
0251 .. [#stefan] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/
0252 
0253 .. [#xiaoguang] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/