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*e0539ae0SZiyangZhang- ``UBLK_CMD_START_USER_RECOVERY`` 148*e0539ae0SZiyangZhang 149*e0539ae0SZiyangZhang This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This 150*e0539ae0SZiyangZhang command is accepted after the old process has exited, ublk device is quiesced 151*e0539ae0SZiyangZhang and ``/dev/ublkc*`` is released. User should send this command before he starts 152*e0539ae0SZiyangZhang a new process which re-opens ``/dev/ublkc*``. When this command returns, the 153*e0539ae0SZiyangZhang ublk device is ready for the new process. 154*e0539ae0SZiyangZhang 155*e0539ae0SZiyangZhang- ``UBLK_CMD_END_USER_RECOVERY`` 156*e0539ae0SZiyangZhang 157*e0539ae0SZiyangZhang This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This 158*e0539ae0SZiyangZhang command is accepted after ublk device is quiesced and a new process has 159*e0539ae0SZiyangZhang opened ``/dev/ublkc*`` and get all ublk queues be ready. When this command 160*e0539ae0SZiyangZhang returns, ublk device is unquiesced and new I/O requests are passed to the 161*e0539ae0SZiyangZhang new process. 162*e0539ae0SZiyangZhang 163*e0539ae0SZiyangZhang- user recovery feature description 164*e0539ae0SZiyangZhang 165*e0539ae0SZiyangZhang Two new features are added for user recovery: ``UBLK_F_USER_RECOVERY`` and 166*e0539ae0SZiyangZhang ``UBLK_F_USER_RECOVERY_REISSUE``. 167*e0539ae0SZiyangZhang 168*e0539ae0SZiyangZhang With ``UBLK_F_USER_RECOVERY`` set, after one ubq_daemon(ublk server's io 169*e0539ae0SZiyangZhang handler) is dying, ublk does not delete ``/dev/ublkb*`` during the whole 170*e0539ae0SZiyangZhang recovery stage and ublk device ID is kept. It is ublk server's 171*e0539ae0SZiyangZhang responsibility to recover the device context by its own knowledge. 172*e0539ae0SZiyangZhang Requests which have not been issued to userspace are requeued. Requests 173*e0539ae0SZiyangZhang which have been issued to userspace are aborted. 174*e0539ae0SZiyangZhang 175*e0539ae0SZiyangZhang With ``UBLK_F_USER_RECOVERY_REISSUE`` set, after one ubq_daemon(ublk 176*e0539ae0SZiyangZhang server's io handler) is dying, contrary to ``UBLK_F_USER_RECOVERY``, 177*e0539ae0SZiyangZhang requests which have been issued to userspace are requeued and will be 178*e0539ae0SZiyangZhang re-issued to the new process after handling ``UBLK_CMD_END_USER_RECOVERY``. 179*e0539ae0SZiyangZhang ``UBLK_F_USER_RECOVERY_REISSUE`` is designed for backends who tolerate 180*e0539ae0SZiyangZhang double-write since the driver may issue the same I/O request twice. It 181*e0539ae0SZiyangZhang might be useful to a read-only FS or a VM backend. 182*e0539ae0SZiyangZhang 1837a3d2225SMing LeiData plane 1847a3d2225SMing Lei---------- 1857a3d2225SMing Lei 1867a3d2225SMing Leiublk server needs to create per-queue IO pthread & io_uring for handling IO 1877a3d2225SMing Leicommands via io_uring passthrough. The per-queue IO pthread 1887a3d2225SMing Leifocuses on IO handling and shouldn't handle any control & management 1897a3d2225SMing Leitasks. 1907a3d2225SMing Lei 1917a3d2225SMing LeiThe's IO is assigned by a unique tag, which is 1:1 mapping with IO 1927a3d2225SMing Leirequest of ``/dev/ublkb*``. 1937a3d2225SMing Lei 1947a3d2225SMing LeiUAPI structure of ``ublksrv_io_desc`` is defined for describing each IO from 1957a3d2225SMing Leithe driver. A fixed mmaped area (array) on ``/dev/ublkc*`` is provided for 1967a3d2225SMing Leiexporting IO info to the server; such as IO offset, length, OP/flags and 1977a3d2225SMing Leibuffer address. Each ``ublksrv_io_desc`` instance can be indexed via queue id 1987a3d2225SMing Leiand IO tag directly. 1997a3d2225SMing Lei 2007a3d2225SMing LeiThe following IO commands are communicated via io_uring passthrough command, 2017a3d2225SMing Leiand each command is only for forwarding the IO and committing the result 2027a3d2225SMing Leiwith specified IO tag in the command data: 2037a3d2225SMing Lei 2047a3d2225SMing Lei- ``UBLK_IO_FETCH_REQ`` 2057a3d2225SMing Lei 2067a3d2225SMing Lei Sent from the server IO pthread for fetching future incoming IO requests 2077a3d2225SMing Lei destined to ``/dev/ublkb*``. This command is sent only once from the server 2087a3d2225SMing Lei IO pthread for ublk driver to setup IO forward environment. 2097a3d2225SMing Lei 2107a3d2225SMing Lei- ``UBLK_IO_COMMIT_AND_FETCH_REQ`` 2117a3d2225SMing Lei 2127a3d2225SMing Lei When an IO request is destined to ``/dev/ublkb*``, the driver stores 2137a3d2225SMing Lei the IO's ``ublksrv_io_desc`` to the specified mapped area; then the 2147a3d2225SMing Lei previous received IO command of this IO tag (either ``UBLK_IO_FETCH_REQ`` 2157a3d2225SMing Lei or ``UBLK_IO_COMMIT_AND_FETCH_REQ)`` is completed, so the server gets 2167a3d2225SMing Lei the IO notification via io_uring. 2177a3d2225SMing Lei 2187a3d2225SMing Lei After the server handles the IO, its result is committed back to the 2197a3d2225SMing Lei driver by sending ``UBLK_IO_COMMIT_AND_FETCH_REQ`` back. Once ublkdrv 2207a3d2225SMing Lei received this command, it parses the result and complete the request to 2217a3d2225SMing Lei ``/dev/ublkb*``. In the meantime setup environment for fetching future 2227a3d2225SMing Lei requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ`` 2237a3d2225SMing Lei is reused for both fetching request and committing back IO result. 2247a3d2225SMing Lei 2257a3d2225SMing Lei- ``UBLK_IO_NEED_GET_DATA`` 2267a3d2225SMing Lei 2277a3d2225SMing Lei With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly 2287a3d2225SMing Lei issued to ublk server without data copy. Then, IO backend of ublk server 2297a3d2225SMing Lei receives the request and it can allocate data buffer and embed its addr 2307a3d2225SMing Lei inside this new io command. After the kernel driver gets the command, 2317a3d2225SMing Lei data copy is done from request pages to this backend's buffer. Finally, 2327a3d2225SMing Lei backend receives the request again with data to be written and it can 2337a3d2225SMing Lei truly handle the request. 2347a3d2225SMing Lei 2357a3d2225SMing Lei ``UBLK_IO_NEED_GET_DATA`` adds one additional round-trip and one 2367a3d2225SMing Lei io_uring_enter() syscall. Any user thinks that it may lower performance 2377a3d2225SMing Lei should not enable UBLK_F_NEED_GET_DATA. ublk server pre-allocates IO 2387a3d2225SMing Lei buffer for each IO by default. Any new project should try to use this 2397a3d2225SMing Lei buffer to communicate with ublk driver. However, existing project may 2407a3d2225SMing Lei break or not able to consume the new buffer interface; that's why this 2417a3d2225SMing Lei command is added for backwards compatibility so that existing projects 2427a3d2225SMing Lei can still consume existing buffers. 2437a3d2225SMing Lei 2447a3d2225SMing Lei- data copy between ublk server IO buffer and ublk block IO request 2457a3d2225SMing Lei 2467a3d2225SMing Lei The driver needs to copy the block IO request pages into the server buffer 2477a3d2225SMing Lei (pages) first for WRITE before notifying the server of the coming IO, so 2487a3d2225SMing Lei that the server can handle WRITE request. 2497a3d2225SMing Lei 2507a3d2225SMing Lei When the server handles READ request and sends 2517a3d2225SMing Lei ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy 2527a3d2225SMing Lei the server buffer (pages) read to the IO request pages. 2537a3d2225SMing Lei 2547a3d2225SMing LeiFuture development 2557a3d2225SMing Lei================== 2567a3d2225SMing Lei 2577a3d2225SMing LeiContainer-aware ublk deivice 2587a3d2225SMing Lei---------------------------- 2597a3d2225SMing Lei 2607a3d2225SMing Leiublk driver doesn't handle any IO logic. Its function is well defined 2617a3d2225SMing Leifor now and very limited userspace interfaces are needed, which is also 2627a3d2225SMing Leiwell defined too. It is possible to make ublk devices container-aware block 2637a3d2225SMing Leidevices in future as Stefan Hajnoczi suggested [#stefan]_, by removing 2647a3d2225SMing LeiADMIN privilege. 2657a3d2225SMing Lei 2667a3d2225SMing LeiZero copy 2677a3d2225SMing Lei--------- 2687a3d2225SMing Lei 2697a3d2225SMing LeiZero copy is a generic requirement for nbd, fuse or similar drivers. A 2707a3d2225SMing Leiproblem [#xiaoguang]_ Xiaoguang mentioned is that pages mapped to userspace 2717a3d2225SMing Leican't be remapped any more in kernel with existing mm interfaces. This can 2727a3d2225SMing Leioccurs when destining direct IO to ``/dev/ublkb*``. Also, he reported that 2737a3d2225SMing Leibig requests (IO size >= 256 KB) may benefit a lot from zero copy. 2747a3d2225SMing Lei 2757a3d2225SMing Lei 2767a3d2225SMing LeiReferences 2777a3d2225SMing Lei========== 2787a3d2225SMing Lei 2797a3d2225SMing Lei.. [#userspace] https://github.com/ming1/ubdsrv 2807a3d2225SMing Lei 2817a3d2225SMing Lei.. [#userspace_lib] https://github.com/ming1/ubdsrv/tree/master/lib 2827a3d2225SMing Lei 2837a3d2225SMing Lei.. [#userspace_nbdublk] https://gitlab.com/rwmjones/libnbd/-/tree/nbdublk 2847a3d2225SMing Lei 2857a3d2225SMing Lei.. [#userspace_readme] https://github.com/ming1/ubdsrv/blob/master/README 2867a3d2225SMing Lei 2877a3d2225SMing Lei.. [#stefan] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/ 2887a3d2225SMing Lei 2897a3d2225SMing Lei.. [#xiaoguang] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/ 290