__blk_mq_free_map_and_rqs() frees set->tags[hctx_idx] first, which starts the tags_srcu grace period, and only then stores NULL into the slot. The store is a plain store, unordered against the plain load in blk_mq_tagset_busy_iter(), which reads set->tags[i] to find the tags to walk. A reader that enters the SRCU read side after the grace period snapshot, but still loads the old, non-NULL pointer, is not waited for by that grace period, so blk_mq_free_tags_callback() can free the tags and everything they point to while the iterator is about to walk them. blk_mq_tagset_busy_iter() is reachable from the nvme and scsi timeout and error recovery paths without any queue freeze gating the free, so nothing else serializes the two. Clear set->tags[hctx_idx] before blk_mq_free_map_and_rqs(), so a reader either sees NULL and skips the tags, or loads the pointer before entering the SRCU read side, in which case the grace period waits for it. Fixes: 995412e23bb25 ("blk-mq: Replace tags->lock with SRCU for tag iterators") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-mq.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 87c1ccdf66ab..b58f73256202 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -4179,10 +4179,11 @@ void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, unsigned int hctx_idx) { - if (!blk_mq_is_shared_tags(set->flags)) - blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx); + struct blk_mq_tags *tags = set->tags[hctx_idx]; set->tags[hctx_idx] = NULL; + if (!blk_mq_is_shared_tags(set->flags)) + blk_mq_free_map_and_rqs(set, tags, hctx_idx); } static void blk_mq_map_swqueue(struct request_queue *q) -- 2.43.7