All of lore.kernel.org
 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 v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration
Date: Mon, 30 Nov 2026 09:34:20 -0800	[thread overview]
Message-ID: <20261130173425.4117363-7-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev>

cgroup.procs keeps its task iterator between reads. A failed migration
used to make the iterator skip the last process of a css_set.

Read one byte of cgroup.procs, so that the iterator waits on the last of
three processes. Then fail a migration of that process, and check that
the rest of the read still lists it. cpuset makes the migration fail: a
partition with one CPU gives that CPU to a child partition, which leaves
its other child without CPUs. The test is skipped if this can't be set
up.

The test fails without "cgroup: don't lose a task in css_task_iter when
a migration fails".

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

diff --git a/tools/testing/selftests/cgroup/test_core.c b/tools/testing/selftests/cgroup/test_core.c
index 53592bf309fa..01c268672d6d 100644
--- a/tools/testing/selftests/cgroup/test_core.c
+++ b/tools/testing/selftests/cgroup/test_core.c
@@ -9,6 +9,7 @@
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#include <ctype.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <sched.h>
@@ -739,6 +740,158 @@ static int test_cgcore_thread_migration(const char *root)
 	return ret;
 }
 
+/* Return the highest CPU number in a cpu list like "0-3,8-11", or -1. */
+static int cpulist_last(const char *list)
+{
+	const char *p = list + strlen(list);
+
+	while (p > list && !isdigit(p[-1]))
+		p--;
+	while (p > list && isdigit(p[-1]))
+		p--;
+	return isdigit(*p) ? atoi(p) : -1;
+}
+
+/* Is @pid on a line of its own in @buf? */
+static bool pid_listed(const char *buf, pid_t pid)
+{
+	const char *p = buf;
+	char *end;
+
+	while (*p) {
+		if (strtol(p, &end, 10) == pid && end != p && *end == '\n')
+			return true;
+		p = strchr(p, '\n');
+		if (!p)
+			break;
+		p++;
+	}
+	return false;
+}
+
+/*
+ * A migration that fails must not make a cgroup.procs reader miss a process.
+ *
+ * cgroup.procs keeps its task iterator between reads.  Read one byte, so that
+ * the iterator waits on the last process, then fail a migration of that
+ * process: cpuset refuses tasks for a cgroup without CPUs.  The process never
+ * left the cgroup, so the rest of the read must list it.
+ */
+static int test_cgcore_procs_failed_migration(const char *root)
+{
+	char *parent = NULL, *full = NULL, *empty = NULL, *cg = NULL;
+	char cpus[256], cpu[16], buf[4096];
+	bool cpuset_enabled = false;
+	pid_t pids[3] = { 0 };
+	int ret = KSFT_FAIL;
+	int fd = -1, i, last;
+	ssize_t len, n;
+
+	if (cg_test_v1_named)
+		return KSFT_SKIP;
+
+	parent = cg_name(root, "cg_test_fail");
+	full = cg_name(root, "cg_test_fail/full");
+	empty = cg_name(root, "cg_test_fail/empty");
+	cg = cg_name(root, "cg_test_src");
+	if (!parent || !full || !empty || !cg)
+		goto cleanup;
+
+	/*
+	 * Make "empty" a cgroup without CPUs: "parent" is a partition with one
+	 * CPU, and its child partition "full" takes that CPU.
+	 */
+	ret = KSFT_SKIP;
+	if (cg_read_strstr(root, "cgroup.subtree_control", "cpuset")) {
+		if (cg_write(root, "cgroup.subtree_control", "+cpuset"))
+			goto cleanup;
+		cpuset_enabled = true;
+	}
+	if (cg_read(root, "cpuset.cpus.effective", cpus, sizeof(cpus)))
+		goto cleanup;
+	last = cpulist_last(cpus);
+	if (last <= 0)
+		goto cleanup;
+	snprintf(cpu, sizeof(cpu), "%d", last);
+
+	if (cg_create(parent) || cg_write(parent, "cpuset.cpus", cpu) ||
+	    cg_write(parent, "cpuset.cpus.partition", "root") ||
+	    cg_read_strcmp(parent, "cpuset.cpus.partition", "root\n") ||
+	    cg_write(parent, "cgroup.subtree_control", "+cpuset") ||
+	    cg_create(full) || cg_write(full, "cpuset.cpus", cpu) ||
+	    cg_write(full, "cpuset.cpus.partition", "root") ||
+	    cg_read_strcmp(full, "cpuset.cpus.partition", "root\n") ||
+	    cg_create(empty) ||
+	    cg_read_strcmp(empty, "cpuset.cpus.effective", "\n"))
+		goto cleanup;
+
+	ret = KSFT_FAIL;
+	if (cg_create(cg))
+		goto cleanup;
+
+	/* Move the processes in one by one, so that they are listed in order. */
+	for (i = 0; i < 3; i++) {
+		pids[i] = fork();
+		if (pids[i] < 0)
+			goto cleanup;
+		if (!pids[i]) {
+			while (1)
+				pause();
+		}
+		if (cg_enter(cg, pids[i]))
+			goto cleanup;
+	}
+
+	/* Without a failing migration there is nothing to test. */
+	if (cg_enter(empty, pids[2]) != -ENOSPC) {
+		ret = KSFT_SKIP;
+		goto cleanup;
+	}
+
+	fd = cg_open(cg, "cgroup.procs", O_RDONLY);
+	if (fd < 0)
+		goto cleanup;
+	if (read(fd, buf, 1) != 1)
+		goto cleanup;
+	len = 1;
+
+	if (cg_enter(empty, pids[2]) != -ENOSPC)
+		goto cleanup;
+
+	while ((n = read(fd, buf + len, sizeof(buf) - 1 - len)) > 0)
+		len += n;
+	if (n < 0)
+		goto cleanup;
+	buf[len] = '\0';
+
+	for (i = 0; i < 3; i++)
+		if (!pid_listed(buf, pids[i]))
+			goto cleanup;
+
+	ret = KSFT_PASS;
+
+cleanup:
+	if (fd >= 0)
+		close(fd);
+	for (i = 0; i < 3; i++) {
+		if (pids[i] > 0) {
+			kill(pids[i], SIGKILL);
+			waitpid(pids[i], NULL, 0);
+		}
+	}
+	cg_destroy(cg);
+	cg_destroy(empty);
+	cg_destroy(full);
+	cg_destroy(parent);
+	if (cpuset_enabled)
+		cg_write(root, "cgroup.subtree_control", "-cpuset");
+	free(cg);
+	free(empty);
+	free(full);
+	free(parent);
+	return ret;
+}
+
 /*
  * cgroup migration permission check should be performed based on the
  * credentials at the time of open instead of write.
@@ -941,6 +1094,7 @@ struct corecg_test {
 	T(test_cgcore_populated),
 	T(test_cgcore_proc_migration),
 	T(test_cgcore_thread_migration),
+	T(test_cgcore_procs_failed_migration),
 	T(test_cgcore_destroy),
 	T(test_cgcore_lesser_euid_open),
 	T(test_cgcore_lesser_ns_open),
-- 
2.53.1-Meta


  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 ` Shakeel Butt [this message]
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 ` [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
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-7-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.