struct blk_mq_tags and the requests are kept alive until the iterators under tags_srcu are gone, and the bitmaps were recently moved into that SRCU callback as well, but the tags->rqs[] and tags->static_rqs[] arrays are still freed right away by blk_mq_free_rq_map(). An iterator holding a tags pointer dereferences both arrays per set bit, e.g. blk_mq_find_and_get_req(), so when the number of hardware queues shrinks the arrays can be freed while blk_mq_tagset_busy_iter() is about to walk them. Free both arrays in the SRCU callback together with the bitmaps. With that blk_mq_free_rq_map() is a trivial wrapper of blk_mq_free_tags(), so drop it and call blk_mq_free_tags() directly. Fixes: 995412e23bb2 ("blk-mq: Replace tags->lock with SRCU for tag iterators") Fixes: ad0d05dbddc1 ("blk-mq: Defer freeing of tags page_list to SRCU callback") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-mq-tag.c | 2 ++ block/blk-mq.c | 18 +++--------------- block/blk-mq.h | 1 - 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/block/blk-mq-tag.c b/block/blk-mq-tag.c index 3515031b3226..ee5413ebab01 100644 --- a/block/blk-mq-tag.c +++ b/block/blk-mq-tag.c @@ -611,6 +611,8 @@ static void blk_mq_free_tags_callback(struct rcu_head *head) kmemleak_free(page_address(page)); __free_pages(page, page->private); } + kfree(tags->rqs); + kfree(tags->static_rqs); /* An iterator may walk the bitmaps for as long as it may see the tags */ sbitmap_queue_free(&tags->bitmap_tags); sbitmap_queue_free(&tags->breserved_tags); diff --git a/block/blk-mq.c b/block/blk-mq.c index 5ae22d556540..ae12f2a52cc9 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3532,16 +3532,6 @@ void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, */ } -void blk_mq_free_rq_map(struct blk_mq_tags *tags) -{ - kfree(tags->rqs); - tags->rqs = NULL; - kfree(tags->static_rqs); - tags->static_rqs = NULL; - - blk_mq_free_tags(tags); -} - static enum hctx_type hctx_idx_to_type(struct blk_mq_tag_set *set, unsigned int hctx_idx) { @@ -3594,12 +3584,10 @@ static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node); if (!tags->static_rqs) - goto err_free_rqs; + goto err_free_tags; return tags; -err_free_rqs: - kfree(tags->rqs); err_free_tags: blk_mq_free_tags(tags); return NULL; @@ -4155,7 +4143,7 @@ struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth); if (ret) { - blk_mq_free_rq_map(tags); + blk_mq_free_tags(tags); return NULL; } @@ -4183,7 +4171,7 @@ void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, { if (tags) { blk_mq_free_rqs(set, tags, hctx_idx); - blk_mq_free_rq_map(tags); + blk_mq_free_tags(tags); } } diff --git a/block/blk-mq.h b/block/blk-mq.h index 1e64236d34d4..ba7bd917c361 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -62,7 +62,6 @@ void blk_mq_put_rq_ref(struct request *rq); */ void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, unsigned int hctx_idx); -void blk_mq_free_rq_map(struct blk_mq_tags *tags); struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, unsigned int hctx_idx, unsigned int depth); void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, -- 2.43.7 When a driver stops a hardware queue with blk_mq_stop_hw_queues() and the number of hardware queues shrinks before it calls blk_mq_start_stopped_hw_queues() again, the parked hctx keeps its BLK_MQ_S_STOPPED flag: the exit path does not clear it, and blk_mq_start_stopped_hw_queues() only walks the live hctxs. A later update that reuses the parked hctx via blk_mq_alloc_and_init_hctx() inherits the flag, so blk_mq_run_hw_queue() refuses to run requests queued on it until the driver happens to stop and restart the queue again. Clear BLK_MQ_S_STOPPED in blk_mq_init_hctx() so a reused hctx starts like a freshly allocated one, matching the BLK_MQ_S_INACTIVE handling. Fixes: 2f8f1336a48b ("blk-mq: always free hctx after request queue is freed") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-mq.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index ae12f2a52cc9..87c1ccdf66ab 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -4016,6 +4016,7 @@ static int blk_mq_init_hctx(struct request_queue *q, hctx->queue_num = hctx_idx; clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); + clear_bit(BLK_MQ_S_STOPPED, &hctx->state); hctx->tags = set->tags[hctx_idx]; -- 2.43.7 ioc_cost_model_write() freezes the queue while holding rq_qos_mutex, which blkg_conf_open_bdev() acquired first. Every other path takes rq_qos_mutex only after the queue is frozen: ioc_qos_write() releases and re-acquires the mutex around blk_mq_freeze_queue(), and rq_qos_add() documents that its caller holds the mutex with the queue frozen. Waiting for in-flight IO while holding rq_qos_mutex breaks that rule, and on the first write to a queue without iocost the path even calls blk_iocost_init() -> rq_qos_add() -> blk_mq_freeze_queue() while nothing has frozen the queue yet. Mirror the release-freeze-reacquire sequence of ioc_qos_write(). The unfreeze moves to the close_bdev label so the error exits after the freeze don't leave the queue frozen, and it runs before blkg_conf_close_bdev() drops the bdev reference, while the queue is still pinned. Fixes: 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 1c1025157d8e..8e06b83691aa 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -3589,6 +3589,15 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, ret = blkg_conf_open_bdev(&ctx); if (ret) return ret; + /* + * Nothing is protecting QoS state yet, so release q->rq_qos_mutex, + * which was first acquired in blkg_conf_open_bdev, and re-acquire it + * after freezing the queue to maintain the correct locking order. + */ + mutex_unlock(&ctx.bdev->bd_queue->rq_qos_mutex); + + memflags = blk_mq_freeze_queue(ctx.bdev->bd_queue); + mutex_lock(&ctx.bdev->bd_queue->rq_qos_mutex); body = ctx.body; q = bdev_get_queue(ctx.bdev); @@ -3605,7 +3614,6 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, ioc = q_to_ioc(q); } - memflags = blk_mq_freeze_queue(q); blk_mq_quiesce_queue(q); spin_lock_irq(&ioc->lock); @@ -3663,9 +3671,9 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, spin_unlock_irq(&ioc->lock); blk_mq_unquiesce_queue(q); - blk_mq_unfreeze_queue(q, memflags); close_bdev: + blk_mq_unfreeze_queue(q, memflags); blkg_conf_close_bdev(&ctx); return ret ?: nbytes; } -- 2.43.7 ioc_qos_write() calls blkg_conf_close_bdev() before blk_mq_unfreeze_queue(). close_bdev drops the bdev reference the writer holds; if that is the last reference, the queue enters its release path and only the RCU-delayed free in blk_free_queue() keeps it alive across the unfreeze. Unfreeze before the close instead, while the writer's own bdev reference still pins the queue, matching ioc_cost_model_write(). Fixes: 9730763f4756 ("block: correct locking order for protecting blk-wbt parameters") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-iocost.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 8e06b83691aa..b251ecc66d8a 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -3370,7 +3370,6 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input, size_t nbytes, loff_t off) { struct blkg_conf_ctx ctx; - struct request_queue *q; struct gendisk *disk; struct ioc *ioc; u32 qos[NR_QOS_PARAMS]; @@ -3511,9 +3510,8 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input, blk_mq_unquiesce_queue(disk->queue); close_bdev: - q = ctx.bdev->bd_queue; + blk_mq_unfreeze_queue(ctx.bdev->bd_queue, memflags); blkg_conf_close_bdev(&ctx); - blk_mq_unfreeze_queue(q, memflags); return ret ?: nbytes; einval: -- 2.43.7 blk_rq_stat_sum() folds a src into a dst by combining dst->mean with src->batch only, so it is only correct while src has never been folded itself: its mean is then either zero or stale. blk-iolatency folds the per-cpu stats into a window-local stat first and then folds that result into iolat->cur_stat, so the second fold drops the whole window and cur_stat.rqs.mean stays at zero. On rotating disks, where the mean decides whether the accumulated window meets the latency target, iolatency_check_latencies() therefore always considers the accumulated window OK and scales up more aggressively than intended. Fold src->mean * src->nr_samples into the result as well. The per-cpu users keep their mean at zero through blk_rq_stat_init(), so their behavior is unchanged. Fixes: 451bb7c33197 ("blk-iolatency: keep track of previous windows stats") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/block/blk-stat.c b/block/blk-stat.c index d57c2fc6bf06..6345ae366658 100644 --- a/block/blk-stat.c +++ b/block/blk-stat.c @@ -24,7 +24,10 @@ void blk_rq_stat_init(struct blk_rq_stat *stat) stat->batch = 0; } -/* src is a per-cpu stat, mean isn't initialized */ +/* + * The mean of @src must have been calculated or set to zero by + * blk_rq_stat_init() before calling this. + */ void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) { if (dst->nr_samples + src->nr_samples <= dst->nr_samples) @@ -33,7 +36,8 @@ void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) dst->min = min(dst->min, src->min); dst->max = max(dst->max, src->max); - dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples, + dst->mean = div_u64(dst->mean * dst->nr_samples + + src->mean * src->nr_samples + src->batch, dst->nr_samples + src->nr_samples); dst->nr_samples += src->nr_samples; -- 2.43.7 blkg_conf_open_bdev() validates disk_live() once under rq_qos_mutex. ioc_qos_write() and ioc_cost_model_write() then release that mutex, freeze the queue and re-acquire it, so __del_gendisk() can run rq_qos_exit() to completion in the window and detach the existing policy. After the re-acquire, q_to_ioc() returning NULL now also means "the policy was torn down", not just "never enabled", and the writer calls blk_iocost_init() to attach a fresh ioc to the dead disk. Nothing tears that ioc down again: rq_qos_exit() already ran, and for disks on a shared queue, e.g. sd/sr, blk_mq_destroy_queue() does not call it either. The ioc with its timer and per-cpu stats is leaked, and QUEUE_FLAG_QOS_ENABLED is resurrected on the dead queue. The same lazy-init pattern in blk_throtl_init() and blk_iolatency_init() is reachable the same way once their respective config writers get the release-freeze-reacquire treatment. Reject a dead disk in all three init helpers. Fixes: 7caa47151ab2 ("blkcg: implement blk-iocost") Fixes: a3166c51702b ("blk-throttle: delay initialization until configuration") Fixes: a13696b83da4 ("blk-iolatency: Make initialization lazy") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-iocost.c | 3 +++ block/blk-iolatency.c | 3 +++ block/blk-throttle.c | 3 +++ 3 files changed, 9 insertions(+) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index b251ecc66d8a..f71d997f232c 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -3009,6 +3009,9 @@ static int blk_iocost_init(struct gendisk *disk) struct ioc *ioc; int i, cpu, ret; + if (test_bit(GD_DEAD, &disk->state)) + return -ENODEV; + /* The caller holds rq_qos_mutex, which is taken with the queue frozen */ ioc = kzalloc_obj(*ioc, GFP_NOIO); if (!ioc) diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c index a3bd153c67be..553f2091730f 100644 --- a/block/blk-iolatency.c +++ b/block/blk-iolatency.c @@ -761,6 +761,9 @@ static int blk_iolatency_init(struct gendisk *disk) struct blk_iolatency *blkiolat; int ret; + if (test_bit(GD_DEAD, &disk->state)) + return -ENODEV; + /* The caller holds rq_qos_mutex, which is taken with the queue frozen */ blkiolat = kzalloc_obj(*blkiolat, GFP_NOIO); if (!blkiolat) diff --git a/block/blk-throttle.c b/block/blk-throttle.c index c5e26551956a..374ac75832ea 100644 --- a/block/blk-throttle.c +++ b/block/blk-throttle.c @@ -1322,6 +1322,9 @@ static int blk_throtl_init(struct gendisk *disk) unsigned int memflags; int ret; + if (test_bit(GD_DEAD, &disk->state)) + return -ENODEV; + /* The caller holds rq_qos_mutex, which is taken with the queue frozen */ td = kzalloc_node(sizeof(*td), GFP_NOIO, q->node); if (!td) -- 2.43.7 __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