From: Shakeel Butt <shakeel.butt@linux.dev>
To: Tejun Heo <tj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Koutny <mkoutny@suse.com>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Christian Brauner <brauner@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Suren Baghdasaryan <surenb@google.com>,
"T . J . Mercier" <tjmercier@google.com>,
Usama Arif <usama.arif@linux.dev>,
Meta kernel team <kernel-team@meta.com>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active()
Date: Mon, 30 Nov 2026 09:34:23 -0800 [thread overview]
Message-ID: <20261130173425.4117363-10-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev>
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@linux.dev>
---
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
next prev parent reply other threads:[~2026-11-30 17:35 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 17:34 [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 05/11] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 07/11] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:55 ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-30 17:34 ` Shakeel Butt [this message]
2026-11-30 17:34 ` [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:55 ` Andrew Morton
2026-12-01 17:56 ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:56 ` Andrew Morton
2026-12-01 17:55 ` [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Andrew Morton
2026-12-01 18:15 ` Shakeel Butt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130173425.4117363-10-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=brauner@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=tjmercier@google.com \
--cc=usama.arif@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.