Skip to content

Commit c2da19e

Browse files
Ming Leiaxboe
authored andcommitted
blk-mq: fix kernel panic during iterating over flush request
For fixing use-after-free during iterating over requests, we grabbed request's refcount before calling ->fn in commit 2e315dc ("blk-mq: grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter"). Turns out this way may cause kernel panic when iterating over one flush request: 1) old flush request's tag is just released, and this tag is reused by one new request, but ->rqs[] isn't updated yet 2) the flush request can be re-used for submitting one new flush command, so blk_rq_init() is called at the same time 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called, flush_rq->end_io may not be updated yet, so NULL pointer dereference is triggered in blk_mq_put_rq_ref(). Fix the issue by calling refcount_set(&flush_rq->ref, 1) after flush_rq->end_io is set. So far the only other caller of blk_rq_init() is scsi_ioctl_reset() in which the request doesn't enter block IO stack and the request reference count isn't used, so the change is safe. Fixes: 2e315dc ("blk-mq: grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter") Reported-by: "Blank-Burian, Markus, Dr." <[email protected]> Tested-by: "Blank-Burian, Markus, Dr." <[email protected]> Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: John Garry <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent c797b40 commit c2da19e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

block/blk-core.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
122122
rq->internal_tag = BLK_MQ_NO_TAG;
123123
rq->start_time_ns = ktime_get_ns();
124124
rq->part = NULL;
125-
refcount_set(&rq->ref, 1);
126125
blk_crypto_rq_set_defaults(rq);
127126
}
128127
EXPORT_SYMBOL(blk_rq_init);

block/blk-flush.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
329329
flush_rq->rq_flags |= RQF_FLUSH_SEQ;
330330
flush_rq->rq_disk = first_rq->rq_disk;
331331
flush_rq->end_io = flush_end_io;
332+
/*
333+
* Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
334+
* implied in refcount_inc_not_zero() called from
335+
* blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
336+
* and READ flush_rq->end_io
337+
*/
338+
smp_wmb();
339+
refcount_set(&flush_rq->ref, 1);
332340

333341
blk_flush_queue_rq(flush_rq, false);
334342
}

0 commit comments

Comments
 (0)