From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70B103AD52A for ; Wed, 15 Jul 2026 03:55:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784087704; cv=none; b=QtdRhvbwzqqBLjgzOAF/iDUjGxU6zgB7QQBwm65/xEqySXu37yYxnX/Bcldd3Ke5kYA9tZwJk/hq3bowFk93YwxOS2waMQao5wwdn8R5ZBlDiI1+rGEfQaFF+MSEBmQaI5S9DDiS7vVdtvVK0Je/wbue4z9FM5XkyV8Jk2D6Xmo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784087704; c=relaxed/simple; bh=ewdPb56xBjFmrnI7fXaoa7wPaFo1hdQQpES79KxD2+g=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=bQIAtMep6Me6vX+6aOgFySOTd1IkTAxmDZmGBz7aPV7vri6bsRDDL77yTn2OANChk5lqfKBRpUX2TnM/A8H6PJwx2nDYDmffq5tlSx2qbILhRdlez/tLT+PBjK30cXrePTvjPqutmiwEXzmwZn+M8Sbo7WDf8SOErPBDb+SP7T0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZlxNdZWy; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZlxNdZWy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784087701; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=GmK/PKs9sAs6/cDg4W7d1yX1cAFZHAmC6KiaUiX97ac=; b=ZlxNdZWyQRpKDcK5hL2FsCnk+cXTdztpQojJ8utJwGKILMUM45vdxtVEClEMM8DsjWzs7v gFpUhnp7aJGaLtJOxQTmVyHr03irsYP3Z0xk4AGK6WRg2ohzIa3bhjpAsd+T29rtOS/0G9 sNKHvEP2EW/7iqyoCJOS/qrskbK6m1k= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-Nqb5ZOT6P26dU6isF68nVQ-1; Tue, 14 Jul 2026 23:54:59 -0400 X-MC-Unique: Nqb5ZOT6P26dU6isF68nVQ-1 X-Mimecast-MFC-AGG-ID: Nqb5ZOT6P26dU6isF68nVQ_1784087697 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 892741956089; Wed, 15 Jul 2026 03:54:57 +0000 (UTC) Received: from llong-thinkpadp1gen5.rmtusnh.csb (unknown [10.22.89.63]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A402A30001A2; Wed, 15 Jul 2026 03:54:55 +0000 (UTC) From: Waiman Long To: Tejun Heo , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Shuah Khan Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Waiman Long Subject: [PATCH v2] selftests/cgroup: Fix intermittent test_cgfreezer_ptrace test failures Date: Tue, 14 Jul 2026 23:54:46 -0400 Message-ID: <20260715035446.565625-1-longman@redhat.com> Precedence: bulk X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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 Signed-off-by: Waiman Long --- 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