Skip to contentSkip to conversation

block fixes found by code inspection

PATCH In review Guixin Liu 2 participants 15 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

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

author
#1 lore raw

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.

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")
---
 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
index 3515031b3226..ee5413ebab01 100644
@@ -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
index 5ae22d556540..ae12f2a52cc9 100644
@@ -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
index 1e64236d34d4..ba7bd917c361 100644
@@ -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
author
#2 lore raw

PATCH 2/7 blk-mq: clear stale stopped flag on hctx reuse

In reply to Guixin Liu

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")
---
 block/blk-mq.c | 1 +
 1 file changed, 1 insertion(+)
block/blk-mq.c +1 -0
index ae12f2a52cc9..87c1ccdf66ab 100644
@@ -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
author
#3 lore raw

PATCH 3/7 blk-iocost: don't take rq_qos_mutex before the queue is frozen in cost model write

In reply to 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.

Fixes 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")
---
 block/blk-iocost.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-iocost.c +10 -2
index 1c1025157d8e..8e06b83691aa 100644
@@ -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
#4 lore raw

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 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.

Quoted 6 lines

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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Quoted 46 lines
Fixes 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling")
---
 block/blk-iocost.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-iocost.c +10 -2
index 1c1025157d8e..8e06b83691aa 100644
@@ -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;
 }
author
#5 lore raw

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 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.

Quoted 6 lines

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.

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")
---
 block/blk-iocost.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-iocost.c +10 -2
index 1c1025157d8e..8e06b83691aa 100644
@@ -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;
 }
#6 lore raw

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 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.

Quoted 6 lines

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

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_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")
---
 block/blk-iocost.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-iocost.c +10 -2
index 1c1025157d8e..8e06b83691aa 100644
@@ -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;
 }
author
#7 lore raw

PATCH 4/7 blk-iocost: unfreeze the queue before dropping the bdev reference in qos write

In reply to Guixin Liu

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")
---
 block/blk-iocost.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
block/blk-iocost.c +1 -3
index 8e06b83691aa..b251ecc66d8a 100644
@@ -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
author
#8 lore raw

PATCH 5/7 blk-stat: account for the mean of the source in blk_rq_stat_sum()

In reply to 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.

Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")
---
 block/blk-stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
index d57c2fc6bf06..6345ae366658 100644
@@ -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
#9 lore raw

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 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.

Quoted 4 lines

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/[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")
---
 block/blk-stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
index d57c2fc6bf06..6345ae366658 100644
@@ -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;
author
#10 lore raw

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 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.

Quoted 4 lines

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.

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")
---
 block/blk-stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
index d57c2fc6bf06..6345ae366658 100644
@@ -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;
#11 lore raw

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 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.

Quoted 4 lines

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/[email protected]/

Quoted 8 lines
Quoted 4 lines

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.

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 Liu

Quoted 34 lines
Quoted 34 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")
---
 block/blk-stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
index d57c2fc6bf06..6345ae366658 100644
@@ -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;
author
#12 lore raw

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 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.

Quoted 4 lines

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/[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,
Tao

Hi 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,
Tao

Quoted 35 lines

Best Regards,
Guixin Liu

Quoted 33 lines
Quoted 33 lines
Fixes 451bb7c33197 ("blk-iolatency: keep track of previous windows stats")
---
 block/blk-stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/blk-stat.c +6 -2
index d57c2fc6bf06..6345ae366658 100644
@@ -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;
author
#13 lore raw

PATCH 6/7 blk-cgroup: don't attach a qos policy to a dead disk

In reply to Guixin Liu

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")
---
 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
index b251ecc66d8a..f71d997f232c 100644
@@ -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
index a3bd153c67be..553f2091730f 100644
@@ -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
index c5e26551956a..374ac75832ea 100644
@@ -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
author
#14 lore raw

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.

Fixes 995412e23bb25 ("blk-mq: Replace tags->lock with SRCU for tag iterators")
---
 block/blk-mq.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-mq.c +3 -2
index 87c1ccdf66ab..b58f73256202 100644
@@ -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