unify and streamline the blk-mq make_request implementations V2
A bunch of cleanups to get us a nice I/O submission path.
Changes since V1:
- rebase on top of the recent blk_mq_try_issue_directly changes
- incorporate comments from Bart
PATCH 1/4 blk-mq: remove BLK_MQ_F_DEFER_ISSUE
This flag was never used since it was introduced.
--- block/blk-mq.c | 8 +------- include/linux/blk-mq.h | 1 - 2 files changed, 1 insertion(+), 8 deletions(-)
block/blk-mq.c +1 -7
@@ -1534,13 +1534,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) } plug = current->plug; -/* -* If the driver supports defer issued based on 'last', then -* queue it up like normal since we can potentially save some -* CPU this way. -*/ -if (((plug && !blk_queue_nomerges (q)) || is_sync) && - !(data.hctx->flags &BLK_MQ_F_DEFER_ISSUE )) { +if (((plug && !blk_queue_nomerges (q)) || is_sync)) {struct request *old_rq =NULL ;blk_mq_bio_to_request (rq, bio);
include/linux/blk-mq.h +0 -1
@@ -152,7 +152,6 @@ enum {BLK_MQ_F_SHOULD_MERGE =1 <<0 ,BLK_MQ_F_TAG_SHARED =1 <<1 ,BLK_MQ_F_SG_MERGE =1 <<2 , -BLK_MQ_F_DEFER_ISSUE =1 <<4 ,BLK_MQ_F_BLOCKING =1 <<5 ,BLK_MQ_F_NO_SCHED =1 <<6 , BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,
Signature · 1 line
2.11.0
Re: [PATCH 1/4] blk-mq: remove BLK_MQ_F_DEFER_ISSUE
On 03/20/2017 04:39 PM, Christoph Hellwig wrote:=0A=
Quoted 1 line
This flag was never used since it was introduced.=0A=
=0A=
Re: [PATCH 1/4] blk-mq: remove BLK_MQ_F_DEFER_ISSUE
In reply to Christoph HellwigQuoted 4 lines · Christoph Hellwig
This flag was never used since it was introduced.
Signed-off-by Christoph Hellwig <[email protected]>---
Signature · 6 lines
Johannes Thumshirn Storage [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg GF: Felix Imend�rffer, Jane Smithard, Graham Norton HRB 21284 (AG N�rnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
PATCH 2/4 blk-mq: merge mq and sq make_request instances
In reply to Christoph HellwigThey are mostly the same code anyway - this just one small conditional for the plug case that is different for both variants.
--- block/blk-mq.c | 164 +++++++++++---------------------------------------------- 1 file changed, 31 insertions(+), 133 deletions(-)
block/blk-mq.c +31 -133
@@ -1478,11 +1478,6 @@ static void blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie)blk_mq_sched_insert_request (rq, false, true, true, false); } -/* -* Multiple hardware queue variant. This will not use per-process plugs, -* but will attempt to bypass the hctx queueing if we can go straight to -* hardware for SYNC IO. -*/ static blk_qc_t blk_mq_make_request (struct request_queue *q,struct bio *bio) {const int is_sync =op_is_sync (bio->bi_opf);
@@ -1534,7 +1529,36 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) } plug = current->plug; -if (((plug && !blk_queue_nomerges (q)) || is_sync)) { +if (plug && q->nr_hw_queues ==1 ) { +struct request *last =NULL ; + +blk_mq_bio_to_request (rq, bio); + +/* +* @request_count may become stale because of schedule +* out, so check the list again. +*/ +if (list_empty (&plug->mq_list)) + request_count =0 ; +else if (blk_queue_nomerges (q)) + request_count =blk_plug_queued_count (q); + +if (!request_count) +trace_block_plug (q); +else + last =list_entry_rq (plug->mq_list.prev); + +blk_mq_put_ctx (data.ctx); + +if (request_count >=BLK_MAX_REQUEST_COUNT || (last && +blk_rq_bytes (last) >=BLK_PLUG_FLUSH_SIZE )) { +blk_flush_plug_list (plug, false); +trace_block_plug (q); + } + +list_add_tail (&rq->queuelist, &plug->mq_list); + goto done; + }else if (((plug && !blk_queue_nomerges (q)) || is_sync)) {struct request *old_rq =NULL ;blk_mq_bio_to_request (rq, bio);
@@ -1596,119 +1620,6 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)return cookie; } -/* -* Single hardware queue variant. This will attempt to use any per-process -* plug for merging and IO deferral. -*/ -static blk_qc_t blk_sq_make_request (struct request_queue *q,struct bio *bio) -{ -const int is_sync =op_is_sync (bio->bi_opf); -const int is_flush_fua =op_is_flush (bio->bi_opf); -struct blk_plug *plug; -unsigned int request_count =0 ; -struct blk_mq_alloc_data data = { .flags =0 }; -struct request *rq; -blk_qc_t cookie; -unsigned int wb_acct; - -blk_queue_bounce (q, &bio); - -if (bio_integrity_enabled (bio) &&bio_integrity_prep (bio)) { -bio_io_error (bio); -return BLK_QC_T_NONE ; - } - -blk_queue_split (q, &bio, q->bio_split); - -if (!is_flush_fua && !blk_queue_nomerges (q)) { -if (blk_attempt_plug_merge (q, bio, &request_count,NULL )) -return BLK_QC_T_NONE ; - }else - request_count =blk_plug_queued_count (q); - -if (blk_mq_sched_bio_merge (q, bio)) -return BLK_QC_T_NONE ; - - wb_acct =wbt_wait (q->rq_wb, bio,NULL ); - -trace_block_getrq (q, bio, bio->bi_opf); - - rq =blk_mq_sched_get_request (q, bio, bio->bi_opf, &data); -if (unlikely (!rq)) { -__wbt_done (q->rq_wb, wb_acct); -return BLK_QC_T_NONE ; - } - -wbt_track (&rq->issue_stat, wb_acct); - - cookie =request_to_qc_t (data.hctx, rq); - -if (unlikely (is_flush_fua)) { -if (q->elevator) - goto elv_insert; -blk_mq_bio_to_request (rq, bio); -blk_insert_flush (rq); - goto run_queue; - } - -/* -* A task plug currently exists. Since this is completely lockless, -* utilize that to temporarily store requests until the task is -* either done or scheduled away. -*/ - plug = current->plug; -if (plug) { -struct request *last =NULL ; - -blk_mq_bio_to_request (rq, bio); - -/* -* @request_count may become stale because of schedule -* out, so check the list again. -*/ -if (list_empty (&plug->mq_list)) - request_count =0 ; -if (!request_count) -trace_block_plug (q); -else - last =list_entry_rq (plug->mq_list.prev); - -blk_mq_put_ctx (data.ctx); - -if (request_count >=BLK_MAX_REQUEST_COUNT || (last && -blk_rq_bytes (last) >=BLK_PLUG_FLUSH_SIZE )) { -blk_flush_plug_list (plug, false); -trace_block_plug (q); - } - -list_add_tail (&rq->queuelist, &plug->mq_list); -return cookie; - } - -if (q->elevator) { -elv_insert: -blk_mq_put_ctx (data.ctx); -blk_mq_bio_to_request (rq, bio); -blk_mq_sched_insert_request (rq, false, true, - !is_sync || is_flush_fua, true); - goto done; - } -if (!blk_mq_merge_queue_io (data.hctx, data.ctx, rq, bio)) { -/* -* For a SYNC request, send it to the hardware immediately. For -* an ASYNC request, just ensure that we run it later on. The -* latter allows for merging opportunities and more efficient -* dispatching. -*/ -run_queue: -blk_mq_run_hw_queue (data.hctx, !is_sync || is_flush_fua); - } - -blk_mq_put_ctx (data.ctx); -done: -return cookie; -} -void blk_mq_free_rqs (struct blk_mq_tag_set *set,struct blk_mq_tags *tags,unsigned int hctx_idx) {
@@ -2366,10 +2277,7 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,INIT_LIST_HEAD (&q->requeue_list);spin_lock_init (&q->requeue_lock); -if (q->nr_hw_queues >1 ) -blk_queue_make_request (q, blk_mq_make_request); -else -blk_queue_make_request (q, blk_sq_make_request); +blk_queue_make_request (q, blk_mq_make_request);/* * Do this after blk_queue_make_request() overrides it...
@@ -2717,16 +2625,6 @@ void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) set->nr_hw_queues = nr_hw_queues;list_for_each_entry (q, &set->tag_list, tag_set_list) {blk_mq_realloc_hw_ctxs (set, q); - -/* -* Manually set the make_request_fn as blk_queue_make_request -* resets a lot of the queue settings. -*/ -if (q->nr_hw_queues >1 ) - q->make_request_fn = blk_mq_make_request; -else - q->make_request_fn = blk_sq_make_request; -blk_mq_queue_reinit (q, cpu_online_mask); }
Signature · 1 line
2.11.0
Re: [PATCH 2/4] blk-mq: merge mq and sq make_request instances
On 03/20/2017 04:39 PM, Christoph Hellwig wrote:=0A=
Review static blk_qc_t blk_mq_make_request(struct request=
Quoted 1 line
(quoted diff) +0
-0@@ -1534,7 +1529,36 @@ static blk_qc_t blk_mq_make_request(struct request=
_queue *q, struct bio *bio)=0A=
Review static blk_qc_t blk_mq_make_request(struct request= · line 1537
Quoted 10 lines
(quoted diff) +3
-1}=0A= =0A= plug =3D current->plug;=0A= - if (((plug && !blk_queue_nomerges(q)) || is_sync)) {=0A= + if (plug && q->nr_hw_queues =3D=3D 1) {=0A= + [ ... ]=0A= + } else if (((plug && !blk_queue_nomerges(q)) || is_sync)) {=0A= struct request *old_rq =3D NULL;=0A= =0A= blk_mq_bio_to_request(rq, bio);=0A=
=0A=
I think this patch will change the behavior for the plug =3D=3D NULL &&=0A= q->nr_hw_queues =3D=3D 1 && is_sync case: with this patch applied the code= =0A=
under "else if" will be executed for that case but that wasn't the case=0A= before this patch.=0A=
=0A=
Bart.=0A=
PATCH 3/4 blk-mq: improve blk_mq_try_issue_directly
In reply to Christoph HellwigRename blk_mq_try_issue_directly to __blk_mq_try_issue_directly and add a new wrapper that takes care of RCU / SRCU locking to avoid having boileplate code in the caller which would get duplicated with new callers.
--- block/blk-mq.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
block/blk-mq.c +18 -14
@@ -1434,7 +1434,7 @@ static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)return blk_tag_to_qc_t (rq->internal_tag, hctx->queue_num, true); } -static void blk_mq_try_issue_directly (struct request *rq,blk_qc_t *cookie) +static void __blk_mq_try_issue_directly (struct request *rq,blk_qc_t *cookie) {struct request_queue *q = rq->q;struct blk_mq_queue_data bd = {
@@ -1478,13 +1478,27 @@ static void blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie)blk_mq_sched_insert_request (rq, false, true, true, false); } +static void blk_mq_try_issue_directly (struct blk_mq_hw_ctx *hctx, +struct request *rq,blk_qc_t *cookie) +{ +if (!(hctx->flags &BLK_MQ_F_BLOCKING )) { +rcu_read_lock (); +__blk_mq_try_issue_directly (rq, cookie); +rcu_read_unlock (); + }else { +unsigned int srcu_idx =srcu_read_lock (&hctx->queue_rq_srcu); +__blk_mq_try_issue_directly (rq, cookie); +srcu_read_unlock (&hctx->queue_rq_srcu, srcu_idx); + } +} +static blk_qc_t blk_mq_make_request (struct request_queue *q,struct bio *bio) {const int is_sync =op_is_sync (bio->bi_opf);const int is_flush_fua =op_is_flush (bio->bi_opf);struct blk_mq_alloc_data data = { .flags =0 };struct request *rq; -unsigned int request_count =0 , srcu_idx; +unsigned int request_count =0 ;struct blk_plug *plug;struct request *same_queue_rq =NULL ;blk_qc_t cookie;
@@ -1582,18 +1596,8 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) }else /* is_sync */ old_rq = rq;blk_mq_put_ctx (data.ctx); -if (!old_rq) - goto done; - -if (!(data.hctx->flags &BLK_MQ_F_BLOCKING )) { -rcu_read_lock (); -blk_mq_try_issue_directly (old_rq, &cookie); -rcu_read_unlock (); - }else { - srcu_idx =srcu_read_lock (&data.hctx->queue_rq_srcu); -blk_mq_try_issue_directly (old_rq, &cookie); -srcu_read_unlock (&data.hctx->queue_rq_srcu, srcu_idx); - } +if (old_rq) +blk_mq_try_issue_directly (data.hctx, old_rq, &cookie); goto done; }
Signature · 1 line
2.11.0
Re: [PATCH 3/4] blk-mq: improve blk_mq_try_issue_directly
On 03/20/2017 04:39 PM, Christoph Hellwig wrote:=0A=
Quoted 1 line
Rename
blk_mq_try_issue_directlyto__blk_mq_try_issue_directlyand add a=
=0A=
Quoted 2 lines
new wrapper that takes care of RCU / SRCU locking to avoid having=0A= boileplate code in the caller which would get duplicated with new callers=
.=0A=
=0A=
PATCH 4/4 blk-mq: streamline blk_mq_make_request
In reply to Christoph HellwigTurn the different ways of merging or issuing I/O into a series of if/else statements instead of the current maze of gotos. Note that this means we pin the CPU a little longer for some cases as the CTX put is moved to common code at the end of the function.
--- block/blk-mq.c | 67 +++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 40 deletions(-)
block/blk-mq.c +27 -40
@@ -1534,16 +1534,17 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) cookie =request_to_qc_t (data.hctx, rq); + plug = current->plug;if (unlikely (is_flush_fua)) { -if (q->elevator) - goto elv_insert;blk_mq_bio_to_request (rq, bio); -blk_insert_flush (rq); - goto run_queue; - } - - plug = current->plug; -if (plug && q->nr_hw_queues ==1 ) { +if (q->elevator) { +blk_mq_sched_insert_request (rq, false, true, + !is_sync || is_flush_fua, true); + }else { +blk_insert_flush (rq); +blk_mq_run_hw_queue (data.hctx, !is_sync || is_flush_fua); + } + }else if (plug && q->nr_hw_queues ==1 ) {struct request *last =NULL ;blk_mq_bio_to_request (rq, bio);
@@ -1562,8 +1563,6 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)else last =list_entry_rq (plug->mq_list.prev); -blk_mq_put_ctx (data.ctx); -if (request_count >=BLK_MAX_REQUEST_COUNT || (last &&blk_rq_bytes (last) >=BLK_PLUG_FLUSH_SIZE )) {blk_flush_plug_list (plug, false);
@@ -1571,56 +1570,44 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) }list_add_tail (&rq->queuelist, &plug->mq_list); - goto done; - }else if (((plug && !blk_queue_nomerges (q)) || is_sync)) { -struct request *old_rq =NULL ; - + }else if (plug && !blk_queue_nomerges (q)) {blk_mq_bio_to_request (rq, bio);/* * We do limited plugging. If the bio can be merged, do that. * Otherwise the existing request in the plug list will be * issued. So the plug list will have one request at most +* +* The plug list might get flushed before this. If that happens, +* the plug list is emptry and same_queue_rq is invalid. */ -if (plug) { -/* -* The plug list might get flushed before this. If that -* happens, same_queue_rq is invalid and plug list is -* empty -*/ -if (same_queue_rq && !list_empty (&plug->mq_list)) { - old_rq = same_queue_rq; -list_del_init (&old_rq->queuelist); - } -list_add_tail (&rq->queuelist, &plug->mq_list); - }else /* is_sync */ - old_rq = rq; -blk_mq_put_ctx (data.ctx); -if (old_rq) -blk_mq_try_issue_directly (data.hctx, old_rq, &cookie); - goto done; - } +if (!list_empty (&plug->mq_list)) +list_del_init (&same_queue_rq->queuelist); +else + same_queue_rq =NULL ; -if (q->elevator) { -elv_insert: -blk_mq_put_ctx (data.ctx); +list_add_tail (&rq->queuelist, &plug->mq_list); +if (same_queue_rq) +blk_mq_try_issue_directly (data.hctx, same_queue_rq, + &cookie); + }else if (is_sync) { +blk_mq_bio_to_request (rq, bio); +blk_mq_try_issue_directly (data.hctx, rq, &cookie); + }else if (q->elevator) {blk_mq_bio_to_request (rq, bio);blk_mq_sched_insert_request (rq, false, true, !is_sync || is_flush_fua, true); - goto done; - } -if (!blk_mq_merge_queue_io (data.hctx, data.ctx, rq, bio)) { + }else if (!blk_mq_merge_queue_io (data.hctx, data.ctx, rq, bio)) {/* * For a SYNC request, send it to the hardware immediately. For * an ASYNC request, just ensure that we run it later on. The * latter allows for merging opportunities and more efficient * dispatching. */ -run_queue:blk_mq_run_hw_queue (data.hctx, !is_sync || is_flush_fua); } +blk_mq_put_ctx (data.ctx); -done:return cookie; }
Signature · 1 line
2.11.0
Quoted 3 lines · Christoph Hellwig
Changes since V1:
=A0=A0- rebase on top of the recentblk_mq_try_issue_directlychanges =A0=A0- incorporate comments from Bart
Hi Christoph,
It seems to me like none of the three comments I had posted on patch 4/4 have been addressed. Please have another look at these comments.
Thanks,
Bart.
=A0=
Quoted 9 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig
Changes since V1:
��- rebase on top of the recentblk_mq_try_issue_directlychanges ��- incorporate comments from BartHi Christoph,
It seems to me like none of the three comments I had posted on patch 4/4 have been addressed. Please have another look at these comments.
I had a look but resend the same old series again due to a rebase bug. I'll resend it ASAP with what should address your comments to the previous series as well as this one.