Skip to contentSkip to conversation

selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures

PATCH In review Waiman Long 2 participants 4 messages Jul 15 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures

It is found that the test_cgfreezer_ptrace test of test_freezer can intermittently fail on some architectures like arm64 and ppc64.

After further tracing of the mechanics of the test_cgfreezer_ptrace test, it is found that the ptrace(PTRACE_DETACH) call will spawn another process to perform the detach by temporarily unfreezes the cgroup and then freezes it again afterward during the detaching process. The reading of the frozen flag from cgroup.events is done by the main test_freezer process running probably on a different CPU. As a result, racing is possible and the intermediate unfrozen state can be read leading to occasional test failures especially on architectures with a weak memory model like arm64.

Fix that by using the cg_prepare_for_wait() and cg_wait_for() helpers to wait until frozen state is set or it times out. A suppress_debug_msg flag is added to suppress the printing of debug message when cg_check_frozen() is used during this waiting process.

By running test_freezer 100 times in a loop, there were 28 test_cgfreezer_ptrace failures out of 100 on an arm64 test system before the patch. After applying the patch, there was no test failure at all in 100 runs of test_freezer.

---
 tools/testing/selftests/cgroup/test_freezer.c | 41 +++++++++++++++++--
 1 file changed, 38 insertions(+), 3 deletions(-)
tools/testing/selftests/cgroup/test_freezer.c +38 -3
index 0569e93fa6b0..9edb2d34658c 100644
@@ -14,9 +14,11 @@
 #include "kselftest.h"
 #include "cgroup_util.h"
 
+static bool suppress_debug_msg;
+
 #define DEBUG
 #ifdef DEBUG
-#define debug(args...) fprintf(stderr, args)
+#define debug(args...) do { if (!suppress_debug_msg) fprintf(stderr, args); } while (0)
 #else
 #define debug(args...)
 #endif
@@ -585,7 +587,8 @@ static int test_cgfreezer_ptrace(const char *root)
 	int ret = KSFT_FAIL;
 	char *cgroup = NULL;
 	siginfo_t siginfo;
-	int pid;
+	int fd = -1, pid, retries;
+	bool frozen;
 
 	cgroup = cg_name(root, "cg_test_ptrace");
 	if (!cgroup)
@@ -622,15 +625,47 @@ static int test_cgfreezer_ptrace(const char *root)
 	if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo))
 		goto cleanup;
 
+	/*
+	 * The ptrace(PTRACE_DETACH) call spawns a different a process to
+	 * temporarily unfreeze the cgroup and then freeze it again in the
+	 * detaching process. The reading of the frozen flag from cgroup.events
+	 * is done by the main test process running probably on a different CPU.
+	 * As a result, racing is possible and the intermediate unfrozen state
+	 * can be read leading to occasional test failure especially on
+	 * architectures with a weak memory model like arm64.
+	 *
+	 * This intermittent test failure can be avoided by using the
+	 * cg_prepare_for_wait() and cg_wait_for() helpers to wait for change
+	 * in the frozen state if necessary.
+	 */
+	fd = cg_prepare_for_wait(cgroup);
+	if (fd < 0)
+		goto cleanup;
+
 	if (ptrace(PTRACE_DETACH, pid, NULL, NULL))
 		goto cleanup;
 
-	if (cg_check_frozen(cgroup, true))
+	suppress_debug_msg = true;
+	frozen = !cg_check_frozen(cgroup, true);
+	if (!frozen) {
+		/* Attempt to wait until frozen */
+		for (retries = 0; retries < 3; retries++) {
+			if (cg_wait_for(fd))
+				break;
+			frozen = !cg_check_frozen(cgroup, true);
+			if (frozen)
+				break;
+		}
+	}
+	suppress_debug_msg = false;
+	if (!frozen)
 		goto cleanup;
 
 	ret = KSFT_PASS;
 
 cleanup:
+	if (fd >= 0)
+		close(fd);
 	if (cgroup)
 		cg_destroy(cgroup);
 	free(cgroup);
Signature · 1 line
2.55.0
maintainer
#1 lore raw

Hi.

Quoted 6 lines · Waiman Long

It is found that the test_cgfreezer_ptrace test of test_freezer can intermittently fail on some architectures like arm64 and ppc64.

After further tracing of the mechanics of the test_cgfreezer_ptrace test, it is found that the ptrace(PTRACE_DETACH) call will spawn another process to perform the detach

I'm reading kernel/ptrace.c and cannot find a new task creation. I'm curious what is this "another process"?

(Or is it referring to the child_fn() process from test_cgfreezer_ptrace()? Then I'm suspicous about the transitional unfreezing of it after PTRACE_DETACH. Wasn't it rightful expectation of the test that the cgroup remains contiguously frozen?)

Quoted 6 lines

by temporarily unfreezes the cgroup and then freezes
it again afterward during the detaching process. The reading of the frozen flag from cgroup.events is done by the main test_freezer process running probably on a different CPU. As a result, racing is possible and the intermediate unfrozen state can be read leading to occasional test failures especially on architectures with a weak memory model like arm64.

What state would the task be in during this window?

