* [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures
@ 2026-07-15 3:54 Waiman Long
2026-07-15 9:36 ` Michal Koutný
0 siblings, 1 reply; 2+ messages in thread
From: Waiman Long @ 2026-07-15 3:54 UTC (permalink / raw)
To: Tejun Heo, Johannes Weiner, Michal Koutný, Shuah Khan
Cc: cgroups, linux-kernel, linux-kselftest, 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 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.
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Waiman Long <longman@redhat.com>
---
tools/testing/selftests/cgroup/test_freezer.c | 41 +++++++++++++++++--
1 file changed, 38 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_freezer.c b/tools/testing/selftests/cgroup/test_freezer.c
index 0569e93fa6b0..9edb2d34658c 100644
--- a/tools/testing/selftests/cgroup/test_freezer.c
+++ b/tools/testing/selftests/cgroup/test_freezer.c
@@ -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);
--
2.55.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures
2026-07-15 3:54 [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures Waiman Long
@ 2026-07-15 9:36 ` Michal Koutný
0 siblings, 0 replies; 2+ messages in thread
From: Michal Koutný @ 2026-07-15 9:36 UTC (permalink / raw)
To: Waiman Long
Cc: Tejun Heo, Johannes Weiner, Shuah Khan, cgroups, linux-kernel,
linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 1282 bytes --]
Hi.
On Tue, Jul 14, 2026 at 11:54:46PM -0400, Waiman Long <longman@redhat.com> wrote:
> 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?)
> 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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-15 9:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 3:54 [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures Waiman Long
2026-07-15 9:36 ` Michal Koutný
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox