mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 5/9] selftests/cgroup: test the frozen state of a cgroup with tasks and children
Date: Sun, 29 Nov 2026 10:35:20 -0800	[thread overview]
Message-ID: <20261129183524.1052436-6-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev>

Add a test for a cgroup that has tasks of its own and a child cgroup. It
must report frozen only when its own tasks and the child are all frozen,
and it must not stay frozen after a thaw.

To get a task that the cgroup2 freezer can't stop, freeze it in a cgroup
v1 freezer hierarchy. It then sleeps in the refrigerator, where the
cgroup2 freezer can't trap it. A stopped task counts as frozen, and a
thaw doesn't wake it. The test is skipped if the v1 freezer can't be
mounted.

Without "cgroup/freezer: check own tasks and descendants for the frozen
state", the parent reports frozen=1 while its child is not frozen.

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 tools/testing/selftests/cgroup/test_freezer.c | 105 ++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_freezer.c b/tools/testing/selftests/cgroup/test_freezer.c
index 266b1a0f889f..e24e97742492 100644
--- a/tools/testing/selftests/cgroup/test_freezer.c
+++ b/tools/testing/selftests/cgroup/test_freezer.c
@@ -4,6 +4,7 @@
 #include <sys/ptrace.h>
 #include <sys/types.h>
 #include <sys/mman.h>
+#include <sys/mount.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
@@ -804,6 +805,109 @@ static int test_cgfreezer_vfork(const char *root)
 	return ret;
 }
 
+/*
+ * A cgroup with tasks of its own and child cgroups is frozen only once its
+ * tasks and all its descendants are frozen.
+ *
+ * A task frozen in a cgroup v1 freezer hierarchy sleeps in the refrigerator,
+ * where the cgroup v2 freezer can't trap it, so its v2 cgroup can't freeze
+ * until it is thawed.  A stopped task counts as frozen and isn't woken by a
+ * thaw.
+ */
+static int test_cgfreezer_nonleaf(const char *root)
+{
+	char *parent = NULL, *child = NULL, *staging = NULL, *v1cg = NULL;
+	char v1root[] = "/tmp/cg_test_freezer_v1_XXXXXX";
+	int ret = KSFT_FAIL, frozen_pid, stopped_pid, i;
+	bool mounted = false;
+
+	if (!mkdtemp(v1root))
+		return KSFT_FAIL;
+
+	if (mount("none", v1root, "cgroup", 0, "freezer")) {
+		debug("Can't mount the cgroup v1 freezer\n");
+		ret = KSFT_SKIP;
+		goto cleanup;
+	}
+	mounted = true;
+
+	parent = cg_name(root, "cg_test_nonleaf");
+	child = cg_name(parent, "child");
+	staging = cg_name(root, "cg_test_nonleaf_staging");
+	v1cg = cg_name(v1root, "cg_test_nonleaf");
+	if (!parent || !child || !staging || !v1cg)
+		goto cleanup;
+
+	if (cg_create(parent) || cg_create(child) || cg_create(staging) ||
+	    cg_create(v1cg))
+		goto cleanup;
+
+	frozen_pid = cg_run_nowait(child, child_fn, NULL);
+	stopped_pid = cg_run_nowait(staging, child_fn, NULL);
+	if (frozen_pid < 0 || stopped_pid < 0 ||
+	    cg_wait_for_proc_count(child, 1) ||
+	    cg_wait_for_proc_count(staging, 1))
+		goto cleanup;
+
+	/* Freeze one task in the v1 hierarchy and stop the other. */
+	if (cg_enter(v1cg, frozen_pid) ||
+	    cg_write(v1cg, "freezer.state", "FROZEN") ||
+	    cg_read_strcmp_wait(v1cg, "freezer.state", "FROZEN\n"))
+		goto cleanup;
+
+	if (kill(stopped_pid, SIGSTOP) ||
+	    waitpid(stopped_pid, NULL, WUNTRACED) != stopped_pid)
+		goto cleanup;
+
+	/*
+	 * Freeze the parent and move the stopped task in.  All of the
+	 * parent's own tasks are frozen now, but its child isn't.
+	 */
+	if (cg_freeze_nowait(parent, true) || cg_enter(parent, stopped_pid))
+		goto cleanup;
+
+	for (i = 0; i < 50; i++) {
+		if (cg_check_frozen(parent, false) ||
+		    cg_check_frozen(child, false))
+			goto cleanup;
+		usleep(10000);
+	}
+
+	/* The thaw doesn't wake the stopped task, the parent stays thawed. */
+	if (cg_freeze_nowait(parent, false) || cg_check_frozen(parent, false))
+		goto cleanup;
+
+	/* Once the task is thawed in v1, the whole subtree can freeze. */
+	if (cg_write(v1cg, "freezer.state", "THAWED"))
+		goto cleanup;
+
+	if (cg_freeze_wait(parent, true) || cg_check_frozen(child, true))
+		goto cleanup;
+
+	if (cg_freeze_wait(parent, false))
+		goto cleanup;
+
+	ret = KSFT_PASS;
+
+cleanup:
+	/* SIGKILL doesn't wake up a task in the refrigerator. */
+	if (mounted && v1cg)
+		cg_write(v1cg, "freezer.state", "THAWED");
+	cg_destroy(child);
+	cg_destroy(parent);
+	cg_destroy(staging);
+	if (mounted) {
+		cg_destroy(v1cg);
+		umount(v1root);
+	}
+	rmdir(v1root);
+	free(v1cg);
+	free(staging);
+	free(child);
+	free(parent);
+	return ret;
+}
+
 /*
  * Get the current frozen_usec for the cgroup.
  */
@@ -1479,6 +1583,7 @@ struct cgfreezer_test {
 	T(test_cgfreezer_stopped),
 	T(test_cgfreezer_ptraced),
 	T(test_cgfreezer_vfork),
+	T(test_cgfreezer_nonleaf),
 	T(test_cgfreezer_time_empty),
 	T(test_cgfreezer_time_simple),
 	T(test_cgfreezer_time_populate),
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-11-29 18:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 18:35 [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-29 18:35 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-29 18:35 ` [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-29 18:35 ` [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-29 18:35 ` [PATCH 4/9] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-29 18:35 ` Shakeel Butt [this message]
2026-11-29 18:35 ` [PATCH 6/9] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-29 18:35 ` [PATCH 7/9] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-29 18:35 ` [PATCH 8/9] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-11-29 18:35 ` [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze 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=20261129183524.1052436-6-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®