Thanks,
Michal

#2 lore raw
Quoted 11 lines · Michal Koutný

Hi.

Quoted 6 lines · Waiman Long

It is found that the test_cgfreezer_ptrace test of test_freezer can intermittently fail on some architectures like arm64 and ppc64.

After further tracing of the mechanics of the test_cgfreezer_ptrace test, it is found that the ptrace(PTRACE_DETACH) call will spawn another process to perform the detach

I'm reading kernel/ptrace.c and cannot find a new task creation. I'm curious what is this "another process"?

I should be more clear about that. It is not exec'ing a new binary. It is just spawning a child process/thread which still has the same name but different pid as well as running on a different CPU which are what I am tracking.

Quoted 5 lines

(Or is it referring to the child_fn() process from test_cgfreezer_ptrace()? Then I'm suspicous about the transitional unfreezing of it after PTRACE_DETACH. Wasn't it rightful expectation of the test that the cgroup remains contiguously frozen?)

As said in my reply to the v1 thread, the detaching process includes a call to ptrace_stop() which will unfreeze the cgroup temporarily and then go back to frozen afterward. It is this transitional unfreeze/freeze that may cause the test to fail if it detects the transitional non-frozen state. My patch is to make sure that it can see the final the frozen state and avoid reporting failure.

Cheers,
Longman

Quoted 11 lines
Quoted 7 lines

by temporarily unfreezes the cgroup and then freezes
it again afterward during the detaching process. The reading of the frozen flag from cgroup.events is done by the main test_freezer process running probably on a different CPU. As a result, racing is possible and the intermediate unfrozen state can be read leading to occasional test failures especially on architectures with a weak memory model like arm64.

What state would the task be in during this window?

Thanks,
Michal

#4 lore raw
In reply to a message not in the archive
Quoted 63 lines · On 7/15/26 12:03 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The retry loop using cg_wait_for() spins instantaneously instead of waiting, because cg_wait_for() does not consume inotify events. --

--- Patch [1]: [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures --- commit 4e3a44a10c22322bc0c2be4b1ba1540164054d05
Author: Waiman Long <[email protected]>

selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures

This commit addresses intermittent failures in the test_cgfreezer_ptrace test on certain architectures by adding logic to wait for the cgroup frozen state. It introduces a retry loop using cg_prepare_for_wait() and cg_wait_for() helpers instead of failing immediately if the cgroup isn't frozen yet.

Quoted 4 lines
tools/testing/selftests/cgroup/test_freezer.c +0 -0
index 0569e93fa6b00..9edb2d34658c7 100644

[ ... ]

Quoted 21 lines
tools/testing/selftests/cgroup/test_freezer.c +17 -0
@@ -622,15 +625,47 @@ static int test_cgfreezer_ptrace(const char *root)
  	if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo))
  		goto cleanup;
 
+	/*
+	 * The ptrace(PTRACE_DETACH) call spawns a different a process to
+	 * temporarily unfreeze the cgroup and then freeze it again in the
+	 * detaching process. The reading of the frozen flag from cgroup.events
+	 * is done by the main test process running probably on a different CPU.
+	 * As a result, racing is possible and the intermediate unfrozen state
+	 * can be read leading to occasional test failure especially on
+	 * architectures with a weak memory model like arm64.
+	 *
+	 * This intermittent test failure can be avoided by using the
+	 * cg_prepare_for_wait() and cg_wait_for() helpers to wait for change
+	 * in the frozen state if necessary.
+	 */
+	fd = cg_prepare_for_wait(cgroup);
+	if (fd < 0)
+		goto cleanup;
+

tools/testing/selftests/cgroup/test_freezer.c static int test_cgfreezer_ptrace(const char *root) · line 653

Quoted 10 lines
tools/testing/selftests/cgroup/test_freezer.c +6 -1
  	if (ptrace(PTRACE_DETACH, pid, NULL, NULL))
  		goto cleanup;
 
-	if (cg_check_frozen(cgroup, true))
+	suppress_debug_msg = true;
+	frozen = !cg_check_frozen(cgroup, true);
+	if (!frozen) {
+		/* Attempt to wait until frozen */
+		for (retries = 0; retries < 3; retries++) {
+			if (cg_wait_for(fd))

[Severity: Medium]
Can this loop turn into a fast spin-loop without actually waiting?

Looking at cg_wait_for() in tools/testing/selftests/cgroup/lib/cgroup_util.c:

     if (ret > 0 && fds.revents & POLLIN) {
         ret = 0;
         break;
     }
Quoted 8 lines

It checks for POLLIN but does not call read() to consume the inotify event.

If an intermediate event (like the cgroup temporarily unfreezing) triggers POLLIN, won't subsequent calls to cg_wait_for() see the file descriptor as continuously readable and return instantly?

This would cause the retries to execute without any delay, failing to wait for the actual frozen state.

That is not true. cg_check_frozen() will read from cgroup.events consuming the inotify event.

Cheers,
Longman

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/cgroup/test_freezer.c +6 -0
+				break;
+			frozen = !cg_check_frozen(cgroup, true);
+			if (frozen)
+				break;
+		}
+	}