Skip to content

Commit e9b267d

Browse files
Christoph Hellwigaxboe
authored andcommitted
blk-mq: add ->init_request and ->exit_request methods
The current blk_mq_init_commands/blk_mq_free_commands interface has a two problems: 1) Because only the constructor is passed to blk_mq_init_commands there is no easy way to clean up when a comman initialization failed. The current code simply leaks the allocations done in the constructor. 2) There is no good place to call blk_mq_free_commands: before blk_cleanup_queue there is no guarantee that all outstanding commands have completed, so we can't free them yet. After blk_cleanup_queue the queue has usually been freed. This can be worked around by grabbing an unconditional reference before calling blk_cleanup_queue and dropping it after blk_mq_free_commands is done, although that's not exatly pretty and driver writers are guaranteed to get it wrong sooner or later. Both issues are easily fixed by making the request constructor and destructor normal blk_mq_ops methods. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 8727af4 commit e9b267d

3 files changed

Lines changed: 55 additions & 87 deletions

File tree

‎block/blk-mq.c‎

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,74 +1031,20 @@ static void blk_mq_hctx_notify(void *data, unsigned long action,
10311031
blk_mq_put_ctx(ctx);
10321032
}
10331033

1034-
static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1035-
int (*init)(void *, struct blk_mq_hw_ctx *,
1036-
struct request *, unsigned int),
1037-
void *data)
1034+
static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx, void *driver_data)
10381035
{
1039-
unsigned int i;
1040-
int ret = 0;
1041-
1042-
for (i = 0; i < hctx->queue_depth; i++) {
1043-
struct request *rq = hctx->rqs[i];
1044-
1045-
ret = init(data, hctx, rq, i);
1046-
if (ret)
1047-
break;
1048-
}
1049-
1050-
return ret;
1051-
}
1052-
1053-
int blk_mq_init_commands(struct request_queue *q,
1054-
int (*init)(void *, struct blk_mq_hw_ctx *,
1055-
struct request *, unsigned int),
1056-
void *data)
1057-
{
1058-
struct blk_mq_hw_ctx *hctx;
1059-
unsigned int i;
1060-
int ret = 0;
1061-
1062-
queue_for_each_hw_ctx(q, hctx, i) {
1063-
ret = blk_mq_init_hw_commands(hctx, init, data);
1064-
if (ret)
1065-
break;
1066-
}
1067-
1068-
return ret;
1069-
}
1070-
EXPORT_SYMBOL(blk_mq_init_commands);
1071-
1072-
static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx *hctx,
1073-
void (*free)(void *, struct blk_mq_hw_ctx *,
1074-
struct request *, unsigned int),
1075-
void *data)
1076-
{
1077-
unsigned int i;
1036+
struct page *page;
10781037

1079-
for (i = 0; i < hctx->queue_depth; i++) {
1080-
struct request *rq = hctx->rqs[i];
1038+
if (hctx->rqs && hctx->queue->mq_ops->exit_request) {
1039+
int i;
10811040

1082-
free(data, hctx, rq, i);
1041+
for (i = 0; i < hctx->queue_depth; i++) {
1042+
if (!hctx->rqs[i])
1043+
continue;
1044+
hctx->queue->mq_ops->exit_request(driver_data, hctx,
1045+
hctx->rqs[i], i);
1046+
}
10831047
}
1084-
}
1085-
1086-
void blk_mq_free_commands(struct request_queue *q,
1087-
void (*free)(void *, struct blk_mq_hw_ctx *,
1088-
struct request *, unsigned int),
1089-
void *data)
1090-
{
1091-
struct blk_mq_hw_ctx *hctx;
1092-
unsigned int i;
1093-
1094-
queue_for_each_hw_ctx(q, hctx, i)
1095-
blk_mq_free_hw_commands(hctx, free, data);
1096-
}
1097-
EXPORT_SYMBOL(blk_mq_free_commands);
1098-
1099-
static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1100-
{
1101-
struct page *page;
11021048

11031049
while (!list_empty(&hctx->page_list)) {
11041050
page = list_first_entry(&hctx->page_list, struct page, lru);
@@ -1123,10 +1069,12 @@ static size_t order_to_size(unsigned int order)
11231069
}
11241070

11251071
static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1126-
unsigned int reserved_tags, int node)
1072+
struct blk_mq_reg *reg, void *driver_data, int node)
11271073
{
1074+
unsigned int reserved_tags = reg->reserved_tags;
11281075
unsigned int i, j, entries_per_page, max_order = 4;
11291076
size_t rq_size, left;
1077+
int error;
11301078

11311079
INIT_LIST_HEAD(&hctx->page_list);
11321080

@@ -1175,27 +1123,38 @@ static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
11751123
for (j = 0; j < to_do; j++) {
11761124
hctx->rqs[i] = p;
11771125
blk_rq_init(hctx->queue, hctx->rqs[i]);
1126+
if (reg->ops->init_request) {
1127+
error = reg->ops->init_request(driver_data,
1128+
hctx, hctx->rqs[i], i);
1129+
if (error)
1130+
goto err_rq_map;
1131+
}
1132+
11781133
p += rq_size;
11791134
i++;
11801135
}
11811136
}
11821137

