Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ================================================
0004 Multi-Queue Block IO Queueing Mechanism (blk-mq)
0005 ================================================
0006 
0007 The Multi-Queue Block IO Queueing Mechanism is an API to enable fast storage
0008 devices to achieve a huge number of input/output operations per second (IOPS)
0009 through queueing and submitting IO requests to block devices simultaneously,
0010 benefiting from the parallelism offered by modern storage devices.
0011 
0012 Introduction
0013 ============
0014 
0015 Background
0016 ----------
0017 
0018 Magnetic hard disks have been the de facto standard from the beginning of the
0019 development of the kernel. The Block IO subsystem aimed to achieve the best
0020 performance possible for those devices with a high penalty when doing random
0021 access, and the bottleneck was the mechanical moving parts, a lot slower than
0022 any layer on the storage stack. One example of such optimization technique
0023 involves ordering read/write requests according to the current position of the
0024 hard disk head.
0025 
0026 However, with the development of Solid State Drives and Non-Volatile Memories
0027 without mechanical parts nor random access penalty and capable of performing
0028 high parallel access, the bottleneck of the stack had moved from the storage
0029 device to the operating system. In order to take advantage of the parallelism
0030 in those devices' design, the multi-queue mechanism was introduced.
0031 
0032 The former design had a single queue to store block IO requests with a single
0033 lock. That did not scale well in SMP systems due to dirty data in cache and the
0034 bottleneck of having a single lock for multiple processors. This setup also
0035 suffered with congestion when different processes (or the same process, moving
0036 to different CPUs) wanted to perform block IO. Instead of this, the blk-mq API
0037 spawns multiple queues with individual entry points local to the CPU, removing
0038 the need for a lock. A deeper explanation on how this works is covered in the
0039 following section (`Operation`_).
0040 
0041 Operation
0042 ---------
0043 
0044 When the userspace performs IO to a block device (reading or writing a file,
0045 for instance), blk-mq takes action: it will store and manage IO requests to
0046 the block device, acting as middleware between the userspace (and a file
0047 system, if present) and the block device driver.
0048 
0049 blk-mq has two group of queues: software staging queues and hardware dispatch
0050 queues. When the request arrives at the block layer, it will try the shortest
0051 path possible: send it directly to the hardware queue. However, there are two
0052 cases that it might not do that: if there's an IO scheduler attached at the
0053 layer or if we want to try to merge requests. In both cases, requests will be
0054 sent to the software queue.
0055 
0056 Then, after the requests are processed by software queues, they will be placed
0057 at the hardware queue, a second stage queue where the hardware has direct access
0058 to process those requests. However, if the hardware does not have enough
0059 resources to accept more requests, blk-mq will places requests on a temporary
0060 queue, to be sent in the future, when the hardware is able.
0061 
0062 Software staging queues
0063 ~~~~~~~~~~~~~~~~~~~~~~~
0064 
0065 The block IO subsystem adds requests in the software staging queues
0066 (represented by struct blk_mq_ctx) in case that they weren't sent
0067 directly to the driver. A request is one or more BIOs. They arrived at the
0068 block layer through the data structure struct bio. The block layer
0069 will then build a new structure from it, the struct request that will
0070 be used to communicate with the device driver. Each queue has its own lock and
0071 the number of queues is defined by a per-CPU or per-node basis.
0072 
0073 The staging queue can be used to merge requests for adjacent sectors. For
0074 instance, requests for sector 3-6, 6-7, 7-9 can become one request for 3-9.
0075 Even if random access to SSDs and NVMs have the same time of response compared
0076 to sequential access, grouped requests for sequential access decreases the
0077 number of individual requests. This technique of merging requests is called
0078 plugging.
0079 
0080 Along with that, the requests can be reordered to ensure fairness of system
0081 resources (e.g. to ensure that no application suffers from starvation) and/or to
0082 improve IO performance, by an IO scheduler.
0083 
0084 IO Schedulers
0085 ^^^^^^^^^^^^^
0086 
0087 There are several schedulers implemented by the block layer, each one following
0088 a heuristic to improve the IO performance. They are "pluggable" (as in plug
0089 and play), in the sense of they can be selected at run time using sysfs. You
0090 can read more about Linux's IO schedulers `here
0091 <https://www.kernel.org/doc/html/latest/block/index.html>`_. The scheduling
0092 happens only between requests in the same queue, so it is not possible to merge
0093 requests from different queues, otherwise there would be cache trashing and a
0094 need to have a lock for each queue. After the scheduling, the requests are
0095 eligible to be sent to the hardware. One of the possible schedulers to be
0096 selected is the NONE scheduler, the most straightforward one. It will just
0097 place requests on whatever software queue the process is running on, without
0098 any reordering. When the device starts processing requests in the hardware
0099 queue (a.k.a. run the hardware queue), the software queues mapped to that
0100 hardware queue will be drained in sequence according to their mapping.
0101 
0102 Hardware dispatch queues
0103 ~~~~~~~~~~~~~~~~~~~~~~~~
0104 
0105 The hardware queue (represented by struct blk_mq_hw_ctx) is a struct
0106 used by device drivers to map the device submission queues (or device DMA ring
0107 buffer), and are the last step of the block layer submission code before the
0108 low level device driver taking ownership of the request. To run this queue, the
0109 block layer removes requests from the associated software queues and tries to
0110 dispatch to the hardware.
0111 
0112 If it's not possible to send the requests directly to hardware, they will be
0113 added to a linked list (``hctx->dispatch``) of requests. Then,
0114 next time the block layer runs a queue, it will send the requests laying at the
0115 ``dispatch`` list first, to ensure a fairness dispatch with those
0116 requests that were ready to be sent first. The number of hardware queues
0117 depends on the number of hardware contexts supported by the hardware and its
0118 device driver, but it will not be more than the number of cores of the system.
0119 There is no reordering at this stage, and each software queue has a set of
0120 hardware queues to send requests for.
0121 
0122 .. note::
0123 
0124         Neither the block layer nor the device protocols guarantee
0125         the order of completion of requests. This must be handled by
0126         higher layers, like the filesystem.
0127 
0128 Tag-based completion
0129 ~~~~~~~~~~~~~~~~~~~~
0130 
0131 In order to indicate which request has been completed, every request is
0132 identified by an integer, ranging from 0 to the dispatch queue size. This tag
0133 is generated by the block layer and later reused by the device driver, removing
0134 the need to create a redundant identifier. When a request is completed in the
0135 driver, the tag is sent back to the block layer to notify it of the finalization.
0136 This removes the need to do a linear search to find out which IO has been
0137 completed.
0138 
0139 Further reading
0140 ---------------
0141 
0142 - `Linux Block IO: Introducing Multi-queue SSD Access on Multi-core Systems <http://kernel.dk/blk-mq.pdf>`_
0143 
0144 - `NOOP scheduler <https://en.wikipedia.org/wiki/Noop_scheduler>`_
0145 
0146 - `Null block device driver <https://www.kernel.org/doc/html/latest/block/null_blk.html>`_
0147 
0148 Source code documentation
0149 =========================
0150 
0151 .. kernel-doc:: include/linux/blk-mq.h
0152 
0153 .. kernel-doc:: block/blk-mq.c