146e90614SAndré Almeida.. SPDX-License-Identifier: GPL-2.0 246e90614SAndré Almeida 346e90614SAndré Almeida================================================ 446e90614SAndré AlmeidaMulti-Queue Block IO Queueing Mechanism (blk-mq) 546e90614SAndré Almeida================================================ 646e90614SAndré Almeida 746e90614SAndré AlmeidaThe Multi-Queue Block IO Queueing Mechanism is an API to enable fast storage 846e90614SAndré Almeidadevices to achieve a huge number of input/output operations per second (IOPS) 946e90614SAndré Almeidathrough queueing and submitting IO requests to block devices simultaneously, 1046e90614SAndré Almeidabenefiting from the parallelism offered by modern storage devices. 1146e90614SAndré Almeida 1246e90614SAndré AlmeidaIntroduction 1346e90614SAndré Almeida============ 1446e90614SAndré Almeida 1546e90614SAndré AlmeidaBackground 1646e90614SAndré Almeida---------- 1746e90614SAndré Almeida 1846e90614SAndré AlmeidaMagnetic hard disks have been the de facto standard from the beginning of the 1946e90614SAndré Almeidadevelopment of the kernel. The Block IO subsystem aimed to achieve the best 2046e90614SAndré Almeidaperformance possible for those devices with a high penalty when doing random 2146e90614SAndré Almeidaaccess, and the bottleneck was the mechanical moving parts, a lot slower than 2246e90614SAndré Almeidaany layer on the storage stack. One example of such optimization technique 2346e90614SAndré Almeidainvolves ordering read/write requests according to the current position of the 2446e90614SAndré Almeidahard disk head. 2546e90614SAndré Almeida 2646e90614SAndré AlmeidaHowever, with the development of Solid State Drives and Non-Volatile Memories 2746e90614SAndré Almeidawithout mechanical parts nor random access penalty and capable of performing 2846e90614SAndré Almeidahigh parallel access, the bottleneck of the stack had moved from the storage 2946e90614SAndré Almeidadevice to the operating system. In order to take advantage of the parallelism 3046e90614SAndré Almeidain those devices' design, the multi-queue mechanism was introduced. 3146e90614SAndré Almeida 3246e90614SAndré AlmeidaThe former design had a single queue to store block IO requests with a single 3346e90614SAndré Almeidalock. That did not scale well in SMP systems due to dirty data in cache and the 3446e90614SAndré Almeidabottleneck of having a single lock for multiple processors. This setup also 3546e90614SAndré Almeidasuffered with congestion when different processes (or the same process, moving 3646e90614SAndré Almeidato different CPUs) wanted to perform block IO. Instead of this, the blk-mq API 3746e90614SAndré Almeidaspawns multiple queues with individual entry points local to the CPU, removing 3846e90614SAndré Almeidathe need for a lock. A deeper explanation on how this works is covered in the 3946e90614SAndré Almeidafollowing section (`Operation`_). 4046e90614SAndré Almeida 4146e90614SAndré AlmeidaOperation 4246e90614SAndré Almeida--------- 4346e90614SAndré Almeida 4446e90614SAndré AlmeidaWhen the userspace performs IO to a block device (reading or writing a file, 4546e90614SAndré Almeidafor instance), blk-mq takes action: it will store and manage IO requests to 4646e90614SAndré Almeidathe block device, acting as middleware between the userspace (and a file 4746e90614SAndré Almeidasystem, if present) and the block device driver. 4846e90614SAndré Almeida 4946e90614SAndré Almeidablk-mq has two group of queues: software staging queues and hardware dispatch 5046e90614SAndré Almeidaqueues. When the request arrives at the block layer, it will try the shortest 5146e90614SAndré Almeidapath possible: send it directly to the hardware queue. However, there are two 5246e90614SAndré Almeidacases that it might not do that: if there's an IO scheduler attached at the 5346e90614SAndré Almeidalayer or if we want to try to merge requests. In both cases, requests will be 5446e90614SAndré Almeidasent to the software queue. 5546e90614SAndré Almeida 5646e90614SAndré AlmeidaThen, after the requests are processed by software queues, they will be placed 57*c19430eeSJinay Jainat the hardware queue, a second stage queue where the hardware has direct access 5846e90614SAndré Almeidato process those requests. However, if the hardware does not have enough 5946e90614SAndré Almeidaresources to accept more requests, blk-mq will places requests on a temporary 6046e90614SAndré Almeidaqueue, to be sent in the future, when the hardware is able. 6146e90614SAndré Almeida 6246e90614SAndré AlmeidaSoftware staging queues 6346e90614SAndré Almeida~~~~~~~~~~~~~~~~~~~~~~~ 6446e90614SAndré Almeida 6546e90614SAndré AlmeidaThe block IO subsystem adds requests in the software staging queues 669303c9d5SMauro Carvalho Chehab(represented by struct blk_mq_ctx) in case that they weren't sent 6746e90614SAndré Almeidadirectly to the driver. A request is one or more BIOs. They arrived at the 689303c9d5SMauro Carvalho Chehabblock layer through the data structure struct bio. The block layer 699303c9d5SMauro Carvalho Chehabwill then build a new structure from it, the struct request that will 7046e90614SAndré Almeidabe used to communicate with the device driver. Each queue has its own lock and 7146e90614SAndré Almeidathe number of queues is defined by a per-CPU or per-node basis. 7246e90614SAndré Almeida 7346e90614SAndré AlmeidaThe staging queue can be used to merge requests for adjacent sectors. For 7446e90614SAndré Almeidainstance, requests for sector 3-6, 6-7, 7-9 can become one request for 3-9. 7546e90614SAndré AlmeidaEven if random access to SSDs and NVMs have the same time of response compared 7646e90614SAndré Almeidato sequential access, grouped requests for sequential access decreases the 7746e90614SAndré Almeidanumber of individual requests. This technique of merging requests is called 7846e90614SAndré Almeidaplugging. 7946e90614SAndré Almeida 8046e90614SAndré AlmeidaAlong with that, the requests can be reordered to ensure fairness of system 8146e90614SAndré Almeidaresources (e.g. to ensure that no application suffers from starvation) and/or to 8246e90614SAndré Almeidaimprove IO performance, by an IO scheduler. 8346e90614SAndré Almeida 8446e90614SAndré AlmeidaIO Schedulers 8546e90614SAndré Almeida^^^^^^^^^^^^^ 8646e90614SAndré Almeida 8746e90614SAndré AlmeidaThere are several schedulers implemented by the block layer, each one following 8846e90614SAndré Almeidaa heuristic to improve the IO performance. They are "pluggable" (as in plug 8946e90614SAndré Almeidaand play), in the sense of they can be selected at run time using sysfs. You 9046e90614SAndré Almeidacan read more about Linux's IO schedulers `here 9146e90614SAndré Almeida<https://www.kernel.org/doc/html/latest/block/index.html>`_. The scheduling 9246e90614SAndré Almeidahappens only between requests in the same queue, so it is not possible to merge 9346e90614SAndré Almeidarequests from different queues, otherwise there would be cache trashing and a 9446e90614SAndré Almeidaneed to have a lock for each queue. After the scheduling, the requests are 9546e90614SAndré Almeidaeligible to be sent to the hardware. One of the possible schedulers to be 9646e90614SAndré Almeidaselected is the NONE scheduler, the most straightforward one. It will just 9746e90614SAndré Almeidaplace requests on whatever software queue the process is running on, without 9846e90614SAndré Almeidaany reordering. When the device starts processing requests in the hardware 9946e90614SAndré Almeidaqueue (a.k.a. run the hardware queue), the software queues mapped to that 10046e90614SAndré Almeidahardware queue will be drained in sequence according to their mapping. 10146e90614SAndré Almeida 10246e90614SAndré AlmeidaHardware dispatch queues 10346e90614SAndré Almeida~~~~~~~~~~~~~~~~~~~~~~~~ 10446e90614SAndré Almeida 1059303c9d5SMauro Carvalho ChehabThe hardware queue (represented by struct blk_mq_hw_ctx) is a struct 10646e90614SAndré Almeidaused by device drivers to map the device submission queues (or device DMA ring 10746e90614SAndré Almeidabuffer), and are the last step of the block layer submission code before the 10846e90614SAndré Almeidalow level device driver taking ownership of the request. To run this queue, the 10946e90614SAndré Almeidablock layer removes requests from the associated software queues and tries to 11046e90614SAndré Almeidadispatch to the hardware. 11146e90614SAndré Almeida 11246e90614SAndré AlmeidaIf it's not possible to send the requests directly to hardware, they will be 1138ac86734SMauro Carvalho Chehabadded to a linked list (``hctx->dispatch``) of requests. Then, 11446e90614SAndré Almeidanext time the block layer runs a queue, it will send the requests laying at the 1158ac86734SMauro Carvalho Chehab``dispatch`` list first, to ensure a fairness dispatch with those 11646e90614SAndré Almeidarequests that were ready to be sent first. The number of hardware queues 11746e90614SAndré Almeidadepends on the number of hardware contexts supported by the hardware and its 11846e90614SAndré Almeidadevice driver, but it will not be more than the number of cores of the system. 11946e90614SAndré AlmeidaThere is no reordering at this stage, and each software queue has a set of 12046e90614SAndré Almeidahardware queues to send requests for. 12146e90614SAndré Almeida 12246e90614SAndré Almeida.. note:: 12346e90614SAndré Almeida 12446e90614SAndré Almeida Neither the block layer nor the device protocols guarantee 12546e90614SAndré Almeida the order of completion of requests. This must be handled by 12646e90614SAndré Almeida higher layers, like the filesystem. 12746e90614SAndré Almeida 12846e90614SAndré AlmeidaTag-based completion 12946e90614SAndré Almeida~~~~~~~~~~~~~~~~~~~~ 13046e90614SAndré Almeida 13146e90614SAndré AlmeidaIn order to indicate which request has been completed, every request is 13246e90614SAndré Almeidaidentified by an integer, ranging from 0 to the dispatch queue size. This tag 13346e90614SAndré Almeidais generated by the block layer and later reused by the device driver, removing 13446e90614SAndré Almeidathe need to create a redundant identifier. When a request is completed in the 1352bc602cbSYue Hudriver, the tag is sent back to the block layer to notify it of the finalization. 13646e90614SAndré AlmeidaThis removes the need to do a linear search to find out which IO has been 13746e90614SAndré Almeidacompleted. 13846e90614SAndré Almeida 13946e90614SAndré AlmeidaFurther reading 14046e90614SAndré Almeida--------------- 14146e90614SAndré Almeida 14246e90614SAndré Almeida- `Linux Block IO: Introducing Multi-queue SSD Access on Multi-core Systems <http://kernel.dk/blk-mq.pdf>`_ 14346e90614SAndré Almeida 14446e90614SAndré Almeida- `NOOP scheduler <https://en.wikipedia.org/wiki/Noop_scheduler>`_ 14546e90614SAndré Almeida 14646e90614SAndré Almeida- `Null block device driver <https://www.kernel.org/doc/html/latest/block/null_blk.html>`_ 14746e90614SAndré Almeida 14846e90614SAndré AlmeidaSource code documentation 14946e90614SAndré Almeida========================= 15046e90614SAndré Almeida 15146e90614SAndré Almeida.. kernel-doc:: include/linux/blk-mq.h 15246e90614SAndré Almeida 15346e90614SAndré Almeida.. kernel-doc:: block/blk-mq.c 154