1183-
if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1138+
if (i < (reserved_tags + BLK_MQ_TAG_MIN)) {
1139+
error = -ENOMEM;
11841140
goto err_rq_map;
1185-
else if (i != hctx->queue_depth) {
1141+
}
1142+
if (i != hctx->queue_depth) {
11861143
hctx->queue_depth = i;
11871144
pr_warn("%s: queue depth set to %u because of low memory\n",
11881145
__func__, i);
11891146
}
11901147

11911148
hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
11921149
if (!hctx->tags) {
1193-
err_rq_map:
1194-
blk_mq_free_rq_map(hctx);
1195-
return -ENOMEM;
1150+
error = -ENOMEM;
1151+
goto err_rq_map;
11961152
}
11971153

11981154
return 0;
1155+
err_rq_map:
1156+
blk_mq_free_rq_map(hctx, driver_data);
1157+
return error;
11991158
}
12001159

12011160
static int blk_mq_init_hw_queues(struct request_queue *q,
@@ -1228,7 +1187,7 @@ static int blk_mq_init_hw_queues(struct request_queue *q,
12281187
blk_mq_hctx_notify, hctx);
12291188
blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
12301189

1231-
if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1190+
if (blk_mq_init_rq_map(hctx, reg, driver_data, node))
12321191
break;
12331192

12341193
/*
@@ -1268,7 +1227,7 @@ static int blk_mq_init_hw_queues(struct request_queue *q,
12681227
reg->ops->exit_hctx(hctx, j);
12691228

12701229
blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1271-
blk_mq_free_rq_map(hctx);
1230+
blk_mq_free_rq_map(hctx, driver_data);
12721231
kfree(hctx->ctxs);
12731232
}
12741233

@@ -1455,7 +1414,7 @@ void blk_mq_free_queue(struct request_queue *q)
14551414
queue_for_each_hw_ctx(q, hctx, i) {
14561415
kfree(hctx->ctx_map);
14571416
kfree(hctx->ctxs);
1458-
blk_mq_free_rq_map(hctx);
1417+
blk_mq_free_rq_map(hctx, q->queuedata);
14591418
blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
14601419
if (q->mq_ops->exit_hctx)
14611420
q->mq_ops->exit_hctx(hctx, i);

‎drivers/block/virtio_blk.c‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,22 @@ static const struct device_attribute dev_attr_cache_type_rw =
480480
__ATTR(cache_type, S_IRUGO|S_IWUSR,
481481
virtblk_cache_type_show, virtblk_cache_type_store);
482482

483+
static int virtblk_init_request(void *data, struct blk_mq_hw_ctx *hctx,
484+
struct request *rq, unsigned int nr)
485+
{
486+
struct virtio_blk *vblk = data;
487+
struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
488+
489+
sg_init_table(vbr->sg, vblk->sg_elems);
490+
return 0;
491+
}
492+
483493
static struct blk_mq_ops virtio_mq_ops = {
484494
.queue_rq = virtio_queue_rq,
485495
.map_queue = blk_mq_map_queue,
486496
.alloc_hctx = blk_mq_alloc_single_hw_queue,
487497
.free_hctx = blk_mq_free_single_hw_queue,
498+
.init_request = virtblk_init_request,
488499
.complete = virtblk_request_done,
489500
};
490501

@@ -497,16 +508,6 @@ static struct blk_mq_reg virtio_mq_reg = {
497508
};
498509
module_param_named(queue_depth, virtio_mq_reg.queue_depth, uint, 0444);
499510

500-
static int virtblk_init_vbr(void *data, struct blk_mq_hw_ctx *hctx,
501-
struct request *rq, unsigned int nr)
502-
{
503-
struct virtio_blk *vblk = data;
504-
struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
505-
506-
sg_init_table(vbr->sg, vblk->sg_elems);
507-
return 0;
508-
}
509-
510511
static int virtblk_probe(struct virtio_device *vdev)
511512
{
512513
struct virtio_blk *vblk;
@@ -577,8 +578,6 @@ static int virtblk_probe(struct virtio_device *vdev)
577578
goto out_put_disk;
578579
}
579580

580-
blk_mq_init_commands(q, virtblk_init_vbr, vblk);
581-
582581
q->queuedata = vblk;
583582

584583
virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);

‎include/linux/blk-mq.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ typedef struct blk_mq_hw_ctx *(alloc_hctx_fn)(struct blk_mq_reg *,unsigned int);
6767
typedef void (free_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
6868
typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
6969
typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
70+
typedef int (init_request_fn)(void *, struct blk_mq_hw_ctx *,
71+
struct request *, unsigned int);
72+
typedef void (exit_request_fn)(void *, struct blk_mq_hw_ctx *,
73+
struct request *, unsigned int);
7074

7175
struct blk_mq_ops {
7276
/*
@@ -99,6 +103,14 @@ struct blk_mq_ops {
99103
*/
100104
init_hctx_fn *init_hctx;
101105
exit_hctx_fn *exit_hctx;
106+
107+
/*
108+
* Called for every command allocated by the block layer to allow
109+
* the driver to set up driver specific data.
110+
* Ditto for exit/teardown.
111+
*/
112+
init_request_fn *init_request;
113+
exit_request_fn *exit_request;
102114
};
103115

104116
enum {
@@ -118,8 +130,6 @@ enum {
118130
struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
119131
int blk_mq_register_disk(struct gendisk *);
120132
void blk_mq_unregister_disk(struct gendisk *);
121-
int blk_mq_init_commands(struct request_queue *, int (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
122-
void blk_mq_free_commands(struct request_queue *, void (*free)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
123133

124134
void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
125135

0 commit comments

Comments
 (0)