• 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 
8 /*
9  * For documentation purposes, here are some membarrier ordering
10  * scenarios to keep in mind:
11  *
12  * A) Userspace thread execution after IPI vs membarrier's memory
13  *    barrier before sending the IPI
14  *
15  * Userspace variables:
16  *
17  * int x = 0, y = 0;
18  *
19  * The memory barrier at the start of membarrier() on CPU0 is necessary in
20  * order to enforce the guarantee that any writes occurring on CPU0 before
21  * the membarrier() is executed will be visible to any code executing on
22  * CPU1 after the IPI-induced memory barrier:
23  *
24  *         CPU0                              CPU1
25  *
26  *         x = 1
27  *         membarrier():
28  *           a: smp_mb()
29  *           b: send IPI                       IPI-induced mb
30  *           c: smp_mb()
31  *         r2 = y
32  *                                           y = 1
33  *                                           barrier()
34  *                                           r1 = x
35  *
36  *                     BUG_ON(r1 == 0 && r2 == 0)
37  *
38  * The write to y and load from x by CPU1 are unordered by the hardware,
39  * so it's possible to have "r1 = x" reordered before "y = 1" at any
40  * point after (b).  If the memory barrier at (a) is omitted, then "x = 1"
41  * can be reordered after (a) (although not after (c)), so we get r1 == 0
42  * and r2 == 0.  This violates the guarantee that membarrier() is
43  * supposed by provide.
44  *
45  * The timing of the memory barrier at (a) has to ensure that it executes
46  * before the IPI-induced memory barrier on CPU1.
47  *
48  * B) Userspace thread execution before IPI vs membarrier's memory
49  *    barrier after completing the IPI
50  *
51  * Userspace variables:
52  *
53  * int x = 0, y = 0;
54  *
55  * The memory barrier at the end of membarrier() on CPU0 is necessary in
56  * order to enforce the guarantee that any writes occurring on CPU1 before
57  * the membarrier() is executed will be visible to any code executing on
58  * CPU0 after the membarrier():
59  *
60  *         CPU0                              CPU1
61  *
62  *                                           x = 1
63  *                                           barrier()
64  *                                           y = 1
65  *         r2 = y
66  *         membarrier():
67  *           a: smp_mb()
68  *           b: send IPI                       IPI-induced mb
69  *           c: smp_mb()
70  *         r1 = x
71  *         BUG_ON(r1 == 0 && r2 == 1)
72  *
73  * The writes to x and y are unordered by the hardware, so it's possible to
74  * have "r2 = 1" even though the write to x doesn't execute until (b).  If
75  * the memory barrier at (c) is omitted then "r1 = x" can be reordered
76  * before (b) (although not before (a)), so we get "r1 = 0".  This violates
77  * the guarantee that membarrier() is supposed to provide.
78  *
79  * The timing of the memory barrier at (c) has to ensure that it executes
80  * after the IPI-induced memory barrier on CPU1.
81  *
82  * C) Scheduling userspace thread -> kthread -> userspace thread vs membarrier
83  *
84  *           CPU0                            CPU1
85  *
86  *           membarrier():
87  *           a: smp_mb()
88  *                                           d: switch to kthread (includes mb)
89  *           b: read rq->curr->mm == NULL
90  *                                           e: switch to user (includes mb)
91  *           c: smp_mb()
92  *
93  * Using the scenario from (A), we can show that (a) needs to be paired
94  * with (e). Using the scenario from (B), we can show that (c) needs to
95  * be paired with (d).
96  *
97  * D) exit_mm vs membarrier
98  *
99  * Two thread groups are created, A and B.  Thread group B is created by
100  * issuing clone from group A with flag CLONE_VM set, but not CLONE_THREAD.
101  * Let's assume we have a single thread within each thread group (Thread A
102  * and Thread B).  Thread A runs on CPU0, Thread B runs on CPU1.
103  *
104  *           CPU0                            CPU1
105  *
106  *           membarrier():
107  *             a: smp_mb()
108  *                                           exit_mm():
109  *                                             d: smp_mb()
110  *                                             e: current->mm = NULL
111  *             b: read rq->curr->mm == NULL
112  *             c: smp_mb()
113  *
114  * Using scenario (B), we can show that (c) needs to be paired with (d).
115  *
116  * E) kthread_{use,unuse}_mm vs membarrier
117  *
118  *           CPU0                            CPU1
119  *
120  *           membarrier():
121  *           a: smp_mb()
122  *                                           kthread_unuse_mm()
123  *                                             d: smp_mb()
124  *                                             e: current->mm = NULL
125  *           b: read rq->curr->mm == NULL
126  *                                           kthread_use_mm()
127  *                                             f: current->mm = mm
128  *                                             g: smp_mb()
129  *           c: smp_mb()
130  *
131  * Using the scenario from (A), we can show that (a) needs to be paired
132  * with (g). Using the scenario from (B), we can show that (c) needs to
133  * be paired with (d).
134  */
135 
136 /*
137  * Bitmask made from a "or" of all commands within enum membarrier_cmd,
138  * except MEMBARRIER_CMD_QUERY.
139  */
140 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
141 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK			\
142 	(MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE			\
143 	| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
144 #else
145 #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK	0
146 #endif
147 
148 #ifdef CONFIG_RSEQ
149 #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK		\
150 	(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ			\
151 	| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ)
152 #else
153 #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK	0
154 #endif
155 
156 #define MEMBARRIER_CMD_BITMASK						\
157 	(MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED	\
158 	| MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED			\
159 	| MEMBARRIER_CMD_PRIVATE_EXPEDITED				\
160 	| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED			\
161 	| MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK		\
162 	| MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK)
163 
164 static DEFINE_MUTEX(membarrier_ipi_mutex);
165 #define SERIALIZE_IPI() guard(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 	SERIALIZE_IPI();
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 	return 0;
311 }
312 
membarrier_private_expedited(int flags,int cpu_id)313 static int membarrier_private_expedited(int flags, int cpu_id)
314 {
315 	cpumask_var_t tmpmask;
316 	struct mm_struct *mm = current->mm;
317 	smp_call_func_t ipi_func = ipi_mb;
318 
319 	if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
320 		if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
321 			return -EINVAL;
322 		if (!(atomic_read(&mm->membarrier_state) &
323 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
324 			return -EPERM;
325 		ipi_func = ipi_sync_core;
326 	} else if (flags == MEMBARRIER_FLAG_RSEQ) {
327 		if (!IS_ENABLED(CONFIG_RSEQ))
328 			return -EINVAL;
329 		if (!(atomic_read(&mm->membarrier_state) &
330 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY))
331 			return -EPERM;
332 		ipi_func = ipi_rseq;
333 	} else {
334 		WARN_ON_ONCE(flags);
335 		if (!(atomic_read(&mm->membarrier_state) &
336 		      MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
337 			return -EPERM;
338 	}
339 
340 	if (flags != MEMBARRIER_FLAG_SYNC_CORE &&
341 	    (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1))
342 		return 0;
343 
344 	/*
345 	 * Matches memory barriers around rq->curr modification in
346 	 * scheduler.
347 	 */
348 	smp_mb();	/* system call entry is not a mb. */
349 
350 	if (cpu_id < 0 && !zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
351 		return -ENOMEM;
352 
353 	SERIALIZE_IPI();
354 	cpus_read_lock();
355 
356 	if (cpu_id >= 0) {
357 		struct task_struct *p;
358 
359 		if (cpu_id >= nr_cpu_ids || !cpu_online(cpu_id))
360 			goto out;
361 		rcu_read_lock();
362 		p = rcu_dereference(cpu_rq(cpu_id)->curr);
363 		if (!p || p->mm != mm) {
364 			rcu_read_unlock();
365 			goto out;
366 		}
367 		rcu_read_unlock();
368 	} else {
369 		int cpu;
370 
371 		rcu_read_lock();
372 		for_each_online_cpu(cpu) {
373 			struct task_struct *p;
374 
375 			p = rcu_dereference(cpu_rq(cpu)->curr);
376 			if (p && p->mm == mm)
377 				__cpumask_set_cpu(cpu, tmpmask);
378 		}
379 		rcu_read_unlock();
380 	}
381 
382 	if (cpu_id >= 0) {
383 		/*
384 		 * smp_call_function_single() will call ipi_func() if cpu_id
385 		 * is the calling CPU.
386 		 */
387 		smp_call_function_single(cpu_id, ipi_func, NULL, 1);
388 	} else {
389 		/*
390 		 * For regular membarrier, we can save a few cycles by
391 		 * skipping the current cpu -- we're about to do smp_mb()
392 		 * below, and if we migrate to a different cpu, this cpu
393 		 * and the new cpu will execute a full barrier in the
394 		 * scheduler.
395 		 *
396 		 * For SYNC_CORE, we do need a barrier on the current cpu --
397 		 * otherwise, if we are migrated and replaced by a different
398 		 * task in the same mm just before, during, or after
399 		 * membarrier, we will end up with some thread in the mm
400 		 * running without a core sync.
401 		 *
402 		 * For RSEQ, don't rseq_preempt() the caller.  User code
403 		 * is not supposed to issue syscalls at all from inside an
404 		 * rseq critical section.
405 		 */
406 		if (flags != MEMBARRIER_FLAG_SYNC_CORE) {
407 			preempt_disable();
408 			smp_call_function_many(tmpmask, ipi_func, NULL, true);
409 			preempt_enable();
410 		} else {
411 			on_each_cpu_mask(tmpmask, ipi_func, NULL, true);
412 		}
413 	}
414 
415 out:
416 	if (cpu_id < 0)
417 		free_cpumask_var(tmpmask);
418 	cpus_read_unlock();
419 
420 	/*
421 	 * Memory barrier on the caller thread _after_ we finished
422 	 * waiting for the last IPI. Matches memory barriers around
423 	 * rq->curr modification in scheduler.
424 	 */
425 	smp_mb();	/* exit from system call is not a mb */
426 
427 	return 0;
428 }
429 
sync_runqueues_membarrier_state(struct mm_struct * mm)430 static int sync_runqueues_membarrier_state(struct mm_struct *mm)
431 {
432 	int membarrier_state = atomic_read(&mm->membarrier_state);
433 	cpumask_var_t tmpmask;
434 	int cpu;
435 
436 	if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
437 		this_cpu_write(runqueues.membarrier_state, membarrier_state);
438 
439 		/*
440 		 * For single mm user, we can simply issue a memory barrier
441 		 * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
442 		 * mm and in the current runqueue to guarantee that no memory
443 		 * access following registration is reordered before
444 		 * registration.
445 		 */
446 		smp_mb();
447 		return 0;
448 	}
449 
450 	if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
451 		return -ENOMEM;
452 
453 	/*
454 	 * For mm with multiple users, we need to ensure all future
455 	 * scheduler executions will observe @mm's new membarrier
456 	 * state.
457 	 */
458 	synchronize_rcu();
459 
460 	/*
461 	 * For each cpu runqueue, if the task's mm match @mm, ensure that all
462 	 * @mm's membarrier state set bits are also set in the runqueue's
463 	 * membarrier state. This ensures that a runqueue scheduling
464 	 * between threads which are users of @mm has its membarrier state
465 	 * updated.
466 	 */
467 	SERIALIZE_IPI();
468 	cpus_read_lock();
469 	rcu_read_lock();
470 	for_each_online_cpu(cpu) {
471 		struct rq *rq = cpu_rq(cpu);
472 		struct task_struct *p;
473 
474 		p = rcu_dereference(rq->curr);
475 		if (p && p->mm == mm)
476 			__cpumask_set_cpu(cpu, tmpmask);
477 	}
478 	rcu_read_unlock();
479 
480 	on_each_cpu_mask(tmpmask, ipi_sync_rq_state, mm, true);
481 
482 	free_cpumask_var(tmpmask);
483 	cpus_read_unlock();
484 
485 	return 0;
486 }
487 
membarrier_register_global_expedited(void)488 static int membarrier_register_global_expedited(void)
489 {
490 	struct task_struct *p = current;
491 	struct mm_struct *mm = p->mm;
492 	int ret;
493 
494 	if (atomic_read(&mm->membarrier_state) &
495 	    MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
496 		return 0;
497 	atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
498 	ret = sync_runqueues_membarrier_state(mm);
499 	if (ret)
500 		return ret;
501 	atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
502 		  &mm->membarrier_state);
503 
504 	return 0;
505 }
506 
membarrier_register_private_expedited(int flags)507 static int membarrier_register_private_expedited(int flags)
508 {
509 	struct task_struct *p = current;
510 	struct mm_struct *mm = p->mm;
511 	int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
512 	    set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
513 	    ret;
514 
515 	if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
516 		if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
517 			return -EINVAL;
518 		ready_state =
519 			MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
520 	} else if (flags == MEMBARRIER_FLAG_RSEQ) {
521 		if (!IS_ENABLED(CONFIG_RSEQ))
522 			return -EINVAL;
523 		ready_state =
524 			MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY;
525 	} else {
526 		WARN_ON_ONCE(flags);
527 	}
528 
529 	/*
530 	 * We need to consider threads belonging to different thread
531 	 * groups, which use the same mm. (CLONE_VM but not
532 	 * CLONE_THREAD).
533 	 */
534 	if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
535 		return 0;
536 	if (flags & MEMBARRIER_FLAG_SYNC_CORE)
537 		set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
538 	if (flags & MEMBARRIER_FLAG_RSEQ)
539 		set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ;
540 	atomic_or(set_state, &mm->membarrier_state);
541 	ret = sync_runqueues_membarrier_state(mm);
542 	if (ret)
543 		return ret;
544 	atomic_or(ready_state, &mm->membarrier_state);
545 
546 	return 0;
547 }
548 
549 /**
550  * sys_membarrier - issue memory barriers on a set of threads
551  * @cmd:    Takes command values defined in enum membarrier_cmd.
552  * @flags:  Currently needs to be 0 for all commands other than
553  *          MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
554  *          case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that @cpu_id
555  *          contains the CPU on which to interrupt (= restart)
556  *          the RSEQ critical section.
557  * @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on which
558  *          RSEQ CS should be interrupted (@cmd must be
559  *          MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
560  *
561  * If this system call is not implemented, -ENOSYS is returned. If the
562  * command specified does not exist, not available on the running
563  * kernel, or if the command argument is invalid, this system call
564  * returns -EINVAL. For a given command, with flags argument set to 0,
565  * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
566  * always return the same value until reboot. In addition, it can return
567  * -ENOMEM if there is not enough memory available to perform the system
568  * call.
569  *
570  * All memory accesses performed in program order from each targeted thread
571  * is guaranteed to be ordered with respect to sys_membarrier(). If we use
572  * the semantic "barrier()" to represent a compiler barrier forcing memory
573  * accesses to be performed in program order across the barrier, and
574  * smp_mb() to represent explicit memory barriers forcing full memory
575  * ordering across the barrier, we have the following ordering table for
576  * each pair of barrier(), sys_membarrier() and smp_mb():
577  *
578  * The pair ordering is detailed as (O: ordered, X: not ordered):
579  *
580  *                        barrier()   smp_mb() sys_membarrier()
581  *        barrier()          X           X            O
582  *        smp_mb()           X           O            O
583  *        sys_membarrier()   O           O            O
584  */
SYSCALL_DEFINE3(membarrier,int,cmd,unsigned int,flags,int,cpu_id)585 SYSCALL_DEFINE3(membarrier, int, cmd, unsigned int, flags, int, cpu_id)
586 {
587 	switch (cmd) {
588 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
589 		if (unlikely(flags && flags != MEMBARRIER_CMD_FLAG_CPU))
590 			return -EINVAL;
591 		break;
592 	default:
593 		if (unlikely(flags))
594 			return -EINVAL;
595 	}
596 
597 	if (!(flags & MEMBARRIER_CMD_FLAG_CPU))
598 		cpu_id = -1;
599 
600 	switch (cmd) {
601 	case MEMBARRIER_CMD_QUERY:
602 	{
603 		int cmd_mask = MEMBARRIER_CMD_BITMASK;
604 
605 		if (tick_nohz_full_enabled())
606 			cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
607 		return cmd_mask;
608 	}
609 	case MEMBARRIER_CMD_GLOBAL:
610 		/* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
611 		if (tick_nohz_full_enabled())
612 			return -EINVAL;
613 		if (num_online_cpus() > 1)
614 			synchronize_rcu();
615 		return 0;
616 	case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
617 		return membarrier_global_expedited();
618 	case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
619 		return membarrier_register_global_expedited();
620 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
621 		return membarrier_private_expedited(0, cpu_id);
622 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
623 		return membarrier_register_private_expedited(0);
624 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
625 		return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE, cpu_id);
626 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
627 		return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
628 	case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
629 		return membarrier_private_expedited(MEMBARRIER_FLAG_RSEQ, cpu_id);
630 	case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
631 		return membarrier_register_private_expedited(MEMBARRIER_FLAG_RSEQ);
632 	default:
633 		return -EINVAL;
634 	}
635 }
636