ublk.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===========================================
  3. Userspace block device driver (ublk driver)
  4. ===========================================
  5. Overview
  6. ========
  7. ublk is a generic framework for implementing block device logic from userspace.
  8. The motivation behind it is that moving virtual block drivers into userspace,
  9. such as loop, nbd and similar can be very helpful. It can help to implement
  10. new virtual block device such as ublk-qcow2 (there are several attempts of
  11. implementing qcow2 driver in kernel).
  12. Userspace block devices are attractive because:
  13. - They can be written many programming languages.
  14. - They can use libraries that are not available in the kernel.
  15. - They can be debugged with tools familiar to application developers.
  16. - Crashes do not kernel panic the machine.
  17. - Bugs are likely to have a lower security impact than bugs in kernel
  18. code.
  19. - They can be installed and updated independently of the kernel.
  20. - They can be used to simulate block device easily with user specified
  21. parameters/setting for test/debug purpose
  22. ublk block device (``/dev/ublkb*``) is added by ublk driver. Any IO request
  23. on the device will be forwarded to ublk userspace program. For convenience,
  24. in this document, ``ublk server`` refers to generic ublk userspace
  25. program. ``ublksrv`` [#userspace]_ is one of such implementation. It
  26. provides ``libublksrv`` [#userspace_lib]_ library for developing specific
  27. user block device conveniently, while also generic type block device is
  28. included, such as loop and null. Richard W.M. Jones wrote userspace nbd device
  29. ``nbdublk`` [#userspace_nbdublk]_ based on ``libublksrv`` [#userspace_lib]_.
  30. After the IO is handled by userspace, the result is committed back to the
  31. driver, thus completing the request cycle. This way, any specific IO handling
  32. logic is totally done by userspace, such as loop's IO handling, NBD's IO
  33. communication, or qcow2's IO mapping.
  34. ``/dev/ublkb*`` is driven by blk-mq request-based driver. Each request is
  35. assigned by one queue wide unique tag. ublk server assigns unique tag to each
  36. IO too, which is 1:1 mapped with IO of ``/dev/ublkb*``.
  37. Both the IO request forward and IO handling result committing are done via
  38. ``io_uring`` passthrough command; that is why ublk is also one io_uring based
  39. block driver. It has been observed that using io_uring passthrough command can
  40. give better IOPS than block IO; which is why ublk is one of high performance
  41. implementation of userspace block device: not only IO request communication is
  42. done by io_uring, but also the preferred IO handling in ublk server is io_uring
  43. based approach too.
  44. ublk provides control interface to set/get ublk block device parameters.
  45. The interface is extendable and kabi compatible: basically any ublk request
  46. queue's parameter or ublk generic feature parameters can be set/get via the
  47. interface. Thus, ublk is generic userspace block device framework.
  48. For example, it is easy to setup a ublk device with specified block
  49. parameters from userspace.
  50. Using ublk
  51. ==========
  52. ublk requires userspace ublk server to handle real block device logic.
  53. Below is example of using ``ublksrv`` to provide ublk-based loop device.
  54. - add a device::
  55. ublk add -t loop -f ublk-loop.img
  56. - format with xfs, then use it::
  57. mkfs.xfs /dev/ublkb0
  58. mount /dev/ublkb0 /mnt
  59. # do anything. all IOs are handled by io_uring
  60. ...
  61. umount /mnt
  62. - list the devices with their info::
  63. ublk list
  64. - delete the device::
  65. ublk del -a
  66. ublk del -n $ublk_dev_id
  67. See usage details in README of ``ublksrv`` [#userspace_readme]_.
  68. Design
  69. ======
  70. Control plane
  71. -------------
  72. ublk driver provides global misc device node (``/dev/ublk-control``) for
  73. managing and controlling ublk devices with help of several control commands:
  74. - ``UBLK_CMD_ADD_DEV``
  75. Add a ublk char device (``/dev/ublkc*``) which is talked with ublk server
  76. WRT IO command communication. Basic device info is sent together with this
  77. command. It sets UAPI structure of ``ublksrv_ctrl_dev_info``,
  78. such as ``nr_hw_queues``, ``queue_depth``, and max IO request buffer size,
  79. for which the info is negotiated with the driver and sent back to the server.
  80. When this command is completed, the basic device info is immutable.
  81. - ``UBLK_CMD_SET_PARAMS`` / ``UBLK_CMD_GET_PARAMS``
  82. Set or get parameters of the device, which can be either generic feature
  83. related, or request queue limit related, but can't be IO logic specific,
  84. because the driver does not handle any IO logic. This command has to be
  85. sent before sending ``UBLK_CMD_START_DEV``.
  86. - ``UBLK_CMD_START_DEV``
  87. After the server prepares userspace resources (such as creating per-queue
  88. pthread & io_uring for handling ublk IO), this command is sent to the
  89. driver for allocating & exposing ``/dev/ublkb*``. Parameters set via
  90. ``UBLK_CMD_SET_PARAMS`` are applied for creating the device.
  91. - ``UBLK_CMD_STOP_DEV``
  92. Halt IO on ``/dev/ublkb*`` and remove the device. When this command returns,
  93. ublk server will release resources (such as destroying per-queue pthread &
  94. io_uring).
  95. - ``UBLK_CMD_DEL_DEV``
  96. Remove ``/dev/ublkc*``. When this command returns, the allocated ublk device
  97. number can be reused.
  98. - ``UBLK_CMD_GET_QUEUE_AFFINITY``
  99. When ``/dev/ublkc`` is added, the driver creates block layer tagset, so
  100. that each queue's affinity info is available. The server sends
  101. ``UBLK_CMD_GET_QUEUE_AFFINITY`` to retrieve queue affinity info. It can
  102. set up the per-queue context efficiently, such as bind affine CPUs with IO
  103. pthread and try to allocate buffers in IO thread context.
  104. - ``UBLK_CMD_GET_DEV_INFO``
  105. For retrieving device info via ``ublksrv_ctrl_dev_info``. It is the server's
  106. responsibility to save IO target specific info in userspace.
  107. - ``UBLK_CMD_GET_DEV_INFO2``
  108. Same purpose with ``UBLK_CMD_GET_DEV_INFO``, but ublk server has to
  109. provide path of the char device of ``/dev/ublkc*`` for kernel to run
  110. permission check, and this command is added for supporting unprivileged
  111. ublk device, and introduced with ``UBLK_F_UNPRIVILEGED_DEV`` together.
  112. Only the user owning the requested device can retrieve the device info.
  113. How to deal with userspace/kernel compatibility:
  114. 1) if kernel is capable of handling ``UBLK_F_UNPRIVILEGED_DEV``
  115. If ublk server supports ``UBLK_F_UNPRIVILEGED_DEV``:
  116. ublk server should send ``UBLK_CMD_GET_DEV_INFO2``, given anytime
  117. unprivileged application needs to query devices the current user owns,
  118. when the application has no idea if ``UBLK_F_UNPRIVILEGED_DEV`` is set
  119. given the capability info is stateless, and application should always
  120. retrieve it via ``UBLK_CMD_GET_DEV_INFO2``
  121. If ublk server doesn't support ``UBLK_F_UNPRIVILEGED_DEV``:
  122. ``UBLK_CMD_GET_DEV_INFO`` is always sent to kernel, and the feature of
  123. UBLK_F_UNPRIVILEGED_DEV isn't available for user
  124. 2) if kernel isn't capable of handling ``UBLK_F_UNPRIVILEGED_DEV``
  125. If ublk server supports ``UBLK_F_UNPRIVILEGED_DEV``:
  126. ``UBLK_CMD_GET_DEV_INFO2`` is tried first, and will be failed, then
  127. ``UBLK_CMD_GET_DEV_INFO`` needs to be retried given
  128. ``UBLK_F_UNPRIVILEGED_DEV`` can't be set
  129. If ublk server doesn't support ``UBLK_F_UNPRIVILEGED_DEV``:
  130. ``UBLK_CMD_GET_DEV_INFO`` is always sent to kernel, and the feature of
  131. ``UBLK_F_UNPRIVILEGED_DEV`` isn't available for user
  132. - ``UBLK_CMD_START_USER_RECOVERY``
  133. This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This
  134. command is accepted after the old process has exited, ublk device is quiesced
  135. and ``/dev/ublkc*`` is released. User should send this command before he starts
  136. a new process which re-opens ``/dev/ublkc*``. When this command returns, the
  137. ublk device is ready for the new process.
  138. - ``UBLK_CMD_END_USER_RECOVERY``
  139. This command is valid if ``UBLK_F_USER_RECOVERY`` feature is enabled. This
  140. command is accepted after ublk device is quiesced and a new process has
  141. opened ``/dev/ublkc*`` and get all ublk queues be ready. When this command
  142. returns, ublk device is unquiesced and new I/O requests are passed to the
  143. new process.
  144. - user recovery feature description
  145. Two new features are added for user recovery: ``UBLK_F_USER_RECOVERY`` and
  146. ``UBLK_F_USER_RECOVERY_REISSUE``.
  147. With ``UBLK_F_USER_RECOVERY`` set, after one ubq_daemon(ublk server's io
  148. handler) is dying, ublk does not delete ``/dev/ublkb*`` during the whole
  149. recovery stage and ublk device ID is kept. It is ublk server's
  150. responsibility to recover the device context by its own knowledge.
  151. Requests which have not been issued to userspace are requeued. Requests
  152. which have been issued to userspace are aborted.
  153. With ``UBLK_F_USER_RECOVERY_REISSUE`` set, after one ubq_daemon(ublk
  154. server's io handler) is dying, contrary to ``UBLK_F_USER_RECOVERY``,
  155. requests which have been issued to userspace are requeued and will be
  156. re-issued to the new process after handling ``UBLK_CMD_END_USER_RECOVERY``.
  157. ``UBLK_F_USER_RECOVERY_REISSUE`` is designed for backends who tolerate
  158. double-write since the driver may issue the same I/O request twice. It
  159. might be useful to a read-only FS or a VM backend.
  160. Unprivileged ublk device is supported by passing ``UBLK_F_UNPRIVILEGED_DEV``.
  161. Once the flag is set, all control commands can be sent by unprivileged
  162. user. Except for command of ``UBLK_CMD_ADD_DEV``, permission check on
  163. the specified char device(``/dev/ublkc*``) is done for all other control
  164. commands by ublk driver, for doing that, path of the char device has to
  165. be provided in these commands' payload from ublk server. With this way,
  166. ublk device becomes container-ware, and device created in one container
  167. can be controlled/accessed just inside this container.
  168. Data plane
  169. ----------
  170. ublk server needs to create per-queue IO pthread & io_uring for handling IO
  171. commands via io_uring passthrough. The per-queue IO pthread
  172. focuses on IO handling and shouldn't handle any control & management
  173. tasks.
  174. The's IO is assigned by a unique tag, which is 1:1 mapping with IO
  175. request of ``/dev/ublkb*``.
  176. UAPI structure of ``ublksrv_io_desc`` is defined for describing each IO from
  177. the driver. A fixed mmapped area (array) on ``/dev/ublkc*`` is provided for
  178. exporting IO info to the server; such as IO offset, length, OP/flags and
  179. buffer address. Each ``ublksrv_io_desc`` instance can be indexed via queue id
  180. and IO tag directly.
  181. The following IO commands are communicated via io_uring passthrough command,
  182. and each command is only for forwarding the IO and committing the result
  183. with specified IO tag in the command data:
  184. - ``UBLK_IO_FETCH_REQ``
  185. Sent from the server IO pthread for fetching future incoming IO requests
  186. destined to ``/dev/ublkb*``. This command is sent only once from the server
  187. IO pthread for ublk driver to setup IO forward environment.
  188. - ``UBLK_IO_COMMIT_AND_FETCH_REQ``
  189. When an IO request is destined to ``/dev/ublkb*``, the driver stores
  190. the IO's ``ublksrv_io_desc`` to the specified mapped area; then the
  191. previous received IO command of this IO tag (either ``UBLK_IO_FETCH_REQ``
  192. or ``UBLK_IO_COMMIT_AND_FETCH_REQ)`` is completed, so the server gets
  193. the IO notification via io_uring.
  194. After the server handles the IO, its result is committed back to the
  195. driver by sending ``UBLK_IO_COMMIT_AND_FETCH_REQ`` back. Once ublkdrv
  196. received this command, it parses the result and complete the request to
  197. ``/dev/ublkb*``. In the meantime setup environment for fetching future
  198. requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ``
  199. is reused for both fetching request and committing back IO result.
  200. - ``UBLK_IO_NEED_GET_DATA``
  201. With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly
  202. issued to ublk server without data copy. Then, IO backend of ublk server
  203. receives the request and it can allocate data buffer and embed its addr
  204. inside this new io command. After the kernel driver gets the command,
  205. data copy is done from request pages to this backend's buffer. Finally,
  206. backend receives the request again with data to be written and it can
  207. truly handle the request.
  208. ``UBLK_IO_NEED_GET_DATA`` adds one additional round-trip and one
  209. io_uring_enter() syscall. Any user thinks that it may lower performance
  210. should not enable UBLK_F_NEED_GET_DATA. ublk server pre-allocates IO
  211. buffer for each IO by default. Any new project should try to use this
  212. buffer to communicate with ublk driver. However, existing project may
  213. break or not able to consume the new buffer interface; that's why this
  214. command is added for backwards compatibility so that existing projects
  215. can still consume existing buffers.
  216. - data copy between ublk server IO buffer and ublk block IO request
  217. The driver needs to copy the block IO request pages into the server buffer
  218. (pages) first for WRITE before notifying the server of the coming IO, so
  219. that the server can handle WRITE request.
  220. When the server handles READ request and sends
  221. ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy
  222. the server buffer (pages) read to the IO request pages.
  223. Future development
  224. ==================
  225. Zero copy
  226. ---------
  227. Zero copy is a generic requirement for nbd, fuse or similar drivers. A
  228. problem [#xiaoguang]_ Xiaoguang mentioned is that pages mapped to userspace
  229. can't be remapped any more in kernel with existing mm interfaces. This can
  230. occurs when destining direct IO to ``/dev/ublkb*``. Also, he reported that
  231. big requests (IO size >= 256 KB) may benefit a lot from zero copy.
  232. References
  233. ==========
  234. .. [#userspace] https://github.com/ming1/ubdsrv
  235. .. [#userspace_lib] https://github.com/ming1/ubdsrv/tree/master/lib
  236. .. [#userspace_nbdublk] https://gitlab.com/rwmjones/libnbd/-/tree/nbdublk
  237. .. [#userspace_readme] https://github.com/ming1/ubdsrv/blob/master/README
  238. .. [#stefan] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/
  239. .. [#xiaoguang] https://lore.kernel.org/linux-block/YoOr6jBfgVm8GvWg@stefanha-x1.localdomain/