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_core(void *info)
36 * The smp_mb() in membarrier after all the IPIs is supposed to
37 * ensure that memory on remote CPUs that occur before the IPI
38 * become visible to membarrier()'s caller -- see scenario B in
39 * the big comment at the top of this file.
41 * A sync_core() would provide this guarantee, but
42 * sync_core_before_usermode() might end up being deferred until
43 * after membarrier()'s smp_mb().
45 smp_mb(); /* IPIs should be serializing but paranoid. */
47 sync_core_before_usermode();
50 static void ipi_sync_rq_state(void *info)
52 struct mm_struct *mm = (struct mm_struct *) info;
54 if (current->mm != mm)
56 this_cpu_write(runqueues.membarrier_state,
57 atomic_read(&mm->membarrier_state));
59 * Issue a memory barrier after setting
60 * MEMBARRIER_STATE_GLOBAL_EXPEDITED in the current runqueue to
61 * guarantee that no memory access following registration is reordered
62 * before registration.
67 void membarrier_exec_mmap(struct mm_struct *mm)
70 * Issue a memory barrier before clearing membarrier_state to
71 * guarantee that no memory access prior to exec is reordered after
72 * clearing this state.
75 atomic_set(&mm->membarrier_state, 0);
77 * Keep the runqueue membarrier_state in sync with this mm
80 this_cpu_write(runqueues.membarrier_state, 0);
83 static int membarrier_global_expedited(void)
86 cpumask_var_t tmpmask;
88 if (num_online_cpus() == 1)
92 * Matches memory barriers around rq->curr modification in
95 smp_mb(); /* system call entry is not a mb. */
97 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
102 for_each_online_cpu(cpu) {
103 struct task_struct *p;
106 * Skipping the current CPU is OK even through we can be
107 * migrated at any point. The current CPU, at the point
108 * where we read raw_smp_processor_id(), is ensured to
109 * be in program order with respect to the caller
110 * thread. Therefore, we can skip this CPU from the
113 if (cpu == raw_smp_processor_id())
116 if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
117 MEMBARRIER_STATE_GLOBAL_EXPEDITED))
121 * Skip the CPU if it runs a kernel thread. The scheduler
122 * leaves the prior task mm in place as an optimization when
123 * scheduling a kthread.
125 p = rcu_dereference(cpu_rq(cpu)->curr);
126 if (p->flags & PF_KTHREAD)
129 __cpumask_set_cpu(cpu, tmpmask);
134 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
137 free_cpumask_var(tmpmask);
141 * Memory barrier on the caller thread _after_ we finished
142 * waiting for the last IPI. Matches memory barriers around
143 * rq->curr modification in scheduler.
145 smp_mb(); /* exit from system call is not a mb */
149 static int membarrier_private_expedited(int flags)
152 cpumask_var_t tmpmask;
153 struct mm_struct *mm = current->mm;
154 smp_call_func_t ipi_func = ipi_mb;
156 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
157 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
159 if (!(atomic_read(&mm->membarrier_state) &
160 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
162 ipi_func = ipi_sync_core;
164 if (!(atomic_read(&mm->membarrier_state) &
165 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
169 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1)
173 * Matches memory barriers around rq->curr modification in
176 smp_mb(); /* system call entry is not a mb. */
178 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
183 for_each_online_cpu(cpu) {
184 struct task_struct *p;
187 * Skipping the current CPU is OK even through we can be
188 * migrated at any point. The current CPU, at the point
189 * where we read raw_smp_processor_id(), is ensured to
190 * be in program order with respect to the caller
191 * thread. Therefore, we can skip this CPU from the
194 if (cpu == raw_smp_processor_id())
196 p = rcu_dereference(cpu_rq(cpu)->curr);
197 if (p && p->mm == mm)
198 __cpumask_set_cpu(cpu, tmpmask);
203 smp_call_function_many(tmpmask, ipi_func, NULL, 1);
206 free_cpumask_var(tmpmask);
210 * Memory barrier on the caller thread _after_ we finished
211 * waiting for the last IPI. Matches memory barriers around
212 * rq->curr modification in scheduler.
214 smp_mb(); /* exit from system call is not a mb */
219 static int sync_runqueues_membarrier_state(struct mm_struct *mm)
221 int membarrier_state = atomic_read(&mm->membarrier_state);
222 cpumask_var_t tmpmask;
225 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
226 this_cpu_write(runqueues.membarrier_state, membarrier_state);
229 * For single mm user, we can simply issue a memory barrier
230 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
231 * mm and in the current runqueue to guarantee that no memory
232 * access following registration is reordered before
239 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
243 * For mm with multiple users, we need to ensure all future
244 * scheduler executions will observe @mm's new membarrier
250 * For each cpu runqueue, if the task's mm match @mm, ensure that all
251 * @mm's membarrier state set bits are also set in in the runqueue's
252 * membarrier state. This ensures that a runqueue scheduling
253 * between threads which are users of @mm has its membarrier state
258 for_each_online_cpu(cpu) {
259 struct rq *rq = cpu_rq(cpu);
260 struct task_struct *p;
262 p = rcu_dereference(rq->curr);
263 if (p && p->mm == mm)
264 __cpumask_set_cpu(cpu, tmpmask);
268 on_each_cpu_mask(tmpmask, ipi_sync_rq_state, mm, true);
270 free_cpumask_var(tmpmask);
276 static int membarrier_register_global_expedited(void)
278 struct task_struct *p = current;
279 struct mm_struct *mm = p->mm;
282 if (atomic_read(&mm->membarrier_state) &
283 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
285 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
286 ret = sync_runqueues_membarrier_state(mm);
289 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
290 &mm->membarrier_state);
295 static int membarrier_register_private_expedited(int flags)
297 struct task_struct *p = current;
298 struct mm_struct *mm = p->mm;
299 int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
300 set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
303 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
304 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
307 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
311 * We need to consider threads belonging to different thread
312 * groups, which use the same mm. (CLONE_VM but not
315 if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
317 if (flags & MEMBARRIER_FLAG_SYNC_CORE)
318 set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
319 atomic_or(set_state, &mm->membarrier_state);
320 ret = sync_runqueues_membarrier_state(mm);
323 atomic_or(ready_state, &mm->membarrier_state);
329 * sys_membarrier - issue memory barriers on a set of threads
330 * @cmd: Takes command values defined in enum membarrier_cmd.
331 * @flags: Currently needs to be 0. For future extensions.
333 * If this system call is not implemented, -ENOSYS is returned. If the
334 * command specified does not exist, not available on the running
335 * kernel, or if the command argument is invalid, this system call
336 * returns -EINVAL. For a given command, with flags argument set to 0,
337 * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
338 * always return the same value until reboot. In addition, it can return
339 * -ENOMEM if there is not enough memory available to perform the system
342 * All memory accesses performed in program order from each targeted thread
343 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
344 * the semantic "barrier()" to represent a compiler barrier forcing memory
345 * accesses to be performed in program order across the barrier, and
346 * smp_mb() to represent explicit memory barriers forcing full memory
347 * ordering across the barrier, we have the following ordering table for
348 * each pair of barrier(), sys_membarrier() and smp_mb():
350 * The pair ordering is detailed as (O: ordered, X: not ordered):
352 * barrier() smp_mb() sys_membarrier()
355 * sys_membarrier() O O O
357 SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
362 case MEMBARRIER_CMD_QUERY:
364 int cmd_mask = MEMBARRIER_CMD_BITMASK;
366 if (tick_nohz_full_enabled())
367 cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
370 case MEMBARRIER_CMD_GLOBAL:
371 /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
372 if (tick_nohz_full_enabled())
374 if (num_online_cpus() > 1)
377 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
378 return membarrier_global_expedited();
379 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
380 return membarrier_register_global_expedited();
381 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
382 return membarrier_private_expedited(0);
383 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
384 return membarrier_register_private_expedited(0);
385 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
386 return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
387 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
388 return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);