Skip to content

Commit 22e4ebb

Browse files
compudjpaulmck
authored andcommitted
membarrier: Provide expedited private command
Implement MEMBARRIER_CMD_PRIVATE_EXPEDITED with IPIs using cpumask built from all runqueues for which current thread's mm is the same as the thread calling sys_membarrier. It executes faster than the non-expedited variant (no blocking). It also works on NOHZ_FULL configurations. Scheduler-wise, it requires a memory barrier before and after context switching between processes (which have different mm). The memory barrier before context switch is already present. For the barrier after context switch: * Our TSO archs can do RELEASE without being a full barrier. Look at x86 spin_unlock() being a regular STORE for example. But for those archs, all atomics imply smp_mb and all of them have atomic ops in switch_mm() for mm_cpumask(), and on x86 the CR3 load acts as a full barrier. * From all weakly ordered machines, only ARM64 and PPC can do RELEASE, the rest does indeed do smp_mb(), so there the spin_unlock() is a full barrier and we're good. * ARM64 has a very heavy barrier in switch_to(), which suffices. * PPC just removed its barrier from switch_to(), but appears to be talking about adding something to switch_mm(). So add a smp_mb__after_unlock_lock() for now, until this is settled on the PPC side. Changes since v3: - Properly document the memory barriers provided by each architecture. Changes since v2: - Address comments from Peter Zijlstra, - Add smp_mb__after_unlock_lock() after finish_lock_switch() in finish_task_switch() to add the memory barrier we need after storing to rq->curr. This is much simpler than the previous approach relying on atomic_dec_and_test() in mmdrop(), which actually added a memory barrier in the common case of switching between userspace processes. - Return -EINVAL when MEMBARRIER_CMD_SHARED is used on a nohz_full kernel, rather than having the whole membarrier system call returning -ENOSYS. Indeed, CMD_PRIVATE_EXPEDITED is compatible with nohz_full. Adapt the CMD_QUERY mask accordingly. Changes since v1: - move membarrier code under kernel/sched/ because it uses the scheduler runqueue, - only add the barrier when we switch from a kernel thread. The case where we switch from a user-space thread is already handled by the atomic_dec_and_test() in mmdrop(). - add a comment to mmdrop() documenting the requirement on the implicit memory barrier. CC: Peter Zijlstra <peterz@infradead.org> CC: Paul E. McKenney <paulmck@linux.vnet.ibm.com> CC: Boqun Feng <boqun.feng@gmail.com> CC: Andrew Hunter <ahh@google.com> CC: Maged Michael <maged.michael@gmail.com> CC: gromer@google.com CC: Avi Kivity <avi@scylladb.com> CC: Benjamin Herrenschmidt <benh@kernel.crashing.org> CC: Paul Mackerras <paulus@samba.org> CC: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Tested-by: Dave Watson <davejwatson@fb.com>
1 parent 955dbdf commit 22e4ebb

8 files changed

Lines changed: 202 additions & 74 deletions

File tree

‎MAINTAINERS‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8621,7 +8621,7 @@ M: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
86218621
M: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
86228622
L: linux-kernel@vger.kernel.org
86238623
S: Supported
8624-
F: kernel/membarrier.c
8624+
F: kernel/sched/membarrier.c
86258625
F: include/uapi/linux/membarrier.h
86268626

86278627
MEMORY MANAGEMENT

‎arch/arm64/kernel/process.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
360360
/*
361361
* Complete any pending TLB or cache maintenance on this CPU in case
362362
* the thread migrates to a different CPU.
363+
* This full barrier is also required by the membarrier system
364+
* call.
363365
*/
364366
dsb(ish);
365367

