Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795805907; b=QxTA8Fjb4BCem8ZR6lfPnhQIDq0G2fHZ+biIVz34b/OC6/TjariWVfVGH5theXzpd9pj ZKgvZG7yGUECjyjpitAlHZwcFohcelX4BFTeC5o8cJOi9526pz3dLYf6vW8UF5XfSdBhf 2iWbha2IICemqXyQtX6NIfmaV7RwJMjzoI1aHE8Mc+SQururOG/G4Sb1NiN6TJkqqVkW4 3QuERe0y9fFDuu8OhlkobOaRC+cbnsKZiT+bOzcVxLeMRXGHfpHizf9VRf5mE3s1STg6o lqHwNIx57cIR+rwu/jQkSBeSf32eh7xeEb/C8asQBmGDNhjp2iMoQotuPbFmvRSOUSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795805907; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=3orQFE08kjrTAUJE8mLYeXKLSOzKBUY432bsO80CLow=; b=sFlWyLv9A6LGIjHk8TRp4NRHrb/0wij4rlKeq2jURNpYDe/SL+hgu7v5PLwUM876cNKi RY/vdSU98StA8/V+MIGZgx+gRIkHmeEyns7ebWvTnWE//FfModQNebi7MxQHpucqTtk+3 qx3Kd07w+NIWyclegO3103/Cilyk45H67ycbvWPmiCo1yTrtBq9VKT3FZHV9YeiAeC/CR G04BXvfovBmuVRYXfdPUm/BwBUrZ6UQGIMLBTfh4FVnuZZSquapSZuY9QZXEW/aZW/VR6 YAatTjKK+7W01CTrXHHUKozInDu3P4v/0xmThy28ty8VtxaivOiuaopdiwqQGnKag4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=vri6d6zD; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38DC7606A33A for ; Fri, 27 Nov 2026 18:58:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 38DC7606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=vri6d6zD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1795805905; bh=3orQFE08kjrTAUJE8mLYeXKLSOzKBUY432bsO80CLow=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vri6d6zDiPU0y0fFDVVYaiFSrZWthF/KlX+ZeuZjh68EFJ0icwjWuAqJtz/7GUrW6 ZnFL6n6vNH6i+ea+FvMPuzrUdKrJ3+WJyDm09Jl5DgWpd9jJPqtJLjQB7j2kAh+tPP GAb2e8pn2vd/bah3ieNJyBew6IjxR1hbFvdPyZi6TgOyKvfj36uUlaGHsjR/1DYZwP FDXZLxZdIkqcQvsSm/IlCrWIHIgag+80KRrZPinSTlF3dPsmc+pg6JRxiY4hasvvrn I65tjVve3BroBH9CdnbzVQI5kkFw5ErU/h8qeSa4K097cq6CZjH0Zy4Ss4N8GkSc6A DxPXWVsZj9yhw== Received: from thinkos.internal.efficios.com (mtl.efficios.com [216.120.195.104]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jbjNx2CpdzXmL; Fri, 27 Nov 2026 13:58:25 -0500 (EST) From: Mathieu Desnoyers To: Peter Zijlstra , Ingo Molnar Cc: Mathieu Desnoyers , "Paul E. McKenney" , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Alan Stern , Andrea Parri , Will Deacon , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , linux-arch@vger.kernel.org, lkmm@lists.linux.dev Subject: [PATCH v2 13/16] selftests/membarrier: Add litmus tests of the memory ordering Date: Fri, 27 Nov 2026 13:58:18 -0500 Message-ID: <20261127185822.158216-14-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261127185822.158216-1-mathieu.desnoyers@efficios.com> References: <20261127185822.158216-1-mathieu.desnoyers@efficios.com> X-Mailing-List: lkmm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit membarrier relies on memory barriers in the system call, in its IPI handler and in the scheduler. The comment at the top of kernel/sched/membarrier.c describes the scenarios. A test on real hardware can check that the kernel executes a barrier. It cannot show that the barrier is needed: a missing barrier may never fail in practice. Check these scenarios with the Linux kernel memory model instead. The litmus tests cover: - a thread which runs after the IPI, scenario (A), - a thread which ran before the IPI, scenario (B), - a thread which is scheduled in while membarrier() sees another task in rq->curr, scenarios (A) and (C), - a thread which is scheduled out before membarrier() loads rq->curr, scenarios (B) and (C). herd7 reports "Never" for these 4 tests. 9 variants drop one barrier each, and herd7 reports "Sometimes" for them. One of the variants replaces the full barrier after the store to rq->curr with smp_store_release(). This is the LoongArch and parisc case. The README describes the model and how to run the tests. Signed-off-by: Mathieu Desnoyers --- kernel/sched/membarrier.c | 3 ++ .../selftests/membarrier/litmus/README | 52 +++++++++++++++++++ .../litmus/ipi-after-thread-no-exit-mb.litmus | 36 +++++++++++++ .../ipi-after-thread-no-handler-mb.litmus | 36 +++++++++++++ .../membarrier/litmus/ipi-after-thread.litmus | 47 +++++++++++++++++ .../ipi-before-thread-no-entry-mb.litmus | 37 +++++++++++++ .../ipi-before-thread-no-handler-mb.litmus | 37 +++++++++++++ .../litmus/ipi-before-thread.litmus | 46 ++++++++++++++++ .../litmus/sched-in-no-entry-mb.litmus | 34 ++++++++++++ .../membarrier/litmus/sched-in-no-mb.litmus | 33 ++++++++++++ .../membarrier/litmus/sched-in-release.litmus | 38 ++++++++++++++ .../membarrier/litmus/sched-in.litmus | 51 ++++++++++++++++++ .../litmus/sched-out-no-exit-mb.litmus | 34 ++++++++++++ .../litmus/sched-out-no-sched-mb.litmus | 34 ++++++++++++ .../membarrier/litmus/sched-out.litmus | 49 +++++++++++++++++ 15 files changed, 567 insertions(+) create mode 100644 tools/testing/selftests/membarrier/litmus/README create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-exit-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-handler-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-after-thread.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-entry-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-handler-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/ipi-before-thread.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-in-no-entry-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-in-no-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-in-release.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-in.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-out-no-exit-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-out-no-sched-mb.litmus create mode 100644 tools/testing/selftests/membarrier/litmus/sched-out.litmus diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c index 46e823a3d668..5dd88ac0957b 100644 --- a/kernel/sched/membarrier.c +++ b/kernel/sched/membarrier.c @@ -133,6 +133,9 @@ * Using the scenario from (A), we can show that (a) needs to be paired * with (g). Using the scenario from (B), we can show that (c) needs to * be paired with (d). + * + * The litmus tests in tools/testing/selftests/membarrier/litmus check + * these scenarios with the Linux kernel memory model. */ /* diff --git a/tools/testing/selftests/membarrier/litmus/README b/tools/testing/selftests/membarrier/litmus/README new file mode 100644 index 000000000000..c9ae572a9d43 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/README @@ -0,0 +1,52 @@ +Litmus tests of the membarrier system call. They check the memory +ordering scenarios of kernel/sched/membarrier.c with the Linux kernel +memory model. Each test with the result "Never" models a case of +these scenarios. A test with the result "Sometimes" drops one +barrier, to show that the scenario needs it. + +In each test, P0 is the caller of membarrier(). P1 is a CPU which runs +a thread of the target process. + +The user-space accesses of the thread are WRITE_ONCE() and READ_ONCE() +in program order, without a memory barrier between them. This is what +barrier() gives in user-space code. + +The model has no interrupt. The IPI is modeled as a request and an +acknowledgment: the caller stores "req", the handler loads it, issues +its memory barrier and stores "ack", and the caller loads "ack". The +"exists" clause of each test keeps the executions where this exchange +took place. + +Run them from tools/memory-model, with herd7 installed: + + cd tools/memory-model + for f in ../testing/selftests/membarrier/litmus/*.litmus; do + herd7 -conf linux-kernel.cfg $f | grep Observation + done + +Each observation must match the "Result" in the comment of its test. + +The thread is interrupted by the IPI: + + ipi-before-thread.litmus, ipi-before-thread-no-entry-mb.litmus, + ipi-before-thread-no-handler-mb.litmus + Scenario (A). The thread runs after the IPI. It sees the stores + which the caller did before membarrier(). + + ipi-after-thread.litmus, ipi-after-thread-no-exit-mb.litmus, + ipi-after-thread-no-handler-mb.litmus + Scenario (B). The thread ran before the IPI. The caller sees its + stores after membarrier(). + +The caller sees another task in rq->curr and sends no IPI: + + sched-in.litmus, sched-in-no-entry-mb.litmus, sched-in-no-mb.litmus, + sched-in-release.litmus + Scenarios (A) and (C). The thread is scheduled in. The scheduler + issues a full barrier after the store to rq->curr. An + smp_store_release() which is not a full barrier is not enough. + + sched-out.litmus, sched-out-no-exit-mb.litmus, + sched-out-no-sched-mb.litmus + Scenarios (B) and (C). The thread is scheduled out. The scheduler + issues a full barrier before the store to rq->curr. diff --git a/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-exit-mb.litmus b/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-exit-mb.litmus new file mode 100644 index 000000000000..fb67beaba996 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-exit-mb.litmus @@ -0,0 +1,36 @@ +C ipi-after-thread-no-exit-mb + +(* + * Result: Sometimes + * + * ipi-after-thread without the barrier (c) at the end of membarrier(). + * The load of x by P0 can then be done before the IPI completes. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + r_req = READ_ONCE(*req); + smp_mb(); + WRITE_ONCE(*ack, 1); +} + +exists (0:r2=1 /\ 1:r_req=1 /\ 0:r_ack=1 /\ 0:r1=0) diff --git a/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-handler-mb.litmus b/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-handler-mb.litmus new file mode 100644 index 000000000000..687e883850a9 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-after-thread-no-handler-mb.litmus @@ -0,0 +1,36 @@ +C ipi-after-thread-no-handler-mb + +(* + * Result: Sometimes + * + * ipi-after-thread without the barrier in the IPI handler. The store to + * x by P1 can then be delayed past the acknowledgment. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + smp_mb(); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + r_req = READ_ONCE(*req); + WRITE_ONCE(*ack, 1); +} + +exists (0:r2=1 /\ 1:r_req=1 /\ 0:r_ack=1 /\ 0:r1=0) diff --git a/tools/testing/selftests/membarrier/litmus/ipi-after-thread.litmus b/tools/testing/selftests/membarrier/litmus/ipi-after-thread.litmus new file mode 100644 index 000000000000..1145faf62b6d --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-after-thread.litmus @@ -0,0 +1,47 @@ +C ipi-after-thread + +(* + * Result: Never + * + * Scenario (B) of kernel/sched/membarrier.c. + * + * P1 is a user-space thread which runs "x = 1; y = 1" in program order, + * without a memory barrier. The IPI of membarrier() interrupts it after + * that. P0 reads y before membarrier() and x after it. + * + * req and ack model the IPI, see README. + * + * The barrier in the IPI handler orders both stores before the + * acknowledgment. The barrier (c) at the end of membarrier() orders the + * acknowledgment before "r1 = x". P0 thus sees x == 1. This holds + * whatever P0 read from y: y is kept to match the scenario. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + smp_mb(); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + r_req = READ_ONCE(*req); + smp_mb(); + WRITE_ONCE(*ack, 1); +} + +exists (0:r2=1 /\ 1:r_req=1 /\ 0:r_ack=1 /\ 0:r1=0) diff --git a/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-entry-mb.litmus b/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-entry-mb.litmus new file mode 100644 index 000000000000..9685e34e224a --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-entry-mb.litmus @@ -0,0 +1,37 @@ +C ipi-before-thread-no-entry-mb + +(* + * Result: Sometimes + * + * ipi-before-thread without the barrier (a) at the start of + * membarrier(). The store to x can be delayed past the IPI. P1 and P0 + * then both read 0. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r2; + + WRITE_ONCE(*x, 1); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + int r1; + + r_req = READ_ONCE(*req); + smp_mb(); + WRITE_ONCE(*ack, 1); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (1:r_req=1 /\ 0:r_ack=1 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-handler-mb.litmus b/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-handler-mb.litmus new file mode 100644 index 000000000000..1559bce76ea5 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-before-thread-no-handler-mb.litmus @@ -0,0 +1,37 @@ +C ipi-before-thread-no-handler-mb + +(* + * Result: Sometimes + * + * ipi-before-thread without the barrier in the IPI handler. The load of + * x by P1 can then be done before the handler loads req. This is why + * ipi_mb() issues smp_mb() rather than rely on the interrupt alone. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r2; + + WRITE_ONCE(*x, 1); + smp_mb(); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + int r1; + + r_req = READ_ONCE(*req); + WRITE_ONCE(*ack, 1); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (1:r_req=1 /\ 0:r_ack=1 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/ipi-before-thread.litmus b/tools/testing/selftests/membarrier/litmus/ipi-before-thread.litmus new file mode 100644 index 000000000000..e21e79e2d2ad --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/ipi-before-thread.litmus @@ -0,0 +1,46 @@ +C ipi-before-thread + +(* + * Result: Never + * + * Scenario (A) of kernel/sched/membarrier.c. + * + * P0 calls membarrier(). P1 is a user-space thread which the IPI + * interrupts before it runs "y = 1; r1 = x". Its two accesses are in + * program order, without a memory barrier. + * + * req and ack model the IPI, see README. + * + * The barrier (a) at the start of membarrier() orders "x = 1" before + * the IPI. P1 thus sees x == 1 after the IPI. This holds whatever P0 + * reads from y: y is kept to match the scenario. + *) + +{} + +P0(int *x, int *y, int *req, int *ack) +{ + int r_ack; + int r2; + + WRITE_ONCE(*x, 1); + smp_mb(); + WRITE_ONCE(*req, 1); + r_ack = READ_ONCE(*ack); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *req, int *ack) +{ + int r_req; + int r1; + + r_req = READ_ONCE(*req); + smp_mb(); + WRITE_ONCE(*ack, 1); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (1:r_req=1 /\ 0:r_ack=1 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-in-no-entry-mb.litmus b/tools/testing/selftests/membarrier/litmus/sched-in-no-entry-mb.litmus new file mode 100644 index 000000000000..a40837fad005 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-in-no-entry-mb.litmus @@ -0,0 +1,34 @@ +C sched-in-no-entry-mb + +(* + * Result: Sometimes + * + * sched-in without the barrier (a) at the start of membarrier(). The + * store to x can be delayed past the load of rq->curr. P0 sees no + * thread and sends no IPI. The thread reads x == 0. + *) + +{} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r2; + + WRITE_ONCE(*x, 1); + r_curr = READ_ONCE(*curr); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *curr) +{ + int r1; + + WRITE_ONCE(*curr, 1); + smp_mb(); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (0:r_curr=0 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-in-no-mb.litmus b/tools/testing/selftests/membarrier/litmus/sched-in-no-mb.litmus new file mode 100644 index 000000000000..6840cb26e13d --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-in-no-mb.litmus @@ -0,0 +1,33 @@ +C sched-in-no-mb + +(* + * Result: Sometimes + * + * sched-in without the full barrier after the store to rq->curr. P0 + * sees no thread and sends no IPI. The thread reads x == 0. + *) + +{} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r2; + + WRITE_ONCE(*x, 1); + smp_mb(); + r_curr = READ_ONCE(*curr); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *curr) +{ + int r1; + + WRITE_ONCE(*curr, 1); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (0:r_curr=0 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-in-release.litmus b/tools/testing/selftests/membarrier/litmus/sched-in-release.litmus new file mode 100644 index 000000000000..d2df75b388fe --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-in-release.litmus @@ -0,0 +1,38 @@ +C sched-in-release + +(* + * Result: Sometimes + * + * sched-in with smp_store_release() in place of the full barrier after + * the store to rq->curr. This is finish_task() on an architecture where + * smp_store_release() is weaker than a full barrier. It does not order + * the store to rq->curr before the loads of the thread. + *) + +{ + int on_cpu = 1; +} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r2; + + WRITE_ONCE(*x, 1); + smp_mb(); + r_curr = READ_ONCE(*curr); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *curr, int *on_cpu) +{ + int r1; + + WRITE_ONCE(*curr, 1); + smp_store_release(on_cpu, 0); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (0:r_curr=0 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-in.litmus b/tools/testing/selftests/membarrier/litmus/sched-in.litmus new file mode 100644 index 000000000000..c89e2c6c4554 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-in.litmus @@ -0,0 +1,51 @@ +C sched-in + +(* + * Result: Never + * + * Scenarios (A) and (C) of kernel/sched/membarrier.c: a thread of the + * target process is scheduled in while P0 calls membarrier(). + * + * curr stands for rq->curr of the CPU of P1: 0 for another task, 1 for + * the thread. P0 loads it between its two barriers. It sends no IPI + * when it reads 0, which is the case the "exists" clause keeps. + * + * P1 is the scheduler, then the thread in user-space. The scheduler + * stores to rq->curr, then issues a full barrier before the return to + * user-space. Each architecture provides this barrier its own way, see + * Documentation/scheduler/membarrier.rst. + * + * Either P0 sees the thread and sends the IPI, or the thread sees + * x == 1. This holds whatever P0 reads from y: y is kept to match the + * scenario. + * + * The same argument holds with tsk->mm in place of rq->curr. It covers + * kthread_use_mm(), scenario (E), and the icache_stale_mask of the + * RISC-V sync core command. + *) + +{} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r2; + + WRITE_ONCE(*x, 1); + smp_mb(); + r_curr = READ_ONCE(*curr); + smp_mb(); + r2 = READ_ONCE(*y); +} + +P1(int *x, int *y, int *curr) +{ + int r1; + + WRITE_ONCE(*curr, 1); + smp_mb(); + WRITE_ONCE(*y, 1); + r1 = READ_ONCE(*x); +} + +exists (0:r_curr=0 /\ 1:r1=0 /\ 0:r2=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-out-no-exit-mb.litmus b/tools/testing/selftests/membarrier/litmus/sched-out-no-exit-mb.litmus new file mode 100644 index 000000000000..e4bb1ee7a935 --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-out-no-exit-mb.litmus @@ -0,0 +1,34 @@ +C sched-out-no-exit-mb + +(* + * Result: Sometimes + * + * sched-out without the barrier (c) at the end of membarrier(). The + * load of x by P0 can then be done before it loads rq->curr. + *) + +{ + int curr = 1; +} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + r_curr = READ_ONCE(*curr); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *curr) +{ + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + smp_mb(); + WRITE_ONCE(*curr, 0); +} + +exists (0:r2=1 /\ 0:r_curr=0 /\ 0:r1=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-out-no-sched-mb.litmus b/tools/testing/selftests/membarrier/litmus/sched-out-no-sched-mb.litmus new file mode 100644 index 000000000000..7b690db2b4be --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-out-no-sched-mb.litmus @@ -0,0 +1,34 @@ +C sched-out-no-sched-mb + +(* + * Result: Sometimes + * + * sched-out without the full barrier before the store to rq->curr. The + * store to x by the thread can then be delayed past it. + *) + +{ + int curr = 1; +} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + r_curr = READ_ONCE(*curr); + smp_mb(); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *curr) +{ + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + WRITE_ONCE(*curr, 0); +} + +exists (0:r2=1 /\ 0:r_curr=0 /\ 0:r1=0) diff --git a/tools/testing/selftests/membarrier/litmus/sched-out.litmus b/tools/testing/selftests/membarrier/litmus/sched-out.litmus new file mode 100644 index 000000000000..8fb1532bd13c --- /dev/null +++ b/tools/testing/selftests/membarrier/litmus/sched-out.litmus @@ -0,0 +1,49 @@ +C sched-out + +(* + * Result: Never + * + * Scenarios (B) and (C) of kernel/sched/membarrier.c: a thread of the + * target process is scheduled out while P0 calls membarrier(). + * + * curr stands for rq->curr of the CPU of P1: 1 for the thread, 0 for + * another task. P0 sends no IPI when it reads 0, which is the case the + * "exists" clause keeps. + * + * P1 is the thread in user-space, which runs "x = 1; y = 1" in program + * order. Then the scheduler issues a full barrier before it stores to + * rq->curr: smp_mb__after_spinlock() in __schedule(). + * + * When P0 saw y == 1 and sees that the thread is gone, it sees x == 1. + * This holds whatever P0 read from y: y is kept to match the scenario. + * + * The same argument holds with tsk->mm in place of rq->curr. It covers + * exit_mm(), scenario (D), and kthread_unuse_mm(), scenario (E). + *) + +{ + int curr = 1; +} + +P0(int *x, int *y, int *curr) +{ + int r_curr; + int r1; + int r2; + + r2 = READ_ONCE(*y); + smp_mb(); + r_curr = READ_ONCE(*curr); + smp_mb(); + r1 = READ_ONCE(*x); +} + +P1(int *x, int *y, int *curr) +{ + WRITE_ONCE(*x, 1); + WRITE_ONCE(*y, 1); + smp_mb(); + WRITE_ONCE(*curr, 0); +} + +exists (0:r2=1 /\ 0:r_curr=0 /\ 0:r1=0) -- 2.47.3