selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures
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
@@ -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
Michal Koutný
maintainerHi.
Quoted 6 lines · Waiman Long
It is found that the
test_cgfreezer_ptracetest oftest_freezercan intermittently fail on some architectures like arm64 and ppc64.After further tracing of the mechanics of the
test_cgfreezer_ptracetest, it is found that theptrace(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 maintest_freezerprocess 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
Quoted 11 lines · Michal Koutný
Hi.
Quoted 6 lines · Waiman Long
It is found that the
test_cgfreezer_ptracetest oftest_freezercan intermittently fail on some architectures like arm64 and ppc64.After further tracing of the mechanics of the
test_cgfreezer_ptracetest, it is found that theptrace(PTRACE_DETACH)call will spawn another process to perform the detachI'm reading
kernel/ptrace.cand 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 fromtest_cgfreezer_ptrace()? Then I'm suspicous about the transitional unfreezing of it afterPTRACE_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 maintest_freezerprocess 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
A message that is not in the archive (sent off-list or not yet received): [email protected]
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 usingcg_wait_for()spins instantaneously instead of waiting, becausecg_wait_for()does not consume inotify events. ----- Patch [1]: [PATCH v2] selftests/cgroup: Fix intermittent
test_cgfreezer_ptracetest failures --- commit4e3a44a10c22322bc0c2be4b1ba1540164054d05
Author: Waiman Long <[email protected]>selftests/cgroup: Fix intermittent
test_cgfreezer_ptracetest failuresThis commit addresses intermittent failures in the
test_cgfreezer_ptracetest on certain architectures by adding logic to wait for the cgroup frozen state. It introduces a retry loop usingcg_prepare_for_wait()andcg_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[ ... ]
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
-1if (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()intools/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 ; + } + }