* [PATCH 0/7] block fixes found by code inspection
@ 2026-11-19 7:05 Guixin Liu
2026-11-19 7:05 ` [PATCH 1/7] blk-mq: free the rqs arrays of tags in the SRCU callback Guixin Liu
` (6 more replies)
0 siblings, 7 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
Seven block layer fixes, all found by code inspection of the tags SRCU
lifetime, the hctx reuse path, and the blk-cgroup configuration writers.
Patch 1 and patch 7 close the two halves of the tags_srcu protection for
tag iterators: patch 1 frees the tags->rqs[] and tags->static_rqs[]
arrays in the SRCU callback together with the bitmaps, patch 7 unpublishes
set->tags[] before the grace period starts, so a reader can no longer
load a pointer that the grace period has already released.
Patch 2 clears a stale BLK_MQ_S_STOPPED flag when a parked hctx is
reused, so requests queued on it do not stall until the driver happens
to stop and restart the queue again.
Patch 3 and patch 4 fix the locking order of ioc_cost_model_write():
patch 3 releases rq_qos_mutex before freezing the queue like
ioc_qos_write() does, patch 4 moves the unfreeze in ioc_qos_write()
ahead of the bdev reference drop, closing the same lifetime gap in both
writers.
Patch 5 fixes the mean folding in blk_rq_stat_sum(), which silently
dropped the samples of an already folded source and kept
blk-iolatency's accumulated window mean at zero on rotating disks.
Patch 6 rejects a dead disk in the three lazy policy init helpers, so a
configuration write racing del_gendisk() can no longer attach an orphan
iocost/iolatency/throttle policy that nothing tears down again.
Found by code inspection.
Tested on a KVM q35 guest with a 7.4-rc1 based kernel (PROVE_LOCKING
enabled): hctx state flags after a stop/shrink/start/grow cycle on
nullb0 show no stale flags and IO on the reused hctx completes; iocost
cost.model and cost.qos writes against a live and a removed nullb
device behave as expected, with the removed device rejected with
-ENODEV and the live device accepting all model, qos and coefficient
writes; concurrent nullb submit_queues resizes against continuous
debugfs busy/tags reads under fio ran for six minutes without errors.
Guixin Liu (7):
blk-mq: free the rqs arrays of tags in the SRCU callback
blk-mq: clear stale stopped flag on hctx reuse
blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost
model write
blk-iocost: unfreeze the queue before dropping the bdev reference in
qos write
blk-stat: account for the mean of the source in blk_rq_stat_sum()
blk-cgroup: don't attach a qos policy to a dead disk
blk-mq: unpublish the tags before deferring their free to SRCU
block/blk-iocost.c | 19 ++++++++++++++-----
block/blk-iolatency.c | 3 +++
block/blk-mq-tag.c | 2 ++
block/blk-mq.c | 24 +++++++-----------------
block/blk-mq.h | 1 -
block/blk-stat.c | 8 ++++++--
block/blk-throttle.c | 3 +++
7 files changed, 35 insertions(+), 25 deletions(-)
--
2.43.7
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/7] blk-mq: free the rqs arrays of tags in the SRCU callback
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 7:05 ` [PATCH 2/7] blk-mq: clear stale stopped flag on hctx reuse Guixin Liu
` (5 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/7] blk-mq: clear stale stopped flag on hctx reuse
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
2026-11-19 7:05 ` [PATCH 1/7] blk-mq: free the rqs arrays of tags in the SRCU callback Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 7:05 ` [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write Guixin Liu
` (4 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
2026-11-19 7:05 ` [PATCH 1/7] blk-mq: free the rqs arrays of tags in the SRCU callback Guixin Liu
2026-11-19 7:05 ` [PATCH 2/7] blk-mq: clear stale stopped flag on hctx reuse Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 13:40 ` Tao Cui
2026-11-19 7:05 ` [PATCH 4/7] blk-iocost: unfreeze the queue before dropping the bdev reference in qos write Guixin Liu
` (3 subsequent siblings)
6 siblings, 1 reply; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 4/7] blk-iocost: unfreeze the queue before dropping the bdev reference in qos write
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
` (2 preceding siblings ...)
2026-11-19 7:05 ` [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 7:05 ` [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum() Guixin Liu
` (2 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
` (3 preceding siblings ...)
2026-11-19 7:05 ` [PATCH 4/7] blk-iocost: unfreeze the queue before dropping the bdev reference in qos write Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 13:32 ` Tao Cui
2026-11-19 7:05 ` [PATCH 6/7] blk-cgroup: don't attach a qos policy to a dead disk Guixin Liu
2026-11-19 7:05 ` [PATCH 7/7] blk-mq: unpublish the tags before deferring their free to SRCU Guixin Liu
6 siblings, 1 reply; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 6/7] blk-cgroup: don't attach a qos policy to a dead disk
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
` (4 preceding siblings ...)
2026-11-19 7:05 ` [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum() Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
2026-11-19 7:05 ` [PATCH 7/7] blk-mq: unpublish the tags before deferring their free to SRCU Guixin Liu
6 siblings, 0 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 7/7] blk-mq: unpublish the tags before deferring their free to SRCU
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
` (5 preceding siblings ...)
2026-11-19 7:05 ` [PATCH 6/7] blk-cgroup: don't attach a qos policy to a dead disk Guixin Liu
@ 2026-11-19 7:05 ` Guixin Liu
6 siblings, 0 replies; 10+ messages in thread
From: Guixin Liu @ 2026-11-19 7:05 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: linux-block
__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 <kanie@linux.alibaba.com>
---
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
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
2026-11-19 7:05 ` [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum() Guixin Liu
@ 2026-11-19 13:32 ` Tao Cui
0 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-11-19 13:32 UTC (permalink / raw)
To: Guixin Liu, Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke, Tang Yizhou
Cc: cui.tao, linux-block
Hello, Guixin,
在 2026/11/19 15:05, Guixin Liu 写道:
> 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.
>
I posted a fix for the same issue before, which Tang Yizhou reviewed:
https://lore.kernel.org/all/20260904065426.1281914-1-cui.tao@linux.dev/
Adding him here to take a look at your approach as well. Hope the link helps.
Thanks,
Tao
> Fixes: 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
> ---
> 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;
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
2026-11-19 7:05 ` [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write Guixin Liu
@ 2026-11-19 13:40 ` Tao Cui
0 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-11-19 13:40 UTC (permalink / raw)
To: Guixin Liu, Jens Axboe, Christoph Hellwig, Tejun Heo, Josef Bacik,
Nilay Shroff, Ming Lei, Hannes Reinecke
Cc: cui.tao, linux-block
Hello, Guixin,
在 2026/11/19 15:05, Guixin Liu 写道:
> 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.
>
I tried the same change in cost model write recently and it turned out flawed, these two reviews explain it better than I can [1][2].
In short: the disk_live() check in blkg_conf_open_bdev() only holds while rq_qos_mutex is kept, and dropping it around the freeze lets the disk and the ioc go away; Tejun reproduced a use-after-free that way.
I've been busy with another thread lately, so I'm glad to see you proposing a fix for this. Hope the links help.
Thanks,
Tao
[1] https://lore.kernel.org/all/20261111163306.GA17267@lst.de/
[2] https://lore.kernel.org/all/bfe5007b2758bb06504b6966abaf1f7e@kernel.org/
> Fixes: 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
> ---
> 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;
> }
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-19 13:40 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 7:05 [PATCH 0/7] block fixes found by code inspection Guixin Liu
2026-11-19 7:05 ` [PATCH 1/7] blk-mq: free the rqs arrays of tags in the SRCU callback Guixin Liu
2026-11-19 7:05 ` [PATCH 2/7] blk-mq: clear stale stopped flag on hctx reuse Guixin Liu
2026-11-19 7:05 ` [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write Guixin Liu
2026-11-19 13:40 ` Tao Cui
2026-11-19 7:05 ` [PATCH 4/7] blk-iocost: unfreeze the queue before dropping the bdev reference in qos write Guixin Liu
2026-11-19 7:05 ` [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum() Guixin Liu
2026-11-19 13:32 ` Tao Cui
2026-11-19 7:05 ` [PATCH 6/7] blk-cgroup: don't attach a qos policy to a dead disk Guixin Liu
2026-11-19 7:05 ` [PATCH 7/7] blk-mq: unpublish the tags before deferring their free to SRCU Guixin Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox