update.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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/signal.h>
  40. #include <linux/sched/debug.h>
  41. #include <linux/atomic.h>
  42. #include <linux/bitops.h>
  43. #include <linux/percpu.h>
  44. #include <linux/notifier.h>
  45. #include <linux/cpu.h>
  46. #include <linux/mutex.h>
  47. #include <linux/export.h>
  48. #include <linux/hardirq.h>
  49. #include <linux/delay.h>
  50. #include <linux/moduleparam.h>
  51. #include <linux/kthread.h>
  52. #include <linux/tick.h>
  53. #include <linux/rcupdate_wait.h>
  54. #include <linux/sched/isolation.h>
  55. #define CREATE_TRACE_POINTS
  56. #include "rcu.h"
  57. #ifdef MODULE_PARAM_PREFIX
  58. #undef MODULE_PARAM_PREFIX
  59. #endif
  60. #define MODULE_PARAM_PREFIX "rcupdate."
  61. #ifndef CONFIG_TINY_RCU
  62. extern int rcu_expedited; /* from sysctl */
  63. module_param(rcu_expedited, int, 0);
  64. extern int rcu_normal; /* from sysctl */
  65. module_param(rcu_normal, int, 0);
  66. static int rcu_normal_after_boot;
  67. module_param(rcu_normal_after_boot, int, 0);
  68. #endif /* #ifndef CONFIG_TINY_RCU */
  69. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  70. /**
  71. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  72. *
  73. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an
  74. * RCU-sched read-side critical section. In absence of
  75. * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
  76. * critical section unless it can prove otherwise. Note that disabling
  77. * of preemption (including disabling irqs) counts as an RCU-sched
  78. * read-side critical section. This is useful for debug checks in functions
  79. * that required that they be called within an RCU-sched read-side
  80. * critical section.
  81. *
  82. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
  83. * and while lockdep is disabled.
  84. *
  85. * Note that if the CPU is in the idle loop from an RCU point of
  86. * view (ie: that we are in the section between rcu_idle_enter() and
  87. * rcu_idle_exit()) then rcu_read_lock_held() returns false even if the CPU
  88. * did an rcu_read_lock(). The reason for this is that RCU ignores CPUs
  89. * that are in such a section, considering these as in extended quiescent
  90. * state, so such a CPU is effectively never in an RCU read-side critical
  91. * section regardless of what RCU primitives it invokes. This state of
  92. * affairs is required --- we need to keep an RCU-free window in idle
  93. * where the CPU may possibly enter into low power mode. This way we can
  94. * notice an extended quiescent state to other CPUs that started a grace
  95. * period. Otherwise we would delay any grace period as long as we run in
  96. * the idle task.
  97. *
  98. * Similarly, we avoid claiming an SRCU read lock held if the current
  99. * CPU is offline.
  100. */
  101. int rcu_read_lock_sched_held(void)
  102. {
  103. int lockdep_opinion = 0;
  104. if (!debug_lockdep_rcu_enabled())
  105. return 1;
  106. if (!rcu_is_watching())
  107. return 0;
  108. if (!rcu_lockdep_current_cpu_online())
  109. return 0;
  110. if (debug_locks)
  111. lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
  112. return lockdep_opinion || !preemptible();
  113. }
  114. EXPORT_SYMBOL(rcu_read_lock_sched_held);
  115. #endif
  116. #ifndef CONFIG_TINY_RCU
  117. /*
  118. * Should expedited grace-period primitives always fall back to their
  119. * non-expedited counterparts? Intended for use within RCU. Note
  120. * that if the user specifies both rcu_expedited and rcu_normal, then
  121. * rcu_normal wins. (Except during the time period during boot from
  122. * when the first task is spawned until the rcu_set_runtime_mode()
  123. * core_initcall() is invoked, at which point everything is expedited.)
  124. */
  125. bool rcu_gp_is_normal(void)
  126. {
  127. return READ_ONCE(rcu_normal) &&
  128. rcu_scheduler_active != RCU_SCHEDULER_INIT;
  129. }
  130. EXPORT_SYMBOL_GPL(rcu_gp_is_normal);
  131. static atomic_t rcu_expedited_nesting = ATOMIC_INIT(1);
  132. /*
  133. * Should normal grace-period primitives be expedited? Intended for
  134. * use within RCU. Note that this function takes the rcu_expedited
  135. * sysfs/boot variable and rcu_scheduler_active into account as well
  136. * as the rcu_expedite_gp() nesting. So looping on rcu_unexpedite_gp()
  137. * until rcu_gp_is_expedited() returns false is a -really- bad idea.
  138. */
  139. bool rcu_gp_is_expedited(void)
  140. {
  141. return rcu_expedited || atomic_read(&rcu_expedited_nesting) ||
  142. rcu_scheduler_active == RCU_SCHEDULER_INIT;
  143. }
  144. EXPORT_SYMBOL_GPL(rcu_gp_is_expedited);
  145. /**
  146. * rcu_expedite_gp - Expedite future RCU grace periods
  147. *
  148. * After a call to this function, future calls to synchronize_rcu() and
  149. * friends act as the corresponding synchronize_rcu_expedited() function
  150. * had instead been called.
  151. */
  152. void rcu_expedite_gp(void)
  153. {
  154. atomic_inc(&rcu_expedited_nesting);
  155. }
  156. EXPORT_SYMBOL_GPL(rcu_expedite_gp);
  157. /**
  158. * rcu_unexpedite_gp - Cancel prior rcu_expedite_gp() invocation
  159. *
  160. * Undo a prior call to rcu_expedite_gp(). If all prior calls to
  161. * rcu_expedite_gp() are undone by a subsequent call to rcu_unexpedite_gp(),
  162. * and if the rcu_expedited sysfs/boot parameter is not set, then all
  163. * subsequent calls to synchronize_rcu() and friends will return to
  164. * their normal non-expedited behavior.
  165. */
  166. void rcu_unexpedite_gp(void)
  167. {
  168. atomic_dec(&rcu_expedited_nesting);
  169. }
  170. EXPORT_SYMBOL_GPL(rcu_unexpedite_gp);
  171. /*
  172. * Inform RCU of the end of the in-kernel boot sequence.
  173. */
  174. void rcu_end_inkernel_boot(void)
  175. {
  176. rcu_unexpedite_gp();
  177. if (rcu_normal_after_boot)
  178. WRITE_ONCE(rcu_normal, 1);
  179. }
  180. #endif /* #ifndef CONFIG_TINY_RCU */
  181. /*
  182. * Test each non-SRCU synchronous grace-period wait API. This is
  183. * useful just after a change in mode for these primitives, and
  184. * during early boot.
  185. */
  186. void rcu_test_sync_prims(void)
  187. {
  188. if (!IS_ENABLED(CONFIG_PROVE_RCU))
  189. return;
  190. synchronize_rcu();
  191. synchronize_rcu_bh();
  192. synchronize_sched();
  193. synchronize_rcu_expedited();
  194. synchronize_rcu_bh_expedited();
  195. synchronize_sched_expedited();
  196. }
  197. #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU)
  198. /*
  199. * Switch to run-time mode once RCU has fully initialized.
  200. */
  201. static int __init rcu_set_runtime_mode(void)
  202. {
  203. rcu_test_sync_prims();
  204. rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
  205. rcu_test_sync_prims();
  206. return 0;
  207. }
  208. core_initcall(rcu_set_runtime_mode);
  209. #endif /* #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU) */
  210. #ifdef CONFIG_PREEMPT_RCU
  211. /*
  212. * Preemptible RCU implementation for rcu_read_lock().
  213. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  214. * if we block.
  215. */
  216. void __rcu_read_lock(void)
  217. {
  218. current->rcu_read_lock_nesting++;
  219. barrier(); /* critical section after entry code. */
  220. }
  221. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  222. /*
  223. * Preemptible RCU implementation for rcu_read_unlock().
  224. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  225. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  226. * invoke rcu_read_unlock_special() to clean up after a context switch
  227. * in an RCU read-side critical section and other special cases.
  228. */
  229. void __rcu_read_unlock(void)
  230. {
  231. struct task_struct *t = current;
  232. if (t->rcu_read_lock_nesting != 1) {
  233. --t->rcu_read_lock_nesting;
  234. } else {
  235. barrier(); /* critical section before exit code. */
  236. t->rcu_read_lock_nesting = INT_MIN;
  237. barrier(); /* assign before ->rcu_read_unlock_special load */
  238. if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
  239. rcu_read_unlock_special(t);
  240. barrier(); /* ->rcu_read_unlock_special load before assign */
  241. t->rcu_read_lock_nesting = 0;
  242. }
  243. #ifdef CONFIG_PROVE_LOCKING
  244. {
  245. int rrln = READ_ONCE(t->rcu_read_lock_nesting);
  246. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  247. }
  248. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  249. }
  250. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  251. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  252. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  253. static struct lock_class_key rcu_lock_key;
  254. struct lockdep_map rcu_lock_map =
  255. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  256. EXPORT_SYMBOL_GPL(rcu_lock_map);
  257. static struct lock_class_key rcu_bh_lock_key;
  258. struct lockdep_map rcu_bh_lock_map =
  259. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
  260. EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
  261. static struct lock_class_key rcu_sched_lock_key;
  262. struct lockdep_map rcu_sched_lock_map =
  263. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
  264. EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
  265. static struct lock_class_key rcu_callback_key;
  266. struct lockdep_map rcu_callback_map =
  267. STATIC_LOCKDEP_MAP_INIT("rcu_callback", &rcu_callback_key);
  268. EXPORT_SYMBOL_GPL(rcu_callback_map);
  269. int notrace debug_lockdep_rcu_enabled(void)
  270. {
  271. return rcu_scheduler_active != RCU_SCHEDULER_INACTIVE && debug_locks &&
  272. current->lockdep_recursion == 0;
  273. }
  274. EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
  275. /**
  276. * rcu_read_lock_held() - might we be in RCU read-side critical section?
  277. *
  278. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU
  279. * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
  280. * this assumes we are in an RCU read-side critical section unless it can
  281. * prove otherwise. This is useful for debug checks in functions that
  282. * require that they be called within an RCU read-side critical section.
  283. *
  284. * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
  285. * and while lockdep is disabled.
  286. *
  287. * Note that rcu_read_lock() and the matching rcu_read_unlock() must
  288. * occur in the same context, for example, it is illegal to invoke
  289. * rcu_read_unlock() in process context if the matching rcu_read_lock()
  290. * was invoked from within an irq handler.
  291. *
  292. * Note that rcu_read_lock() is disallowed if the CPU is either idle or
  293. * offline from an RCU perspective, so check for those as well.
  294. */
  295. int rcu_read_lock_held(void)
  296. {
  297. if (!debug_lockdep_rcu_enabled())
  298. return 1;
  299. if (!rcu_is_watching())
  300. return 0;
  301. if (!rcu_lockdep_current_cpu_online())
  302. return 0;
  303. return lock_is_held(&rcu_lock_map);
  304. }
  305. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  306. /**
  307. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  308. *
  309. * Check for bottom half being disabled, which covers both the
  310. * CONFIG_PROVE_RCU and not cases. Note that if someone uses
  311. * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
  312. * will show the situation. This is useful for debug checks in functions
  313. * that require that they be called within an RCU read-side critical
  314. * section.
  315. *
  316. * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
  317. *
  318. * Note that rcu_read_lock() is disallowed if the CPU is either idle or
  319. * offline from an RCU perspective, so check for those as well.
  320. */
  321. int rcu_read_lock_bh_held(void)
  322. {
  323. if (!debug_lockdep_rcu_enabled())
  324. return 1;
  325. if (!rcu_is_watching())
  326. return 0;
  327. if (!rcu_lockdep_current_cpu_online())
  328. return 0;
  329. return in_softirq() || irqs_disabled();
  330. }
  331. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  332. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  333. /**
  334. * wakeme_after_rcu() - Callback function to awaken a task after grace period
  335. * @head: Pointer to rcu_head member within rcu_synchronize structure
  336. *
  337. * Awaken the corresponding task now that a grace period has elapsed.
  338. */
  339. void wakeme_after_rcu(struct rcu_head *head)
  340. {
  341. struct rcu_synchronize *rcu;
  342. rcu = container_of(head, struct rcu_synchronize, head);
  343. complete(&rcu->completion);
  344. }
  345. EXPORT_SYMBOL_GPL(wakeme_after_rcu);
  346. void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
  347. struct rcu_synchronize *rs_array)
  348. {
  349. int i;
  350. int j;
  351. /* Initialize and register callbacks for each flavor specified. */
  352. for (i = 0; i < n; i++) {
  353. if (checktiny &&
  354. (crcu_array[i] == call_rcu ||
  355. crcu_array[i] == call_rcu_bh)) {
  356. might_sleep();
  357. continue;
  358. }
  359. init_rcu_head_on_stack(&rs_array[i].head);
  360. init_completion(&rs_array[i].completion);
  361. for (j = 0; j < i; j++)
  362. if (crcu_array[j] == crcu_array[i])
  363. break;
  364. if (j == i)
  365. (crcu_array[i])(&rs_array[i].head, wakeme_after_rcu);
  366. }
  367. /* Wait for all callbacks to be invoked. */
  368. for (i = 0; i < n; i++) {
  369. if (checktiny &&
  370. (crcu_array[i] == call_rcu ||
  371. crcu_array[i] == call_rcu_bh))
  372. continue;
  373. for (j = 0; j < i; j++)
  374. if (crcu_array[j] == crcu_array[i])
  375. break;
  376. if (j == i)
  377. wait_for_completion(&rs_array[i].completion);
  378. destroy_rcu_head_on_stack(&rs_array[i].head);
  379. }
  380. }
  381. EXPORT_SYMBOL_GPL(__wait_rcu_gp);
  382. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  383. void init_rcu_head(struct rcu_head *head)
  384. {
  385. debug_object_init(head, &rcuhead_debug_descr);
  386. }
  387. EXPORT_SYMBOL_GPL(init_rcu_head);
  388. void destroy_rcu_head(struct rcu_head *head)
  389. {
  390. debug_object_free(head, &rcuhead_debug_descr);
  391. }
  392. EXPORT_SYMBOL_GPL(destroy_rcu_head);
  393. static bool rcuhead_is_static_object(void *addr)
  394. {
  395. return true;
  396. }
  397. /**
  398. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  399. * @head: pointer to rcu_head structure to be initialized
  400. *
  401. * This function informs debugobjects of a new rcu_head structure that
  402. * has been allocated as an auto variable on the stack. This function
  403. * is not required for rcu_head structures that are statically defined or
  404. * that are dynamically allocated on the heap. This function has no
  405. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  406. */
  407. void init_rcu_head_on_stack(struct rcu_head *head)
  408. {
  409. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  410. }
  411. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  412. /**
  413. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  414. * @head: pointer to rcu_head structure to be initialized
  415. *
  416. * This function informs debugobjects that an on-stack rcu_head structure
  417. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  418. * function is not required for rcu_head structures that are statically
  419. * defined or that are dynamically allocated on the heap. Also as with
  420. * init_rcu_head_on_stack(), this function has no effect for
  421. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  422. */
  423. void destroy_rcu_head_on_stack(struct rcu_head *head)
  424. {
  425. debug_object_free(head, &rcuhead_debug_descr);
  426. }
  427. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  428. struct debug_obj_descr rcuhead_debug_descr = {
  429. .name = "rcu_head",
  430. .is_static_object = rcuhead_is_static_object,
  431. };
  432. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  433. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  434. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  435. void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
  436. unsigned long secs,
  437. unsigned long c_old, unsigned long c)
  438. {
  439. trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
  440. }
  441. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  442. #else
  443. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  444. do { } while (0)
  445. #endif
  446. #ifdef CONFIG_RCU_STALL_COMMON
  447. #ifdef CONFIG_PROVE_RCU
  448. #define RCU_STALL_DELAY_DELTA (5 * HZ)
  449. #else
  450. #define RCU_STALL_DELAY_DELTA 0
  451. #endif
  452. int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
  453. EXPORT_SYMBOL_GPL(rcu_cpu_stall_suppress);
  454. static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
  455. module_param(rcu_cpu_stall_suppress, int, 0644);
  456. module_param(rcu_cpu_stall_timeout, int, 0644);
  457. int rcu_jiffies_till_stall_check(void)
  458. {
  459. int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
  460. /*
  461. * Limit check must be consistent with the Kconfig limits
  462. * for CONFIG_RCU_CPU_STALL_TIMEOUT.
  463. */
  464. if (till_stall_check < 3) {
  465. WRITE_ONCE(rcu_cpu_stall_timeout, 3);
  466. till_stall_check = 3;
  467. } else if (till_stall_check > 300) {
  468. WRITE_ONCE(rcu_cpu_stall_timeout, 300);
  469. till_stall_check = 300;
  470. }
  471. return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
  472. }
  473. void rcu_sysrq_start(void)
  474. {
  475. if (!rcu_cpu_stall_suppress)
  476. rcu_cpu_stall_suppress = 2;
  477. }
  478. void rcu_sysrq_end(void)
  479. {
  480. if (rcu_cpu_stall_suppress == 2)
  481. rcu_cpu_stall_suppress = 0;
  482. }
  483. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  484. {
  485. rcu_cpu_stall_suppress = 1;
  486. return NOTIFY_DONE;
  487. }
  488. static struct notifier_block rcu_panic_block = {
  489. .notifier_call = rcu_panic,
  490. };
  491. static int __init check_cpu_stall_init(void)
  492. {
  493. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  494. return 0;
  495. }
  496. early_initcall(check_cpu_stall_init);
  497. #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
  498. #ifdef CONFIG_TASKS_RCU
  499. /*
  500. * Simple variant of RCU whose quiescent states are voluntary context switch,
  501. * user-space execution, and idle. As such, grace periods can take one good
  502. * long time. There are no read-side primitives similar to rcu_read_lock()
  503. * and rcu_read_unlock() because this implementation is intended to get
  504. * the system into a safe state for some of the manipulations involved in
  505. * tracing and the like. Finally, this implementation does not support
  506. * high call_rcu_tasks() rates from multiple CPUs. If this is required,
  507. * per-CPU callback lists will be needed.
  508. */
  509. /* Global list of callbacks and associated lock. */
  510. static struct rcu_head *rcu_tasks_cbs_head;
  511. static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  512. static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
  513. static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
  514. /* Track exiting tasks in order to allow them to be waited for. */
  515. DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
  516. /* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
  517. #define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
  518. static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
  519. module_param(rcu_task_stall_timeout, int, 0644);
  520. static struct task_struct *rcu_tasks_kthread_ptr;
  521. /**
  522. * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
  523. * @rhp: structure to be used for queueing the RCU updates.
  524. * @func: actual callback function to be invoked after the grace period
  525. *
  526. * The callback function will be invoked some time after a full grace
  527. * period elapses, in other words after all currently executing RCU
  528. * read-side critical sections have completed. call_rcu_tasks() assumes
  529. * that the read-side critical sections end at a voluntary context
  530. * switch (not a preemption!), entry into idle, or transition to usermode
  531. * execution. As such, there are no read-side primitives analogous to
  532. * rcu_read_lock() and rcu_read_unlock() because this primitive is intended
  533. * to determine that all tasks have passed through a safe state, not so
  534. * much for data-strcuture synchronization.
  535. *
  536. * See the description of call_rcu() for more detailed information on
  537. * memory ordering guarantees.
  538. */
  539. void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
  540. {
  541. unsigned long flags;
  542. bool needwake;
  543. rhp->next = NULL;
  544. rhp->func = func;
  545. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  546. needwake = !rcu_tasks_cbs_head;
  547. *rcu_tasks_cbs_tail = rhp;
  548. rcu_tasks_cbs_tail = &rhp->next;
  549. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  550. /* We can't create the thread unless interrupts are enabled. */
  551. if (needwake && READ_ONCE(rcu_tasks_kthread_ptr))
  552. wake_up(&rcu_tasks_cbs_wq);
  553. }
  554. EXPORT_SYMBOL_GPL(call_rcu_tasks);
  555. /**
  556. * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
  557. *
  558. * Control will return to the caller some time after a full rcu-tasks
  559. * grace period has elapsed, in other words after all currently
  560. * executing rcu-tasks read-side critical sections have elapsed. These
  561. * read-side critical sections are delimited by calls to schedule(),
  562. * cond_resched_rcu_qs(), idle execution, userspace execution, calls
  563. * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
  564. *
  565. * This is a very specialized primitive, intended only for a few uses in
  566. * tracing and other situations requiring manipulation of function
  567. * preambles and profiling hooks. The synchronize_rcu_tasks() function
  568. * is not (yet) intended for heavy use from multiple CPUs.
  569. *
  570. * Note that this guarantee implies further memory-ordering guarantees.
  571. * On systems with more than one CPU, when synchronize_rcu_tasks() returns,
  572. * each CPU is guaranteed to have executed a full memory barrier since the
  573. * end of its last RCU-tasks read-side critical section whose beginning
  574. * preceded the call to synchronize_rcu_tasks(). In addition, each CPU
  575. * having an RCU-tasks read-side critical section that extends beyond
  576. * the return from synchronize_rcu_tasks() is guaranteed to have executed
  577. * a full memory barrier after the beginning of synchronize_rcu_tasks()
  578. * and before the beginning of that RCU-tasks read-side critical section.
  579. * Note that these guarantees include CPUs that are offline, idle, or
  580. * executing in user mode, as well as CPUs that are executing in the kernel.
  581. *
  582. * Furthermore, if CPU A invoked synchronize_rcu_tasks(), which returned
  583. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  584. * to have executed a full memory barrier during the execution of
  585. * synchronize_rcu_tasks() -- even if CPU A and CPU B are the same CPU
  586. * (but again only if the system has more than one CPU).
  587. */
  588. void synchronize_rcu_tasks(void)
  589. {
  590. /* Complain if the scheduler has not started. */
  591. RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
  592. "synchronize_rcu_tasks called too soon");
  593. /* Wait for the grace period. */
  594. wait_rcu_gp(call_rcu_tasks);
  595. }
  596. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
  597. /**
  598. * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
  599. *
  600. * Although the current implementation is guaranteed to wait, it is not
  601. * obligated to, for example, if there are no pending callbacks.
  602. */
  603. void rcu_barrier_tasks(void)
  604. {
  605. /* There is only one callback queue, so this is easy. ;-) */
  606. synchronize_rcu_tasks();
  607. }
  608. EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
  609. /* See if tasks are still holding out, complain if so. */
  610. static void check_holdout_task(struct task_struct *t,
  611. bool needreport, bool *firstreport)
  612. {
  613. int cpu;
  614. if (!READ_ONCE(t->rcu_tasks_holdout) ||
  615. t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
  616. !READ_ONCE(t->on_rq) ||
  617. (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
  618. !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
  619. WRITE_ONCE(t->rcu_tasks_holdout, false);
  620. list_del_init(&t->rcu_tasks_holdout_list);
  621. put_task_struct(t);
  622. return;
  623. }
  624. rcu_request_urgent_qs_task(t);
  625. if (!needreport)
  626. return;
  627. if (*firstreport) {
  628. pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
  629. *firstreport = false;
  630. }
  631. cpu = task_cpu(t);
  632. pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
  633. t, ".I"[is_idle_task(t)],
  634. "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
  635. t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
  636. t->rcu_tasks_idle_cpu, cpu);
  637. sched_show_task(t);
  638. }
  639. /* RCU-tasks kthread that detects grace periods and invokes callbacks. */
  640. static int __noreturn rcu_tasks_kthread(void *arg)
  641. {
  642. unsigned long flags;
  643. struct task_struct *g, *t;
  644. unsigned long lastreport;
  645. struct rcu_head *list;
  646. struct rcu_head *next;
  647. LIST_HEAD(rcu_tasks_holdouts);
  648. /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
  649. housekeeping_affine(current, HK_FLAG_RCU);
  650. /*
  651. * Each pass through the following loop makes one check for
  652. * newly arrived callbacks, and, if there are some, waits for
  653. * one RCU-tasks grace period and then invokes the callbacks.
  654. * This loop is terminated by the system going down. ;-)
  655. */
  656. for (;;) {
  657. /* Pick up any new callbacks. */
  658. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  659. list = rcu_tasks_cbs_head;
  660. rcu_tasks_cbs_head = NULL;
  661. rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  662. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  663. /* If there were none, wait a bit and start over. */
  664. if (!list) {
  665. wait_event_interruptible(rcu_tasks_cbs_wq,
  666. rcu_tasks_cbs_head);
  667. if (!rcu_tasks_cbs_head) {
  668. WARN_ON(signal_pending(current));
  669. schedule_timeout_interruptible(HZ/10);
  670. }
  671. continue;
  672. }
  673. /*
  674. * Wait for all pre-existing t->on_rq and t->nvcsw
  675. * transitions to complete. Invoking synchronize_sched()
  676. * suffices because all these transitions occur with
  677. * interrupts disabled. Without this synchronize_sched(),
  678. * a read-side critical section that started before the
  679. * grace period might be incorrectly seen as having started
  680. * after the grace period.
  681. *
  682. * This synchronize_sched() also dispenses with the
  683. * need for a memory barrier on the first store to
  684. * ->rcu_tasks_holdout, as it forces the store to happen
  685. * after the beginning of the grace period.
  686. */
  687. synchronize_sched();
  688. /*
  689. * There were callbacks, so we need to wait for an
  690. * RCU-tasks grace period. Start off by scanning
  691. * the task list for tasks that are not already
  692. * voluntarily blocked. Mark these tasks and make
  693. * a list of them in rcu_tasks_holdouts.
  694. */
  695. rcu_read_lock();
  696. for_each_process_thread(g, t) {
  697. if (t != current && READ_ONCE(t->on_rq) &&
  698. !is_idle_task(t)) {
  699. get_task_struct(t);
  700. t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
  701. WRITE_ONCE(t->rcu_tasks_holdout, true);
  702. list_add(&t->rcu_tasks_holdout_list,
  703. &rcu_tasks_holdouts);
  704. }
  705. }
  706. rcu_read_unlock();
  707. /*
  708. * Wait for tasks that are in the process of exiting.
  709. * This does only part of the job, ensuring that all
  710. * tasks that were previously exiting reach the point
  711. * where they have disabled preemption, allowing the
  712. * later synchronize_sched() to finish the job.
  713. */
  714. synchronize_srcu(&tasks_rcu_exit_srcu);
  715. /*
  716. * Each pass through the following loop scans the list
  717. * of holdout tasks, removing any that are no longer
  718. * holdouts. When the list is empty, we are done.
  719. */
  720. lastreport = jiffies;
  721. while (!list_empty(&rcu_tasks_holdouts)) {
  722. bool firstreport;
  723. bool needreport;
  724. int rtst;
  725. struct task_struct *t1;
  726. schedule_timeout_interruptible(HZ);
  727. rtst = READ_ONCE(rcu_task_stall_timeout);
  728. needreport = rtst > 0 &&
  729. time_after(jiffies, lastreport + rtst);
  730. if (needreport)
  731. lastreport = jiffies;
  732. firstreport = true;
  733. WARN_ON(signal_pending(current));
  734. list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
  735. rcu_tasks_holdout_list) {
  736. check_holdout_task(t, needreport, &firstreport);
  737. cond_resched();
  738. }
  739. }
  740. /*
  741. * Because ->on_rq and ->nvcsw are not guaranteed
  742. * to have a full memory barriers prior to them in the
  743. * schedule() path, memory reordering on other CPUs could
  744. * cause their RCU-tasks read-side critical sections to
  745. * extend past the end of the grace period. However,
  746. * because these ->nvcsw updates are carried out with
  747. * interrupts disabled, we can use synchronize_sched()
  748. * to force the needed ordering on all such CPUs.
  749. *
  750. * This synchronize_sched() also confines all
  751. * ->rcu_tasks_holdout accesses to be within the grace
  752. * period, avoiding the need for memory barriers for
  753. * ->rcu_tasks_holdout accesses.
  754. *
  755. * In addition, this synchronize_sched() waits for exiting
  756. * tasks to complete their final preempt_disable() region
  757. * of execution, cleaning up after the synchronize_srcu()
  758. * above.
  759. */
  760. synchronize_sched();
  761. /* Invoke the callbacks. */
  762. while (list) {
  763. next = list->next;
  764. local_bh_disable();
  765. list->func(list);
  766. local_bh_enable();
  767. list = next;
  768. cond_resched();
  769. }
  770. schedule_timeout_uninterruptible(HZ/10);
  771. }
  772. }
  773. /* Spawn rcu_tasks_kthread() at core_initcall() time. */
  774. static int __init rcu_spawn_tasks_kthread(void)
  775. {
  776. struct task_struct *t;
  777. t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
  778. BUG_ON(IS_ERR(t));
  779. smp_mb(); /* Ensure others see full kthread. */
  780. WRITE_ONCE(rcu_tasks_kthread_ptr, t);
  781. return 0;
  782. }
  783. core_initcall(rcu_spawn_tasks_kthread);
  784. /* Do the srcu_read_lock() for the above synchronize_srcu(). */
  785. void exit_tasks_rcu_start(void)
  786. {
  787. preempt_disable();
  788. current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
  789. preempt_enable();
  790. }
  791. /* Do the srcu_read_unlock() for the above synchronize_srcu(). */
  792. void exit_tasks_rcu_finish(void)
  793. {
  794. preempt_disable();
  795. __srcu_read_unlock(&tasks_rcu_exit_srcu, current->rcu_tasks_idx);
  796. preempt_enable();
  797. }
  798. #endif /* #ifdef CONFIG_TASKS_RCU */
  799. #ifndef CONFIG_TINY_RCU
  800. /*
  801. * Print any non-default Tasks RCU settings.
  802. */
  803. static void __init rcu_tasks_bootup_oddness(void)
  804. {
  805. #ifdef CONFIG_TASKS_RCU
  806. if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
  807. pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
  808. else
  809. pr_info("\tTasks RCU enabled.\n");
  810. #endif /* #ifdef CONFIG_TASKS_RCU */
  811. }
  812. #endif /* #ifndef CONFIG_TINY_RCU */
  813. #ifdef CONFIG_PROVE_RCU
  814. /*
  815. * Early boot self test parameters, one for each flavor
  816. */
  817. static bool rcu_self_test;
  818. static bool rcu_self_test_bh;
  819. static bool rcu_self_test_sched;
  820. module_param(rcu_self_test, bool, 0444);
  821. module_param(rcu_self_test_bh, bool, 0444);
  822. module_param(rcu_self_test_sched, bool, 0444);
  823. static int rcu_self_test_counter;
  824. static void test_callback(struct rcu_head *r)
  825. {
  826. rcu_self_test_counter++;
  827. pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
  828. }
  829. static void early_boot_test_call_rcu(void)
  830. {
  831. static struct rcu_head head;
  832. call_rcu(&head, test_callback);
  833. }
  834. static void early_boot_test_call_rcu_bh(void)
  835. {
  836. static struct rcu_head head;
  837. call_rcu_bh(&head, test_callback);
  838. }
  839. static void early_boot_test_call_rcu_sched(void)
  840. {
  841. static struct rcu_head head;
  842. call_rcu_sched(&head, test_callback);
  843. }
  844. void rcu_early_boot_tests(void)
  845. {
  846. pr_info("Running RCU self tests\n");
  847. if (rcu_self_test)
  848. early_boot_test_call_rcu();
  849. if (rcu_self_test_bh)
  850. early_boot_test_call_rcu_bh();
  851. if (rcu_self_test_sched)
  852. early_boot_test_call_rcu_sched();
  853. rcu_test_sync_prims();
  854. }
  855. static int rcu_verify_early_boot_tests(void)
  856. {
  857. int ret = 0;
  858. int early_boot_test_counter = 0;
  859. if (rcu_self_test) {
  860. early_boot_test_counter++;
  861. rcu_barrier();
  862. }
  863. if (rcu_self_test_bh) {
  864. early_boot_test_counter++;
  865. rcu_barrier_bh();
  866. }
  867. if (rcu_self_test_sched) {
  868. early_boot_test_counter++;
  869. rcu_barrier_sched();
  870. }
  871. if (rcu_self_test_counter != early_boot_test_counter) {
  872. WARN_ON(1);
  873. ret = -1;
  874. }
  875. return ret;
  876. }
  877. late_initcall(rcu_verify_early_boot_tests);
  878. #else
  879. void rcu_early_boot_tests(void) {}
  880. #endif /* CONFIG_PROVE_RCU */
  881. #ifndef CONFIG_TINY_RCU
  882. /*
  883. * Print any significant non-default boot-time settings.
  884. */
  885. void __init rcupdate_announce_bootup_oddness(void)
  886. {
  887. if (rcu_normal)
  888. pr_info("\tNo expedited grace period (rcu_normal).\n");
  889. else if (rcu_normal_after_boot)
  890. pr_info("\tNo expedited grace period (rcu_normal_after_boot).\n");
  891. else if (rcu_expedited)
  892. pr_info("\tAll grace periods are expedited (rcu_expedited).\n");
  893. if (rcu_cpu_stall_suppress)
  894. pr_info("\tRCU CPU stall warnings suppressed (rcu_cpu_stall_suppress).\n");
  895. if (rcu_cpu_stall_timeout != CONFIG_RCU_CPU_STALL_TIMEOUT)
  896. pr_info("\tRCU CPU stall warnings timeout set to %d (rcu_cpu_stall_timeout).\n", rcu_cpu_stall_timeout);
  897. rcu_tasks_bootup_oddness();
  898. }
  899. #endif /* #ifndef CONFIG_TINY_RCU */