block: fix mq request allocation
PATCH 1/1 block: fix mq request allocation
blk_mq_alloc_request_pinned() may return NULL request in case of !__GFP_WAIT, so cause its callers to derefence NULL pointer for releasing current context.
This patch introduces two flags to address the issue.
--- block/blk-mq.c | 27 ++++++++++++++++----------- block/blk-mq.h | 3 +++ 2 files changed, 19 insertions(+), 11 deletions(-)
block/blk-mq.c +16 -11
@@ -188,26 +188,32 @@ static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,static struct request *blk_mq_alloc_request_pinned (struct request_queue *q,int rw,gfp_t gfp, -bool reserved) +unsigned int flags) {struct request *rq; +struct blk_mq_ctx *ctx; +struct blk_mq_hw_ctx *hctx;do { -struct blk_mq_ctx *ctx =blk_mq_get_ctx (q); -struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue (q, ctx->cpu); + ctx =blk_mq_get_ctx (q); + hctx = q->mq_ops->map_queue (q, ctx->cpu); - rq =__blk_mq_alloc_request (hctx, gfp & ~__GFP_WAIT, reserved); + rq =__blk_mq_alloc_request (hctx, gfp & ~__GFP_WAIT, + !!(flags &MQ_ALLOC_RESERVED ));if (rq) {blk_mq_rq_ctx_init (q, ctx, rq, rw); -break ; + goto exit; }else if (!(gfp & __GFP_WAIT)) -break ; + goto exit;blk_mq_put_ctx (ctx);__blk_mq_run_hw_queue (hctx);blk_mq_wait_for_tags (hctx->tags); }while (1 ); +exit: +if (!(flags &MQ_ALLOC_HOLD_CTX )) +blk_mq_put_ctx (ctx);return rq; }
@@ -219,8 +225,8 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw,if (blk_mq_queue_enter (q))return NULL ; - rq =blk_mq_alloc_request_pinned (q, rw, gfp, reserved); -blk_mq_put_ctx (rq->mq_ctx); + rq =blk_mq_alloc_request_pinned (q, rw, gfp, reserved ? +MQ_ALLOC_RESERVED :0 );return rq; }
@@ -232,8 +238,7 @@ struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,if (blk_mq_queue_enter (q))return NULL ; - rq =blk_mq_alloc_request_pinned (q, rw, gfp, true); -blk_mq_put_ctx (rq->mq_ctx); + rq =blk_mq_alloc_request_pinned (q, rw, gfp,MQ_ALLOC_RESERVED );return rq; }EXPORT_SYMBOL (blk_mq_alloc_reserved_request);
@@ -890,7 +895,7 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)blk_mq_put_ctx (ctx);trace_block_sleeprq (q, bio, rw); rq =blk_mq_alloc_request_pinned (q, rw, __GFP_WAIT|GFP_ATOMIC , - false); +MQ_ALLOC_HOLD_CTX ); ctx = rq->mq_ctx; hctx = q->mq_ops->map_queue (q, ctx->cpu); }
block/blk-mq.h +3 -0
@@ -22,6 +22,9 @@ struct blk_mq_ctx {struct kobject kobj; }; +#define MQ_ALLOC_RESERVED (1U << 0) +#define MQ_ALLOC_HOLD_CTX (1U << 1) +void __blk_mq_end_io (struct request *rq,int error);void blk_mq_complete_request (struct request *rq,int error);void blk_mq_run_request (struct request *rq,bool run_queue,bool async);
Signature · 1 line
1.7.9.5
Quoted 5 lines · Ming Lei
blk_mq_alloc_request_pinned()may return NULL request in case of !__GFP_WAIT, so cause its callers to derefence NULL pointer for releasing current context.This patch introduces two flags to address the issue.
Hi, Ming,
Good catch, but your patch seems overly complicated. How about something like the following (compile-tested only), instead? Note that I did not touch blk_make_request, as the put_ctx there seems to correlate to a get_ctx earlier in the function (not a leaked reference from __blk_mq_alloc_request).
-Jeff
p.s. Jens, every time I see GFP_ATOMIC|__GFP_WAIT, my head explodes. Just sayin'.
block/blk-mq.c +8 -4
@@ -202,10 +202,12 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,if (rq) {blk_mq_rq_ctx_init (q, ctx, rq, rw);break ; - }else if (!(gfp & __GFP_WAIT)) -break ; + }blk_mq_put_ctx (ctx); +if (!(gfp & __GFP_WAIT)) +break ; +__blk_mq_run_hw_queue (hctx);blk_mq_wait_for_tags (hctx->tags); }while (1 );
@@ -222,7 +224,8 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw,return NULL ; rq =blk_mq_alloc_request_pinned (q, rw, gfp, reserved); -blk_mq_put_ctx (rq->mq_ctx); +if (rq) +blk_mq_put_ctx (rq->mq_ctx);return rq; }
@@ -235,7 +238,8 @@ struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,return NULL ; rq =blk_mq_alloc_request_pinned (q, rw, gfp, true); -blk_mq_put_ctx (rq->mq_ctx); +if (rq) +blk_mq_put_ctx (rq->mq_ctx);return rq; }EXPORT_SYMBOL (blk_mq_alloc_reserved_request);
Jens Axboe
maintainerQuoted 8 lines · Jeff Moyer
Quoted 5 lines · Ming Lei
blk_mq_alloc_request_pinned()may return NULL request in case of !__GFP_WAIT, so cause its callers to derefence NULL pointer for releasing current context.This patch introduces two flags to address the issue.
Quoted 8 lines
Hi, Ming,
Good catch, but your patch seems overly complicated. How about something like the following (compile-tested only), instead? Note that I did not touch
blk_make_request, as theput_ctxthere seems to correlate to aget_ctxearlier in the function (not a leaked reference from__blk_mq_alloc_request).
I would tend to agree, it's overly complicated. The bug is real, however.
Quoted 1 line
p.s. Jens, every time I see GFP_ATOMIC|__GFP_WAIT, my head explodes. Just sayin'.
It's perfectly fine :-)
Signature · 1 line
Jens Axboe
Quoted 12 lines · Jens Axboe
Quoted 11 lines · Jeff Moyer
Quoted 5 lines · Ming Lei
blk_mq_alloc_request_pinned()may return NULL request in case of !__GFP_WAIT, so cause its callers to derefence NULL pointer for releasing current context.This patch introduces two flags to address the issue.
Hi, Ming,
Quoted 7 lines
Quoted 5 lines
Good catch, but your patch seems overly complicated. How about something like the following (compile-tested only), instead? Note that I did not touch
blk_make_request, as theput_ctxthere seems to correlate to aget_ctxearlier in the function (not a leaked reference from__blk_mq_alloc_request).I would tend to agree, it's overly complicated. The bug is real, however.
Hmm, did I make it sound as though I thought it wasn't a bug? Your response leaves me wondering whether my patch made it to your inbox.
Quoted 3 lines
Quoted 1 line
p.s. Jens, every time I see GFP_ATOMIC|__GFP_WAIT, my head explodes. Just sayin'.
It's perfectly fine :-)
Sure, it's not *your* head! ;-)
-Jeff
Hi Jeff,
Quoted 6 lines · Jeff Moyer
Quoted 4 lines · Ming Lei
blk_mq_alloc_request_pinned()may return NULL request in case of !__GFP_WAIT, so cause its callers to derefence NULL pointer for releasing current context.
Quoted 7 lines
Quoted 1 line
This patch introduces two flags to address the issue.
Hi, Ming,
Good catch, but your patch seems overly complicated. How about something like the following (compile-tested only), instead? Note that
Looks your patch is better and simpler, :-)
Quoted 9 lines
I did not touch
blk_make_request, as theput_ctxthere seems to correlate to aget_ctxearlier in the function (not a leaked reference from__blk_mq_alloc_request).-Jeff
p.s. Jens, every time I see GFP_ATOMIC|__GFP_WAIT, my head explodes. Just sayin'.
Signed-off-by Jeff Moyer <[email protected]>
Quoted 32 lines
block/blk-mq.c +6
-3@@ -202,10 +202,12 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,if (rq) {blk_mq_rq_ctx_init (q, ctx, rq, rw);break ; - }else if (!(gfp & __GFP_WAIT)) -break ; + }blk_mq_put_ctx (ctx); +if (!(gfp & __GFP_WAIT)) +break ; +__blk_mq_run_hw_queue (hctx);blk_mq_wait_for_tags (hctx->tags); }while (1 );@@ -222,7 +224,8 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw,return NULL ; rq =blk_mq_alloc_request_pinned (q, rw, gfp, reserved); -blk_mq_put_ctx (rq->mq_ctx); +if (rq) +blk_mq_put_ctx (rq->mq_ctx);return rq; }
block/blk-mq.c struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, · line 245
Quoted 9 lines
block/blk-mq.c +2
-1@@ -235,7 +238,8 @@ struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,return NULL ; rq =blk_mq_alloc_request_pinned (q, rw, gfp, true); -blk_mq_put_ctx (rq->mq_ctx); +if (rq) +blk_mq_put_ctx (rq->mq_ctx);return rq; }EXPORT_SYMBOL (blk_mq_alloc_reserved_request);
Thanks,
Signature · 1 line
Ming Lei