* [PATCH v4] blk-cgroup: fix leaks and online flag on radix_tree_insert failure
@ 2026-07-15 13:24 Tao Cui
2026-07-15 22:34 ` Jens Axboe
0 siblings, 1 reply; 2+ messages in thread
From: Tao Cui @ 2026-07-15 13:24 UTC (permalink / raw)
To: tj, axboe, josef; +Cc: cgroups, linux-block, linux-kernel, cui.tao, Tao Cui
From: Tao Cui <cuitao@kylinos.cn>
When radix_tree_insert() fails in blkg_create(), the error path has two
issues:
1. blkg->online is set to true unconditionally, even when the blkg was
never fully inserted. Move the assignment inside the success block.
2. The error path calls blkg_put() without first calling
percpu_ref_kill(). Because the refcount is still in percpu mode,
percpu_ref_put() only does this_cpu_sub() without checking for zero,
so blkg_release() is never triggered. This permanently leaks the
blkg memory, its percpu iostat, policy data, the parent blkg
reference, and the cgroup css reference — the latter preventing the
cgroup from ever being destroyed.
Fix by replacing blkg_put() with percpu_ref_kill(), matching the pattern
used in blkg_destroy().
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
v4:
- Resend. No functional change. Folded in Acked-by from Tejun Heo and
rebased onto current mainline (context-only: the err_put_css label was
renamed to err_free_blkg upstream).
v3:
- Remove the redundant blkg_put() after percpu_ref_kill() to avoid a
double-put that causes the refcount to go negative and bypass
blkg_release(), as pointed out by the sashiko AI review.
v3: https://lore.kernel.org/all/20260507061229.57466-1-cuitao@kylinos.cn/
v2:
- Also fix the percpu_ref leak on the radix_tree_insert() error path by
adding percpu_ref_kill() before blkg_put(), as pointed out by the
sashiko AI review.
v1: https://lore.kernel.org/all/20260506131124.16755-1-cuitao@kylinos.cn/
---
block/blk-cgroup.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index d2a1f5903f24..d9676126c5b5 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -434,15 +434,15 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
blkg->pd[i]->online = true;
}
}
+ blkg->online = true;
}
- blkg->online = true;
spin_unlock(&blkcg->lock);
if (!ret)
return blkg;
/* @blkg failed fully initialized, use the usual release path */
- blkg_put(blkg);
+ percpu_ref_kill(&blkg->refcnt);
return ERR_PTR(ret);
err_free_blkg:
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v4] blk-cgroup: fix leaks and online flag on radix_tree_insert failure
2026-07-15 13:24 [PATCH v4] blk-cgroup: fix leaks and online flag on radix_tree_insert failure Tao Cui
@ 2026-07-15 22:34 ` Jens Axboe
0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2026-07-15 22:34 UTC (permalink / raw)
To: tj, josef, Tao Cui; +Cc: cgroups, linux-block, linux-kernel, Tao Cui
On Wed, 15 Jul 2026 21:24:07 +0800, Tao Cui wrote:
> When radix_tree_insert() fails in blkg_create(), the error path has two
> issues:
>
> 1. blkg->online is set to true unconditionally, even when the blkg was
> never fully inserted. Move the assignment inside the success block.
>
> 2. The error path calls blkg_put() without first calling
> percpu_ref_kill(). Because the refcount is still in percpu mode,
> percpu_ref_put() only does this_cpu_sub() without checking for zero,
> so blkg_release() is never triggered. This permanently leaks the
> blkg memory, its percpu iostat, policy data, the parent blkg
> reference, and the cgroup css reference — the latter preventing the
> cgroup from ever being destroyed.
>
> [...]
Applied, thanks!
[1/1] blk-cgroup: fix leaks and online flag on radix_tree_insert failure
commit: dbbca20764382b4d411ec2918f4e278ffe547acc
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-15 22:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 13:24 [PATCH v4] blk-cgroup: fix leaks and online flag on radix_tree_insert failure Tao Cui
2026-07-15 22:34 ` Jens Axboe
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.