Skip to content

Commit bd6737f

Browse files
committed
blk-mq-sched: add flush insertion into blk_mq_sched_insert_request()
Instead of letting the caller check this and handle the details of inserting a flush request, put the logic in the scheduler insertion function. This fixes direct flush insertion outside of the usual make_request_fn calls, like from dm via blk_insert_cloned_request(). Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent f73f44e commit bd6737f

8 files changed

Lines changed: 89 additions & 55 deletions

File tree

‎block/blk-core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
21292129
if (q->mq_ops) {
21302130
if (blk_queue_io_stat(q))
21312131
blk_account_io_start(rq, true);
2132-
blk_mq_sched_insert_request(rq, false, true, false);
2132+
blk_mq_sched_insert_request(rq, false, true, false, false);
21332133
return 0;
21342134
}
21352135

‎block/blk-exec.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
6666
* be reused after dying flag is set
6767
*/
6868
if (q->mq_ops) {
69-
blk_mq_sched_insert_request(rq, at_head, true, false);
69+
blk_mq_sched_insert_request(rq, at_head, true, false, false);
7070
return;
7171
}
7272

‎block/blk-flush.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ void blk_insert_flush(struct request *rq)
456456
if ((policy & REQ_FSEQ_DATA) &&
457457
!(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
458458
if (q->mq_ops)
459-
blk_mq_sched_insert_request(rq, false, true, false);
459+
blk_mq_sched_insert_request(rq, false, true, false, false);
460460
else
461461
list_add_tail(&rq->queuelist, &q->queue_head);
462462
return;

‎block/blk-mq-sched.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,64 @@ void blk_mq_sched_restart_queues(struct blk_mq_hw_ctx *hctx)
335335
}
336336
}
337337

338+
/*
339+
* Add flush/fua to the queue. If we fail getting a driver tag, then
340+
* punt to the requeue list. Requeue will re-invoke us from a context
341+
* that's safe to block from.
342+
*/
343+
static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx,
344+
struct request *rq, bool can_block)
345+
{
346+
if (blk_mq_get_driver_tag(rq, &hctx, can_block)) {
347+
blk_insert_flush(rq);
348+
blk_mq_run_hw_queue(hctx, true);
349+
} else
350+
blk_mq_add_to_requeue_list(rq, true, true);
351+
}
352+
353+
void blk_mq_sched_insert_request(struct request *rq, bool at_head,
354+
bool run_queue, bool async, bool can_block)
355+
{
356+
struct request_queue *q = rq->q;
357+
struct elevator_queue *e = q->elevator;
358+
struct blk_mq_ctx *ctx = rq->mq_ctx;
359+
struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
360+
361+
if (rq->tag == -1 && (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))) {
362+
blk_mq_sched_insert_flush(hctx, rq, can_block);
363+
return;
364+
}
365+
366+
if (e && e->type->ops.mq.insert_requests) {
367+
LIST_HEAD(list);
368+
369+
list_add(&rq->queuelist, &list);
370+
e->type->ops.mq.insert_requests(hctx, &list, at_head);
371+
} else {
372+
spin_lock(&ctx->lock);
373+
__blk_mq_insert_request(hctx, rq, at_head);
374+
spin_unlock(&ctx->lock);
375+
}
376+
377+
if (run_queue)
378+
blk_mq_run_hw_queue(hctx, async);
379+
}
380+
381+
void blk_mq_sched_insert_requests(struct request_queue *q,
382+
struct blk_mq_ctx *ctx,
383+
struct list_head *list, bool run_queue_async)
384+
{
385+
struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
386+
struct elevator_queue *e = hctx->queue->elevator;
387+
388+
if (e && e->type->ops.mq.insert_requests)
389+
e->type->ops.mq.insert_requests(hctx, list, false);
390+
else
391+
blk_mq_insert_requests(hctx, ctx, list);
392+
393+
blk_mq_run_hw_queue(hctx, run_queue_async);
394+
}
395+
338396
static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
339397
struct blk_mq_hw_ctx *hctx,
340398
unsigned int hctx_idx)

