1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Ming Lei <ming.lei@redhat.com>
3 Date: Fri, 10 Sep 2021 14:30:16 +0200
4 Subject: [PATCH] blk-mq: fix is_flush_rq
6 commit a9ed27a764156929efe714033edb3e9023c5f321 upstream.
8 is_flush_rq() is called from bt_iter()/bt_tags_iter(), and runs the
11 hctx->fq->flush_rq == req
13 but the passed hctx from bt_iter()/bt_tags_iter() may be NULL because:
15 1) memory re-order in blk_mq_rq_ctx_init():
17 rq->mq_hctx = data->hctx;
19 refcount_set(&rq->ref, 1);
23 2) tag re-use and ->rqs[] isn't updated with new request.
25 Fix the issue by re-writing is_flush_rq() as:
27 return rq->end_io == flush_end_io;
29 which turns out simpler to follow and immune to data race since we have
30 ordered WRITE rq->end_io and refcount_set(&rq->ref, 1).
32 Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter")
33 Cc: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
34 Cc: Yufen Yu <yuyufen@huawei.com>
35 Signed-off-by: Ming Lei <ming.lei@redhat.com>
36 Link: https://lore.kernel.org/r/20210818010925.607383-1-ming.lei@redhat.com
37 Signed-off-by: Jens Axboe <axboe@kernel.dk>
38 Cc: Yi Zhang <yi.zhang@redhat.com>
39 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
41 block/blk-flush.c | 5 +++++
43 block/blk.h | 6 +-----
44 3 files changed, 7 insertions(+), 6 deletions(-)
46 diff --git a/block/blk-flush.c b/block/blk-flush.c
47 index 8b11ab3b3762..705ee6c99020 100644
48 --- a/block/blk-flush.c
49 +++ b/block/blk-flush.c
50 @@ -262,6 +262,11 @@ static void flush_end_io(struct request *flush_rq, blk_status_t error)
51 spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
54 +bool is_flush_rq(struct request *rq)
56 + return rq->end_io == flush_end_io;
60 * blk_kick_flush - consider issuing flush request
61 * @q: request_queue being kicked
62 diff --git a/block/blk-mq.c b/block/blk-mq.c
63 index cb619ec8aaf2..601e40204d06 100644
66 @@ -937,7 +937,7 @@ static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
68 void blk_mq_put_rq_ref(struct request *rq)
70 - if (is_flush_rq(rq, rq->mq_hctx))
71 + if (is_flush_rq(rq))
73 else if (refcount_dec_and_test(&rq->ref))
74 __blk_mq_free_request(rq);
75 diff --git a/block/blk.h b/block/blk.h
76 index 7550364c326c..4a4ffd992790 100644
79 @@ -43,11 +43,7 @@ static inline void __blk_get_queue(struct request_queue *q)
80 kobject_get(&q->kobj);
84 -is_flush_rq(struct request *req, struct blk_mq_hw_ctx *hctx)
86 - return hctx->fq->flush_rq == req;
88 +bool is_flush_rq(struct request *req);
90 struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,