Skip to content

Commit a14d11a

Browse files
andrea-parripalmer-dabbelt
authored andcommitted
membarrier: Create Documentation/scheduler/membarrier.rst
To gather the architecture requirements of the "private/global expedited" membarrier commands. The file will be expanded to integrate further information about the membarrier syscall (as needed/desired in the future). While at it, amend some related inline comments in the membarrier codebase. Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Andrea Parri <parri.andrea@gmail.com> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://lore.kernel.org/r/20240131144936.29190-3-parri.andrea@gmail.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent d6cfd17 commit a14d11a

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

Documentation/scheduler/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Scheduler
77

88

99
completion
10+
membarrier
1011
sched-arch
1112
sched-bwc
1213
sched-deadline
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
========================
4+
membarrier() System Call
5+
========================
6+
7+
MEMBARRIER_CMD_{PRIVATE,GLOBAL}_EXPEDITED - Architecture requirements
8+
=====================================================================
9+
10+
Memory barriers before updating rq->curr
11+
----------------------------------------
12+
13+
The commands MEMBARRIER_CMD_PRIVATE_EXPEDITED and MEMBARRIER_CMD_GLOBAL_EXPEDITED
14+
require each architecture to have a full memory barrier after coming from
15+
user-space, before updating rq->curr. This barrier is implied by the sequence
16+
rq_lock(); smp_mb__after_spinlock() in __schedule(). The barrier matches a full
17+
barrier in the proximity of the membarrier system call exit, cf.
18+
membarrier_{private,global}_expedited().
19+
20+
Memory barriers after updating rq->curr
21+
---------------------------------------
22+
23+
The commands MEMBARRIER_CMD_PRIVATE_EXPEDITED and MEMBARRIER_CMD_GLOBAL_EXPEDITED
24+
require each architecture to have a full memory barrier after updating rq->curr,
25+
before returning to user-space. The schemes providing this barrier on the various
26+
architectures are as follows.
27+
28+
- alpha, arc, arm, hexagon, mips rely on the full barrier implied by
29+
spin_unlock() in finish_lock_switch().
30+
31+
- arm64 relies on the full barrier implied by switch_to().
32+
33+
- powerpc, riscv, s390, sparc, x86 rely on the full barrier implied by
34+
switch_mm(), if mm is not NULL; they rely on the full barrier implied
35+
by mmdrop(), otherwise. On powerpc and riscv, switch_mm() relies on
36+
membarrier_arch_switch_mm().
37+
38+
The barrier matches a full barrier in the proximity of the membarrier system call
39+
entry, cf. membarrier_{private,global}_expedited().

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14039,6 +14039,7 @@ M: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1403914039
M: "Paul E. McKenney" <paulmck@kernel.org>
1404014040
L: linux-kernel@vger.kernel.org
1404114041
S: Supported
14042+
F: Documentation/scheduler/membarrier.rst
1404214043
F: arch/*/include/asm/membarrier.h
1404314044
F: include/uapi/linux/membarrier.h
1404414045
F: kernel/sched/membarrier.c

kernel/sched/core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6638,7 +6638,9 @@ static void __sched notrace __schedule(unsigned int sched_mode)
66386638
* if (signal_pending_state()) if (p->state & @state)
66396639
*
66406640
* Also, the membarrier system call requires a full memory barrier
6641-
* after coming from user-space, before storing to rq->curr.
6641+
* after coming from user-space, before storing to rq->curr; this
6642+
* barrier matches a full barrier in the proximity of the membarrier
6643+
* system call exit.
66426644
*/
66436645
rq_lock(rq, &rf);
66446646
smp_mb__after_spinlock();
@@ -6716,6 +6718,9 @@ static void __sched notrace __schedule(unsigned int sched_mode)
67166718
* architectures where spin_unlock is a full barrier,
67176719
* - switch_to() for arm64 (weakly-ordered, spin_unlock
67186720
* is a RELEASE barrier),
6721+
*
6722+
* The barrier matches a full barrier in the proximity of
6723+
* the membarrier system call entry.
67196724
*/
67206725
++*switch_count;
67216726

kernel/sched/membarrier.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static int membarrier_global_expedited(void)
251251
return 0;
252252

253253
/*
254-
* Matches memory barriers around rq->curr modification in
254+
* Matches memory barriers after rq->curr modification in
255255
* scheduler.
256256
*/
257257
smp_mb(); /* system call entry is not a mb. */
@@ -300,7 +300,7 @@ static int membarrier_global_expedited(void)
300300

301301
/*
302302
* Memory barrier on the caller thread _after_ we finished
303-
* waiting for the last IPI. Matches memory barriers around
303+
* waiting for the last IPI. Matches memory barriers before
304304
* rq->curr modification in scheduler.
305305
*/
306306
smp_mb(); /* exit from system call is not a mb */
@@ -339,7 +339,7 @@ static int membarrier_private_expedited(int flags, int cpu_id)
339339
return 0;
340340

341341
/*
342-
* Matches memory barriers around rq->curr modification in
342+
* Matches memory barriers after rq->curr modification in
343343
* scheduler.
344344
*/
345345
smp_mb(); /* system call entry is not a mb. */
@@ -415,7 +415,7 @@ static int membarrier_private_expedited(int flags, int cpu_id)
415415

416416
/*
417417
* Memory barrier on the caller thread _after_ we finished
418-
* waiting for the last IPI. Matches memory barriers around
418+
* waiting for the last IPI. Matches memory barriers before
419419
* rq->curr modification in scheduler.
420420
*/
421421
smp_mb(); /* exit from system call is not a mb */

0 commit comments

Comments
 (0)