blk-mq: set RQF_USE_SCHED when the operation is known
PATCH v2 1/2 blk-mq: set RQF_USE_SCHED when the operation is known
From: Keith Busch <[email protected]>
The cached requests are allocated for one operation but can be handed out for another. A passthrough command has RQF_USE_SCHED cleared, so using those flags for a subsequent read/write bio will insert it into the scheduler without ->prepare_request() and frees it without ->finish_request(). For kyber, this leaks the domain token acquired at dispatch and stalls the queue.
Don't set RQF_USE_SCHED based on the first operation the batch happened to be allocated for. Instead, set it after the request is claimed by an operation. Introduce a helper function, blk_mq_rq_late_init() for this, and absorb blk_mq_rq_time_init() since the two run together.
---v1->v2:
Introduced a new helper function for the late request init
Split off the flush handling into a separate patch.
Updated mq-deadline comment
block/blk-mq.c | 59 ++++++++++++++++++++++++++++----------------- block/mq-deadline.c | 2 +- 2 files changed, 38 insertions(+), 23 deletions(-)
block/blk-mq.c +37 -22
@@ -447,16 +447,6 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,WRITE_ONCE (rq->deadline,0 );req_ref_set (rq,1 ); -if (rq->rq_flags &RQF_USE_SCHED ) { -struct elevator_queue *e = data->q->elevator; - -INIT_HLIST_NODE (&rq->hash); -RB_CLEAR_NODE (&rq->rb_node); - -if (e->type->ops.prepare_request) - e->type->ops.prepare_request (rq); - } -return rq; }
@@ -498,6 +488,12 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)return rq_list_pop (data->cached_rqs); } +static bool blk_op_bypass_sched (blk_opf_t opf) +{ +return (opf &REQ_OP_MASK ) ==REQ_OP_FLUSH || +blk_op_is_passthrough (opf); +} +static void blk_mq_limit_depth (struct blk_mq_alloc_data *data) {struct elevator_mq_ops *ops;
@@ -518,12 +514,10 @@ static void blk_mq_limit_depth(struct blk_mq_alloc_data *data)* Flush/passthrough requests are special and go directly to the * dispatch list, they are not subject to the async_depth limit. */ -if ((data->cmd_flags &REQ_OP_MASK ) ==REQ_OP_FLUSH || -blk_op_is_passthrough (data->cmd_flags)) +if (blk_op_bypass_sched (data->cmd_flags))return ;WARN_ON_ONCE (data->flags &BLK_MQ_REQ_RESERVED ); - data->rq_flags |=RQF_USE_SCHED ;/* * By default, sync requests have no limit, and async requests are
@@ -534,6 +528,29 @@ static void blk_mq_limit_depth(struct blk_mq_alloc_data *data) ops->limit_depth (data->cmd_flags, data); } +/* +* Finish initializing a request once it has been claimed for an operation. +* Cached requests are allocated before that operation is known. +*/ +static void blk_mq_rq_late_init (struct request *rq,u64 alloc_time_ns) +{ +struct elevator_queue *e; + +blk_mq_rq_time_init (rq, alloc_time_ns); + +if (!(rq->rq_flags &RQF_SCHED_TAGS ) || (rq->rq_flags &RQF_RESV ) || +blk_op_bypass_sched (rq->cmd_flags)) +return ; + + rq->rq_flags |=RQF_USE_SCHED ; +INIT_HLIST_NODE (&rq->hash); +RB_CLEAR_NODE (&rq->rb_node); + + e = rq->q->elevator; +if (e->type->ops.prepare_request) + e->type->ops.prepare_request (rq); +} +static struct request *__blk_mq_alloc_requests (struct blk_mq_alloc_data *data) {struct request_queue *q = data->q;
@@ -562,7 +579,7 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)if (data->nr_tags >1 ) { rq =__blk_mq_alloc_requests_batch (data);if (rq) { -blk_mq_rq_time_init (rq, alloc_time_ns); +blk_mq_rq_late_init (rq, alloc_time_ns);return rq; } data->nr_tags =1 ;
@@ -590,7 +607,7 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)if (!(data->rq_flags &RQF_SCHED_TAGS ))blk_mq_inc_active_requests (data->hctx); rq =blk_mq_rq_ctx_init (data,blk_mq_tags_from_data (data), tag); -blk_mq_rq_time_init (rq, alloc_time_ns); +blk_mq_rq_late_init (rq, alloc_time_ns);return rq; }
@@ -637,8 +654,6 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,if (plug->nr_ios ==1 )return NULL ; rq =blk_mq_rq_cache_fill (q, plug, opf, flags); -if (!rq) -return NULL ; }else { rq =rq_list_peek (&plug->cached_rqs);if (!rq || rq->q != q)
@@ -650,11 +665,11 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,return NULL ;rq_list_pop (&plug->cached_rqs); -blk_mq_rq_time_init (rq,blk_time_get_ns ()); + rq->cmd_flags = opf; +INIT_LIST_HEAD (&rq->queuelist); +blk_mq_rq_late_init (rq,blk_time_get_ns ()); } - rq->cmd_flags = opf; -INIT_LIST_HEAD (&rq->queuelist);return rq; }
@@ -766,7 +781,7 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,if (!(data.rq_flags &RQF_SCHED_TAGS ))blk_mq_inc_active_requests (data.hctx); rq =blk_mq_rq_ctx_init (&data,blk_mq_tags_from_data (&data), tag); -blk_mq_rq_time_init (rq, alloc_time_ns); +blk_mq_rq_late_init (rq, alloc_time_ns); rq->__data_len =0 ; rq->phys_gap_bit =0 ; rq->__sector = (sector_t) -1 ;
@@ -3163,9 +3178,9 @@ void blk_mq_submit_bio(struct bio *bio) new_request:if (rq) {rq_qos_throttle (rq->q, bio); -blk_mq_rq_time_init (rq,blk_time_get_ns ()); rq->cmd_flags = bio->bi_opf;INIT_LIST_HEAD (&rq->queuelist); +blk_mq_rq_late_init (rq,blk_time_get_ns ()); }else { rq =blk_mq_get_new_requests (q, plug, bio);if (unlikely (!rq)) {
block/mq-deadline.c +1 -1
@@ -685,7 +685,7 @@ static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,blk_mq_free_requests (&free); } -/* Callback from inside blk_mq_rq_ctx_init(). */ +/* Callback from inside blk_mq_rq_late_init(). */ static void dd_prepare_request (struct request *rq) { rq->elv.priv[0 ] =NULL ;
Signature · 1 line
2.52.0
PATCH v2 2/2 blk-mq: allow cached requests to be used for flush operations
From: Keith Busch <[email protected]>
The cached request lookups reject a request whose op_is_flush() state does not match the incoming operation. That check is from when rq->elv and rq->flush shared a union, so a request could not be both a scheduler request and carry flush state. Commit be4c427809b0 ("blk-mq: use the I/O scheduler for writes from the flush state machine") made them separate members of struct request.
A cached request now gets its scheduler state when it is claimed by an operation, so one claimed for a flush is set up exactly as a freshly allocated request. Drop the check so a cached batch can mix flush and non-flush operations.
--- block/blk-mq.c | 4 ---- 1 file changed, 4 deletions(-)
block/blk-mq.c +0 -4
@@ -661,8 +661,6 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,if (blk_mq_get_hctx_type (opf) != rq->mq_hctx->type)return NULL ; -if (op_is_flush (rq->cmd_flags) !=op_is_flush (opf)) -return NULL ;rq_list_pop (&plug->cached_rqs); rq->cmd_flags = opf;
@@ -3075,8 +3073,6 @@ static struct request *blk_mq_get_cached_request(struct blk_plug *plug,if (type != rq->mq_hctx->type && (type !=HCTX_TYPE_READ || rq->mq_hctx->type !=HCTX_TYPE_DEFAULT ))return NULL ; -if (op_is_flush (rq->cmd_flags) !=op_is_flush (opf)) -return NULL ;rq_list_pop (&plug->cached_rqs);return rq; }
Signature · 1 line
2.52.0
Re: [PATCHv2 2/2] blk-mq: allow cached requests to be used for flush operations
Looks good:
Looks good:
Quoted 8 lines · Keith Busch
The cached requests are allocated for one operation but can be handed out for another. A passthrough command has
RQF_USE_SCHEDcleared, so using those flags for a subsequent read/write bio will insert it into the scheduler without ->prepare_request() and frees it without ->finish_request(). For kyber, this leaks the domain token acquired at dispatch and stalls the queue.[...]
Applied, thanks!
[1/2] blk-mq: set RQF_USE_SCHED when the operation is known
commit: ab6c756f28c741d704a7c3e04814bfc3adf6f818
[2/2] blk-mq: allow cached requests to be used for flush operations
commit: 9d2c70986bb7838c1c441a93bda415eecb52f3ddBest regards,
Signature · 1 line
Jens Axboe