Skip to content

Commit 320ae51

Browse files
committed
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices: - The classic request_fn based approach, where drivers use struct request units for IO. The block layer provides various helper functionalities to let drivers share code, things like tag management, timeout handling, queueing, etc. - The "stacked" approach, where a driver squeezes in between the block layer and IO submitter. Since this bypasses the IO stack, driver generally have to manage everything themselves. With drivers being written for new high IOPS devices, the classic request_fn based driver doesn't work well enough. The design dates back to when both SMP and high IOPS was rare. It has problems with scaling to bigger machines, and runs into scaling issues even on smaller machines when you have IOPS in the hundreds of thousands per device. The stacked approach is then most often selected as the model for the driver. But this means that everybody has to re-invent everything, and along with that we get all the problems again that the shared approach solved. This commit introduces blk-mq, block multi queue support. The design is centered around per-cpu queues for queueing IO, which then funnel down into x number of hardware submission queues. We might have a 1:1 mapping between the two, or it might be an N:M mapping. That all depends on what the hardware supports. blk-mq provides various helper functions, which include: - Scalable support for request tagging. Most devices need to be able to uniquely identify a request both in the driver and to the hardware. The tagging uses per-cpu caches for freed tags, to enable cache hot reuse. - Timeout handling without tracking request on a per-device basis. Basically the driver should be able to get a notification, if a request happens to fail. - Optional support for non 1:1 mappings between issue and submission queues. blk-mq can redirect IO completions to the desired location. - Support for per-request payloads. Drivers almost always need to associate a request structure with some driver private command structure. Drivers can tell blk-mq this at init time, and then any request handed to the driver will have the required size of memory associated with it. - Support for merging of IO, and plugging. The stacked model gets neither of these. Even for high IOPS devices, merging sequential IO reduces per-command overhead and thus increases bandwidth. For now, this is provided as a potential 3rd queueing model, with the hope being that, as it matures, it can replace both the classic and stacked model. That would get us back to having just 1 real model for block devices, leaving the stacked approach to dm/md devices (as it was originally intended). Contributions in this patch from the following people: Shaohua Li <shli@fusionio.com> Alexander Gordeev <agordeev@redhat.com> Christoph Hellwig <hch@infradead.org> Mike Christie <michaelc@cs.wisc.edu> Matias Bjorling <m@bjorling.me> Jeff Moyer <jmoyer@redhat.com> Acked-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 1dddc01 commit 320ae51

18 files changed

+2890
-109
lines changed

block/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
obj-$(CONFIG_BLOCK) := elevator.o blk-core.o blk-tag.o blk-sysfs.o \
66
blk-flush.o blk-settings.o blk-ioc.o blk-map.o \
77
blk-exec.o blk-merge.o blk-softirq.o blk-timeout.o \
8-
blk-iopoll.o blk-lib.o ioctl.o genhd.o scsi_ioctl.o \
9-
partition-generic.o partitions/
8+
blk-iopoll.o blk-lib.o blk-mq.o blk-mq-tag.o \
9+
blk-mq-sysfs.o blk-mq-cpu.o blk-mq-cpumap.o ioctl.o \
10+
genhd.o scsi_ioctl.o partition-generic.o partitions/
1011

1112
obj-$(CONFIG_BLK_DEV_BSG) += bsg.o
1213
obj-$(CONFIG_BLK_DEV_BSGLIB) += bsg-lib.o

block/blk-core.c

+84-58
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/backing-dev.h>
1717
#include <linux/bio.h>
1818
#include <linux/blkdev.h>
19+
#include <linux/blk-mq.h>
1920
#include <linux/highmem.h>
2021
#include <linux/mm.h>
2122
#include <linux/kernel_stat.h>
@@ -48,7 +49,7 @@ DEFINE_IDA(blk_queue_ida);
4849
/*
4950
* For the allocated request tables
5051
*/
51-
static struct kmem_cache *request_cachep;
52+
struct kmem_cache *request_cachep = NULL;
5253

5354
/*
5455
* For queue allocation
@@ -60,42 +61,6 @@ struct kmem_cache *blk_requestq_cachep;
6061
*/
6162
static struct workqueue_struct *kblockd_workqueue;
6263

