blk-mq: always take a queue reference in blk_mq_submit_bio
PATCH 1/1 blk-mq: always take a queue reference in blk_mq_submit_bio
When submitting a bio to blk-mq, if the task should sleep after peeking a cached request, but before it pops it, the plug flushes and calls blk_mq_free_plug_rqs, freeing the cached_rqs. This creates a use-after-free bug.
Fix this by alway grabbing a queue usage reference in blk_mq_submit_bio. While past commit claims that this is slow, we're better safe than fast as a first priority.
The code had already warned of this possibility, and specifically popped the request before other known blocking calls, but it didn't handle a blocking GFP_NOIO alloc. Under memory pressure, allocating the split bio or the integrity payload are two such cases that can block. The blk-mq submit_bio function continues using the peeked request that was already freed and re-initialized, so the driver receives that request with a NULL'ed mq_hctx, and inevitably panics.
Large parts of the commit message are stolen from an earlier attempt to fix this issue by Keith Busch <[email protected]>.
--- block/blk-mq.c | 65 +++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 41 deletions(-)
block/blk-mq.c +24 -41
@@ -3075,43 +3075,42 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q, }/* -* Check if there is a suitable cached request and return it. +* Check if there is a suitable cached request and use it if possible. */ -static struct request *blk_mq_peek_cached_request (struct blk_plug *plug, -struct request_queue *q,blk_opf_t opf) +static struct request *blk_mq_pop_cached_request (struct blk_plug *plug, +struct request_queue *q,struct bio *bio) { -enum hctx_type type =blk_mq_get_hctx_type (opf); +enum hctx_type type =blk_mq_get_hctx_type (bio->bi_opf);struct request *rq;if (!plug)return NULL ; + +/* +* Note: we must not sleep between the peek of the cached_rqs list here +* and the pop below. +*/ rq =rq_list_peek (&plug->cached_rqs);if (!rq || rq->q != q)return NULL ;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)) +if (op_is_flush (rq->cmd_flags) !=op_is_flush (bio->bi_opf))return NULL ; -return rq; -} - -static void blk_mq_use_cached_rq (struct request *rq,struct blk_plug *plug, -struct bio *bio) -{if (rq_list_pop (&plug->cached_rqs) != rq)WARN_ON_ONCE (1 ); -/* -* If any qos ->throttle() end up blocking, we will have flushed the -* plug and hence killed the cached_rq list as well. Pop this entry -* before we throttle. -*/ 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); + +/* +* Drop the extra queue reference from the cached request. +*/ +blk_queue_exit (q); +return rq; }static bool bio_unaligned (const struct bio *bio,struct request_queue *q)
@@ -3149,11 +3148,6 @@ void blk_mq_submit_bio(struct bio *bio)struct request *rq;blk_status_t ret; -/* -* If the plug has a cached request for this queue, try to use it. -*/ - rq =blk_mq_peek_cached_request (plug, q, bio->bi_opf); -/* * A BIO that was released from a zone write plug has already been * through the preparation in this function, already holds a reference
@@ -3162,19 +3156,11 @@ void blk_mq_submit_bio(struct bio *bio)*/ if (bio_zone_write_plugging (bio)) { nr_segs = bio->__bi_nr_segments; -if (rq) -blk_queue_exit (q); goto new_request; } -/* -* The cached request already holds a q_usage_counter reference and we -* don't have to acquire a new one if we use it. -*/ -if (!rq) { -if (unlikely (bio_queue_enter (bio))) -return ; - } +if (unlikely (bio_queue_enter (bio))) +return ;/* * Device reconfiguration may change logical block size or reduce the
@@ -3210,9 +3196,11 @@ void blk_mq_submit_bio(struct bio *bio) } new_request: -if (rq) { -blk_mq_use_cached_rq (rq, plug, bio); - }else { +/* +* If the plug has a cached request for this queue, try to use it. +*/ + rq =blk_mq_pop_cached_request (plug, q, bio); +if (!rq) { rq =blk_mq_get_new_requests (q, plug, bio);if (unlikely (!rq)) {if (bio->bi_opf &REQ_NOWAIT )
@@ -3257,12 +3245,7 @@ void blk_mq_submit_bio(struct bio *bio)return ; queue_exit: -/* -* Don't drop the queue reference if we were trying to use a cached -* request and thus didn't acquire one. -*/ -if (!rq) -blk_queue_exit (q); +blk_queue_exit (q); }#ifdef CONFIG_BLK_MQ_STACKING
Signature · 1 line
2.53.0
Quoted 9 lines · Christoph Hellwig
When submitting a bio to blk-mq, if the task should sleep after peeking a cached request, but before it pops it, the plug flushes and calls
blk_mq_free_plug_rqs, freeing thecached_rqs. This creates a use-after-free bug.Fix this by alway grabbing a queue usage reference in
blk_mq_submit_bio. While past commit claims that this is slow, we're better safe than fast as a first priority.
Quoted 7 lines
The code had already warned of this possibility, and specifically popped the request before other known blocking calls, but it didn't handle a blocking
GFP_NOIOalloc. Under memory pressure, allocating the split bio or the integrity payload are two such cases that can block. The blk-mqsubmit_biofunction continues using the peeked request that was already freed and re-initialized, so the driver receives that request with a NULL'edmq_hctx, and inevitably panics.
Thanks, this looks good to me!
Jens Axboe
maintainerQuoted 17 lines · Christoph Hellwig
When submitting a bio to blk-mq, if the task should sleep after peeking a cached request, but before it pops it, the plug flushes and calls
blk_mq_free_plug_rqs, freeing thecached_rqs. This creates a use-after-free bug.Fix this by alway grabbing a queue usage reference in
blk_mq_submit_bio. While past commit claims that this is slow, we're better safe than fast as a first priority.The code had already warned of this possibility, and specifically popped the request before other known blocking calls, but it didn't handle a blocking
GFP_NOIOalloc. Under memory pressure, allocating the split bio or the integrity payload are two such cases that can block. The blk-mqsubmit_biofunction continues using the peeked request that was already freed and re-initialized, so the driver receives that request with a NULL'edmq_hctx, and inevitably panics.
Quoted 2 lines
Large parts of the commit message are stolen from an earlier attempt to fix this issue by Keith Busch <[email protected]>.
This is about a 5% regression in peak performance, fwiw. I'm all for eliminating this issue, but this kind of big hammer approach is pretty crap in terms of adding overhead to the IO path. Maybe we just cover blocking split bio case, and look into doing a proper fix for 7.2 that isn't such a massive regression?
Signature · 1 line
Jens Axboe
Quoted 5 lines · Jens Axboe
This is about a 5% regression in peak performance, fwiw. I'm all for eliminating this issue, but this kind of big hammer approach is pretty crap in terms of adding overhead to the IO path. Maybe we just cover blocking split bio case, and look into doing a proper fix for 7.2 that isn't such a massive regression?
Well, that assumes we have an idea how to fix it? So much overhead for a simple per-cpu counters sounds really weird to start with. We'd need to look at the numbers from your setup as I don't have anything equivalent.
I'd honestly still prefer to get this right first over optimizations either way.