2 * Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * membarrier system call
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.
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.
19 * Bitmask made from a "or" of all commands within enum membarrier_cmd,
20 * except MEMBARRIER_CMD_QUERY.
22 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
23 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
24 (MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE \
25 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
27 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK 0
30 #define MEMBARRIER_CMD_BITMASK \
31 (MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED \
32 | MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED \
33 | MEMBARRIER_CMD_PRIVATE_EXPEDITED \
34 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED \
35 | MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK)
37 static void ipi_mb(void *info)
39 smp_mb(); /* IPIs should be serializing but paranoid. */
42 static int membarrier_global_expedited(void)
45 bool fallback = false;
46 cpumask_var_t tmpmask;
48 if (num_online_cpus() == 1)
52 * Matches memory barriers around rq->curr modification in
55 smp_mb(); /* system call entry is not a mb. */
58 * Expedited membarrier commands guarantee that they won't
59 * block, hence the GFP_NOWAIT allocation flag and fallback
62 if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
63 /* Fallback for OOM. */
68 for_each_online_cpu(cpu) {
69 struct task_struct *p;
72 * Skipping the current CPU is OK even through we can be
73 * migrated at any point. The current CPU, at the point
74 * where we read raw_smp_processor_id(), is ensured to
75 * be in program order with respect to the caller
76 * thread. Therefore, we can skip this CPU from the
79 if (cpu == raw_smp_processor_id())
83 p = task_rcu_dereference(&cpu_rq(cpu)->curr);
84 if (p && p->mm && (atomic_read(&p->mm->membarrier_state) &
85 MEMBARRIER_STATE_GLOBAL_EXPEDITED)) {
87 __cpumask_set_cpu(cpu, tmpmask);
89 smp_call_function_single(cpu, ipi_mb, NULL, 1);
95 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
97 free_cpumask_var(tmpmask);
102 * Memory barrier on the caller thread _after_ we finished
103 * waiting for the last IPI. Matches memory barriers around
104 * rq->curr modification in scheduler.
106 smp_mb(); /* exit from system call is not a mb */
110 static int membarrier_private_expedited(int flags)
113 bool fallback = false;
114 cpumask_var_t tmpmask;
116 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
117 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
119 if (!(atomic_read(¤t->mm->membarrier_state) &
120 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
123 if (!(atomic_read(¤t->mm->membarrier_state) &
124 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
128 if (num_online_cpus() == 1)
132 * Matches memory barriers around rq->curr modification in
135 smp_mb(); /* system call entry is not a mb. */
138 * Expedited membarrier commands guarantee that they won't
139 * block, hence the GFP_NOWAIT allocation flag and fallback
142 if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
143 /* Fallback for OOM. */
148 for_each_online_cpu(cpu) {
149 struct task_struct *p;
152 * Skipping the current CPU is OK even through we can be
153 * migrated at any point. The current CPU, at the point
154 * where we read raw_smp_processor_id(), is ensured to
155 * be in program order with respect to the caller
156 * thread. Therefore, we can skip this CPU from the
159 if (cpu == raw_smp_processor_id())
162 p = task_rcu_dereference(&cpu_rq(cpu)->curr);
163 if (p && p->mm == current->mm) {
165 __cpumask_set_cpu(cpu, tmpmask);
167 smp_call_function_single(cpu, ipi_mb, NULL, 1);
173 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
175 free_cpumask_var(tmpmask);
180 * Memory barrier on the caller thread _after_ we finished
181 * waiting for the last IPI. Matches memory barriers around
182 * rq->curr modification in scheduler.
184 smp_mb(); /* exit from system call is not a mb */
189 static int membarrier_register_global_expedited(void)
191 struct task_struct *p = current;
192 struct mm_struct *mm = p->mm;
194 if (atomic_read(&mm->membarrier_state) &
195 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
197 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
198 if (atomic_read(&mm->mm_users) == 1 && get_nr_threads(p) == 1) {
200 * For single mm user, single threaded process, we can
201 * simply issue a memory barrier after setting
202 * MEMBARRIER_STATE_GLOBAL_EXPEDITED to guarantee that
203 * no memory access following registration is reordered
204 * before registration.
209 * For multi-mm user threads, we need to ensure all
210 * future scheduler executions will observe the new
211 * thread flag state for this mm.
215 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
216 &mm->membarrier_state);
221 static int membarrier_register_private_expedited(int flags)
223 struct task_struct *p = current;
224 struct mm_struct *mm = p->mm;
225 int state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY;
227 if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
228 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
230 state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
234 * We need to consider threads belonging to different thread
235 * groups, which use the same mm. (CLONE_VM but not
238 if ((atomic_read(&mm->membarrier_state) & state) == state)
240 atomic_or(MEMBARRIER_STATE_PRIVATE_EXPEDITED, &mm->membarrier_state);
241 if (flags & MEMBARRIER_FLAG_SYNC_CORE)
242 atomic_or(MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE,
243 &mm->membarrier_state);
244 if (!(atomic_read(&mm->mm_users) == 1 && get_nr_threads(p) == 1)) {
246 * Ensure all future scheduler executions will observe the
247 * new thread flag state for this process.
251 atomic_or(state, &mm->membarrier_state);
257 * sys_membarrier - issue memory barriers on a set of threads
258 * @cmd: Takes command values defined in enum membarrier_cmd.
259 * @flags: Currently needs to be 0. For future extensions.
261 * If this system call is not implemented, -ENOSYS is returned. If the
262 * command specified does not exist, not available on the running
263 * kernel, or if the command argument is invalid, this system call
264 * returns -EINVAL. For a given command, with flags argument set to 0,
265 * this system call is guaranteed to always return the same value until
268 * All memory accesses performed in program order from each targeted thread
269 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
270 * the semantic "barrier()" to represent a compiler barrier forcing memory
271 * accesses to be performed in program order across the barrier, and
272 * smp_mb() to represent explicit memory barriers forcing full memory
273 * ordering across the barrier, we have the following ordering table for
274 * each pair of barrier(), sys_membarrier() and smp_mb():
276 * The pair ordering is detailed as (O: ordered, X: not ordered):
278 * barrier() smp_mb() sys_membarrier()
281 * sys_membarrier() O O O
283 SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
288 case MEMBARRIER_CMD_QUERY:
290 int cmd_mask = MEMBARRIER_CMD_BITMASK;
292 if (tick_nohz_full_enabled())
293 cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
296 case MEMBARRIER_CMD_GLOBAL:
297 /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
298 if (tick_nohz_full_enabled())
300 if (num_online_cpus() > 1)
303 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
304 return membarrier_global_expedited();
305 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
306 return membarrier_register_global_expedited();
307 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
308 return membarrier_private_expedited(0);
309 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
310 return membarrier_register_private_expedited(0);
311 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
312 return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
313 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
314 return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);