membarrier.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. * For documentation purposes, here are some membarrier ordering
  9. * scenarios to keep in mind:
  10. *
  11. * A) Userspace thread execution after IPI vs membarrier's memory
  12. * barrier before sending the IPI
  13. *
  14. * Userspace variables:
  15. *
  16. * int x = 0, y = 0;
  17. *
  18. * The memory barrier at the start of membarrier() on CPU0 is necessary in
  19. * order to enforce the guarantee that any writes occurring on CPU0 before
  20. * the membarrier() is executed will be visible to any code executing on
  21. * CPU1 after the IPI-induced memory barrier:
  22. *
  23. * CPU0 CPU1
  24. *
  25. * x = 1
  26. * membarrier():
  27. * a: smp_mb()
  28. * b: send IPI IPI-induced mb
  29. * c: smp_mb()
  30. * r2 = y
  31. * y = 1
  32. * barrier()
  33. * r1 = x
  34. *
  35. * BUG_ON(r1 == 0 && r2 == 0)
  36. *
  37. * The write to y and load from x by CPU1 are unordered by the hardware,
  38. * so it's possible to have "r1 = x" reordered before "y = 1" at any
  39. * point after (b). If the memory barrier at (a) is omitted, then "x = 1"
  40. * can be reordered after (a) (although not after (c)), so we get r1 == 0
  41. * and r2 == 0. This violates the guarantee that membarrier() is
  42. * supposed by provide.
  43. *
  44. * The timing of the memory barrier at (a) has to ensure that it executes
  45. * before the IPI-induced memory barrier on CPU1.
  46. *
  47. * B) Userspace thread execution before IPI vs membarrier's memory
  48. * barrier after completing the IPI
  49. *
  50. * Userspace variables:
  51. *
  52. * int x = 0, y = 0;
  53. *
  54. * The memory barrier at the end of membarrier() on CPU0 is necessary in
  55. * order to enforce the guarantee that any writes occurring on CPU1 before
  56. * the membarrier() is executed will be visible to any code executing on
  57. * CPU0 after the membarrier():
  58. *
  59. * CPU0 CPU1
  60. *
  61. * x = 1
  62. * barrier()
  63. * y = 1
  64. * r2 = y
  65. * membarrier():
  66. * a: smp_mb()
  67. * b: send IPI IPI-induced mb
  68. * c: smp_mb()
  69. * r1 = x
  70. * BUG_ON(r1 == 0 && r2 == 1)
  71. *
  72. * The writes to x and y are unordered by the hardware, so it's possible to
  73. * have "r2 = 1" even though the write to x doesn't execute until (b). If
  74. * the memory barrier at (c) is omitted then "r1 = x" can be reordered
  75. * before (b) (although not before (a)), so we get "r1 = 0". This violates
  76. * the guarantee that membarrier() is supposed to provide.
  77. *
  78. * The timing of the memory barrier at (c) has to ensure that it executes
  79. * after the IPI-induced memory barrier on CPU1.
  80. *
  81. * C) Scheduling userspace thread -> kthread -> userspace thread vs membarrier
  82. *
  83. * CPU0 CPU1
  84. *
  85. * membarrier():
  86. * a: smp_mb()
  87. * d: switch to kthread (includes mb)
  88. * b: read rq->curr->mm == NULL
  89. * e: switch to user (includes mb)
  90. * c: smp_mb()
  91. *
  92. * Using the scenario from (A), we can show that (a) needs to be paired
  93. * with (e). Using the scenario from (B), we can show that (c) needs to
  94. * be paired with (d).
  95. *
  96. * D) exit_mm vs membarrier
  97. *
  98. * Two thread groups are created, A and B. Thread group B is created by
  99. * issuing clone from group A with flag CLONE_VM set, but not CLONE_THREAD.
  100. * Let's assume we have a single thread within each thread group (Thread A
  101. * and Thread B). Thread A runs on CPU0, Thread B runs on CPU1.
  102. *
  103. * CPU0 CPU1
  104. *
  105. * membarrier():
  106. * a: smp_mb()
  107. * exit_mm():
  108. * d: smp_mb()
  109. * e: current->mm = NULL
  110. * b: read rq->curr->mm == NULL
  111. * c: smp_mb()
  112. *
  113. * Using scenario (B), we can show that (c) needs to be paired with (d).
  114. *
  115. * E) kthread_{use,unuse}_mm vs membarrier
  116. *
  117. * CPU0 CPU1
  118. *
  119. * membarrier():
  120. * a: smp_mb()
  121. * kthread_unuse_mm()
  122. * d: smp_mb()
  123. * e: current->mm = NULL
  124. * b: read rq->curr->mm == NULL
  125. * kthread_use_mm()
  126. * f: current->mm = mm
  127. * g: smp_mb()
  128. * c: smp_mb()
  129. *
  130. * Using the scenario from (A), we can show that (a) needs to be paired
  131. * with (g). Using the scenario from (B), we can show that (c) needs to
  132. * be paired with (d).
  133. */
  134. /*
  135. * Bitmask made from a "or" of all commands within enum membarrier_cmd,
  136. * except MEMBARRIER_CMD_QUERY.
  137. */
  138. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE
  139. #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
  140. (MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE \
  141. | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
  142. #else
  143. #define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK 0
  144. #endif
  145. #ifdef CONFIG_RSEQ
  146. #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK \
  147. (MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ \
  148. | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ)
  149. #else
  150. #define MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK 0
  151. #endif
  152. #define MEMBARRIER_CMD_BITMASK \
  153. (MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED \
  154. | MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED \
  155. | MEMBARRIER_CMD_PRIVATE_EXPEDITED \
  156. | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED \
  157. | MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK \
  158. | MEMBARRIER_PRIVATE_EXPEDITED_RSEQ_BITMASK \
  159. | MEMBARRIER_CMD_GET_REGISTRATIONS)
  160. static DEFINE_MUTEX(membarrier_ipi_mutex);
  161. #define SERIALIZE_IPI() guard(mutex)(&membarrier_ipi_mutex)
  162. static void ipi_mb(void *info)
  163. {
  164. smp_mb(); /* IPIs should be serializing but paranoid. */
  165. }
  166. static void ipi_sync_core(void *info)
  167. {
  168. /*
  169. * The smp_mb() in membarrier after all the IPIs is supposed to
  170. * ensure that memory on remote CPUs that occur before the IPI
  171. * become visible to membarrier()'s caller -- see scenario B in
  172. * the big comment at the top of this file.
  173. *
  174. * A sync_core() would provide this guarantee, but
  175. * sync_core_before_usermode() might end up being deferred until
  176. * after membarrier()'s smp_mb().
  177. */
  178. smp_mb(); /* IPIs should be serializing but paranoid. */
  179. sync_core_before_usermode();
  180. }
  181. static void ipi_rseq(void *info)
  182. {
  183. /*
  184. * Ensure that all stores done by the calling thread are visible
  185. * to the current task before the current task resumes. We could
  186. * probably optimize this away on most architectures, but by the
  187. * time we've already sent an IPI, the cost of the extra smp_mb()
  188. * is negligible.
  189. */
  190. smp_mb();
  191. rseq_preempt(current);
  192. }
  193. static void ipi_sync_rq_state(void *info)
  194. {
  195. struct mm_struct *mm = (struct mm_struct *) info;
  196. if (current->mm != mm)
  197. return;
  198. this_cpu_write(runqueues.membarrier_state,
  199. atomic_read(&mm->membarrier_state));
  200. /*
  201. * Issue a memory barrier after setting
  202. * MEMBARRIER_STATE_GLOBAL_EXPEDITED in the current runqueue to
  203. * guarantee that no memory access following registration is reordered
  204. * before registration.
  205. */
  206. smp_mb();
  207. }
  208. void membarrier_exec_mmap(struct mm_struct *mm)
  209. {
  210. /*
  211. * Issue a memory barrier before clearing membarrier_state to
  212. * guarantee that no memory access prior to exec is reordered after
  213. * clearing this state.
  214. */
  215. smp_mb();
  216. atomic_set(&mm->membarrier_state, 0);
  217. /*
  218. * Keep the runqueue membarrier_state in sync with this mm
  219. * membarrier_state.
  220. */
  221. this_cpu_write(runqueues.membarrier_state, 0);
  222. }
  223. void membarrier_update_current_mm(struct mm_struct *next_mm)
  224. {
  225. struct rq *rq = this_rq();
  226. int membarrier_state = 0;
  227. if (next_mm)
  228. membarrier_state = atomic_read(&next_mm->membarrier_state);
  229. if (READ_ONCE(rq->membarrier_state) == membarrier_state)
  230. return;
  231. WRITE_ONCE(rq->membarrier_state, membarrier_state);
  232. }
  233. static int membarrier_global_expedited(void)
  234. {
  235. int cpu;
  236. cpumask_var_t tmpmask;
  237. if (num_online_cpus() == 1)
  238. return 0;
  239. /*
  240. * Matches memory barriers after rq->curr modification in
  241. * scheduler.
  242. */
  243. smp_mb(); /* system call entry is not a mb. */
  244. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  245. return -ENOMEM;
  246. SERIALIZE_IPI();
  247. cpus_read_lock();
  248. rcu_read_lock();
  249. for_each_online_cpu(cpu) {
  250. struct task_struct *p;
  251. /*
  252. * Skipping the current CPU is OK even through we can be
  253. * migrated at any point. The current CPU, at the point
  254. * where we read raw_smp_processor_id(), is ensured to
  255. * be in program order with respect to the caller
  256. * thread. Therefore, we can skip this CPU from the
  257. * iteration.
  258. */
  259. if (cpu == raw_smp_processor_id())
  260. continue;
  261. if (!(READ_ONCE(cpu_rq(cpu)->membarrier_state) &
  262. MEMBARRIER_STATE_GLOBAL_EXPEDITED))
  263. continue;
  264. /*
  265. * Skip the CPU if it runs a kernel thread which is not using
  266. * a task mm.
  267. */
  268. p = rcu_dereference(cpu_rq(cpu)->curr);
  269. if (!p->mm)
  270. continue;
  271. __cpumask_set_cpu(cpu, tmpmask);
  272. }
  273. rcu_read_unlock();
  274. preempt_disable();
  275. smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
  276. preempt_enable();
  277. free_cpumask_var(tmpmask);
  278. cpus_read_unlock();
  279. /*
  280. * Memory barrier on the caller thread _after_ we finished
  281. * waiting for the last IPI. Matches memory barriers before
  282. * rq->curr modification in scheduler.
  283. */
  284. smp_mb(); /* exit from system call is not a mb */
  285. return 0;
  286. }
  287. static int membarrier_private_expedited(int flags, int cpu_id)
  288. {
  289. cpumask_var_t tmpmask;
  290. struct mm_struct *mm = current->mm;
  291. smp_call_func_t ipi_func = ipi_mb;
  292. if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
  293. if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
  294. return -EINVAL;
  295. if (!(atomic_read(&mm->membarrier_state) &
  296. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
  297. return -EPERM;
  298. ipi_func = ipi_sync_core;
  299. prepare_sync_core_cmd(mm);
  300. } else if (flags == MEMBARRIER_FLAG_RSEQ) {
  301. if (!IS_ENABLED(CONFIG_RSEQ))
  302. return -EINVAL;
  303. if (!(atomic_read(&mm->membarrier_state) &
  304. MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY))
  305. return -EPERM;
  306. ipi_func = ipi_rseq;
  307. } else {
  308. WARN_ON_ONCE(flags);
  309. if (!(atomic_read(&mm->membarrier_state) &
  310. MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
  311. return -EPERM;
  312. }
  313. if (flags != MEMBARRIER_FLAG_SYNC_CORE &&
  314. (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1))
  315. return 0;
  316. /*
  317. * Matches memory barriers after rq->curr modification in
  318. * scheduler.
  319. *
  320. * On RISC-V, this barrier pairing is also needed for the
  321. * SYNC_CORE command when switching between processes, cf.
  322. * the inline comments in membarrier_arch_switch_mm().
  323. */
  324. smp_mb(); /* system call entry is not a mb. */
  325. if (cpu_id < 0 && !zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  326. return -ENOMEM;
  327. SERIALIZE_IPI();
  328. cpus_read_lock();
  329. if (cpu_id >= 0) {
  330. struct task_struct *p;
  331. if (cpu_id >= nr_cpu_ids || !cpu_online(cpu_id))
  332. goto out;
  333. rcu_read_lock();
  334. p = rcu_dereference(cpu_rq(cpu_id)->curr);
  335. if (!p || p->mm != mm) {
  336. rcu_read_unlock();
  337. goto out;
  338. }
  339. rcu_read_unlock();
  340. } else {
  341. int cpu;
  342. rcu_read_lock();
  343. for_each_online_cpu(cpu) {
  344. struct task_struct *p;
  345. p = rcu_dereference(cpu_rq(cpu)->curr);
  346. if (p && p->mm == mm)
  347. __cpumask_set_cpu(cpu, tmpmask);
  348. }
  349. rcu_read_unlock();
  350. }
  351. if (cpu_id >= 0) {
  352. /*
  353. * smp_call_function_single() will call ipi_func() if cpu_id
  354. * is the calling CPU.
  355. */
  356. smp_call_function_single(cpu_id, ipi_func, NULL, 1);
  357. } else {
  358. /*
  359. * For regular membarrier, we can save a few cycles by
  360. * skipping the current cpu -- we're about to do smp_mb()
  361. * below, and if we migrate to a different cpu, this cpu
  362. * and the new cpu will execute a full barrier in the
  363. * scheduler.
  364. *
  365. * For SYNC_CORE, we do need a barrier on the current cpu --
  366. * otherwise, if we are migrated and replaced by a different
  367. * task in the same mm just before, during, or after
  368. * membarrier, we will end up with some thread in the mm
  369. * running without a core sync.
  370. *
  371. * For RSEQ, don't rseq_preempt() the caller. User code
  372. * is not supposed to issue syscalls at all from inside an
  373. * rseq critical section.
  374. */
  375. if (flags != MEMBARRIER_FLAG_SYNC_CORE) {
  376. preempt_disable();
  377. smp_call_function_many(tmpmask, ipi_func, NULL, true);
  378. preempt_enable();
  379. } else {
  380. on_each_cpu_mask(tmpmask, ipi_func, NULL, true);
  381. }
  382. }
  383. out:
  384. if (cpu_id < 0)
  385. free_cpumask_var(tmpmask);
  386. cpus_read_unlock();
  387. /*
  388. * Memory barrier on the caller thread _after_ we finished
  389. * waiting for the last IPI. Matches memory barriers before
  390. * rq->curr modification in scheduler.
  391. */
  392. smp_mb(); /* exit from system call is not a mb */
  393. return 0;
  394. }
  395. static int sync_runqueues_membarrier_state(struct mm_struct *mm)
  396. {
  397. int membarrier_state = atomic_read(&mm->membarrier_state);
  398. cpumask_var_t tmpmask;
  399. int cpu;
  400. if (atomic_read(&mm->mm_users) == 1 || num_online_cpus() == 1) {
  401. this_cpu_write(runqueues.membarrier_state, membarrier_state);
  402. /*
  403. * For single mm user, we can simply issue a memory barrier
  404. * after setting MEMBARRIER_STATE_GLOBAL_EXPEDITED in the
  405. * mm and in the current runqueue to guarantee that no memory
  406. * access following registration is reordered before
  407. * registration.
  408. */
  409. smp_mb();
  410. return 0;
  411. }
  412. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  413. return -ENOMEM;
  414. /*
  415. * For mm with multiple users, we need to ensure all future
  416. * scheduler executions will observe @mm's new membarrier
  417. * state.
  418. */
  419. synchronize_rcu();
  420. /*
  421. * For each cpu runqueue, if the task's mm match @mm, ensure that all
  422. * @mm's membarrier state set bits are also set in the runqueue's
  423. * membarrier state. This ensures that a runqueue scheduling
  424. * between threads which are users of @mm has its membarrier state
  425. * updated.
  426. */
  427. SERIALIZE_IPI();
  428. cpus_read_lock();
  429. rcu_read_lock();
  430. for_each_online_cpu(cpu) {
  431. struct rq *rq = cpu_rq(cpu);
  432. struct task_struct *p;
  433. p = rcu_dereference(rq->curr);
  434. if (p && p->mm == mm)
  435. __cpumask_set_cpu(cpu, tmpmask);
  436. }
  437. rcu_read_unlock();
  438. on_each_cpu_mask(tmpmask, ipi_sync_rq_state, mm, true);
  439. free_cpumask_var(tmpmask);
  440. cpus_read_unlock();
  441. return 0;
  442. }
  443. static int membarrier_register_global_expedited(void)
  444. {
  445. struct task_struct *p = current;
  446. struct mm_struct *mm = p->mm;
  447. int ret;
  448. if (atomic_read(&mm->membarrier_state) &
  449. MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)
  450. return 0;
  451. atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED, &mm->membarrier_state);
  452. ret = sync_runqueues_membarrier_state(mm);
  453. if (ret)
  454. return ret;
  455. atomic_or(MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
  456. &mm->membarrier_state);
  457. return 0;
  458. }
  459. static int membarrier_register_private_expedited(int flags)
  460. {
  461. struct task_struct *p = current;
  462. struct mm_struct *mm = p->mm;
  463. int ready_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
  464. set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
  465. ret;
  466. if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
  467. if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
  468. return -EINVAL;
  469. ready_state =
  470. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
  471. } else if (flags == MEMBARRIER_FLAG_RSEQ) {
  472. if (!IS_ENABLED(CONFIG_RSEQ))
  473. return -EINVAL;
  474. ready_state =
  475. MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY;
  476. } else {
  477. WARN_ON_ONCE(flags);
  478. }
  479. /*
  480. * We need to consider threads belonging to different thread
  481. * groups, which use the same mm. (CLONE_VM but not
  482. * CLONE_THREAD).
  483. */
  484. if ((atomic_read(&mm->membarrier_state) & ready_state) == ready_state)
  485. return 0;
  486. if (flags & MEMBARRIER_FLAG_SYNC_CORE)
  487. set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
  488. if (flags & MEMBARRIER_FLAG_RSEQ)
  489. set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ;
  490. atomic_or(set_state, &mm->membarrier_state);
  491. ret = sync_runqueues_membarrier_state(mm);
  492. if (ret)
  493. return ret;
  494. atomic_or(ready_state, &mm->membarrier_state);
  495. return 0;
  496. }
  497. static int membarrier_get_registrations(void)
  498. {
  499. struct task_struct *p = current;
  500. struct mm_struct *mm = p->mm;
  501. int registrations_mask = 0, membarrier_state, i;
  502. static const int states[] = {
  503. MEMBARRIER_STATE_GLOBAL_EXPEDITED |
  504. MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY,
  505. MEMBARRIER_STATE_PRIVATE_EXPEDITED |
  506. MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
  507. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE |
  508. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY,
  509. MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ |
  510. MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY
  511. };
  512. static const int registration_cmds[] = {
  513. MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED,
  514. MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED,
  515. MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE,
  516. MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ
  517. };
  518. BUILD_BUG_ON(ARRAY_SIZE(states) != ARRAY_SIZE(registration_cmds));
  519. membarrier_state = atomic_read(&mm->membarrier_state);
  520. for (i = 0; i < ARRAY_SIZE(states); ++i) {
  521. if (membarrier_state & states[i]) {
  522. registrations_mask |= registration_cmds[i];
  523. membarrier_state &= ~states[i];
  524. }
  525. }
  526. WARN_ON_ONCE(membarrier_state != 0);
  527. return registrations_mask;
  528. }
  529. /**
  530. * sys_membarrier - issue memory barriers on a set of threads
  531. * @cmd: Takes command values defined in enum membarrier_cmd.
  532. * @flags: Currently needs to be 0 for all commands other than
  533. * MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
  534. * case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that @cpu_id
  535. * contains the CPU on which to interrupt (= restart)
  536. * the RSEQ critical section.
  537. * @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on which
  538. * RSEQ CS should be interrupted (@cmd must be
  539. * MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
  540. *
  541. * If this system call is not implemented, -ENOSYS is returned. If the
  542. * command specified does not exist, not available on the running
  543. * kernel, or if the command argument is invalid, this system call
  544. * returns -EINVAL. For a given command, with flags argument set to 0,
  545. * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
  546. * always return the same value until reboot. In addition, it can return
  547. * -ENOMEM if there is not enough memory available to perform the system
  548. * call.
  549. *
  550. * All memory accesses performed in program order from each targeted thread
  551. * is guaranteed to be ordered with respect to sys_membarrier(). If we use
  552. * the semantic "barrier()" to represent a compiler barrier forcing memory
  553. * accesses to be performed in program order across the barrier, and
  554. * smp_mb() to represent explicit memory barriers forcing full memory
  555. * ordering across the barrier, we have the following ordering table for
  556. * each pair of barrier(), sys_membarrier() and smp_mb():
  557. *
  558. * The pair ordering is detailed as (O: ordered, X: not ordered):
  559. *
  560. * barrier() smp_mb() sys_membarrier()
  561. * barrier() X X O
  562. * smp_mb() X O O
  563. * sys_membarrier() O O O
  564. */
  565. SYSCALL_DEFINE3(membarrier, int, cmd, unsigned int, flags, int, cpu_id)
  566. {
  567. switch (cmd) {
  568. case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
  569. if (unlikely(flags && flags != MEMBARRIER_CMD_FLAG_CPU))
  570. return -EINVAL;
  571. break;
  572. default:
  573. if (unlikely(flags))
  574. return -EINVAL;
  575. }
  576. if (!(flags & MEMBARRIER_CMD_FLAG_CPU))
  577. cpu_id = -1;
  578. switch (cmd) {
  579. case MEMBARRIER_CMD_QUERY:
  580. {
  581. int cmd_mask = MEMBARRIER_CMD_BITMASK;
  582. if (tick_nohz_full_enabled())
  583. cmd_mask &= ~MEMBARRIER_CMD_GLOBAL;
  584. return cmd_mask;
  585. }
  586. case MEMBARRIER_CMD_GLOBAL:
  587. /* MEMBARRIER_CMD_GLOBAL is not compatible with nohz_full. */
  588. if (tick_nohz_full_enabled())
  589. return -EINVAL;
  590. if (num_online_cpus() > 1)
  591. synchronize_rcu();
  592. return 0;
  593. case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
  594. return membarrier_global_expedited();
  595. case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
  596. return membarrier_register_global_expedited();
  597. case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
  598. return membarrier_private_expedited(0, cpu_id);
  599. case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
  600. return membarrier_register_private_expedited(0);
  601. case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
  602. return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE, cpu_id);
  603. case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
  604. return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
  605. case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
  606. return membarrier_private_expedited(MEMBARRIER_FLAG_RSEQ, cpu_id);
  607. case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
  608. return membarrier_register_private_expedited(MEMBARRIER_FLAG_RSEQ);
  609. case MEMBARRIER_CMD_GET_REGISTRATIONS:
  610. return membarrier_get_registrations();
  611. default:
  612. return -EINVAL;
  613. }
  614. }