‎block/blk-mq-sched.h‎

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio);
2121
bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq);
2222
void blk_mq_sched_restart_queues(struct blk_mq_hw_ctx *hctx);
2323

24+
void blk_mq_sched_insert_request(struct request *rq, bool at_head,
25+
bool run_queue, bool async, bool can_block);
26+
void blk_mq_sched_insert_requests(struct request_queue *q,
27+
struct blk_mq_ctx *ctx,
28+
struct list_head *list, bool run_queue_async);
29+
2430
void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx);
2531
void blk_mq_sched_move_to_dispatch(struct blk_mq_hw_ctx *hctx,
2632
struct list_head *rq_list,
@@ -62,45 +68,6 @@ static inline void blk_mq_sched_put_rq_priv(struct request_queue *q,
6268
e->type->ops.mq.put_rq_priv(q, rq);
6369
}
6470

65-
static inline void
66-
blk_mq_sched_insert_request(struct request *rq, bool at_head, bool run_queue,
67-
bool async)
68-
{
69-
struct request_queue *q = rq->q;
70-
struct elevator_queue *e = q->elevator;
71-
struct blk_mq_ctx *ctx = rq->mq_ctx;
72-
struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
73-
74-
if (e && e->type->ops.mq.insert_requests) {
75-
LIST_HEAD(list);
76-
77-
list_add(&rq->queuelist, &list);
78-
e->type->ops.mq.insert_requests(hctx, &list, at_head);
79-
} else {
80-
spin_lock(&ctx->lock);
81-
__blk_mq_insert_request(hctx, rq, at_head);
82-
spin_unlock(&ctx->lock);
83-
}
84-
85-
if (run_queue)
86-
blk_mq_run_hw_queue(hctx, async);
87-
}
88-
89-
static inline void
90-
blk_mq_sched_insert_requests(struct request_queue *q, struct blk_mq_ctx *ctx,
91-
struct list_head *list, bool run_queue_async)
92-
{
93-
struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
94-
struct elevator_queue *e = hctx->queue->elevator;
95-
96-
if (e && e->type->ops.mq.insert_requests)
97-
e->type->ops.mq.insert_requests(hctx, list, false);
98-
else
99-
blk_mq_insert_requests(hctx, ctx, list);
100-
101-
blk_mq_run_hw_queue(hctx, run_queue_async);
102-
}
103-
10471
static inline bool
10572
blk_mq_sched_allow_merge(struct request_queue *q, struct request *rq,
10673
struct bio *bio)

‎block/blk-mq-tag.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
106106
struct sbq_wait_state *ws;
107107
DEFINE_WAIT(wait);
108108
unsigned int tag_offset;
109+
bool drop_ctx;
109110
int tag;
110111

111112
if (data->flags & BLK_MQ_REQ_RESERVED) {
@@ -128,6 +129,7 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
128129
return BLK_MQ_TAG_FAIL;
129130

130131
ws = bt_wait_ptr(bt, data->hctx);
132+
drop_ctx = data->ctx == NULL;
131133
do {
132134
prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
133135

@@ -150,7 +152,8 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
150152
if (tag != -1)
151153
break;
152154

153-
blk_mq_put_ctx(data->ctx);
155+
if (data->ctx)
156+
blk_mq_put_ctx(data->ctx);
154157

155158
io_schedule();
156159

@@ -166,6 +169,9 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
166169
ws = bt_wait_ptr(bt, data->hctx);
167170
} while (1);
168171

172+
if (drop_ctx && data->ctx)
173+
blk_mq_put_ctx(data->ctx);
174+
169175
finish_wait(&ws->wait, &wait);
170176

171177
found_tag:

‎block/blk-mq.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ static void blk_mq_requeue_work(struct work_struct *work)
568568

569569
rq->rq_flags &= ~RQF_SOFTBARRIER;
570570
list_del_init(&rq->queuelist);
571-
blk_mq_sched_insert_request(rq, true, false, false);
571+
blk_mq_sched_insert_request(rq, true, false, false, true);
572572
}
573573

574574
while (!list_empty(&rq_list)) {
575575
rq = list_entry(rq_list.next, struct request, queuelist);
576576
list_del_init(&rq->queuelist);
577-
blk_mq_sched_insert_request(rq, false, false, false);
577+
blk_mq_sched_insert_request(rq, false, false, false, true);
578578
}
579579

580580
blk_mq_run_hw_queues(q, false);
@@ -847,12 +847,11 @@ static inline unsigned int queued_to_index(unsigned int queued)
847847
return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
848848
}
849849

