Each iolatency group tracks its last contribution to the parent's nr_samples so that subsequent latency checks can replace it. However, this contribution is not removed when the group goes offline. Repeatedly creating and removing cgroups can therefore inflate the sample threshold used by check_scale_change(), causing active groups to incorrectly skip queue-depth reductions and weakening latency protection for their peers. Remove the group's contribution from the parent's nr_samples and clear its own nr_samples under the parent's child_lat.lock in iolatency_pd_offline(). Recheck min_lat_nsec under the same lock in iolatency_check_latencies() to prevent an in-flight bio completion from adding samples back after the offline cleanup. Tested on next-20261126 in an x86_64 QEMU guest with cgroup v2 and null_blk. After 10 cycles in each run of creating a child cgroup with a latency target, issuing direct reads and removing it, the unpatched parent retained 973 and 814 stale samples in two runs. With the patch, the parent's sample count matched the surviving child's contribution after every removal. In a scale-down test, a competing sibling contributed 11 samples while the protected group missed its target. Without the patch, the parent had 880 samples, the threshold was 44 and the sibling remained at depth=max. With the patch, the parent had 128 samples, the threshold was 6 and the sibling's depth dropped to 32. Removing a child with an in-flight read also left no stale samples after I/O completion with the patch. The completion observed a cleared latency target in this test. Fixes: d70675121546 ("block: introduce blk-iolatency io controller") Signed-off-by: Zhe Liu --- block/blk-iolatency.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c index a3bd153c67be..44e5015d2295 100644 --- a/block/blk-iolatency.c +++ b/block/blk-iolatency.c @@ -547,6 +547,10 @@ static void iolatency_check_latencies(struct iolatency_grp *iolat, u64 now) /* Somebody beat us to the punch, just bail. */ spin_lock_irqsave(&lat_info->lock, flags); + /* An in-flight bio may complete after the group went offline. */ + if (!iolat->min_lat_nsec) + goto out; + latency_stat_sum(iolat, &iolat->cur_stat, &stat); lat_info->nr_samples -= iolat->nr_samples; lat_info->nr_samples += latency_stat_samples(iolat, &iolat->cur_stat); @@ -1028,9 +1032,18 @@ static void iolatency_pd_offline(struct blkg_policy_data *pd) { struct iolatency_grp *iolat = pd_to_lat(pd); struct blkcg_gq *blkg = lat_to_blkg(iolat); + struct iolatency_grp *parent = blkg_to_lat(blkg->parent); + unsigned long flags; iolatency_set_min_lat_nsec(blkg, 0); iolatency_clear_scaling(blkg); + + if (parent) { + spin_lock_irqsave(&parent->child_lat.lock, flags); + parent->child_lat.nr_samples -= iolat->nr_samples; + iolat->nr_samples = 0; + spin_unlock_irqrestore(&parent->child_lat.lock, flags); + } } static void iolat_release(struct rcu_head *rcu) base-commit: a9c48428cbd8d96b26f92631d0db6f8519c5fcc5