]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/cgroup: Don't require synchronous populated update on task exit
authorTejun Heo <tj@kernel.org>
Tue, 24 Mar 2026 20:21:47 +0000 (10:21 -1000)
committerTejun Heo <tj@kernel.org>
Tue, 24 Mar 2026 20:21:57 +0000 (10:21 -1000)
test_cgcore_populated (test_core) and test_cgkill_{simple,tree,forkbomb}
(test_kill) check cgroup.events "populated 0" immediately after reaping
child tasks with waitpid(). This used to work because cgroup_task_exit() in
do_exit() unlinked tasks from css_sets before exit_notify() woke up
waitpid().

d245698d727a ("cgroup: Defer task cgroup unlink until after the task is done
switching out") moved the unlink to cgroup_task_dead() in
finish_task_switch(), which runs after exit_notify(). The populated counter
is now decremented after the parent's waitpid() can return, so there is no
longer a synchronous ordering guarantee. On PREEMPT_RT, where
cgroup_task_dead() is further deferred through lazy irq_work, the race
window is even larger.

The synchronous populated transition was never part of the cgroup interface
contract - it was an implementation artifact. Use cg_read_strcmp_wait() which
retries for up to 1 second, matching what these tests actually need to
verify: that the cgroup eventually becomes unpopulated after all tasks exit.

Fixes: d245698d727a ("cgroup: Defer task cgroup unlink until after the task is done switching out")
Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Tejun Heo <tj@kernel.org>
Tested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Christian Brauner <brauner@kernel.org>
Cc: cgroups@vger.kernel.org
tools/testing/selftests/cgroup/lib/cgroup_util.c
tools/testing/selftests/cgroup/lib/include/cgroup_util.h
tools/testing/selftests/cgroup/test_core.c
tools/testing/selftests/cgroup/test_kill.c

index ce6c2642fd9bbb622123e3ba23b6a14846f85e7f..6a7295347e90b1cc1298aa80aa01a8dbfa21e936 100644 (file)
@@ -123,6 +123,21 @@ int cg_read_strcmp(const char *cgroup, const char *control,
        return ret;
 }
 
+int cg_read_strcmp_wait(const char *cgroup, const char *control,
+                           const char *expected)
+{
+       int i, ret;
+
+       for (i = 0; i < 100; i++) {
+               ret = cg_read_strcmp(cgroup, control, expected);
+               if (!ret)
+                       return ret;
+               usleep(10000);
+       }
+
+       return ret;
+}
+
 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
 {
        char buf[PAGE_SIZE];
index 77f386dab5e8fbe16b6859316b2e0d2200ca78c4..567b1082974c539752e43c0327f9497d02f091ce 100644 (file)
@@ -61,6 +61,8 @@ extern int cg_read(const char *cgroup, const char *control,
                   char *buf, size_t len);
 extern int cg_read_strcmp(const char *cgroup, const char *control,
                          const char *expected);
+extern int cg_read_strcmp_wait(const char *cgroup, const char *control,
+                                  const char *expected);
 extern int cg_read_strstr(const char *cgroup, const char *control,
                          const char *needle);
 extern long cg_read_long(const char *cgroup, const char *control);
index 102262555a599bb00043bf86a4c036a59e605d8f..7b83c7e7c9d4fa5a8accee3f4f9c3a6fe06c3aa7 100644 (file)
@@ -233,7 +233,8 @@ static int test_cgcore_populated(const char *root)
        if (err)
                goto cleanup;
 
-       if (cg_read_strcmp(cg_test_d, "cgroup.events", "populated 0\n"))
+       if (cg_read_strcmp_wait(cg_test_d, "cgroup.events",
+                                  "populated 0\n"))
                goto cleanup;
 
        /* Remove cgroup. */
index c8c9d306925b63ae5cbae59d834b2370f2b853a3..f6cd23a8ecc71cdff78ee3c95a29c18643f3731e 100644 (file)
@@ -86,7 +86,7 @@ cleanup:
                wait_for_pid(pids[i]);
 
        if (ret == KSFT_PASS &&
-           cg_read_strcmp(cgroup, "cgroup.events", "populated 0\n"))
+           cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
                ret = KSFT_FAIL;
 
        if (cgroup)
@@ -190,7 +190,8 @@ cleanup:
                wait_for_pid(pids[i]);
 
        if (ret == KSFT_PASS &&
-           cg_read_strcmp(cgroup[0], "cgroup.events", "populated 0\n"))
+           cg_read_strcmp_wait(cgroup[0], "cgroup.events",
+                                  "populated 0\n"))
                ret = KSFT_FAIL;
 
        for (i = 9; i >= 0 && cgroup[i]; i--) {
@@ -251,7 +252,7 @@ cleanup:
                wait_for_pid(pid);
 
        if (ret == KSFT_PASS &&
-           cg_read_strcmp(cgroup, "cgroup.events", "populated 0\n"))
+           cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
                ret = KSFT_FAIL;
 
        if (cgroup)