63-
static void drive_stat_acct(struct request *rq, int new_io)
64-
{
65-
struct hd_struct *part;
66-
int rw = rq_data_dir(rq);
67-
int cpu;
68-
69-
if (!blk_do_io_stat(rq))
70-
return;
71-
72-
cpu = part_stat_lock();
73-
74-
if (!new_io) {
75-
part = rq->part;
76-
part_stat_inc(cpu, part, merges[rw]);
77-
} else {
78-
part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
79-
if (!hd_struct_try_get(part)) {
80-
/*
81-
* The partition is already being removed,
82-
* the request will be accounted on the disk only
83-
*
84-
* We take a reference on disk->part0 although that
85-
* partition will never be deleted, so we can treat
86-
* it as any other partition.
87-
*/
88-
part = &rq->rq_disk->part0;
89-
hd_struct_get(part);
90-
}
91-
part_round_stats(cpu, part);
92-
part_inc_in_flight(part, rw);
93-
rq->part = part;
94-
}
95-
96-
part_stat_unlock();
97-
}
98-
9964
void blk_queue_congestion_threshold(struct request_queue *q)
10065
{
10166
int nr;
@@ -594,9 +559,12 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
594559
if (!q)
595560
return NULL;
596561

562+
if (percpu_counter_init(&q->mq_usage_counter, 0))
563+
goto fail_q;
564+
597565
q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
598566
if (q->id < 0)
599-
goto fail_q;
567+
goto fail_c;
600568

601569
q->backing_dev_info.ra_pages =
602570
(VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
@@ -643,13 +611,17 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
643611
q->bypass_depth = 1;
644612
__set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
645613

614+
init_waitqueue_head(&q->mq_freeze_wq);
615+
646616
if (blkcg_init_queue(q))
647617
goto fail_id;
648618

649619
return q;
650620

651621
fail_id:
652622
ida_simple_remove(&blk_queue_ida, q->id);
623+
fail_c:
624+
percpu_counter_destroy(&q->mq_usage_counter);
653625
fail_q:
654626
kmem_cache_free(blk_requestq_cachep, q);
655627
return NULL;
@@ -1108,7 +1080,8 @@ static struct request *get_request(struct request_queue *q, int rw_flags,
11081080
goto retry;
11091081
}
11101082

1111-
struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1083+
static struct request *blk_old_get_request(struct request_queue *q, int rw,
1084+
gfp_t gfp_mask)
11121085
{
11131086
struct request *rq;
11141087

@@ -1125,6 +1098,14 @@ struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
11251098

11261099
return rq;
11271100
}
1101+
1102+
struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1103+
{
1104+
if (q->mq_ops)
1105+
return blk_mq_alloc_request(q, rw, gfp_mask);
1106+
else
1107+
return blk_old_get_request(q, rw, gfp_mask);
1108+
}
11281109
EXPORT_SYMBOL(blk_get_request);
11291110

11301111
/**
@@ -1210,7 +1191,7 @@ EXPORT_SYMBOL(blk_requeue_request);
12101191
static void add_acct_request(struct request_queue *q, struct request *rq,
12111192
int where)
12121193
{
1213-
drive_stat_acct(rq, 1);
1194+
blk_account_io_start(rq, true);
12141195
__elv_add_request(q, rq, where);
12151196
}
12161197

@@ -1299,12 +1280,17 @@ EXPORT_SYMBOL_GPL(__blk_put_request);
12991280

13001281
void blk_put_request(struct request *req)
13011282
{
1302-
unsigned long flags;
13031283
struct request_queue *q = req->q;
13041284

1305-
spin_lock_irqsave(q->queue_lock, flags);
1306-
__blk_put_request(q, req);
1307-
spin_unlock_irqrestore(q->queue_lock, flags);
1285+
if (q->mq_ops)
1286+
blk_mq_free_request(req);
1287+
else {
1288+
unsigned long flags;
1289+
1290+
spin_lock_irqsave(q->queue_lock, flags);
1291+
__blk_put_request(q, req);
1292+
spin_unlock_irqrestore(q->queue_lock, flags);
1293+
}
13081294
}
13091295
EXPORT_SYMBOL(blk_put_request);
13101296

@@ -1340,8 +1326,8 @@ void blk_add_request_payload(struct request *rq, struct page *page,
13401326
}
13411327
EXPORT_SYMBOL_GPL(blk_add_request_payload);
13421328

1343-
static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1344-
struct bio *bio)
1329+
bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1330+
struct bio *bio)
13451331
{
13461332
const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
13471333

@@ -1358,12 +1344,12 @@ static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
13581344
req->__data_len += bio->bi_size;
13591345
req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
13601346

1361-
drive_stat_acct(req, 0);
1347+
blk_account_io_start(req, false);
13621348
return true;
13631349
}
13641350

1365-
static bool bio_attempt_front_merge(struct request_queue *q,
1366-
struct request *req, struct bio *bio)
1351+
bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1352+
struct bio *bio)
13671353
{
13681354
const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
13691355

@@ -1388,12 +1374,12 @@ static bool bio_attempt_front_merge(struct request_queue *q,
13881374
req->__data_len += bio->bi_size;
13891375
req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
13901376

1391-
drive_stat_acct(req, 0);
1377+
blk_account_io_start(req, false);
13921378
return true;
13931379
}
13941380

13951381
/**
1396-
* attempt_plug_merge - try to merge with %current's plugged list
1382+
* blk_attempt_plug_merge - try to merge with %current's plugged list
13971383
* @q: request_queue new bio is being queued at
13981384
* @bio: new bio being queued
13991385
* @request_count: out parameter for number of traversed plugged requests
@@ -1409,8 +1395,8 @@ static bool bio_attempt_front_merge(struct request_queue *q,
14091395
* reliable access to the elevator outside queue lock. Only check basic
14101396
* merging parameters without querying the elevator.
14111397
*/
1412-
static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1413-
unsigned int *request_count)
1398+
bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1399+
unsigned int *request_count)
14141400
{
14151401
struct blk_plug *plug;
14161402
struct request *rq;
@@ -1489,7 +1475,7 @@ void blk_queue_bio(struct request_queue *q, struct bio *bio)
14891475
* Check if we can merge with the plugged list before grabbing
14901476
* any locks.
14911477
*/
1492-
if (attempt_plug_merge(q, bio, &request_count))
1478+
if (blk_attempt_plug_merge(q, bio, &request_count))
14931479
return;
14941480

14951481
spin_lock_irq(q->queue_lock);
@@ -1557,7 +1543,7 @@ void blk_queue_bio(struct request_queue *q, struct bio *bio)
15571543
}
15581544
}
15591545
list_add_tail(&req->queuelist, &plug->list);
1560-
drive_stat_acct(req, 1);
1546+
blk_account_io_start(req, true);
15611547
} else {
15621548
spin_lock_irq(q->queue_lock);
15631549
add_acct_request(q, req, where);
@@ -2011,7 +1997,7 @@ unsigned int blk_rq_err_bytes(const struct request *rq)
20111997
}
20121998
EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
20131999

