blob: 3d2825408e3a2544fd19ace94839d8ed34e20929 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04002/*
3 * Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * membarrier system call
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04006 */
Ingo Molnar325ea102018-03-03 12:20:47 +01007#include "sched.h"
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04008
9/*
Mathieu Desnoyers25595eb2020-10-20 09:47:15 -040010 * For documentation purposes, here are some membarrier ordering
11 * scenarios to keep in mind:
12 *
13 * A) Userspace thread execution after IPI vs membarrier's memory
14 * barrier before sending the IPI
15 *
16 * Userspace variables:
17 *
18 * int x = 0, y = 0;
19 *
20 * The memory barrier at the start of membarrier() on CPU0 is necessary in
21 * order to enforce the guarantee that any writes occurring on CPU0 before
22 * the membarrier() is executed will be visible to any code executing on
23 * CPU1 after the IPI-induced memory barrier:
24 *
25 * CPU0 CPU1
26 *
27 * x = 1
28 * membarrier():
29 * a: smp_mb()
30 * b: send IPI IPI-induced mb
31 * c: smp_mb()
32 * r2 = y
33 * y = 1
34 * barrier()
35 * r1 = x
36 *
37 * BUG_ON(r1 == 0 && r2 == 0)
38 *
39 * The write to y and load from x by CPU1 are unordered by the hardware,
40 * so it's possible to have "r1 = x" reordered before "y = 1" at any
41 * point after (b). If the memory barrier at (a) is omitted, then "x = 1"
42 * can be reordered after (a) (although not after (c)), so we get r1 == 0
43 * and r2 == 0. This violates the guarantee that membarrier() is
44 * supposed by provide.
45 *
46 * The timing of the memory barrier at (a) has to ensure that it executes
47 * before the IPI-induced memory barrier on CPU1.
48 *
49 * B) Userspace thread execution before IPI vs membarrier's memory
50 * barrier after completing the IPI
51 *
52 * Userspace variables:
53 *
54 * int x = 0, y = 0;
55 *
56 * The memory barrier at the end of membarrier() on CPU0 is necessary in
57 * order to enforce the guarantee that any writes occurring on CPU1 before
58 * the membarrier() is executed will be visible to any code executing on
59 * CPU0 after the membarrier():
60 *
61 * CPU0 CPU1
62 *
63 * x = 1
64 * barrier()
65 * y = 1
66 * r2 = y
67 * membarrier():
68 * a: smp_mb()
69 * b: send IPI IPI-induced mb
70 * c: smp_mb()
71 * r1 = x
72 * BUG_ON(r1 == 0 && r2 == 1)
73 *
74 * The writes to x and y are unordered by the hardware, so it's possible to
75 * have "r2 = 1" even though the write to x doesn't execute until (b). If
76 * the memory barrier at (c) is omitted then "r1 = x" can be reordered
77 * before (b) (although not before (a)), so we get "r1 = 0". This violates
78 * the guarantee that membarrier() is supposed to provide.
79 *
80 * The timing of the memory barrier at (c) has to ensure that it executes
81 * after the IPI-induced memory barrier on CPU1.
82 *
83 * C) Scheduling userspace thread -> kthread -> userspace thread vs membarrier
84 *
85 * CPU0 CPU1
86 *
87 * membarrier():
88 * a: smp_mb()
89 * d: switch to kthread (includes mb)
90 * b: read rq->curr->mm == NULL
91 * e: switch to user (includes mb)
92 * c: smp_mb()
93 *
94 * Using the scenario from (A), we can show that (a) needs to be paired
95 * with (e). Using the scenario from (B), we can show that (c) needs to
96 * be paired with (d).
97 *
98 * D) exit_mm vs membarrier
99 *
100 * Two thread groups are created, A and B. Thread group B is created by
101 * issuing clone from group A with flag CLONE_VM set, but not CLONE_THREAD.
102 * Let's assume we have a single thread within each thread group (Thread A
103 * and Thread B). Thread A runs on CPU0, Thread B runs on CPU1.
104 *
105 * CPU0 CPU1
106 *
107 * membarrier():
108 * a: smp_mb()
109 * exit_mm():
110 * d: smp_mb()
111 * e: current->mm = NULL
112 * b: read rq->curr->mm == NULL
113 * c: smp_mb()
114 *
115 * Using scenario (B), we can show that (c) needs to be paired with (d).
116 *
117 * E) kthread_{use,unuse}_mm vs membarrier
118 *
119 * CPU0 CPU1
120 *
121 * membarrier():
122 * a: smp_mb()
123 * kthread_unuse_mm()
124 * d: smp_mb()
125 * e: current->mm = NULL
126 * b: read rq->curr->mm == NULL
127 * kthread_use_mm()
128 * f: current->mm = mm
129 * g: smp_mb()
130 * c: smp_mb()
131 *
132 * Using the scenario from (A), we can show that (a) needs to be paired
133 * with (g). Using the scenario from (B), we can show that (c) needs to
134 * be paired with (d).
135 */
136
137/*
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400138 * Bitmask made from a "or" of all commands within enum membarrier_cmd,
139 * except MEMBARRIER_CMD_QUERY.
140 */
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500141#ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100142#define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
143 (MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE \
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500144 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
145#else
146#define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK 0
147#endif
148
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700149#ifdef CONFIG_RSEQ
Mathieu Desnoyers80923262022-01-17 15:30:10 -0500150#define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK \
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700151 (MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ \
Mathieu Desnoyers80923262022-01-17 15:30:10 -0500152 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ)
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700153#else
Mathieu Desnoyers80923262022-01-17 15:30:10 -0500154#define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK 0
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700155#endif
156
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100157#define MEMBARRIER_CMD_BITMASK \
158 (MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED \
159 | MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED \
160 | MEMBARRIER_CMD_PRIVATE_EXPEDITED \
161 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED \
Mathieu Desnoyers80923262022-01-17 15:30:10 -0500162 | MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
163 | MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK)
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400164
165static void ipi_mb(void *info)
166{
167 smp_mb(); /* IPIs should be serializing but paranoid. */
168}
169
Andy Lutomirski758c9372020-12-03 21:07:05 -0800170static void ipi_sync_core(void *info)
171{
172 /*
173 * The smp_mb() in membarrier after all the IPIs is supposed to
174 * ensure that memory on remote CPUs that occur before the IPI
175 * become visible to membarrier()'s caller -- see scenario B in
176 * the big comment at the top of this file.
177 *
178 * A sync_core() would provide this guarantee, but
179 * sync_core_before_usermode() might end up being deferred until
180 * after membarrier()'s smp_mb().
181 */
182 smp_mb(); /* IPIs should be serializing but paranoid. */
183
184 sync_core_before_usermode();
185}
186
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700187static void ipi_rseq(void *info)
188{
Andy Lutomirski2ecedd72020-12-03 21:07:04 -0800189 /*
190 * Ensure that all stores done by the calling thread are visible
191 * to the current task before the current task resumes. We could
192 * probably optimize this away on most architectures, but by the
193 * time we've already sent an IPI, the cost of the extra smp_mb()
194 * is negligible.
195 */
196 smp_mb();
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700197 rseq_preempt(current);
198}
199
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400200static void ipi_sync_rq_state(void *info)
201{
202 struct mm_struct *mm = (struct mm_struct *) info;
203
204 if (current->mm != mm)
205 return;
206 this_cpu_write(runqueues.membarrier_state,
207 atomic_read(&mm->membarrier_state));
208 /*
209 * Issue a memory barrier after setting
210 * MEMBARRIER_STATE_GLOBAL_EXPEDITED in the current runqueue to
211 * guarantee that no memory access following registration is reordered
212 * before registration.
213 */
214 smp_mb();
215}
216
217void membarrier_exec_mmap(struct mm_struct *mm)
218{
219 /*
220 * Issue a memory barrier before clearing membarrier_state to
221 * guarantee that no memory access prior to exec is reordered after
222 * clearing this state.
223 */
224 smp_mb();
225 atomic_set(&mm->membarrier_state, 0);
226 /*
227 * Keep the runqueue membarrier_state in sync with this mm
228 * membarrier_state.
229 */
230 this_cpu_write(runqueues.membarrier_state, 0);
231}
232
Mathieu Desnoyers5bc78502020-10-20 09:47:13 -0400233void membarrier_update_current_mm(struct mm_struct *next_mm)
234{
235 struct rq *rq = this_rq();
236 int membarrier_state = 0;
237
238 if (next_mm)
239 membarrier_state = atomic_read(&next_mm->membarrier_state);
240 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
241 return;
242 WRITE_ONCE(rq->membarrier_state, membarrier_state);
243}
244
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500245static int membarrier_global_expedited(void)
246{
247 int cpu;
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500248 cpumask_var_t tmpmask;
249
250 if (num_online_cpus() == 1)
251 return 0;
252
253 /*
254 * Matches memory barriers around rq->curr modification in
255 * scheduler.
256 */
257 smp_mb(); /* system call entry is not a mb. */
258
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400259 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
260 return -ENOMEM;
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500261
262 cpus_read_lock();
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400263 rcu_read_lock();
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500264 for_each_online_cpu(cpu) {
265 struct task_struct *p;
266
267 /*
268 * Skipping the current CPU is OK even through we can be
269 * migrated at any point. The current CPU, at the point
270 * where we read raw_smp_processor_id(), is ensured to
271 * be in program order with respect to the caller
272 * thread. Therefore, we can skip this CPU from the
273 * iteration.
274 */
275 if (cpu == raw_smp_processor_id())
276 continue;
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100277
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400278 if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
279 MEMBARRIER_STATE_GLOBAL_EXPEDITED))
280 continue;
281
282 /*
Mathieu Desnoyers618758e2020-10-20 09:47:14 -0400283 * Skip the CPU if it runs a kernel thread which is not using
284 * a task mm.
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400285 */
Eric W. Biederman154abaf2019-09-14 07:34:30 -0500286 p = rcu_dereference(cpu_rq(cpu)->curr);
Mathieu Desnoyers618758e2020-10-20 09:47:14 -0400287 if (!p->mm)
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400288 continue;
289
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400290 __cpumask_set_cpu(cpu, tmpmask);
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500291 }
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400292 rcu_read_unlock();
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400293
294 preempt_disable();
295 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
296 preempt_enable();
297
298 free_cpumask_var(tmpmask);
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500299 cpus_read_unlock();
300
301 /*
302 * Memory barrier on the caller thread _after_ we finished
303 * waiting for the last IPI. Matches memory barriers around
304 * rq->curr modification in scheduler.
305 */
306 smp_mb(); /* exit from system call is not a mb */
307 return 0;
308}
309
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700310static int membarrier_private_expedited(int flags, int cpu_id)
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400311{
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400312 cpumask_var_t tmpmask;
Mathieu Desnoyersc6d68c12019-09-19 13:37:04 -0400313 struct mm_struct *mm = current->mm;
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700314 smp_call_func_t ipi_func = ipi_mb;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400315
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700316 if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500317 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
318 return -EINVAL;
Mathieu Desnoyersc6d68c12019-09-19 13:37:04 -0400319 if (!(atomic_read(&mm->membarrier_state) &
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500320 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
321 return -EPERM;
Andy Lutomirski758c9372020-12-03 21:07:05 -0800322 ipi_func = ipi_sync_core;
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700323 } else if (flags == MEMBARRIER_FLAG_RSEQ) {
324 if (!IS_ENABLED(CONFIG_RSEQ))
325 return -EINVAL;
326 if (!(atomic_read(&mm->membarrier_state) &
327 MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY))
328 return -EPERM;
329 ipi_func = ipi_rseq;
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500330 } else {
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700331 WARN_ON_ONCE(flags);
Mathieu Desnoyersc6d68c12019-09-19 13:37:04 -0400332 if (!(atomic_read(&mm->membarrier_state) &
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500333 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
334 return -EPERM;
335 }
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400336
Andy Lutomirskie45cdc72020-12-03 21:07:06 -0800337 if (flags != MEMBARRIER_FLAG_SYNC_CORE &&
338 (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1))
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400339 return 0;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400340
341 /*
342 * Matches memory barriers around rq->curr modification in
343 * scheduler.
344 */
345 smp_mb(); /* system call entry is not a mb. */
346
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700347 if (cpu_id < 0 && !zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400348 return -ENOMEM;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400349
350 cpus_read_lock();
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700351
352 if (cpu_id >= 0) {
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400353 struct task_struct *p;
354
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700355 if (cpu_id >= nr_cpu_ids || !cpu_online(cpu_id))
356 goto out;
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700357 rcu_read_lock();
358 p = rcu_dereference(cpu_rq(cpu_id)->curr);
359 if (!p || p->mm != mm) {
360 rcu_read_unlock();
361 goto out;
362 }
363 rcu_read_unlock();
364 } else {
365 int cpu;
366
367 rcu_read_lock();
368 for_each_online_cpu(cpu) {
369 struct task_struct *p;
370
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700371 p = rcu_dereference(cpu_rq(cpu)->curr);
372 if (p && p->mm == mm)
373 __cpumask_set_cpu(cpu, tmpmask);
374 }
375 rcu_read_unlock();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400376 }
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400377
Andy Lutomirskie45cdc72020-12-03 21:07:06 -0800378 if (cpu_id >= 0) {
379 /*
380 * smp_call_function_single() will call ipi_func() if cpu_id
381 * is the calling CPU.
382 */
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700383 smp_call_function_single(cpu_id, ipi_func, NULL, 1);
Andy Lutomirskie45cdc72020-12-03 21:07:06 -0800384 } else {
385 /*
386 * For regular membarrier, we can save a few cycles by
387 * skipping the current cpu -- we're about to do smp_mb()
388 * below, and if we migrate to a different cpu, this cpu
389 * and the new cpu will execute a full barrier in the
390 * scheduler.
391 *
392 * For SYNC_CORE, we do need a barrier on the current cpu --
393 * otherwise, if we are migrated and replaced by a different
394 * task in the same mm just before, during, or after
395 * membarrier, we will end up with some thread in the mm
396 * running without a core sync.
397 *
398 * For RSEQ, don't rseq_preempt() the caller. User code
399 * is not supposed to issue syscalls at all from inside an
400 * rseq critical section.
401 */
402 if (flags != MEMBARRIER_FLAG_SYNC_CORE) {
403 preempt_disable();
404 smp_call_function_many(tmpmask, ipi_func, NULL, true);
405 preempt_enable();
406 } else {
407 on_each_cpu_mask(tmpmask, ipi_func, NULL, true);
408 }
409 }
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400410
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700411out:
412 if (cpu_id < 0)
413 free_cpumask_var(tmpmask);
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400414 cpus_read_unlock();
415
416 /*
417 * Memory barrier on the caller thread _after_ we finished
418 * waiting for the last IPI. Matches memory barriers around
419 * rq->curr modification in scheduler.
420 */
421 smp_mb(); /* exit from system call is not a mb */
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100422
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400423 return 0;
424}
425
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400426static int sync_runqueues_membarrier_state(struct mm_struct *mm)
427{
428 int membarrier_state = atomic_read(&mm->membarrier_state);
429 cpumask_var_t tmpmask;
430 int cpu;
431
432 if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
433 this_cpu_write(runqueues.membarrier_state, membarrier_state);
434
435 /*
436 * For single mm user, we can simply issue a memory barrier
437 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
438 * mm and in the current runqueue to guarantee that no memory
439 * access following registration is reordered before
440 * registration.
441 */
442 smp_mb();
443 return 0;
444 }
445
446 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
447 return -ENOMEM;
448
449 /*
450 * For mm with multiple users, we need to ensure all future
451 * scheduler executions will observe @mm's new membarrier
452 * state.
453 */
454 synchronize_rcu();
455
456 /*
457 * For each cpu runqueue, if the task's mm match @mm, ensure that all
Randy Dunlapc034f482021-02-25 17:21:10 -0800458 * @mm's membarrier state set bits are also set in the runqueue's
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400459 * membarrier state. This ensures that a runqueue scheduling
460 * between threads which are users of @mm has its membarrier state
461 * updated.
462 */
463 cpus_read_lock();
464 rcu_read_lock();
465 for_each_online_cpu(cpu) {
466 struct rq *rq = cpu_rq(cpu);
467 struct task_struct *p;
468
Mathieu Desnoyersc172e0a2019-09-19 13:37:05 -0400469 p = rcu_dereference(rq->curr);
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400470 if (p && p->mm == mm)
471 __cpumask_set_cpu(cpu, tmpmask);
472 }
473 rcu_read_unlock();
474
Mathieu Desnoyersce29ddc2021-02-17 11:56:51 -0500475 on_each_cpu_mask(tmpmask, ipi_sync_rq_state, mm, true);
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400476
477 free_cpumask_var(tmpmask);
478 cpus_read_unlock();
479
480 return 0;
481}
482
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500483static int membarrier_register_global_expedited(void)
484{
485 struct task_struct *p = current;
486 struct mm_struct *mm = p->mm;
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400487 int ret;
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500488
489 if (atomic_read(&mm->membarrier_state) &
490 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
491 return 0;
492 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400493 ret = sync_runqueues_membarrier_state(mm);
494 if (ret)
495 return ret;
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500496 atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
497 &mm->membarrier_state);
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100498
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500499 return 0;
500}
501
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500502static int membarrier_register_private_expedited(int flags)
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400503{
504 struct task_struct *p = current;
505 struct mm_struct *mm = p->mm;
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400506 int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
507 set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
508 ret;
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500509
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700510 if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500511 if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
512 return -EINVAL;
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400513 ready_state =
514 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700515 } else if (flags == MEMBARRIER_FLAG_RSEQ) {
516 if (!IS_ENABLED(CONFIG_RSEQ))
517 return -EINVAL;
518 ready_state =
519 MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY;
520 } else {
521 WARN_ON_ONCE(flags);
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500522 }
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400523
524 /*
525 * We need to consider threads belonging to different thread
526 * groups, which use the same mm. (CLONE_VM but not
527 * CLONE_THREAD).
528 */
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400529 if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500530 return 0;
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500531 if (flags & MEMBARRIER_FLAG_SYNC_CORE)
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400532 set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700533 if (flags & MEMBARRIER_FLAG_RSEQ)
534 set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ;
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400535 atomic_or(set_state, &mm->membarrier_state);
536 ret = sync_runqueues_membarrier_state(mm);
537 if (ret)
538 return ret;
539 atomic_or(ready_state, &mm->membarrier_state);
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100540
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500541 return 0;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400542}
543
544/**
545 * sys_membarrier - issue memory barriers on a set of threads
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700546 * @cmd: Takes command values defined in enum membarrier_cmd.
547 * @flags: Currently needs to be 0 for all commands other than
548 * MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
549 * case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that @cpu_id
550 * contains the CPU on which to interrupt (= restart)
551 * the RSEQ critical section.
552 * @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on which
553 * RSEQ CS should be interrupted (@cmd must be
554 * MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400555 *
556 * If this system call is not implemented, -ENOSYS is returned. If the
557 * command specified does not exist, not available on the running
558 * kernel, or if the command argument is invalid, this system call
559 * returns -EINVAL. For a given command, with flags argument set to 0,
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -0400560 * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
561 * always return the same value until reboot. In addition, it can return
562 * -ENOMEM if there is not enough memory available to perform the system
563 * call.
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400564 *
565 * All memory accesses performed in program order from each targeted thread
566 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
567 * the semantic "barrier()" to represent a compiler barrier forcing memory
568 * accesses to be performed in program order across the barrier, and
569 * smp_mb() to represent explicit memory barriers forcing full memory
570 * ordering across the barrier, we have the following ordering table for
571 * each pair of barrier(), sys_membarrier() and smp_mb():
572 *
573 * The pair ordering is detailed as (O: ordered, X: not ordered):
574 *
575 * barrier() smp_mb() sys_membarrier()
576 * barrier() X X O
577 * smp_mb() X O O
578 * sys_membarrier() O O O
579 */
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700580SYSCALL_DEFINE3(membarrier, int, cmd, unsigned int, flags, int, cpu_id)
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400581{
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700582 switch (cmd) {
583 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
584 if (unlikely(flags && flags != MEMBARRIER_CMD_FLAG_CPU))
585 return -EINVAL;
586 break;
587 default:
588 if (unlikely(flags))
589 return -EINVAL;
590 }
591
592 if (!(flags & MEMBARRIER_CMD_FLAG_CPU))
593 cpu_id = -1;
594
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400595 switch (cmd) {
596 case MEMBARRIER_CMD_QUERY:
597 {
598 int cmd_mask = MEMBARRIER_CMD_BITMASK;
599
600 if (tick_nohz_full_enabled())
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500601 cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400602 return cmd_mask;
603 }
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500604 case MEMBARRIER_CMD_GLOBAL:
605 /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400606 if (tick_nohz_full_enabled())
607 return -EINVAL;
608 if (num_online_cpus() > 1)
Paul E. McKenney78d125d2018-07-11 15:36:43 -0700609 synchronize_rcu();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400610 return 0;
Mathieu Desnoyersc5f58bd2018-01-29 15:20:13 -0500611 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
612 return membarrier_global_expedited();
613 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
614 return membarrier_register_global_expedited();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400615 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700616 return membarrier_private_expedited(0, cpu_id);
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400617 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500618 return membarrier_register_private_expedited(0);
619 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700620 return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE, cpu_id);
Mathieu Desnoyers70216e12018-01-29 15:20:17 -0500621 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
622 return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
Peter Oskolkov2a36ab7172020-09-23 16:36:16 -0700623 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
624 return membarrier_private_expedited(MEMBARRIER_FLAG_RSEQ, cpu_id);
625 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
626 return membarrier_register_private_expedited(MEMBARRIER_FLAG_RSEQ);
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400627 default:
628 return -EINVAL;
629 }
630}