xref: /openbmc/linux/Documentation/block/ublk.rst (revision 4093cb5a06343ea3936ae46664d132c82576b153)
17a3d2225SMing Lei.. SPDX-License-Identifier: GPL-2.0
27a3d2225SMing Lei
37a3d2225SMing Lei===========================================
47a3d2225SMing LeiUserspace block device driver (ublk driver)
57a3d2225SMing Lei===========================================
67a3d2225SMing Lei
77a3d2225SMing LeiOverview
87a3d2225SMing Lei========
97a3d2225SMing Lei
107a3d2225SMing Leiublk is a generic framework for implementing block device logic from userspace.
117a3d2225SMing LeiThe motivation behind it is that moving virtual block drivers into userspace,
127a3d2225SMing Leisuch as loop, nbd and similar can be very helpful. It can help to implement
137a3d2225SMing Leinew virtual block device such as ublk-qcow2 (there are several attempts of
147a3d2225SMing Leiimplementing qcow2 driver in kernel).
157a3d2225SMing Lei
167a3d2225SMing LeiUserspace block devices are attractive because:
177a3d2225SMing Lei
187a3d2225SMing Lei- They can be written many programming languages.
197a3d2225SMing Lei- They can use libraries that are not available in the kernel.
207a3d2225SMing Lei- They can be debugged with tools familiar to application developers.
217a3d2225SMing Lei- Crashes do not kernel panic the machine.
227a3d2225SMing Lei- Bugs are likely to have a lower security impact than bugs in kernel
237a3d2225SMing Lei  code.
247a3d2225SMing Lei- They can be installed and updated independently of the kernel.
257a3d2225SMing Lei- They can be used to simulate block device easily with user specified
267a3d2225SMing Lei  parameters/setting for test/debug purpose
277a3d2225SMing Lei
287a3d2225SMing Leiublk block device (``/dev/ublkb*``) is added by ublk driver. Any IO request
297a3d2225SMing Leion the device will be forwarded to ublk userspace program. For convenience,
307a3d2225SMing Leiin this document, ``ublk server`` refers to generic ublk userspace
317a3d2225SMing Leiprogram. ``ublksrv`` [#userspace]_ is one of such implementation. It
327a3d2225SMing Leiprovides ``libublksrv`` [#userspace_lib]_ library for developing specific
337a3d2225SMing Leiuser block device conveniently, while also generic type block device is
347a3d2225SMing Leiincluded, such as loop and null. Richard W.M. Jones wrote userspace nbd device
357a3d2225SMing Lei``nbdublk`` [#userspace_nbdublk]_  based on ``libublksrv`` [#userspace_lib]_.
367a3d2225SMing Lei
377a3d2225SMing LeiAfter the IO is handled by userspace, the result is committed back to the
387a3d2225SMing Leidriver, thus completing the request cycle. This way, any specific IO handling
397a3d2225SMing Leilogic is totally done by userspace, such as loop's IO handling, NBD's IO
407a3d2225SMing Leicommunication, or qcow2's IO mapping.
417a3d2225SMing Lei
427a3d2225SMing Lei``/dev/ublkb*`` is driven by blk-mq request-based driver. Each request is
437a3d2225SMing Leiassigned by one queue wide unique tag. ublk server assigns unique tag to each
447a3d2225SMing LeiIO too, which is 1:1 mapped with IO of ``/dev/ublkb*``.
457a3d2225SMing Lei
467a3d2225SMing LeiBoth the IO request forward and IO handling result committing are done via
477a3d2225SMing Lei``io_uring`` passthrough command; that is why ublk is also one io_uring based
487a3d2225SMing Leiblock driver. It has been observed that using io_uring passthrough command can
497a3d2225SMing Leigive better IOPS than block IO; which is why ublk is one of high performance
507a3d2225SMing Leiimplementation of userspace block device: not only IO request communication is
517a3d2225SMing Leidone by io_uring, but also the preferred IO handling in ublk server is io_uring
527a3d2225SMing Leibased approach too.
537a3d2225SMing Lei
547a3d2225SMing Leiublk provides control interface to set/get ublk block device parameters.
557a3d2225SMing LeiThe interface is extendable and kabi compatible: basically any ublk request
567a3d2225SMing Leiqueue's parameter or ublk generic feature parameters can be set/get via the
577a3d2225SMing Leiinterface. Thus, ublk is generic userspace block device framework.
587a3d2225SMing LeiFor example, it is easy to setup a ublk device with specified block
597a3d2225SMing Leiparameters from userspace.
607a3d2225SMing Lei
617a3d2225SMing LeiUsing ublk
627a3d2225SMing Lei==========
637a3d2225SMing Lei
647a3d2225SMing Leiublk requires userspace ublk server to handle real block device logic.
657a3d2225SMing Lei
667a3d2225SMing LeiBelow is example of using ``ublksrv`` to provide ublk-based loop device.
677a3d2225SMing Lei
687a3d2225SMing Lei- add a device::
697a3d2225SMing Lei
707a3d2225SMing Lei     ublk add -t loop -f ublk-loop.img
717a3d2225SMing Lei
727a3d2225SMing Lei- format with xfs, then use it::
737a3d2225SMing Lei
747a3d2225SMing Lei     mkfs.xfs /dev/ublkb0
757a3d2225SMing Lei     mount /dev/ublkb0 /mnt
767a3d2225SMing Lei     # do anything. all IOs are handled by io_uring
777a3d2225SMing Lei     ...
787a3d2225SMing Lei     umount /mnt
797a3d2225SMing Lei
807a3d2225SMing Lei- list the devices with their info::
817a3d2225SMing Lei
827a3d2225SMing Lei     ublk list
837a3d2225SMing Lei
847a3d2225SMing Lei- delete the device::
857a3d2225SMing Lei
867a3d2225SMing Lei     ublk del -a
877a3d2225SMing Lei     ublk del -n $ublk_dev_id
887a3d2225SMing Lei
897a3d2225SMing LeiSee usage details in README of ``ublksrv`` [#userspace_readme]_.
907a3d2225SMing Lei
917a3d2225SMing LeiDesign
927a3d2225SMing Lei======
937a3d2225SMing Lei
947a3d2225SMing LeiControl plane
957a3d2225SMing Lei-------------
967a3d2225SMing Lei
977a3d2225SMing Leiublk driver provides global misc device node (``/dev/ublk-control``) for
987a3d2225SMing Leimanaging and controlling ublk devices with help of several control commands:
997a3d2225SMing Lei
1007a3d2225SMing Lei- ``UBLK_CMD_ADD_DEV``
1017a3d2225SMing Lei
1027a3d2225SMing Lei  Add a ublk char device (``/dev/ublkc*``) which is talked with ublk server
1037a3d2225SMing Lei  WRT IO command communication. Basic device info is sent together with this
1047a3d2225SMing Lei  command. It sets UAPI structure of ``ublksrv_ctrl_dev_info``,
1057a3d2225SMing Lei  such as ``nr_hw_queues``, ``queue_depth``, and max IO request buffer size,
1067a3d2225SMing Lei  for which the info is negotiated with the driver and sent back to the server.
1077a3d2225SMing Lei  When this command is completed, the basic device info is immutable.
1087a3d2225SMing Lei
1097a3d2225SMing Lei- ``UBLK_CMD_SET_PARAMS`` / ``UBLK_CMD_GET_PARAMS``
1107a3d2225SMing Lei
1117a3d2225SMing Lei  Set or get parameters of the device, which can be either generic feature
1127a3d2225SMing Lei  related, or request queue limit related, but can't be IO logic specific,
1137a3d2225SMing Lei  because the driver does not handle any IO logic. This command has to be
1147a3d2225SMing Lei  sent before sending ``UBLK_CMD_START_DEV``.
1157a3d2225SMing Lei
1167a3d2225SMing Lei- ``UBLK_CMD_START_DEV``
1177a3d2225SMing Lei
1187a3d2225SMing Lei  After the server prepares userspace resources (such as creating per-queue
1197a3d2225SMing Lei  pthread & io_uring for handling ublk IO), this command is sent to the
1207a3d2225SMing Lei  driver for allocating & exposing ``/dev/ublkb*``. Parameters set via
1217a3d2225SMing Lei  ``UBLK_CMD_SET_PARAMS`` are applied for creating the device.
1227a3d2225SMing Lei
1237a3d2225SMing Lei- ``UBLK_CMD_STOP_DEV``
1247a3d2225SMing Lei
1257a3d2225SMing Lei  Halt IO on ``/dev/ublkb*`` and remove the device. When this command returns,
1267a3d2225SMing Lei  ublk server will release resources (such as destroying per-queue pthread &
1277a3d2225SMing Lei  io_uring).
1287a3d2225SMing Lei
1297a3d2225SMing Lei- ``UBLK_CMD_DEL_DEV``
1307a3d2225SMing Lei
1317a3d2225SMing Lei  Remove ``/dev/ublkc*``. When this command returns, the allocated ublk device
1327a3d2225SMing Lei  number can be reused.
1337a3d2225SMing Lei
1347a3d2225SMing Lei- ``UBLK_CMD_GET_QUEUE_AFFINITY``
1357a3d2225SMing Lei
1367a3d2225SMing Lei  When ``/dev/ublkc`` is added, the driver creates block layer tagset, so
1377a3d2225SMing Lei  that each queue's affinity info is available. The server sends
1387a3d2225SMing Lei  ``UBLK_CMD_GET_QUEUE_AFFINITY`` to retrieve queue affinity info. It can
1397a3d2225SMing Lei  set up the per-queue context efficiently, such as bind affine CPUs with IO
1407a3d2225SMing Lei  pthread and try to allocate buffers in IO thread context.
1417a3d2225SMing Lei
1427a3d2225SMing Lei- ``UBLK_CMD_GET_DEV_INFO``
1437a3d2225SMing Lei
1447a3d2225SMing Lei  For retrieving device info via ``ublksrv_ctrl_dev_info``. It is the server's
1457a3d2225SMing Lei  responsibility to save IO target specific info in userspace.
1467a3d2225SMing Lei
147*4093cb5aSMing Lei- ``UBLK_CMD_GET_DEV_INFO2``
148*4093cb5aSMing Lei  Same purpose with ``UBLK_CMD_GET_DEV_INFO``, but ublk server has to
149*4093cb5aSMing Lei  provide path of the char device of ``/dev/ublkc*`` for kernel to run
150*4093cb5aSMing Lei  permission check, and this command is added for supporting unprivileged
151*4093cb5aSMing Lei  ublk device, and introduced with ``UBLK_F_UNPRIVILEGED_DEV`` together.
152*4093cb5aSMing Lei  Only the user owning the requested device can retrieve the device info.
153*4093cb5aSMing Lei
154*4093cb5aSMing Lei  How to deal with userspace/kernel compatibility:
155*4093cb5aSMing Lei
156*4093cb5aSMing Lei  1) if kernel is capable of handling ``UBLK_F_UNPRIVILEGED_DEV``
157*4093cb5aSMing Lei    If ublk server supports ``UBLK_F_UNPRIVILEGED_DEV``:
158*4093cb5aSMing Lei    ublk server should send ``UBLK_CMD_GET_DEV_INFO2``, given anytime
159*4093cb5aSMing Lei    unprivileged application needs to query devices the current user owns,
160*4093cb5aSMing Lei    when the application has no idea if ``UBLK_F_UNPRIVILEGED_DEV`` is set
161*4093cb5aSMing Lei    given the capability info is stateless, and application should always
162*4093cb5aSMing Lei    retrieve it via ``UBLK_CMD_GET_DEV_INFO2``
163*4093cb5aSMing Lei
164*4093cb5aSMing Lei    If ublk server doesn't support ``UBLK_F_UNPRIVILEGED_DEV``:
165*4093cb5aSMing Lei    ``UBLK_CMD_GET_DEV_INFO`` is always sent to kernel, and the feature of
166*4093cb5aSMing Lei    UBLK_F_UNPRIVILEGED_DEV isn't available for user
167*4093cb5aSMing Lei
168*4093cb5aSMing Lei  2) if kernel isn't capable of handling ``UBLK_F_UNPRIVILEGED_DEV``
169*4093cb5aSMing Lei    If ublk server supports ``UBLK_F_UNPRIVILEGED_DEV``:
170*4093cb5aSMing Lei    ``UBLK_CMD_GET_DEV_INFO2`` is tried first, and will be failed, then
171*4093cb5aSMing Lei    ``UBLK_CMD_GET_DEV_INFO`` needs to be retried given
172*4093cb5aSMing Lei    ``UBLK_F_UNPRIVILEGED_DEV`` can't be set
173*4093cb5aSMing Lei
174*4093cb5aSMing Lei    If ublk server doesn't support ``UBLK_F_UNPRIVILEGED_DEV``:
175*4093cb5aSMing Lei    ``UBLK_CMD_GET_DEV_INFO`` is always sent to kernel, and the feature of
176*4093cb5aSMing Lei    ``UBLK_F_UNPRIVILEGED_DEV`` isn't available for user
177*4093cb5aSMing Lei
178e0539ae0SZiyangZhang- ``UBLK_CMD_START_USER_RECOVERY``
179e0539ae0SZiyangZhang
180e0539ae0SZiyangZhang  This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This
181e0539ae0SZiyangZhang  command is accepted after the old process has exited, ublk device is quiesced
182e0539ae0SZiyangZhang  and ``/dev/ublkc*`` is released. User should send this command before he starts
183e0539ae0SZiyangZhang  a new process which re-opens ``/dev/ublkc*``. When this command returns, the
184e0539ae0SZiyangZhang  ublk device is ready for the new process.
185e0539ae0SZiyangZhang
186e0539ae0SZiyangZhang- ``UBLK_CMD_END_USER_RECOVERY``
187e0539ae0SZiyangZhang
188e0539ae0SZiyangZhang  This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This
189e0539ae0SZiyangZhang  command is accepted after ublk device is quiesced and a new process has
190e0539ae0SZiyangZhang  opened ``/dev/ublkc*`` and get all ublk queues be ready. When this command
191e0539ae0SZiyangZhang  returns, ublk device is unquiesced and new I/O requests are passed to the
192e0539ae0SZiyangZhang  new process.
193e0539ae0SZiyangZhang
194e0539ae0SZiyangZhang- user recovery feature description
195e0539ae0SZiyangZhang
196e0539ae0SZiyangZhang  Two new features are added for user recovery: ``UBLK_F_USER_RECOVERY`` and
197e0539ae0SZiyangZhang  ``UBLK_F_USER_RECOVERY_REISSUE``.
198e0539ae0SZiyangZhang
199e0539ae0SZiyangZhang  With ``UBLK_F_USER_RECOVERY`` set, after one ubq_daemon(ublk server's io
200e0539ae0SZiyangZhang  handler) is dying, ublk does not delete ``/dev/ublkb*`` during the whole
201e0539ae0SZiyangZhang  recovery stage and ublk device ID is kept. It is ublk server's
202e0539ae0SZiyangZhang  responsibility to recover the device context by its own knowledge.
203e0539ae0SZiyangZhang  Requests which have not been issued to userspace are requeued. Requests
204e0539ae0SZiyangZhang  which have been issued to userspace are aborted.
205e0539ae0SZiyangZhang
206e0539ae0SZiyangZhang  With ``UBLK_F_USER_RECOVERY_REISSUE`` set, after one ubq_daemon(ublk
207e0539ae0SZiyangZhang  server's io handler) is dying, contrary to ``UBLK_F_USER_RECOVERY``,
208e0539ae0SZiyangZhang  requests which have been issued to userspace are requeued and will be
209e0539ae0SZiyangZhang  re-issued to the new process after handling ``UBLK_CMD_END_USER_RECOVERY``.
210e0539ae0SZiyangZhang  ``UBLK_F_USER_RECOVERY_REISSUE`` is designed for backends who tolerate
211e0539ae0SZiyangZhang  double-write since the driver may issue the same I/O request twice. It
212e0539ae0SZiyangZhang  might be useful to a read-only FS or a VM backend.
213e0539ae0SZiyangZhang
214*4093cb5aSMing LeiUnprivileged ublk device is supported by passing ``UBLK_F_UNPRIVILEGED_DEV``.
215*4093cb5aSMing LeiOnce the flag is set, all control commands can be sent by unprivileged
216*4093cb5aSMing Leiuser. Except for command of ``UBLK_CMD_ADD_DEV``, permission check on
217*4093cb5aSMing Leithe specified char device(``/dev/ublkc*``) is done for all other control
218*4093cb5aSMing Leicommands by ublk driver, for doing that, path of the char device has to
219*4093cb5aSMing Leibe provided in these commands' payload from ublk server. With this way,
220*4093cb5aSMing Leiublk device becomes container-ware, and device created in one container
221*4093cb5aSMing Leican be controlled/accessed just inside this container.
222*4093cb5aSMing Lei
2237a3d2225SMing LeiData plane
2247a3d2225SMing Lei----------
2257a3d2225SMing Lei
2267a3d2225SMing Leiublk server needs to create per-queue IO pthread & io_uring for handling IO
2277a3d2225SMing Leicommands via io_uring passthrough. The per-queue IO pthread
2287a3d2225SMing Leifocuses on IO handling and shouldn't handle any control & management
2297a3d2225SMing Leitasks.
2307a3d2225SMing Lei
2317a3d2225SMing LeiThe's IO is assigned by a unique tag, which is 1:1 mapping with IO
2327a3d2225SMing Leirequest of ``/dev/ublkb*``.
2337a3d2225SMing Lei
2347a3d2225SMing LeiUAPI structure of ``ublksrv_io_desc`` is defined for describing each IO from
2357a3d2225SMing Leithe driver. A fixed mmaped area (array) on ``/dev/ublkc*`` is provided for
2367a3d2225SMing Leiexporting IO info to the server; such as IO offset, length, OP/flags and
2377a3d2225SMing Leibuffer address. Each ``ublksrv_io_desc`` instance can be indexed via queue id
2387a3d2225SMing Leiand IO tag directly.
2397a3d2225SMing Lei
2407a3d2225SMing LeiThe following IO commands are communicated via io_uring passthrough command,
2417a3d2225SMing Leiand each command is only for forwarding the IO and committing the result
2427a3d2225SMing Leiwith specified IO tag in the command data:
2437a3d2225SMing Lei
2447a3d2225SMing Lei- ``UBLK_IO_FETCH_REQ``
2457a3d2225SMing Lei
2467a3d2225SMing Lei  Sent from the server IO pthread for fetching future incoming IO requests
2477a3d2225SMing Lei  destined to ``/dev/ublkb*``. This command is sent only once from the server
2487a3d2225SMing Lei  IO pthread for ublk driver to setup IO forward environment.
2497a3d2225SMing Lei
2507a3d2225SMing Lei- ``UBLK_IO_COMMIT_AND_FETCH_REQ``
2517a3d2225SMing Lei
2527a3d2225SMing Lei  When an IO request is destined to ``/dev/ublkb*``, the driver stores
2537a3d2225SMing Lei  the IO's ``ublksrv_io_desc`` to the specified mapped area; then the
2547a3d2225SMing Lei  previous received IO command of this IO tag (either ``UBLK_IO_FETCH_REQ``
2557a3d2225SMing Lei  or ``UBLK_IO_COMMIT_AND_FETCH_REQ)`` is completed, so the server gets
2567a3d2225SMing Lei  the IO notification via io_uring.
2577a3d2225SMing Lei
2587a3d2225SMing Lei  After the server handles the IO, its result is committed back to the
2597a3d2225SMing Lei  driver by sending ``UBLK_IO_COMMIT_AND_FETCH_REQ`` back. Once ublkdrv
2607a3d2225SMing Lei  received this command, it parses the result and complete the request to
2617a3d2225SMing Lei  ``/dev/ublkb*``. In the meantime setup environment for fetching future
2627a3d2225SMing Lei  requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ``
2637a3d2225SMing Lei  is reused for both fetching request and committing back IO result.
2647a3d2225SMing Lei
2657a3d2225SMing Lei- ``UBLK_IO_NEED_GET_DATA``
2667a3d2225SMing Lei
2677a3d2225SMing Lei  With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly
2687a3d2225SMing Lei  issued to ublk server without data copy. Then, IO backend of ublk server
2697a3d2225SMing Lei  receives the request and it can allocate data buffer and embed its addr
2707a3d2225SMing Lei  inside this new io command. After the kernel driver gets the command,
2717a3d2225SMing Lei  data copy is done from request pages to this backend's buffer. Finally,
2727a3d2225SMing Lei  backend receives the request again with data to be written and it can
2737a3d2225SMing Lei  truly handle the request.
2747a3d2225SMing Lei
2757a3d2225SMing Lei  ``UBLK_IO_NEED_GET_DATA`` adds one additional round-trip and one
2767a3d2225SMing Lei  io_uring_enter() syscall. Any user thinks that it may lower performance
2777a3d2225SMing Lei  should not enable UBLK_F_NEED_GET_DATA. ublk server pre-allocates IO
2787a3d2225SMing Lei  buffer for each IO by default. Any new project should try to use this
2797a3d2225SMing Lei  buffer to communicate with ublk driver. However, existing project may
2807a3d2225SMing Lei  break or not able to consume the new buffer interface; that's why this
2817a3d2225SMing Lei  command is added for backwards compatibility so that existing projects
2827a3d2225SMing Lei  can still consume existing buffers.
2837a3d2225SMing Lei
2847a3d2225SMing Lei- data copy between ublk server IO buffer and ublk block IO request
2857a3d2225SMing Lei
2867a3d2225SMing Lei  The driver needs to copy the block IO request pages into the server buffer
2877a3d2225SMing Lei  (pages) first for WRITE before notifying the server of the coming IO, so
2887a3d2225SMing Lei  that the server can handle WRITE request.
2897a3d2225SMing Lei
2907a3d2225SMing Lei  When the server handles READ request and sends
2917a3d2225SMing Lei  ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy
2927a3d2225SMing Lei  the server buffer (pages) read to the IO request pages.
2937a3d2225SMing Lei
2947a3d2225SMing LeiFuture development
2957a3d2225SMing Lei==================
2967a3d2225SMing Lei
2977a3d2225SMing LeiZero copy
2987a3d2225SMing Lei---------
2997a3d2225SMing Lei
3007a3d2225SMing LeiZero copy is a generic requirement for nbd, fuse or similar drivers. A
3017a3d2225SMing Leiproblem [#xiaoguang]_ Xiaoguang mentioned is that pages mapped to userspace
3027a3d2225SMing Leican't be remapped any more in kernel with existing mm interfaces. This can
3037a3d2225SMing Leioccurs when destining direct IO to ``/dev/ublkb*``. Also, he reported that
3047a3d2225SMing Leibig requests (IO size >= 256 KB) may benefit a lot from zero copy.
3057a3d2225SMing Lei
3067a3d2225SMing Lei
3077a3d2225SMing LeiReferences
3087a3d2225SMing Lei==========
3097a3d2225SMing Lei
3107a3d2225SMing Lei.. [#userspace] https://github.com/ming1/ubdsrv
3117a3d2225SMing Lei
3127a3d2225SMing Lei.. [#userspace_lib] https://github.com/ming1/ubdsrv/tree/master/lib
3137a3d2225SMing Lei
3147a3d2225SMing Lei.. [#userspace_nbdublk] https://gitlab.com/rwmjones/libnbd/-/tree/nbdublk
3157a3d2225SMing Lei
3167a3d2225SMing Lei.. [#userspace_readme] https://github.com/ming1/ubdsrv/blob/master/README
3177a3d2225SMing Lei
3187a3d2225SMing Lei.. [#stefan] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/
3197a3d2225SMing Lei
3207a3d2225SMing Lei.. [#xiaoguang] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/
321