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 cpumask_var_t tmpmask;
71 if (num_online_cpus() == 1)
75 * Matches memory barriers around rq->curr modification in
78 smp_mb(); /* system call entry is not a mb. */
80 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
85 for_each_online_cpu(cpu) {
86 struct task_struct *p;
89 * Skipping the current CPU is OK even through we can be
90 * migrated at any point. The current CPU, at the point
91 * where we read raw_smp_processor_id(), is ensured to
92 * be in program order with respect to the caller
93 * thread. Therefore, we can skip this CPU from the
96 if (cpu == raw_smp_processor_id())
99 if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
100 MEMBARRIER_STATE_GLOBAL_EXPEDITED))
104 * Skip the CPU if it runs a kernel thread. The scheduler
105 * leaves the prior task mm in place as an optimization when
106 * scheduling a kthread.
108 p = rcu_dereference(cpu_rq(cpu)->curr);
109 if (p->flags & PF_KTHREAD)
112 __cpumask_set_cpu(cpu, tmpmask);
117 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
120 free_cpumask_var(tmpmask);
124 * Memory barrier on the caller thread _after_ we finished
125 * waiting for the last IPI. Matches memory barriers around
126 * rq->curr modification in scheduler.
128 smp_mb(); /* exit from system call is not a mb */
132 static int membarrier_private_expedited(int flags)
135 cpumask_var_t tmpmask;
136 struct mm_struct *mm = current->mm;
138 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
139 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
141 if (!(atomic_read(&mm->membarrier_state) &
142 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
145 if (!(atomic_read(&mm->membarrier_state) &
146 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
150 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1)
154 * Matches memory barriers around rq->curr modification in
157 smp_mb(); /* system call entry is not a mb. */
159 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
164 for_each_online_cpu(cpu) {
165 struct task_struct *p;
168 * Skipping the current CPU is OK even through we can be
169 * migrated at any point. The current CPU, at the point
170 * where we read raw_smp_processor_id(), is ensured to
171 * be in program order with respect to the caller
172 * thread. Therefore, we can skip this CPU from the
175 if (cpu == raw_smp_processor_id())
177 p = rcu_dereference(cpu_rq(cpu)->curr);
178 if (p && p->mm == mm)
179 __cpumask_set_cpu(cpu, tmpmask);
184 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
187 free_cpumask_var(tmpmask);
191 * Memory barrier on the caller thread _after_ we finished
192 * waiting for the last IPI. Matches memory barriers around
193 * rq->curr modification in scheduler.
195 smp_mb(); /* exit from system call is not a mb */
200 static int sync_runqueues_membarrier_state(struct mm_struct *mm)
202 int membarrier_state = atomic_read(&mm->membarrier_state);
203 cpumask_var_t tmpmask;
206 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
207 this_cpu_write(runqueues.membarrier_state, membarrier_state);
210 * For single mm user, we can simply issue a memory barrier
211 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
212 * mm and in the current runqueue to guarantee that no memory
213 * access following registration is reordered before
220 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
224 * For mm with multiple users, we need to ensure all future
225 * scheduler executions will observe @mm's new membarrier
231 * For each cpu runqueue, if the task's mm match @mm, ensure that all
232 * @mm's membarrier state set bits are also set in in the runqueue's
233 * membarrier state. This ensures that a runqueue scheduling
234 * between threads which are users of @mm has its membarrier state
239 for_each_online_cpu(cpu) {
240 struct rq *rq = cpu_rq(cpu);
241 struct task_struct *p;
243 p = rcu_dereference(rq->curr);
244 if (p && p->mm == mm)
245 __cpumask_set_cpu(cpu, tmpmask);
250 smp_call_function_many(tmpmask, ipi_sync_rq_state, mm, 1);
253 free_cpumask_var(tmpmask);
259 static int membarrier_register_global_expedited(void)
261 struct task_struct *p = current;
262 struct mm_struct *mm = p->mm;
265 if (atomic_read(&mm->membarrier_state) &
266 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
268 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
269 ret = sync_runqueues_membarrier_state(mm);
272 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
273 &mm->membarrier_state);
278 static int membarrier_register_private_expedited(int flags)
280 struct task_struct *p = current;
281 struct mm_struct *mm = p->mm;
282 int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
283 set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
286 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
287 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
290 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
294 * We need to consider threads belonging to different thread
295 * groups, which use the same mm. (CLONE_VM but not
298 if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
300 if (flags & MEMBARRIER_FLAG_SYNC_CORE)
301 set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
302 atomic_or(set_state, &mm->membarrier_state);
303 ret = sync_runqueues_membarrier_state(mm);
306 atomic_or(ready_state, &mm->membarrier_state);
312 * sys_membarrier - issue memory barriers on a set of threads
313 * @cmd: Takes command values defined in enum membarrier_cmd.
314 * @flags: Currently needs to be 0. For future extensions.
316 * If this system call is not implemented, -ENOSYS is returned. If the
317 * command specified does not exist, not available on the running
318 * kernel, or if the command argument is invalid, this system call
319 * returns -EINVAL. For a given command, with flags argument set to 0,
320 * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
321 * always return the same value until reboot. In addition, it can return
322 * -ENOMEM if there is not enough memory available to perform the system
325 * All memory accesses performed in program order from each targeted thread
326 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
327 * the semantic "barrier()" to represent a compiler barrier forcing memory
328 * accesses to be performed in program order across the barrier, and
329 * smp_mb() to represent explicit memory barriers forcing full memory
330 * ordering across the barrier, we have the following ordering table for
331 * each pair of barrier(), sys_membarrier() and smp_mb():
333 * The pair ordering is detailed as (O: ordered, X: not ordered):
335 * barrier() smp_mb() sys_membarrier()
338 * sys_membarrier() O O O
340 SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
345 case MEMBARRIER_CMD_QUERY:
347 int cmd_mask = MEMBARRIER_CMD_BITMASK;
349 if (tick_nohz_full_enabled())
350 cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
353 case MEMBARRIER_CMD_GLOBAL:
354 /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
355 if (tick_nohz_full_enabled())
357 if (num_online_cpus() > 1)
360 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
361 return membarrier_global_expedited();
362 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
363 return membarrier_register_global_expedited();
364 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
365 return membarrier_private_expedited(0);
366 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
367 return membarrier_register_private_expedited(0);
368 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
369 return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
370 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
371 return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);