mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration
@ 2026-10-24 14:09 Sasha Levin
  2026-10-27 12:51 ` Tao Cui
  2026-10-27 17:48 ` Tejun Heo
  0 siblings, 2 replies; 3+ messages in thread
From: Sasha Levin @ 2026-10-24 14:09 UTC (permalink / raw)
  To: Tejun Heo, Johannes Weiner, Michal Koutný, Shuah Khan
  Cc: cgroups, linux-kselftest, linux-kernel

test_cgcore_proc_migration() moves a process with 13 extra threads to
cg_dst and checks that they all moved by counting the lines of
cg_dst/cgroup.threads.

test_stress.sh runs test_core while enabling and disabling cpuset in the
root cgroup.subtree_control in a loop. Each change moves every task in
the hierarchy to a new css_set. Reading cgroup.threads walks the
cgroup's css_sets without holding cgroup_mutex, so when such a change
lands in the middle of the read, threads that were already listed from
the old css_set are listed again from the new one. The test then fails
with "not ok 8 test_cgcore_proc_migration" although the migration
worked.

Running the test's steps 40000 times in a 2 CPU KVM guest with the
cpuset changes going on, 5 reads listed the 14 threads two or three
times over (up to 36 lines), and a second read right after gave 14.
Without the cpuset changes it did not happen in 40000 runs. With
test_stress.sh, the test failed in 2 of 150 runs.

Check the cgroup of each thread of the process in /proc instead, which
other migrations can't affect.

Fixes: 1a99fcc035fb ("selftests: cgroup: Run test_core under interfering stress")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 tools/testing/selftests/cgroup/test_core.c | 36 +++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/cgroup/test_core.c b/tools/testing/selftests/cgroup/test_core.c
index 20d2b63774c37..53592bf309fac 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 <dirent.h>
 #include <fcntl.h>
 #include <sched.h>
 #include <stdio.h>
@@ -567,6 +568,39 @@ static void *dummy_thread_fn(void *arg)
 	return (void *)(size_t)pause();
 }
 
