1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * membarrier system call
10 * Bitmask made from a "or" of all commands within enum membarrier_cmd,
11 * except MEMBARRIER_CMD_QUERY.
13 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
14 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
15 (MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE \
16 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
18 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK 0
21 #define MEMBARRIER_CMD_BITMASK \
22 (MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED \
23 | MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED \
24 | MEMBARRIER_CMD_PRIVATE_EXPEDITED \
25 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED \
26 | MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK)
28 static void ipi_mb(void *info)
30 smp_mb(); /* IPIs should be serializing but paranoid. */
33 static void ipi_sync_rq_state(void *info)
35 struct mm_struct *mm = (struct mm_struct *) info;
37 if (current->mm != mm)
39 this_cpu_write(runqueues.membarrier_state,
40 atomic_read(&mm->membarrier_state));
42 * Issue a memory barrier after setting
43 * MEMBARRIER_STATE_GLOBAL_EXPEDITED in the current runqueue to
44 * guarantee that no memory access following registration is reordered
45 * before registration.
50 void membarrier_exec_mmap(struct mm_struct *mm)
53 * Issue a memory barrier before clearing membarrier_state to
54 * guarantee that no memory access prior to exec is reordered after
55 * clearing this state.
58 atomic_set(&mm->membarrier_state, 0);
60 * Keep the runqueue membarrier_state in sync with this mm
63 this_cpu_write(runqueues.membarrier_state, 0);
66 static int membarrier_global_expedited(void)
69 bool fallback = false;
70 cpumask_var_t tmpmask;
72 if (num_online_cpus() == 1)
76 * Matches memory barriers around rq->curr modification in
79 smp_mb(); /* system call entry is not a mb. */
82 * Expedited membarrier commands guarantee that they won't
83 * block, hence the GFP_NOWAIT allocation flag and fallback
86 if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
87 /* Fallback for OOM. */
93 for_each_online_cpu(cpu) {
94 struct task_struct *p;
97 * Skipping the current CPU is OK even through we can be
98 * migrated at any point. The current CPU, at the point
99 * where we read raw_smp_processor_id(), is ensured to
100 * be in program order with respect to the caller
101 * thread. Therefore, we can skip this CPU from the
104 if (cpu == raw_smp_processor_id())
107 if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
108 MEMBARRIER_STATE_GLOBAL_EXPEDITED))
112 * Skip the CPU if it runs a kernel thread. The scheduler
113 * leaves the prior task mm in place as an optimization when
114 * scheduling a kthread.
116 p = rcu_dereference(cpu_rq(cpu)->curr);
117 if (p->flags & PF_KTHREAD)
121 __cpumask_set_cpu(cpu, tmpmask);
123 smp_call_function_single(cpu, ipi_mb, NULL, 1);
128 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
130 free_cpumask_var(tmpmask);
135 * Memory barrier on the caller thread _after_ we finished
136 * waiting for the last IPI. Matches memory barriers around
137 * rq->curr modification in scheduler.
139 smp_mb(); /* exit from system call is not a mb */
143 static int membarrier_private_expedited(int flags)
146 bool fallback = false;
147 cpumask_var_t tmpmask;
149 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
150 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
152 if (!(atomic_read(¤t->mm->membarrier_state) &
153 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
156 if (!(atomic_read(¤t->mm->membarrier_state) &
157 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
161 if (num_online_cpus() == 1)
165 * Matches memory barriers around rq->curr modification in
168 smp_mb(); /* system call entry is not a mb. */
171 * Expedited membarrier commands guarantee that they won't
172 * block, hence the GFP_NOWAIT allocation flag and fallback
175 if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
176 /* Fallback for OOM. */
182 for_each_online_cpu(cpu) {
183 struct task_struct *p;
186 * Skipping the current CPU is OK even through we can be
187 * migrated at any point. The current CPU, at the point
188 * where we read raw_smp_processor_id(), is ensured to
189 * be in program order with respect to the caller
190 * thread. Therefore, we can skip this CPU from the
193 if (cpu == raw_smp_processor_id())
196 p = rcu_dereference(cpu_rq(cpu)->curr);
197 if (p && p->mm == current->mm) {
199 __cpumask_set_cpu(cpu, tmpmask);
201 smp_call_function_single(cpu, ipi_mb, NULL, 1);
207 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
209 free_cpumask_var(tmpmask);
214 * Memory barrier on the caller thread _after_ we finished
215 * waiting for the last IPI. Matches memory barriers around
216 * rq->curr modification in scheduler.
218 smp_mb(); /* exit from system call is not a mb */
223 static int sync_runqueues_membarrier_state(struct mm_struct *mm)
225 int membarrier_state = atomic_read(&mm->membarrier_state);
226 cpumask_var_t tmpmask;
229 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
230 this_cpu_write(runqueues.membarrier_state, membarrier_state);
233 * For single mm user, we can simply issue a memory barrier
234 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
235 * mm and in the current runqueue to guarantee that no memory
236 * access following registration is reordered before
243 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
247 * For mm with multiple users, we need to ensure all future
248 * scheduler executions will observe @mm's new membarrier
254 * For each cpu runqueue, if the task's mm match @mm, ensure that all
255 * @mm's membarrier state set bits are also set in in the runqueue's
256 * membarrier state. This ensures that a runqueue scheduling
257 * between threads which are users of @mm has its membarrier state
262 for_each_online_cpu(cpu) {
263 struct rq *rq = cpu_rq(cpu);
264 struct task_struct *p;
266 p = rcu_dereference(&rq->curr);
267 if (p && p->mm == mm)
268 __cpumask_set_cpu(cpu, tmpmask);
273 smp_call_function_many(tmpmask, ipi_sync_rq_state, mm, 1);
276 free_cpumask_var(tmpmask);
282 static int membarrier_register_global_expedited(void)
284 struct task_struct *p = current;
285 struct mm_struct *mm = p->mm;
288 if (atomic_read(&mm->membarrier_state) &
289 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
291 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
292 ret = sync_runqueues_membarrier_state(mm);
295 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
296 &mm->membarrier_state);
301 static int membarrier_register_private_expedited(int flags)
303 struct task_struct *p = current;
304 struct mm_struct *mm = p->mm;
305 int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
306 set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
309 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
310 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
313 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
317 * We need to consider threads belonging to different thread
318 * groups, which use the same mm. (CLONE_VM but not
321 if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
323 if (flags & MEMBARRIER_FLAG_SYNC_CORE)
324 set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
325 atomic_or(set_state, &mm->membarrier_state);
326 ret = sync_runqueues_membarrier_state(mm);
329 atomic_or(ready_state, &mm->membarrier_state);
335 * sys_membarrier - issue memory barriers on a set of threads
336 * @cmd: Takes command values defined in enum membarrier_cmd.
337 * @flags: Currently needs to be 0. For future extensions.
339 * If this system call is not implemented, -ENOSYS is returned. If the
340 * command specified does not exist, not available on the running
341 * kernel, or if the command argument is invalid, this system call
342 * returns -EINVAL. For a given command, with flags argument set to 0,
343 * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
344 * always return the same value until reboot. In addition, it can return
345 * -ENOMEM if there is not enough memory available to perform the system
348 * All memory accesses performed in program order from each targeted thread
349 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
350 * the semantic "barrier()" to represent a compiler barrier forcing memory
351 * accesses to be performed in program order across the barrier, and
352 * smp_mb() to represent explicit memory barriers forcing full memory
353 * ordering across the barrier, we have the following ordering table for
354 * each pair of barrier(), sys_membarrier() and smp_mb():
356 * The pair ordering is detailed as (O: ordered, X: not ordered):
358 * barrier() smp_mb() sys_membarrier()
361 * sys_membarrier() O O O
363 SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
368 case MEMBARRIER_CMD_QUERY:
370 int cmd_mask = MEMBARRIER_CMD_BITMASK;
372 if (tick_nohz_full_enabled())
373 cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
376 case MEMBARRIER_CMD_GLOBAL:
377 /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
378 if (tick_nohz_full_enabled())
380 if (num_online_cpus() > 1)
383 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
384 return membarrier_global_expedited();
385 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
386 return membarrier_register_global_expedited();
387 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
388 return membarrier_private_expedited(0);
389 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
390 return membarrier_register_private_expedited(0);
391 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
392 return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
393 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
394 return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);