block fixes found by code inspection
Subsystems (2)
cover block fixes found by code inspection
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
PATCH 1/7 blk-mq: free the rqs arrays of tags in the SRCU callback
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.
--- block/blk-mq-tag.c | 2 ++ block/blk-mq.c | 18 +++--------------- block/blk-mq.h | 1 - 3 files changed, 5 insertions(+), 16 deletions(-)
block/blk-mq-tag.c +2 -0
@@ -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);
block/blk-mq.c +3 -15
@@ -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); } }
block/blk-mq.h +0 -1
@@ -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,
Signature · 1 line
2.43.7
PATCH 2/7 blk-mq: clear stale stopped flag on hctx reuse
In reply to Guixin LiuWhen 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.
--- block/blk-mq.c | 1 + 1 file changed, 1 insertion(+)
block/blk-mq.c +1 -0
@@ -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];
Signature · 1 line
2.43.7
PATCH 3/7 blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
In reply to Guixin Liuioc_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.
--- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-iocost.c +10 -2
@@ -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; }
Signature · 1 line
2.43.7
Re: [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
Hello, Guixin,
Quoted 10 lines · Guixin Liu
ioc_cost_model_write()freezes the queue while holdingrq_qos_mutex, whichblkg_conf_open_bdev()acquired first. Every other path takesrq_qos_mutexonly after the queue is frozen:ioc_qos_write()releases and re-acquires the mutex aroundblk_mq_freeze_queue(), andrq_qos_add()documents that its caller holds the mutex with the queue frozen. Waiting for in-flight IO while holdingrq_qos_mutexbreaks that rule, and on the first write to a queue without iocost the path even callsblk_iocost_init()->rq_qos_add()->blk_mq_freeze_queue()while nothing has frozen the queue yet.
Quoted 6 lines
Mirror the release-freeze-reacquire sequence of
ioc_qos_write(). The unfreeze moves to theclose_bdevlabel so the error exits after the freeze don't leave the queue frozen, and it runs beforeblkg_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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Quoted 46 lines
Fixes 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")Signed-off-by Guixin Liu <[email protected]>--- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)block/blk-iocost.c +10
-2@@ -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; }
Re: [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
Quoted 23 lines · Tao Cui
Hello, Guixin,
Quoted 10 lines · Guixin Liu
ioc_cost_model_write()freezes the queue while holdingrq_qos_mutex, whichblkg_conf_open_bdev()acquired first. Every other path takesrq_qos_mutexonly after the queue is frozen:ioc_qos_write()releases and re-acquires the mutex aroundblk_mq_freeze_queue(), andrq_qos_add()documents that its caller holds the mutex with the queue frozen. Waiting for in-flight IO while holdingrq_qos_mutexbreaks that rule, and on the first write to a queue without iocost the path even callsblk_iocost_init()->rq_qos_add()->blk_mq_freeze_queue()while nothing has frozen the queue yet.Quoted 6 lines
Mirror the release-freeze-reacquire sequence of
ioc_qos_write(). The unfreeze moves to theclose_bdevlabel so the error exits after the freeze don't leave the queue frozen, and it runs beforeblkg_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 inblkg_conf_open_bdev()only holds whilerq_qos_mutexis kept, and dropping it around the freeze lets the disk and the ioc go away; Tejun reproduced a use-after-free that way.
Quoted 8 lines
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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Hi Tao,
Thanks for the review, right, this patch enter the same issue, I will drop this in next version.
And about the ioc_qos_write()'s disk_live race window, My patch 6 fixed the issue, could you please take a look?
Best Regards,
Guixin Liu
Quoted 46 lines
Quoted 46 lines
Fixes 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")Signed-off-by Guixin Liu <[email protected]>--- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)block/blk-iocost.c +10
-2@@ -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; }
Re: [PATCH 3/7] blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write
Hello, Guixin,
Quoted 31 lines · Guixin Liu
Quoted 28 lines · Tao Cui
Hello, Guixin,
Quoted 10 lines · Guixin Liu
ioc_cost_model_write()freezes the queue while holdingrq_qos_mutex, whichblkg_conf_open_bdev()acquired first. Every other path takesrq_qos_mutexonly after the queue is frozen:ioc_qos_write()releases and re-acquires the mutex aroundblk_mq_freeze_queue(), andrq_qos_add()documents that its caller holds the mutex with the queue frozen. Waiting for in-flight IO while holdingrq_qos_mutexbreaks that rule, and on the first write to a queue without iocost the path even callsblk_iocost_init()->rq_qos_add()->blk_mq_freeze_queue()while nothing has frozen the queue yet.Quoted 6 lines
Mirror the release-freeze-reacquire sequence of
ioc_qos_write(). The unfreeze moves to theclose_bdevlabel so the error exits after the freeze don't leave the queue frozen, and it runs beforeblkg_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 inblkg_conf_open_bdev()only holds whilerq_qos_mutexis 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
Quoted 7 lines
Quoted 3 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Hi Tao,
Thanks for the review, right, this patch enter the same issue, I will drop this in next version.
Thanks for checking the history before reworking. I'm still busy with another thread, so if you have time, feel free to take this forward.
Thanks,
Tao
Quoted 52 lines
And about the ioc_qos_write()'s
disk_liverace window, My patch 6 fixed the issue, could you please take a look?Best Regards,
Guixin LiuQuoted 46 lines
Quoted 46 lines
Fixes 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")Signed-off-by Guixin Liu <[email protected]>--- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)block/blk-iocost.c +10
-2@@ -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; }
PATCH 4/7 blk-iocost: unfreeze the queue before dropping the bdev reference in qos write
In reply to Guixin Liuioc_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().
--- block/blk-iocost.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
block/blk-iocost.c +1 -3
@@ -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:
Signature · 1 line
2.43.7
PATCH 5/7 blk-stat: account for the mean of the source in blk_rq_stat_sum()
In reply to Guixin Liublk_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.
--- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
@@ -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;
Signature · 1 line
2.43.7
Re: [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
Hello, Guixin,
Quoted 10 lines · Guixin Liu
blk_rq_stat_sum()folds a src into a dst by combiningdst->meanwithsrc->batchonly, 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 intoiolat->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.
Quoted 4 lines
Fold
src->mean*src->nr_samplesinto the result as well. The per-cpu users keep their mean at zero throughblk_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/[email protected]/
Adding him here to take a look at your approach as well. Hope the link helps.
Thanks,
Tao
Quoted 33 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")Signed-off-by Guixin Liu <[email protected]>--- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)block/blk-stat.c +6
-2@@ -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;
Re: [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
Quoted 17 lines · Tao Cui
Hello, Guixin,
Quoted 10 lines · Guixin Liu
blk_rq_stat_sum()folds a src into a dst by combiningdst->meanwithsrc->batchonly, 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 intoiolat->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.Quoted 4 lines
Fold
src->mean*src->nr_samplesinto the result as well. The per-cpu users keep their mean at zero throughblk_rq_stat_init(), so their behavior is unchanged.
Quoted 8 lines
I posted a fix for the same issue before, which Tang Yizhou reviewed:
https://lore.kernel.org/all/[email protected]/
Adding him here to take a look at your approach as well. Hope the link helps.
Thanks,
Tao
Hi Tao,
My thanks for the review, your v3 is better, I will drop my patch5 in next version.
Best Regards,
Guixin Liu
Quoted 34 lines
Quoted 34 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")Signed-off-by Guixin Liu <[email protected]>--- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)block/blk-stat.c +6
-2@@ -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;
Re: [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
Hello, Guixin
Quoted 24 lines · Guixin Liu
Quoted 21 lines · Tao Cui
Hello, Guixin,
Quoted 10 lines · Guixin Liu
blk_rq_stat_sum()folds a src into a dst by combiningdst->meanwithsrc->batchonly, 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 intoiolat->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.Quoted 4 lines
Fold
src->mean*src->nr_samplesinto the result as well. The per-cpu users keep their mean at zero throughblk_rq_stat_init(), so their behavior is unchanged.I posted a fix for the same issue before, which Tang Yizhou reviewed:
Quoted 8 lines
Quoted 4 lines
Adding him here to take a look at your approach as well. Hope the link helps.
Thanks,
TaoHi Tao,
My thanks for the review, your v3 is better, I will drop my patch5 in next version.
Sorry, that's not what I meant. The issue is real and your patch works; I just felt changing the contract of blk_rq_stat_sum() has a wider impact, so I adapted the fix to the contract in the caller instead. That said, my v3 has been pending for months without review, so it may well have problems I haven't noticed myself, seeing your patch I also realized my Fixes tag is wrong.
Thanks,
Tao
Quoted 37 lines
Best Regards,
Guixin LiuQuoted 34 lines
Quoted 34 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")Signed-off-by Guixin Liu <[email protected]>--- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)block/blk-stat.c +6
-2@@ -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;
Re: [PATCH 5/7] blk-stat: account for the mean of the source in blk_rq_stat_sum()
Quoted 28 lines · Tao Cui
Hello, Guixin
Quoted 25 lines · Guixin Liu
Quoted 23 lines · Tao Cui
Hello, Guixin,
Quoted 10 lines · Guixin Liu
blk_rq_stat_sum()folds a src into a dst by combiningdst->meanwithsrc->batchonly, 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 intoiolat->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.Quoted 4 lines
Fold
src->mean*src->nr_samplesinto the result as well. The per-cpu users keep their mean at zero throughblk_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/[email protected]/
Adding him here to take a look at your approach as well. Hope the link helps.
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Thanks,
TaoHi Tao,
My thanks for the review, your v3 is better, I will drop my patch5 in next version.Sorry, that's not what I meant. The issue is real and your patch works; I just felt changing the contract of
blk_rq_stat_sum()has a wider impact, so I adapted the fix to the contract in the caller instead. That said, my v3 has been pending for months without review, so it may well have problems I haven't noticed myself, seeing your patch I also realized my Fixes tag is wrong.
OK, I will hold, waiting for other miantainers to review this patch.
Best Regards,
Guixin Liu
Quoted 38 lines
Thanks,
TaoQuoted 35 lines
Best Regards,
Guixin LiuQuoted 33 lines
Quoted 33 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")Signed-off-by Guixin Liu <[email protected]>--- block/blk-stat.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)block/blk-stat.c +6
-2@@ -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;
PATCH 6/7 blk-cgroup: don't attach a qos policy to a dead disk
In reply to Guixin Liublkg_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.
--- block/blk-iocost.c | 3 +++ block/blk-iolatency.c | 3 +++ block/blk-throttle.c | 3 +++ 3 files changed, 9 insertions(+)
block/blk-iocost.c +3 -0
@@ -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)
block/blk-iolatency.c +3 -0
@@ -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)
block/blk-throttle.c +3 -0
@@ -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)
Signature · 1 line
2.43.7
PATCH 7/7 blk-mq: unpublish the tags before deferring their free to SRCU
In reply to Guixin Liu__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.
--- block/blk-mq.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
@@ -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)
Signature · 1 line
2.43.7