[PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active()

From: Shakeel Butt

Date: Mon Nov 30 2026 - 12:35:47 EST


Move the part of cgroup_kn_lock_live() that runs before cgroup_mutex into
cgroup_kn_break_active(). It pins the cgroup and drops the node's active
reference. cgroup_kn_unbreak_active() undoes it. Later patches use them in
writes that don't take cgroup_mutex. No functional change.

Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
kernel/cgroup/cgroup.c | 52 ++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 3972cf413797..bdc044ad21d3 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -1669,6 +1669,37 @@ static u32 cgroup_calc_subtree_ss_mask(u32 subtree_control, u32 this_ss_mask)
return cur_ss_mask;
}

+/*
+ * Pin the cgroup of @kn and drop @kn's active reference, so that removing the
+ * cgroup doesn't wait for the caller. The cgroup stays accessible but can go
+ * offline at any time: the caller checks cgroup_is_dead() under a lock that
+ * removal also takes. Undo with cgroup_kn_unbreak_active(), or
+ * cgroup_kn_unlock() if cgroup_mutex is held.
+ */
+static struct cgroup *cgroup_kn_break_active(struct kernfs_node *kn)
+{
+ struct cgroup *cgrp;
+
+ if (kernfs_type(kn) == KERNFS_DIR)
+ cgrp = kn->priv;
+ else
+ cgrp = kn_priv(kn);
+
+ /* ensure @cgrp stays accessible and break the active_ref protection */
+ if (!cgroup_tryget(cgrp))
+ return ERR_PTR(-ENODEV);
+ kernfs_break_active_protection(kn);
+ return cgrp;
+}
+
+/* undo cgroup_kn_break_active() */
+static void cgroup_kn_unbreak_active(struct kernfs_node *kn,
+ struct cgroup *cgrp)
+{
+ kernfs_unbreak_active_protection(kn);
+ cgroup_put(cgrp);
+}
+
/**
* cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
* @kn: the kernfs_node being serviced
@@ -1689,9 +1720,7 @@ void cgroup_kn_unlock(struct kernfs_node *kn)
cgrp = kn_priv(kn);

cgroup_unlock();
-
- kernfs_unbreak_active_protection(kn);
- cgroup_put(cgrp);
+ cgroup_kn_unbreak_active(kn, cgrp);
}

/**
@@ -1717,26 +1746,19 @@ struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
struct cgroup *cgrp;
int ret;

- if (kernfs_type(kn) == KERNFS_DIR)
- cgrp = kn->priv;
- else
- cgrp = kn_priv(kn);
-
/*
* We're gonna grab cgroup_mutex which nests outside kernfs
* active_ref. cgroup liveliness check alone provides enough
- * protection against removal. Ensure @cgrp stays accessible and
- * break the active_ref protection.
+ * protection against removal.
*/
- if (!cgroup_tryget(cgrp))
- return ERR_PTR(-ENODEV);
- kernfs_break_active_protection(kn);
+ cgrp = cgroup_kn_break_active(kn);
+ if (IS_ERR(cgrp))
+ return cgrp;

if (drain_offline) {
ret = cgroup_lock_and_drain_offline(cgrp);
if (unlikely(ret)) {
- kernfs_unbreak_active_protection(kn);
- cgroup_put(cgrp);
+ cgroup_kn_unbreak_active(kn, cgrp);
return ERR_PTR(ret);
}
} else {
--
2.53.1-Meta