+/*
+ * Count the threads of the current process, or return -1 if any of them
+ * is not in @cgroup. Unlike reading cgroup.threads, this is not affected
+ * by other migrations happening at the same time.
+ */
+static int count_threads_in_cgroup(const char *root, const char *cgroup)
+{
+	char line[PATH_MAX];
+	struct dirent *de;
+	int cnt = 0;
+	DIR *dir;
+
+	snprintf(line, sizeof(line), CG_PATH_FORMAT, cgroup + strlen(root));
+
+	dir = opendir("/proc/self/task");
+	if (!dir)
+		return -1;
+
+	while ((de = readdir(dir))) {
+		if (de->d_name[0] == '.')
+			continue;
+
+		if (proc_read_strstr(atoi(de->d_name), false, "cgroup", line)) {
+			cnt = -1;
+			break;
+		}
+		cnt++;
+	}
+
+	closedir(dir);
+	return cnt;
+}
+
 /*
  * Test threadgroup migration.
  * All threads of a process are migrated together.
@@ -597,7 +631,7 @@ static int test_cgcore_proc_migration(const char *root)
 	}
 
 	cg_enter_current(dst);
-	if (cg_read_lc(dst, CG_THREADS_FILE) != n_threads + 1)
+	if (count_threads_in_cgroup(root, dst) != n_threads + 1)
 		goto cleanup;
 
 	ret = KSFT_PASS;
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration
  2026-10-24 14:09 [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration Sasha Levin
@ 2026-10-27 12:51 ` Tao Cui
  2026-10-27 17:48 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tao Cui @ 2026-10-27 12:51 UTC (permalink / raw)
  To: Sasha Levin, Tejun Heo, Johannes Weiner, Michal Koutný, Shuah Khan
  Cc: cui.tao, cgroups, linux-kselftest, linux-kernel

Hi Sasha,

在 2026/10/24 22:09, Sasha Levin 写道:
> test_cgcore_proc_migration() moves a process with 13 extra threads to
> cg_dst and checks that they all moved by counting the lines of
> cg_dst/cgroup.threads.
> 
> test_stress.sh runs test_core while enabling and disabling cpuset in the
> root cgroup.subtree_control in a loop. Each change moves every task in
> the hierarchy to a new css_set. Reading cgroup.threads walks the
> cgroup's css_sets without holding cgroup_mutex, so when such a change
> lands in the middle of the read, threads that were already listed from
> the old css_set are listed again from the new one. The test then fails
> with "not ok 8 test_cgcore_proc_migration" although the migration
> worked.
> 
> Running the test's steps 40000 times in a 2 CPU KVM guest with the
> cpuset changes going on, 5 reads listed the 14 threads two or three
> times over (up to 36 lines), and a second read right after gave 14.
> Without the cpuset changes it did not happen in 40000 runs. With
> test_stress.sh, the test failed in 2 of 150 runs.
> 
> Check the cgroup of each thread of the process in /proc instead, which
> other migrations can't affect.
> 

Reproduced on next-20261023 under test_stress-style interference
(pinned to 2 vCPUs): without the patch the test failed once in 17161
runs, and reading cgroup.threads in a loop under the same churn showed
duplicated threads, up to 38 lines. With the patch, no failures in
18578 runs.

Reviewed-by: Tao Cui <cuitao@kylinos.cn>
Tested-by: Tao Cui <cuitao@kylinos.cn>

Thanks,
Tao

> Fixes: 1a99fcc035fb ("selftests: cgroup: Run test_core under interfering stress")
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---
>  tools/testing/selftests/cgroup/test_core.c | 36 +++++++++++++++++++++-
>  1 file changed, 35 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/cgroup/test_core.c b/tools/testing/selftests/cgroup/test_core.c
> index 20d2b63774c37..53592bf309fac 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 <dirent.h>
>  #include <fcntl.h>
>  #include <sched.h>
>  #include <stdio.h>
> @@ -567,6 +568,39 @@ static void *dummy_thread_fn(void *arg)
>  	return (void *)(size_t)pause();
>  }
>  
> +/*
> + * Count the threads of the current process, or return -1 if any of them
> + * is not in @cgroup. Unlike reading cgroup.threads, this is not affected
> + * by other migrations happening at the same time.
> + */
> +static int count_threads_in_cgroup(const char *root, const char *cgroup)
> +{
> +	char line[PATH_MAX];
> +	struct dirent *de;
> +	int cnt = 0;
> +	DIR *dir;
> +
> +	snprintf(line, sizeof(line), CG_PATH_FORMAT, cgroup + strlen(root));
> +
> +	dir = opendir("/proc/self/task");
> +	if (!dir)
> +		return -1;
> +
> +	while ((de = readdir(dir))) {
> +		if (de->d_name[0] == '.')
> +			continue;
> +
> +		if (proc_read_strstr(atoi(de->d_name), false, "cgroup", line)) {
> +			cnt = -1;
> +			break;
> +		}
> +		cnt++;
> +	}
> +
> +	closedir(dir);
> +	return cnt;
> +}
> +
>  /*
>   * Test threadgroup migration.
>   * All threads of a process are migrated together.
> @@ -597,7 +631,7 @@ static int test_cgcore_proc_migration(const char *root)
>  	}
>  
>  	cg_enter_current(dst);
> -	if (cg_read_lc(dst, CG_THREADS_FILE) != n_threads + 1)
> +	if (count_threads_in_cgroup(root, dst) != n_threads + 1)
>  		goto cleanup;
>  
>  	ret = KSFT_PASS;


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration
  2026-10-24 14:09 [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration Sasha Levin
  2026-10-27 12:51 ` Tao Cui
@ 2026-10-27 17:48 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-10-27 17:48 UTC (permalink / raw)
  To: Sasha Levin
  Cc: Johannes Weiner, Michal Koutný,
	Shuah Khan, Tao Cui, cgroups, linux-kselftest, linux-kernel

Applied to cgroup/for-7.5 with the Fixes tag dropped and the following tags
added:

  Reviewed-by: Tao Cui <cuitao@kylinos.cn>
  Tested-by: Tao Cui <cuitao@kylinos.cn>

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-10-27 17:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-24 14:09 [PATCH] selftests/cgroup: Don't count cgroup.threads in test_cgcore_proc_migration Sasha Levin
2026-10-27 12:51 ` Tao Cui
2026-10-27 17:48 ` Tejun Heo

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®