2014-
static void blk_account_io_completion(struct request *req, unsigned int bytes)
2000+
void blk_account_io_completion(struct request *req, unsigned int bytes)
20152001
{
20162002
if (blk_do_io_stat(req)) {
20172003
const int rw = rq_data_dir(req);
@@ -2025,7 +2011,7 @@ static void blk_account_io_completion(struct request *req, unsigned int bytes)
20252011
}
20262012
}
20272013

2028-
static void blk_account_io_done(struct request *req)
2014+
void blk_account_io_done(struct request *req)
20292015
{
20302016
/*
20312017
* Account IO completion. flush_rq isn't accounted as a
@@ -2073,6 +2059,42 @@ static inline struct request *blk_pm_peek_request(struct request_queue *q,
20732059
}
20742060
#endif
20752061

2062+
void blk_account_io_start(struct request *rq, bool new_io)
2063+
{
2064+
struct hd_struct *part;
2065+
int rw = rq_data_dir(rq);
2066+
int cpu;
2067+
2068+
if (!blk_do_io_stat(rq))
2069+
return;
2070+
2071+
cpu = part_stat_lock();
2072+
2073+
if (!new_io) {
2074+
part = rq->part;
2075+
part_stat_inc(cpu, part, merges[rw]);
2076+
} else {
2077+
part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2078+
if (!hd_struct_try_get(part)) {
2079+
/*
2080+
* The partition is already being removed,
2081+
* the request will be accounted on the disk only
2082+
*
2083+
* We take a reference on disk->part0 although that
2084+
* partition will never be deleted, so we can treat
2085+
* it as any other partition.
2086+
*/
2087+
part = &rq->rq_disk->part0;
2088+
hd_struct_get(part);
2089+
}
2090+
part_round_stats(cpu, part);
2091+
part_inc_in_flight(part, rw);
2092+
rq->part = part;
2093+
}
2094+
2095+
part_stat_unlock();
2096+
}
2097+
20762098
/**
20772099
* blk_peek_request - peek at the top of a request queue
20782100
* @q: request queue to peek at
@@ -2448,7 +2470,6 @@ static void blk_finish_request(struct request *req, int error)
24482470
if (req->cmd_flags & REQ_DONTPREP)
24492471
blk_unprep_request(req);
24502472

2451-
24522473
blk_account_io_done(req);
24532474

24542475
if (req->end_io)
@@ -2870,6 +2891,7 @@ void blk_start_plug(struct blk_plug *plug)
28702891

28712892
plug->magic = PLUG_MAGIC;
28722893
INIT_LIST_HEAD(&plug->list);
2894+
INIT_LIST_HEAD(&plug->mq_list);
28732895
INIT_LIST_HEAD(&plug->cb_list);
28742896

28752897
/*
@@ -2967,6 +2989,10 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
29672989
BUG_ON(plug->magic != PLUG_MAGIC);
29682990

29692991
flush_plug_callbacks(plug, from_schedule);
2992+
2993+
if (!list_empty(&plug->mq_list))
2994+
blk_mq_flush_plug_list(plug, from_schedule);
2995+
29702996
if (list_empty(&plug->list))
29712997
return;
29722998

block/blk-exec.c

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/module.h>
66
#include <linux/bio.h>
77
#include <linux/blkdev.h>
8+
#include <linux/blk-mq.h>
89
#include <linux/sched/sysctl.h>
910

1011
#include "blk.h"
@@ -58,6 +59,12 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
5859

5960
rq->rq_disk = bd_disk;
6061
rq->end_io = done;
62+
63+
if (q->mq_ops) {
64+
blk_mq_insert_request(q, rq, true);
65+
return;
66+
}
67+
6168
/*
6269
* need to check this before __blk_run_queue(), because rq can
6370
* be freed before that returns.

0 commit comments

Comments
 (0)