update.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2001
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. *
  23. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  24. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  25. * Papers:
  26. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  27. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  28. *
  29. * For detailed explanation of Read-Copy Update mechanism see -
  30. * http://lse.sourceforge.net/locking/rcupdate.html
  31. *
  32. */
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/smp.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/percpu.h>
  43. #include <linux/notifier.h>
  44. #include <linux/cpu.h>
  45. #include <linux/mutex.h>
  46. #include <linux/export.h>
  47. #include <linux/hardirq.h>
  48. #include <linux/delay.h>
  49. #include <linux/module.h>
  50. #include <linux/kthread.h>
  51. #define CREATE_TRACE_POINTS
  52. #include "rcu.h"
  53. MODULE_ALIAS("rcupdate");
  54. #ifdef MODULE_PARAM_PREFIX
  55. #undef MODULE_PARAM_PREFIX
  56. #endif
  57. #define MODULE_PARAM_PREFIX "rcupdate."
  58. module_param(rcu_expedited, int, 0);
  59. #ifdef CONFIG_PREEMPT_RCU
  60. /*
  61. * Preemptible RCU implementation for rcu_read_lock().
  62. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  63. * if we block.
  64. */
  65. void __rcu_read_lock(void)
  66. {
  67. current->rcu_read_lock_nesting++;
  68. barrier(); /* critical section after entry code. */
  69. }
  70. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  71. /*
  72. * Preemptible RCU implementation for rcu_read_unlock().
  73. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  74. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  75. * invoke rcu_read_unlock_special() to clean up after a context switch
  76. * in an RCU read-side critical section and other special cases.
  77. */
  78. void __rcu_read_unlock(void)
  79. {
  80. struct task_struct *t = current;
  81. if (t->rcu_read_lock_nesting != 1) {
  82. --t->rcu_read_lock_nesting;
  83. } else {
  84. barrier(); /* critical section before exit code. */
  85. t->rcu_read_lock_nesting = INT_MIN;
  86. barrier(); /* assign before ->rcu_read_unlock_special load */
  87. if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  88. rcu_read_unlock_special(t);
  89. barrier(); /* ->rcu_read_unlock_special load before assign */
  90. t->rcu_read_lock_nesting = 0;
  91. }
  92. #ifdef CONFIG_PROVE_LOCKING
  93. {
  94. int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
  95. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  96. }
  97. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  98. }
  99. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  100. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  101. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  102. static struct lock_class_key rcu_lock_key;
  103. struct lockdep_map rcu_lock_map =
  104. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  105. EXPORT_SYMBOL_GPL(rcu_lock_map);
  106. static struct lock_class_key rcu_bh_lock_key;
  107. struct lockdep_map rcu_bh_lock_map =
  108. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
  109. EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
  110. static struct lock_class_key rcu_sched_lock_key;
  111. struct lockdep_map rcu_sched_lock_map =
  112. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
  113. EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
  114. static struct lock_class_key rcu_callback_key;
  115. struct lockdep_map rcu_callback_map =
  116. STATIC_LOCKDEP_MAP_INIT("rcu_callback", &rcu_callback_key);
  117. EXPORT_SYMBOL_GPL(rcu_callback_map);
  118. int notrace debug_lockdep_rcu_enabled(void)
  119. {
  120. return rcu_scheduler_active && debug_locks &&
  121. current->lockdep_recursion == 0;
  122. }
  123. EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
  124. /**
  125. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  126. *
  127. * Check for bottom half being disabled, which covers both the
  128. * CONFIG_PROVE_RCU and not cases. Note that if someone uses
  129. * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
  130. * will show the situation. This is useful for debug checks in functions
  131. * that require that they be called within an RCU read-side critical
  132. * section.
  133. *
  134. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
  135. *
  136. * Note that rcu_read_lock() is disallowed if the CPU is either idle or
  137. * offline from an RCU perspective, so check for those as well.
  138. */
  139. int rcu_read_lock_bh_held(void)
  140. {
  141. if (!debug_lockdep_rcu_enabled())
  142. return 1;
  143. if (!rcu_is_watching())
  144. return 0;
  145. if (!rcu_lockdep_current_cpu_online())
  146. return 0;
  147. return in_softirq() || irqs_disabled();
  148. }
  149. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  150. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  151. struct rcu_synchronize {
  152. struct rcu_head head;
  153. struct completion completion;
  154. };
  155. /*
  156. * Awaken the corresponding synchronize_rcu() instance now that a
  157. * grace period has elapsed.
  158. */
  159. static void wakeme_after_rcu(struct rcu_head *head)
  160. {
  161. struct rcu_synchronize *rcu;
  162. rcu = container_of(head, struct rcu_synchronize, head);
  163. complete(&rcu->completion);
  164. }
  165. void wait_rcu_gp(call_rcu_func_t crf)
  166. {
  167. struct rcu_synchronize rcu;
  168. init_rcu_head_on_stack(&rcu.head);
  169. init_completion(&rcu.completion);
  170. /* Will wake me after RCU finished. */
  171. crf(&rcu.head, wakeme_after_rcu);
  172. /* Wait for it. */
  173. wait_for_completion(&rcu.completion);
  174. destroy_rcu_head_on_stack(&rcu.head);
  175. }
  176. EXPORT_SYMBOL_GPL(wait_rcu_gp);
  177. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  178. void init_rcu_head(struct rcu_head *head)
  179. {
  180. debug_object_init(head, &rcuhead_debug_descr);
  181. }
  182. void destroy_rcu_head(struct rcu_head *head)
  183. {
  184. debug_object_free(head, &rcuhead_debug_descr);
  185. }
  186. /*
  187. * fixup_activate is called when:
  188. * - an active object is activated
  189. * - an unknown object is activated (might be a statically initialized object)
  190. * Activation is performed internally by call_rcu().
  191. */
  192. static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
  193. {
  194. struct rcu_head *head = addr;
  195. switch (state) {
  196. case ODEBUG_STATE_NOTAVAILABLE:
  197. /*
  198. * This is not really a fixup. We just make sure that it is
  199. * tracked in the object tracker.
  200. */
  201. debug_object_init(head, &rcuhead_debug_descr);
  202. debug_object_activate(head, &rcuhead_debug_descr);
  203. return 0;
  204. default:
  205. return 1;
  206. }
  207. }
  208. /**
  209. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  210. * @head: pointer to rcu_head structure to be initialized
  211. *
  212. * This function informs debugobjects of a new rcu_head structure that
  213. * has been allocated as an auto variable on the stack. This function
  214. * is not required for rcu_head structures that are statically defined or
  215. * that are dynamically allocated on the heap. This function has no
  216. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  217. */
  218. void init_rcu_head_on_stack(struct rcu_head *head)
  219. {
  220. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  221. }
  222. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  223. /**
  224. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  225. * @head: pointer to rcu_head structure to be initialized
  226. *
  227. * This function informs debugobjects that an on-stack rcu_head structure
  228. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  229. * function is not required for rcu_head structures that are statically
  230. * defined or that are dynamically allocated on the heap. Also as with
  231. * init_rcu_head_on_stack(), this function has no effect for
  232. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  233. */
  234. void destroy_rcu_head_on_stack(struct rcu_head *head)
  235. {
  236. debug_object_free(head, &rcuhead_debug_descr);
  237. }
  238. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  239. struct debug_obj_descr rcuhead_debug_descr = {
  240. .name = "rcu_head",
  241. .fixup_activate = rcuhead_fixup_activate,
  242. };
  243. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  244. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  245. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  246. void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
  247. unsigned long secs,
  248. unsigned long c_old, unsigned long c)
  249. {
  250. trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
  251. }
  252. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  253. #else
  254. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  255. do { } while (0)
  256. #endif
  257. #ifdef CONFIG_RCU_STALL_COMMON
  258. #ifdef CONFIG_PROVE_RCU
  259. #define RCU_STALL_DELAY_DELTA (5 * HZ)
  260. #else
  261. #define RCU_STALL_DELAY_DELTA 0
  262. #endif
  263. int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
  264. static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
  265. module_param(rcu_cpu_stall_suppress, int, 0644);
  266. module_param(rcu_cpu_stall_timeout, int, 0644);
  267. int rcu_jiffies_till_stall_check(void)
  268. {
  269. int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout);
  270. /*
  271. * Limit check must be consistent with the Kconfig limits
  272. * for CONFIG_RCU_CPU_STALL_TIMEOUT.
  273. */
  274. if (till_stall_check < 3) {
  275. ACCESS_ONCE(rcu_cpu_stall_timeout) = 3;
  276. till_stall_check = 3;
  277. } else if (till_stall_check > 300) {
  278. ACCESS_ONCE(rcu_cpu_stall_timeout) = 300;
  279. till_stall_check = 300;
  280. }
  281. return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
  282. }
  283. void rcu_sysrq_start(void)
  284. {
  285. if (!rcu_cpu_stall_suppress)
  286. rcu_cpu_stall_suppress = 2;
  287. }
  288. void rcu_sysrq_end(void)
  289. {
  290. if (rcu_cpu_stall_suppress == 2)
  291. rcu_cpu_stall_suppress = 0;
  292. }
  293. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  294. {
  295. rcu_cpu_stall_suppress = 1;
  296. return NOTIFY_DONE;
  297. }
  298. static struct notifier_block rcu_panic_block = {
  299. .notifier_call = rcu_panic,
  300. };
  301. static int __init check_cpu_stall_init(void)
  302. {
  303. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  304. return 0;
  305. }
  306. early_initcall(check_cpu_stall_init);
  307. #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
  308. #ifdef CONFIG_TASKS_RCU
  309. /*
  310. * Simple variant of RCU whose quiescent states are voluntary context switch,
  311. * user-space execution, and idle. As such, grace periods can take one good
  312. * long time. There are no read-side primitives similar to rcu_read_lock()
  313. * and rcu_read_unlock() because this implementation is intended to get
  314. * the system into a safe state for some of the manipulations involved in
  315. * tracing and the like. Finally, this implementation does not support
  316. * high call_rcu_tasks() rates from multiple CPUs. If this is required,
  317. * per-CPU callback lists will be needed.
  318. */
  319. /* Global list of callbacks and associated lock. */
  320. static struct rcu_head *rcu_tasks_cbs_head;
  321. static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  322. static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
  323. static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
  324. /* Track exiting tasks in order to allow them to be waited for. */
  325. DEFINE_SRCU(tasks_rcu_exit_srcu);
  326. /* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
  327. static int rcu_task_stall_timeout __read_mostly = HZ * 60 * 10;
  328. module_param(rcu_task_stall_timeout, int, 0644);
  329. static void rcu_spawn_tasks_kthread(void);
  330. /*
  331. * Post an RCU-tasks callback. First call must be from process context
  332. * after the scheduler if fully operational.
  333. */
  334. void call_rcu_tasks(struct rcu_head *rhp, void (*func)(struct rcu_head *rhp))
  335. {
  336. unsigned long flags;
  337. bool needwake;
  338. rhp->next = NULL;
  339. rhp->func = func;
  340. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  341. needwake = !rcu_tasks_cbs_head;
  342. *rcu_tasks_cbs_tail = rhp;
  343. rcu_tasks_cbs_tail = &rhp->next;
  344. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  345. if (needwake) {
  346. rcu_spawn_tasks_kthread();
  347. wake_up(&rcu_tasks_cbs_wq);
  348. }
  349. }
  350. EXPORT_SYMBOL_GPL(call_rcu_tasks);
  351. /**
  352. * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
  353. *
  354. * Control will return to the caller some time after a full rcu-tasks
  355. * grace period has elapsed, in other words after all currently
  356. * executing rcu-tasks read-side critical sections have elapsed. These
  357. * read-side critical sections are delimited by calls to schedule(),
  358. * cond_resched_rcu_qs(), idle execution, userspace execution, calls
  359. * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
  360. *
  361. * This is a very specialized primitive, intended only for a few uses in
  362. * tracing and other situations requiring manipulation of function
  363. * preambles and profiling hooks. The synchronize_rcu_tasks() function
  364. * is not (yet) intended for heavy use from multiple CPUs.
  365. *
  366. * Note that this guarantee implies further memory-ordering guarantees.
  367. * On systems with more than one CPU, when synchronize_rcu_tasks() returns,
  368. * each CPU is guaranteed to have executed a full memory barrier since the
  369. * end of its last RCU-tasks read-side critical section whose beginning
  370. * preceded the call to synchronize_rcu_tasks(). In addition, each CPU
  371. * having an RCU-tasks read-side critical section that extends beyond
  372. * the return from synchronize_rcu_tasks() is guaranteed to have executed
  373. * a full memory barrier after the beginning of synchronize_rcu_tasks()
  374. * and before the beginning of that RCU-tasks read-side critical section.
  375. * Note that these guarantees include CPUs that are offline, idle, or
  376. * executing in user mode, as well as CPUs that are executing in the kernel.
  377. *
  378. * Furthermore, if CPU A invoked synchronize_rcu_tasks(), which returned
  379. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  380. * to have executed a full memory barrier during the execution of
  381. * synchronize_rcu_tasks() -- even if CPU A and CPU B are the same CPU
  382. * (but again only if the system has more than one CPU).
  383. */
  384. void synchronize_rcu_tasks(void)
  385. {
  386. /* Complain if the scheduler has not started. */
  387. rcu_lockdep_assert(!rcu_scheduler_active,
  388. "synchronize_rcu_tasks called too soon");
  389. /* Wait for the grace period. */
  390. wait_rcu_gp(call_rcu_tasks);
  391. }
  392. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
  393. /**
  394. * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
  395. *
  396. * Although the current implementation is guaranteed to wait, it is not
  397. * obligated to, for example, if there are no pending callbacks.
  398. */
  399. void rcu_barrier_tasks(void)
  400. {
  401. /* There is only one callback queue, so this is easy. ;-) */
  402. synchronize_rcu_tasks();
  403. }
  404. EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
  405. /* See if tasks are still holding out, complain if so. */
  406. static void check_holdout_task(struct task_struct *t,
  407. bool needreport, bool *firstreport)
  408. {
  409. if (!ACCESS_ONCE(t->rcu_tasks_holdout) ||
  410. t->rcu_tasks_nvcsw != ACCESS_ONCE(t->nvcsw) ||
  411. !ACCESS_ONCE(t->on_rq) ||
  412. (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
  413. !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
  414. ACCESS_ONCE(t->rcu_tasks_holdout) = false;
  415. list_del_init(&t->rcu_tasks_holdout_list);
  416. put_task_struct(t);
  417. return;
  418. }
  419. if (!needreport)
  420. return;
  421. if (*firstreport) {
  422. pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
  423. *firstreport = false;
  424. }
  425. sched_show_task(t);
  426. }
  427. /* RCU-tasks kthread that detects grace periods and invokes callbacks. */
  428. static int __noreturn rcu_tasks_kthread(void *arg)
  429. {
  430. unsigned long flags;
  431. struct task_struct *g, *t;
  432. unsigned long lastreport;
  433. struct rcu_head *list;
  434. struct rcu_head *next;
  435. LIST_HEAD(rcu_tasks_holdouts);
  436. /* FIXME: Add housekeeping affinity. */
  437. /*
  438. * Each pass through the following loop makes one check for
  439. * newly arrived callbacks, and, if there are some, waits for
  440. * one RCU-tasks grace period and then invokes the callbacks.
  441. * This loop is terminated by the system going down. ;-)
  442. */
  443. for (;;) {
  444. /* Pick up any new callbacks. */
  445. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  446. list = rcu_tasks_cbs_head;
  447. rcu_tasks_cbs_head = NULL;
  448. rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  449. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  450. /* If there were none, wait a bit and start over. */
  451. if (!list) {
  452. wait_event_interruptible(rcu_tasks_cbs_wq,
  453. rcu_tasks_cbs_head);
  454. if (!rcu_tasks_cbs_head) {
  455. WARN_ON(signal_pending(current));
  456. schedule_timeout_interruptible(HZ/10);
  457. }
  458. continue;
  459. }
  460. /*
  461. * Wait for all pre-existing t->on_rq and t->nvcsw
  462. * transitions to complete. Invoking synchronize_sched()
  463. * suffices because all these transitions occur with
  464. * interrupts disabled. Without this synchronize_sched(),
  465. * a read-side critical section that started before the
  466. * grace period might be incorrectly seen as having started
  467. * after the grace period.
  468. *
  469. * This synchronize_sched() also dispenses with the
  470. * need for a memory barrier on the first store to
  471. * ->rcu_tasks_holdout, as it forces the store to happen
  472. * after the beginning of the grace period.
  473. */
  474. synchronize_sched();
  475. /*
  476. * There were callbacks, so we need to wait for an
  477. * RCU-tasks grace period. Start off by scanning
  478. * the task list for tasks that are not already
  479. * voluntarily blocked. Mark these tasks and make
  480. * a list of them in rcu_tasks_holdouts.
  481. */
  482. rcu_read_lock();
  483. for_each_process_thread(g, t) {
  484. if (t != current && ACCESS_ONCE(t->on_rq) &&
  485. !is_idle_task(t)) {
  486. get_task_struct(t);
  487. t->rcu_tasks_nvcsw = ACCESS_ONCE(t->nvcsw);
  488. ACCESS_ONCE(t->rcu_tasks_holdout) = true;
  489. list_add(&t->rcu_tasks_holdout_list,
  490. &rcu_tasks_holdouts);
  491. }
  492. }
  493. rcu_read_unlock();
  494. /*
  495. * Wait for tasks that are in the process of exiting.
  496. * This does only part of the job, ensuring that all
  497. * tasks that were previously exiting reach the point
  498. * where they have disabled preemption, allowing the
  499. * later synchronize_sched() to finish the job.
  500. */
  501. synchronize_srcu(&tasks_rcu_exit_srcu);
  502. /*
  503. * Each pass through the following loop scans the list
  504. * of holdout tasks, removing any that are no longer
  505. * holdouts. When the list is empty, we are done.
  506. */
  507. lastreport = jiffies;
  508. while (!list_empty(&rcu_tasks_holdouts)) {
  509. bool firstreport;
  510. bool needreport;
  511. int rtst;
  512. struct task_struct *t1;
  513. schedule_timeout_interruptible(HZ);
  514. rtst = ACCESS_ONCE(rcu_task_stall_timeout);
  515. needreport = rtst > 0 &&
  516. time_after(jiffies, lastreport + rtst);
  517. if (needreport)
  518. lastreport = jiffies;
  519. firstreport = true;
  520. WARN_ON(signal_pending(current));
  521. list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
  522. rcu_tasks_holdout_list) {
  523. check_holdout_task(t, needreport, &firstreport);
  524. cond_resched();
  525. }
  526. }
  527. /*
  528. * Because ->on_rq and ->nvcsw are not guaranteed
  529. * to have a full memory barriers prior to them in the
  530. * schedule() path, memory reordering on other CPUs could
  531. * cause their RCU-tasks read-side critical sections to
  532. * extend past the end of the grace period. However,
  533. * because these ->nvcsw updates are carried out with
  534. * interrupts disabled, we can use synchronize_sched()
  535. * to force the needed ordering on all such CPUs.
  536. *
  537. * This synchronize_sched() also confines all
  538. * ->rcu_tasks_holdout accesses to be within the grace
  539. * period, avoiding the need for memory barriers for
  540. * ->rcu_tasks_holdout accesses.
  541. *
  542. * In addition, this synchronize_sched() waits for exiting
  543. * tasks to complete their final preempt_disable() region
  544. * of execution, cleaning up after the synchronize_srcu()
  545. * above.
  546. */
  547. synchronize_sched();
  548. /* Invoke the callbacks. */
  549. while (list) {
  550. next = list->next;
  551. local_bh_disable();
  552. list->func(list);
  553. local_bh_enable();
  554. list = next;
  555. cond_resched();
  556. }
  557. schedule_timeout_uninterruptible(HZ/10);
  558. }
  559. }
  560. /* Spawn rcu_tasks_kthread() at first call to call_rcu_tasks(). */
  561. static void rcu_spawn_tasks_kthread(void)
  562. {
  563. static DEFINE_MUTEX(rcu_tasks_kthread_mutex);
  564. static struct task_struct *rcu_tasks_kthread_ptr;
  565. struct task_struct *t;
  566. if (ACCESS_ONCE(rcu_tasks_kthread_ptr)) {
  567. smp_mb(); /* Ensure caller sees full kthread. */
  568. return;
  569. }
  570. mutex_lock(&rcu_tasks_kthread_mutex);
  571. if (rcu_tasks_kthread_ptr) {
  572. mutex_unlock(&rcu_tasks_kthread_mutex);
  573. return;
  574. }
  575. t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
  576. BUG_ON(IS_ERR(t));
  577. smp_mb(); /* Ensure others see full kthread. */
  578. ACCESS_ONCE(rcu_tasks_kthread_ptr) = t;
  579. mutex_unlock(&rcu_tasks_kthread_mutex);
  580. }
  581. #endif /* #ifdef CONFIG_TASKS_RCU */