‎include/uapi/linux/membarrier.h‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,33 @@
4040
* (non-running threads are de facto in such a
4141
* state). This covers threads from all processes
4242
* running on the system. This command returns 0.
43+
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED:
44+
* Execute a memory barrier on each running
45+
* thread belonging to the same process as the current
46+
* thread. Upon return from system call, the
47+
* caller thread is ensured that all its running
48+
* threads siblings have passed through a state
49+
* where all memory accesses to user-space
50+
* addresses match program order between entry
51+
* to and return from the system call
52+
* (non-running threads are de facto in such a
53+
* state). This only covers threads from the
54+
* same processes as the caller thread. This
55+
* command returns 0. The "expedited" commands
56+
* complete faster than the non-expedited ones,
57+
* they never block, but have the downside of
58+
* causing extra overhead.
4359
*
4460
* Command to be passed to the membarrier system call. The commands need to
4561
* be a single bit each, except for MEMBARRIER_CMD_QUERY which is assigned to
4662
* the value 0.
4763
*/
4864
enum membarrier_cmd {
49-
MEMBARRIER_CMD_QUERY = 0,
50-
MEMBARRIER_CMD_SHARED = (1 << 0),
65+
MEMBARRIER_CMD_QUERY = 0,
66+
MEMBARRIER_CMD_SHARED = (1 << 0),
67+
/* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
68+
/* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
69+
MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
5170
};
5271

5372
#endif /* _UAPI_LINUX_MEMBARRIER_H */

‎kernel/Makefile‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
108108
obj-$(CONFIG_JUMP_LABEL) += jump_label.o
109109
obj-$(CONFIG_CONTEXT_TRACKING) += context_tracking.o
110110
obj-$(CONFIG_TORTURE_TEST) += torture.o
111-
obj-$(CONFIG_MEMBARRIER) += membarrier.o
112111

113112
obj-$(CONFIG_HAS_IOMEM) += memremap.o
114113

‎kernel/membarrier.c‎

Lines changed: 0 additions & 70 deletions
This file was deleted.

‎kernel/sched/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ obj-$(CONFIG_SCHED_DEBUG) += debug.o
2525
obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o
2626
obj-$(CONFIG_CPU_FREQ) += cpufreq.o
2727
obj-$(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) += cpufreq_schedutil.o
28+
obj-$(CONFIG_MEMBARRIER) += membarrier.o

‎kernel/sched/core.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,16 @@ static struct rq *finish_task_switch(struct task_struct *prev)
26402640
prev_state = prev->state;
26412641
vtime_task_switch(prev);
26422642
perf_event_task_sched_in(prev, current);
2643+
/*
2644+
* The membarrier system call requires a full memory barrier
2645+
* after storing to rq->curr, before going back to user-space.
2646+
*
2647+
* TODO: This smp_mb__after_unlock_lock can go away if PPC end
2648+
* up adding a full barrier to switch_mm(), or we should figure
2649+
* out if a smp_mb__after_unlock_lock is really the proper API
2650+
* to use.
2651+
*/
2652+
smp_mb__after_unlock_lock();
26432653
finish_lock_switch(rq, prev);
26442654
finish_arch_post_lock_switch();
26452655

@@ -3329,6 +3339,21 @@ static void __sched notrace __schedule(bool preempt)
33293339
if (likely(prev != next)) {
33303340
rq->nr_switches++;
33313341
rq->curr = next;
3342+
/*
3343+
* The membarrier system call requires each architecture
3344+
* to have a full memory barrier after updating
3345+
* rq->curr, before returning to user-space. For TSO
3346+
* (e.g. x86), the architecture must provide its own
3347+
* barrier in switch_mm(). For weakly ordered machines
3348+
* for which spin_unlock() acts as a full memory
3349+
* barrier, finish_lock_switch() in common code takes
3350+
* care of this barrier. For weakly ordered machines for
3351+
* which spin_unlock() acts as a RELEASE barrier (only
3352+
* arm64 and PowerPC), arm64 has a full barrier in
3353+
* switch_to(), and PowerPC has
3354+
* smp_mb__after_unlock_lock() before
3355+
* finish_lock_switch().
3356+
*/
33323357
++*switch_count;
33333358

33343359
trace_sched_switch(preempt, prev, next);

‎kernel/sched/membarrier.c‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3+
*
4+
* membarrier system call
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*/
16+
17+
#include <linux/syscalls.h>
18+
#include <linux/membarrier.h>
19+
#include <linux/tick.h>
20+
#include <linux/cpumask.h>
21+
22+
#include "sched.h" /* for cpu_rq(). */
23+
24+
/*
25+
* Bitmask made from a "or" of all commands within enum membarrier_cmd,
26+
* except MEMBARRIER_CMD_QUERY.
27+
*/
28+
#define MEMBARRIER_CMD_BITMASK \
29+
(MEMBARRIER_CMD_SHARED | MEMBARRIER_CMD_PRIVATE_EXPEDITED)
30+
31+
static void ipi_mb(void *info)
32+
{
33+
smp_mb(); /* IPIs should be serializing but paranoid. */
34+
}
35+
36+
static void membarrier_private_expedited(void)
37+
{
38+
int cpu;
39+
bool fallback = false;
40+
cpumask_var_t tmpmask;
41+
42+
if (num_online_cpus() == 1)
43+
return;
44+
45+
/*
46+
* Matches memory barriers around rq->curr modification in
47+
* scheduler.
48+
*/
49+
smp_mb(); /* system call entry is not a mb. */
50+
51+
/*
52+
* Expedited membarrier commands guarantee that they won't
53+
* block, hence the GFP_NOWAIT allocation flag and fallback
54+
* implementation.
55+
*/
56+
if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
57+
/* Fallback for OOM. */
58+
fallback = true;
59+
}
60+
61+
cpus_read_lock();
62+
for_each_online_cpu(cpu) {
63+
struct task_struct *p;
64+
65+
/*
66+
* Skipping the current CPU is OK even through we can be
67+
* migrated at any point. The current CPU, at the point
68+
* where we read raw_smp_processor_id(), is ensured to
69+
* be in program order with respect to the caller
70+
* thread. Therefore, we can skip this CPU from the
71+
* iteration.
72+
*/
73+
if (cpu == raw_smp_processor_id())
74+
continue;
75+
rcu_read_lock();
76+
p = task_rcu_dereference(&cpu_rq(cpu)->curr);
77+
if (p && p->mm == current->mm) {
78+
if (!fallback)
79+
__cpumask_set_cpu(cpu, tmpmask);
80+
else
81+
smp_call_function_single(cpu, ipi_mb, NULL, 1);
82+
}
83+
rcu_read_unlock();
84+
}
85+
if (!fallback) {
86+
smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
87+
free_cpumask_var(tmpmask);
88+
}
89+
cpus_read_unlock();
90+
91+
/*
92+
* Memory barrier on the caller thread _after_ we finished
93+
* waiting for the last IPI. Matches memory barriers around
94+
* rq->curr modification in scheduler.
95+
*/
96+
smp_mb(); /* exit from system call is not a mb */
97+
}
98+
99+
/**
100+
* sys_membarrier - issue memory barriers on a set of threads
101+
* @cmd: Takes command values defined in enum membarrier_cmd.
102+
* @flags: Currently needs to be 0. For future extensions.
103+
*
104+
* If this system call is not implemented, -ENOSYS is returned. If the
105+
* command specified does not exist, not available on the running
106+
* kernel, or if the command argument is invalid, this system call
107+
* returns -EINVAL. For a given command, with flags argument set to 0,
108+
* this system call is guaranteed to always return the same value until
109+
* reboot.
110+
*
111+
* All memory accesses performed in program order from each targeted thread
112+
* is guaranteed to be ordered with respect to sys_membarrier(). If we use
113+
* the semantic "barrier()" to represent a compiler barrier forcing memory
114+
* accesses to be performed in program order across the barrier, and
115+
* smp_mb() to represent explicit memory barriers forcing full memory
116+
* ordering across the barrier, we have the following ordering table for
117+
* each pair of barrier(), sys_membarrier() and smp_mb():
118+
*
119+
* The pair ordering is detailed as (O: ordered, X: not ordered):
120+
*
121+
* barrier() smp_mb() sys_membarrier()
122+
* barrier() X X O
123+
* smp_mb() X O O
124+
* sys_membarrier() O O O
125+
*/
126+
SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
127+
{
128+
if (unlikely(flags))
129+
return -EINVAL;
130+
switch (cmd) {
131+
case MEMBARRIER_CMD_QUERY:
132+
{
133+
int cmd_mask = MEMBARRIER_CMD_BITMASK;
134+
135+
if (tick_nohz_full_enabled())
136+
cmd_mask &= ~MEMBARRIER_CMD_SHARED;
137+
return cmd_mask;
138+
}
139+
case MEMBARRIER_CMD_SHARED:
140+
/* MEMBARRIER_CMD_SHARED is not compatible with nohz_full. */
141+
if (tick_nohz_full_enabled())
142+
return -EINVAL;
143+
if (num_online_cpus() > 1)
144+
synchronize_sched();
145+
return 0;
146+
case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
147+
membarrier_private_expedited();
148+
return 0;
149+
default:
150+
return -EINVAL;
151+
}
152+
}

0 commit comments

Comments
 (0)