• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4  *
5  * membarrier system call
6  */
7 #include "sched.h"
8 
9 /*
10  * 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 /*
138  * Bitmask made from a "or" of all commands within enum membarrier_cmd,
139  * except MEMBARRIER_CMD_QUERY.
140  */
141 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
142 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK			\
143 	(MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE			\
144 	| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
145 #else
146 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK	0
147 #endif
148 
149 #ifdef CONFIG_RSEQ
150 #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK		\
151 	(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ			\
152 	| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ)
153 #else
154 #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK	0
155 #endif
156 
157 #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			\
162 	| MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK		\
163 	| MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK)
164 
165 static DEFINE_MUTEX(membarrier_ipi_mutex);
166 
ipi_mb(void * info)167 static void ipi_mb(void *info)
168 {
169 	smp_mb();	/* IPIs should be serializing but paranoid. */
170 }
171 
ipi_sync_core(void * info)172 static void ipi_sync_core(void *info)
173 {
174 	/*
175 	 * The smp_mb() in membarrier after all the IPIs is supposed to
176 	 * ensure that memory on remote CPUs that occur before the IPI
177 	 * become visible to membarrier()'s caller -- see scenario B in
178 	 * the big comment at the top of this file.
179 	 *
180 	 * A sync_core() would provide this guarantee, but
181 	 * sync_core_before_usermode() might end up being deferred until
182 	 * after membarrier()'s smp_mb().
183 	 */
184 	smp_mb();	/* IPIs should be serializing but paranoid. */
185 
186 	sync_core_before_usermode();
187 }
188 
ipi_rseq(void * info)189 static void ipi_rseq(void *info)
190 {
191 	/*
192 	 * Ensure that all stores done by the calling thread are visible
193 	 * to the current task before the current task resumes.  We could
194 	 * probably optimize this away on most architectures, but by the
195 	 * time we've already sent an IPI, the cost of the extra smp_mb()
196 	 * is negligible.
197 	 */
198 	smp_mb();
199 	rseq_preempt(current);
200 }
201 
ipi_sync_rq_state(void * info)202 static void ipi_sync_rq_state(void *info)
203 {
204 	struct mm_struct *mm = (struct mm_struct *) info;
205 
206 	if (current->mm != mm)
207 		return;
208 	this_cpu_write(runqueues.membarrier_state,
209 		       atomic_read(&mm->membarrier_state));
210 	/*
211 	 * Issue a memory barrier after setting
212 	 * MEMBARRIER_STATE_GLOBAL_EXPEDITED in the current runqueue to
213 	 * guarantee that no memory access following registration is reordered
214 	 * before registration.
215 	 */
216 	smp_mb();
217 }
218 
membarrier_exec_mmap(struct mm_struct * mm)219 void membarrier_exec_mmap(struct mm_struct *mm)
220 {
221 	/*
222 	 * Issue a memory barrier before clearing membarrier_state to
223 	 * guarantee that no memory access prior to exec is reordered after
224 	 * clearing this state.
225 	 */
226 	smp_mb();
227 	atomic_set(&mm->membarrier_state, 0);
228 	/*
229 	 * Keep the runqueue membarrier_state in sync with this mm
230 	 * membarrier_state.
231 	 */
232 	this_cpu_write(runqueues.membarrier_state, 0);
233 }
234 
membarrier_update_current_mm(struct mm_struct * next_mm)235 void membarrier_update_current_mm(struct mm_struct *next_mm)
236 {
237 	struct rq *rq = this_rq();
238 	int membarrier_state = 0;
239 
240 	if (next_mm)
241 		membarrier_state = atomic_read(&next_mm->membarrier_state);
242 	if (READ_ONCE(rq->membarrier_state) == membarrier_state)
243 		return;
244 	WRITE_ONCE(rq->membarrier_state, membarrier_state);
245 }
246 
membarrier_global_expedited(void)247 static int membarrier_global_expedited(void)
248 {
249 	int cpu;
250 	cpumask_var_t tmpmask;
251 
252 	if (num_online_cpus() == 1)
253 		return 0;
254 
255 	/*
256 	 * Matches memory barriers around rq->curr modification in
257 	 * scheduler.
258 	 */
259 	smp_mb();	/* system call entry is not a mb. */
260 
261 	if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
262 		return -ENOMEM;
263 
264 	mutex_lock(&membarrier_ipi_mutex);
265 	cpus_read_lock();
266 	rcu_read_lock();
267 	for_each_online_cpu(cpu) {
268 		struct task_struct *p;
269 
270 		/*
271 		 * Skipping the current CPU is OK even through we can be
272 		 * migrated at any point. The current CPU, at the point
273 		 * where we read raw_smp_processor_id(), is ensured to
274 		 * be in program order with respect to the caller
275 		 * thread. Therefore, we can skip this CPU from the
276 		 * iteration.
277 		 */
278 		if (cpu == raw_smp_processor_id())
279 			continue;
280 
281 		if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
282 		    MEMBARRIER_STATE_GLOBAL_EXPEDITED))
283 			continue;
284 
285 		/*
286 		 * Skip the CPU if it runs a kernel thread which is not using
287 		 * a task mm.
288 		 */
289 		p = rcu_dereference(cpu_rq(cpu)->curr);
290 		if (!p->mm)
291 			continue;
292 
293 		__cpumask_set_cpu(cpu, tmpmask);
294 	}
295 	rcu_read_unlock();
296 
297 	preempt_disable();
298 	smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
299 	preempt_enable();
300 
301 	free_cpumask_var(tmpmask);
302 	cpus_read_unlock();
303 
304 	/*
305 	 * Memory barrier on the caller thread _after_ we finished
306 	 * waiting for the last IPI. Matches memory barriers around
307 	 * rq->curr modification in scheduler.
308 	 */
309 	smp_mb();	/* exit from system call is not a mb */
310 	mutex_unlock(&membarrier_ipi_mutex);
311 
312 	return 0;
313 }
314 
membarrier_private_expedited(int flags,int cpu_id)315 static int membarrier_private_expedited(int flags, int cpu_id)
316 {
317 	cpumask_var_t tmpmask;
318 	struct mm_struct *mm = current->mm;
319 	smp_call_func_t ipi_func = ipi_mb;
320 
321 	if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
322 		if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
323 			return -EINVAL;
324 		if (!(atomic_read(&mm->membarrier_state) &
325 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
326 			return -EPERM;
327 		ipi_func = ipi_sync_core;
328 	} else if (flags == MEMBARRIER_FLAG_RSEQ) {
329 		if (!IS_ENABLED(CONFIG_RSEQ))
330 			return -EINVAL;
331 		if (!(atomic_read(&mm->membarrier_state) &
332 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY))
333 			return -EPERM;
334 		ipi_func = ipi_rseq;
335 	} else {
336 		WARN_ON_ONCE(flags);
337 		if (!(atomic_read(&mm->membarrier_state) &
338 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
339 			return -EPERM;
340 	}
341 
342 	if (flags != MEMBARRIER_FLAG_SYNC_CORE &&
343 	    (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1))
344 		return 0;
345 
346 	/*
347 	 * Matches memory barriers around rq->curr modification in
348 	 * scheduler.
349 	 */
350 	smp_mb();	/* system call entry is not a mb. */
351 
352 	if (cpu_id < 0 && !zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
353 		return -ENOMEM;
354 
355 	mutex_lock(&membarrier_ipi_mutex);
356 	cpus_read_lock();
357 
358 	if (cpu_id >= 0) {
359 		struct task_struct *p;
360 
361 		if (cpu_id >= nr_cpu_ids || !cpu_online(cpu_id))
362 			goto out;
363 		rcu_read_lock();
364 		p = rcu_dereference(cpu_rq(cpu_id)->curr);
365 		if (!p || p->mm != mm) {
366 			rcu_read_unlock();
367 			goto out;
368 		}
369 		rcu_read_unlock();
370 	} else {
371 		int cpu;
372 
373 		rcu_read_lock();
374 		for_each_online_cpu(cpu) {
375 			struct task_struct *p;
376 
377 			p = rcu_dereference(cpu_rq(cpu)->curr);
378 			if (p && p->mm == mm)
379 				__cpumask_set_cpu(cpu, tmpmask);
380 		}
381 		rcu_read_unlock();
382 	}
383 
384 	if (cpu_id >= 0) {
385 		/*
386 		 * smp_call_function_single() will call ipi_func() if cpu_id
387 		 * is the calling CPU.
388 		 */
389 		smp_call_function_single(cpu_id, ipi_func, NULL, 1);
390 	} else {
391 		/*
392 		 * For regular membarrier, we can save a few cycles by
393 		 * skipping the current cpu -- we're about to do smp_mb()
394 		 * below, and if we migrate to a different cpu, this cpu
395 		 * and the new cpu will execute a full barrier in the
396 		 * scheduler.
397 		 *
398 		 * For SYNC_CORE, we do need a barrier on the current cpu --
399 		 * otherwise, if we are migrated and replaced by a different
400 		 * task in the same mm just before, during, or after
401 		 * membarrier, we will end up with some thread in the mm
402 		 * running without a core sync.
403 		 *
404 		 * For RSEQ, don't rseq_preempt() the caller.  User code
405 		 * is not supposed to issue syscalls at all from inside an
406 		 * rseq critical section.
407 		 */
408 		if (flags != MEMBARRIER_FLAG_SYNC_CORE) {
409 			preempt_disable();
410 			smp_call_function_many(tmpmask, ipi_func, NULL, true);
411 			preempt_enable();
412 		} else {
413 			on_each_cpu_mask(tmpmask, ipi_func, NULL, true);
414 		}
415 	}
416 
417 out:
418 	if (cpu_id < 0)
419 		free_cpumask_var(tmpmask);
420 	cpus_read_unlock();
421 
422 	/*
423 	 * Memory barrier on the caller thread _after_ we finished
424 	 * waiting for the last IPI. Matches memory barriers around
425 	 * rq->curr modification in scheduler.
426 	 */
427 	smp_mb();	/* exit from system call is not a mb */
428 	mutex_unlock(&membarrier_ipi_mutex);
429 
430 	return 0;
431 }
432 
sync_runqueues_membarrier_state(struct mm_struct * mm)433 static int sync_runqueues_membarrier_state(struct mm_struct *mm)
434 {
435 	int membarrier_state = atomic_read(&mm->membarrier_state);
436 	cpumask_var_t tmpmask;
437 	int cpu;
438 
439 	if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
440 		this_cpu_write(runqueues.membarrier_state, membarrier_state);
441 
442 		/*
443 		 * For single mm user, we can simply issue a memory barrier
444 		 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
445 		 * mm and in the current runqueue to guarantee that no memory
446 		 * access following registration is reordered before
447 		 * registration.
448 		 */
449 		smp_mb();
450 		return 0;
451 	}
452 
453 	if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
454 		return -ENOMEM;
455 
456 	/*
457 	 * For mm with multiple users, we need to ensure all future
458 	 * scheduler executions will observe @mm's new membarrier
459 	 * state.
460 	 */
461 	synchronize_rcu();
462 
463 	/*
464 	 * For each cpu runqueue, if the task's mm match @mm, ensure that all
465 	 * @mm's membarrier state set bits are also set in the runqueue's
466 	 * membarrier state. This ensures that a runqueue scheduling
467 	 * between threads which are users of @mm has its membarrier state
468 	 * updated.
469 	 */
470 	mutex_lock(&membarrier_ipi_mutex);
471 	cpus_read_lock();
472 	rcu_read_lock();
473 	for_each_online_cpu(cpu) {
474 		struct rq *rq = cpu_rq(cpu);
475 		struct task_struct *p;
476 
477 		p = rcu_dereference(rq->curr);
478 		if (p && p->mm == mm)
479 			__cpumask_set_cpu(cpu, tmpmask);
480 	}
481 	rcu_read_unlock();
482 
483 	on_each_cpu_mask(tmpmask, ipi_sync_rq_state, mm, true);
484 
485 	free_cpumask_var(tmpmask);
486 	cpus_read_unlock();
487 	mutex_unlock(&membarrier_ipi_mutex);
488 
489 	return 0;
490 }
491 
membarrier_register_global_expedited(void)492 static int membarrier_register_global_expedited(void)
493 {
494 	struct task_struct *p = current;
495 	struct mm_struct *mm = p->mm;
496 	int ret;
497 
498 	if (atomic_read(&mm->membarrier_state) &
499 	    MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
500 		return 0;
501 	atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
502 	ret = sync_runqueues_membarrier_state(mm);
503 	if (ret)
504 		return ret;
505 	atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
506 		  &mm->membarrier_state);
507 
508 	return 0;
509 }
510 
membarrier_register_private_expedited(int flags)511 static int membarrier_register_private_expedited(int flags)
512 {
513 	struct task_struct *p = current;
514 	struct mm_struct *mm = p->mm;
515 	int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
516 	    set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
517 	    ret;
518 
519 	if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
520 		if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
521 			return -EINVAL;
522 		ready_state =
523 			MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
524 	} else if (flags == MEMBARRIER_FLAG_RSEQ) {
525 		if (!IS_ENABLED(CONFIG_RSEQ))
526 			return -EINVAL;
527 		ready_state =
528 			MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY;
529 	} else {
530 		WARN_ON_ONCE(flags);
531 	}
532 
533 	/*
534 	 * We need to consider threads belonging to different thread
535 	 * groups, which use the same mm. (CLONE_VM but not
536 	 * CLONE_THREAD).
537 	 */
538 	if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
539 		return 0;
540 	if (flags & MEMBARRIER_FLAG_SYNC_CORE)
541 		set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
542 	if (flags & MEMBARRIER_FLAG_RSEQ)
543 		set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ;
544 	atomic_or(set_state, &mm->membarrier_state);
545 	ret = sync_runqueues_membarrier_state(mm);
546 	if (ret)
547 		return ret;
548 	atomic_or(ready_state, &mm->membarrier_state);
549 
550 	return 0;
551 }
552 
553 /**
554  * sys_membarrier - issue memory barriers on a set of threads
555  * @cmd:    Takes command values defined in enum membarrier_cmd.
556  * @flags:  Currently needs to be 0 for all commands other than
557  *          MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
558  *          case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that @cpu_id
559  *          contains the CPU on which to interrupt (= restart)
560  *          the RSEQ critical section.
561  * @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on which
562  *          RSEQ CS should be interrupted (@cmd must be
563  *          MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
564  *
565  * If this system call is not implemented, -ENOSYS is returned. If the
566  * command specified does not exist, not available on the running
567  * kernel, or if the command argument is invalid, this system call
568  * returns -EINVAL. For a given command, with flags argument set to 0,
569  * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
570  * always return the same value until reboot. In addition, it can return
571  * -ENOMEM if there is not enough memory available to perform the system
572  * call.
573  *
574  * All memory accesses performed in program order from each targeted thread
575  * is guaranteed to be ordered with respect to sys_membarrier(). If we use
576  * the semantic "barrier()" to represent a compiler barrier forcing memory
577  * accesses to be performed in program order across the barrier, and
578  * smp_mb() to represent explicit memory barriers forcing full memory
579  * ordering across the barrier, we have the following ordering table for
580  * each pair of barrier(), sys_membarrier() and smp_mb():
581  *
582  * The pair ordering is detailed as (O: ordered, X: not ordered):
583  *
584  *                        barrier()   smp_mb() sys_membarrier()
585  *        barrier()          X           X            O
586  *        smp_mb()           X           O            O
587  *        sys_membarrier()   O           O            O
588  */
SYSCALL_DEFINE3(membarrier,int,cmd,unsigned int,flags,int,cpu_id)589 SYSCALL_DEFINE3(membarrier, int, cmd, unsigned int, flags, int, cpu_id)
590 {
591 	switch (cmd) {
592 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
593 		if (unlikely(flags && flags != MEMBARRIER_CMD_FLAG_CPU))
594 			return -EINVAL;
595 		break;
596 	default:
597 		if (unlikely(flags))
598 			return -EINVAL;
599 	}
600 
601 	if (!(flags & MEMBARRIER_CMD_FLAG_CPU))
602 		cpu_id = -1;
603 
604 	switch (cmd) {
605 	case MEMBARRIER_CMD_QUERY:
606 	{
607 		int cmd_mask = MEMBARRIER_CMD_BITMASK;
608 
609 		if (tick_nohz_full_enabled())
610 			cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
611 		return cmd_mask;
612 	}
613 	case MEMBARRIER_CMD_GLOBAL:
614 		/* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
615 		if (tick_nohz_full_enabled())
616 			return -EINVAL;
617 		if (num_online_cpus() > 1)
618 			synchronize_rcu();
619 		return 0;
620 	case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
621 		return membarrier_global_expedited();
622 	case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
623 		return membarrier_register_global_expedited();
624 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
625 		return membarrier_private_expedited(0, cpu_id);
626 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
627 		return membarrier_register_private_expedited(0);
628 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
629 		return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE, cpu_id);
630 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
631 		return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
632 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
633 		return membarrier_private_expedited(MEMBARRIER_FLAG_RSEQ, cpu_id);
634 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
635 		return membarrier_register_private_expedited(MEMBARRIER_FLAG_RSEQ);
636 	default:
637 		return -EINVAL;
638 	}
639 }
640