850-
static bool blk_mq_get_driver_tag(struct request *rq,
851-
struct blk_mq_hw_ctx **hctx, bool wait)
850+
bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
851+
bool wait)
852852
{
853853
struct blk_mq_alloc_data data = {
854854
.q = rq->q,
855-
.ctx = rq->mq_ctx,
856855
.hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
857856
.flags = wait ? 0 : BLK_MQ_REQ_NOWAIT,
858857
};
@@ -1395,7 +1394,7 @@ static void blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie)
13951394
}
13961395

13971396
insert:
1398-
blk_mq_sched_insert_request(rq, false, true, true);
1397+
blk_mq_sched_insert_request(rq, false, true, true, false);
13991398
}
14001399

14011400
/*
@@ -1446,10 +1445,12 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
14461445
cookie = request_to_qc_t(data.hctx, rq);
14471446

14481447
if (unlikely(is_flush_fua)) {
1448+
blk_mq_put_ctx(data.ctx);
14491449
blk_mq_bio_to_request(rq, bio);
14501450
blk_mq_get_driver_tag(rq, NULL, true);
14511451
blk_insert_flush(rq);
1452-
goto run_queue;
1452+
blk_mq_run_hw_queue(data.hctx, true);
1453+
goto done;
14531454
}
14541455

14551456
plug = current->plug;
@@ -1502,7 +1503,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
15021503
blk_mq_put_ctx(data.ctx);
15031504
blk_mq_bio_to_request(rq, bio);
15041505
blk_mq_sched_insert_request(rq, false, true,
1505-
!is_sync || is_flush_fua);
1506+
!is_sync || is_flush_fua, true);
15061507
goto done;
15071508
}
15081509
if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
@@ -1512,7 +1513,6 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
15121513
* latter allows for merging opportunities and more efficient
15131514
* dispatching.
15141515
*/
1515-
run_queue:
15161516
blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
15171517
}
15181518
blk_mq_put_ctx(data.ctx);
@@ -1568,10 +1568,12 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
15681568
cookie = request_to_qc_t(data.hctx, rq);
15691569

15701570
if (unlikely(is_flush_fua)) {
1571+
blk_mq_put_ctx(data.ctx);
15711572
blk_mq_bio_to_request(rq, bio);
15721573
blk_mq_get_driver_tag(rq, NULL, true);
15731574
blk_insert_flush(rq);
1574-
goto run_queue;
1575+
blk_mq_run_hw_queue(data.hctx, true);
1576+
goto done;
15751577
}
15761578

15771579
/*
@@ -1612,7 +1614,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
16121614
blk_mq_put_ctx(data.ctx);
16131615
blk_mq_bio_to_request(rq, bio);
16141616
blk_mq_sched_insert_request(rq, false, true,
1615-
!is_sync || is_flush_fua);
1617+
!is_sync || is_flush_fua, true);
16161618
goto done;
16171619
}
16181620
if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
@@ -1622,7 +1624,6 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
16221624
* latter allows for merging opportunities and more efficient
16231625
* dispatching.
16241626
*/
1625-
run_queue:
16261627
blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
16271628
}
16281629

‎block/blk-mq.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void blk_mq_wake_waiters(struct request_queue *q);
3434
bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *, struct list_head *);
3535
void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
3636
bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx);
37+
bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
38+
bool wait);
3739

3840
/*
3941
* Internal helpers for allocating/freeing the request map

0 commit comments

Comments
 (0)