update.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. void destroy_rcu_head(struct rcu_head *head)
  388. {
  389. debug_object_free(head, &rcuhead_debug_descr);
  390. }
  391. static bool rcuhead_is_static_object(void *addr)
  392. {
  393. return true;
  394. }
  395. /**
  396. * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
  397. * @head: pointer to rcu_head structure to be initialized
  398. *
  399. * This function informs debugobjects of a new rcu_head structure that
  400. * has been allocated as an auto variable on the stack. This function
  401. * is not required for rcu_head structures that are statically defined or
  402. * that are dynamically allocated on the heap. This function has no
  403. * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  404. */
  405. void init_rcu_head_on_stack(struct rcu_head *head)
  406. {
  407. debug_object_init_on_stack(head, &rcuhead_debug_descr);
  408. }
  409. EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
  410. /**
  411. * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
  412. * @head: pointer to rcu_head structure to be initialized
  413. *
  414. * This function informs debugobjects that an on-stack rcu_head structure
  415. * is about to go out of scope. As with init_rcu_head_on_stack(), this
  416. * function is not required for rcu_head structures that are statically
  417. * defined or that are dynamically allocated on the heap. Also as with
  418. * init_rcu_head_on_stack(), this function has no effect for
  419. * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
  420. */
  421. void destroy_rcu_head_on_stack(struct rcu_head *head)
  422. {
  423. debug_object_free(head, &rcuhead_debug_descr);
  424. }
  425. EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
  426. struct debug_obj_descr rcuhead_debug_descr = {
  427. .name = "rcu_head",
  428. .is_static_object = rcuhead_is_static_object,
  429. };
  430. EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
  431. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  432. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
  433. void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
  434. unsigned long secs,
  435. unsigned long c_old, unsigned long c)
  436. {
  437. trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
  438. }
  439. EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
  440. #else
  441. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  442. do { } while (0)
  443. #endif
  444. #ifdef CONFIG_RCU_STALL_COMMON
  445. #ifdef CONFIG_PROVE_RCU
  446. #define RCU_STALL_DELAY_DELTA (5 * HZ)
  447. #else
  448. #define RCU_STALL_DELAY_DELTA 0
  449. #endif
  450. int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
  451. EXPORT_SYMBOL_GPL(rcu_cpu_stall_suppress);
  452. static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
  453. module_param(rcu_cpu_stall_suppress, int, 0644);
  454. module_param(rcu_cpu_stall_timeout, int, 0644);
  455. int rcu_jiffies_till_stall_check(void)
  456. {
  457. int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
  458. /*
  459. * Limit check must be consistent with the Kconfig limits
  460. * for CONFIG_RCU_CPU_STALL_TIMEOUT.
  461. */
  462. if (till_stall_check < 3) {
  463. WRITE_ONCE(rcu_cpu_stall_timeout, 3);
  464. till_stall_check = 3;
  465. } else if (till_stall_check > 300) {
  466. WRITE_ONCE(rcu_cpu_stall_timeout, 300);
  467. till_stall_check = 300;
  468. }
  469. return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
  470. }
  471. void rcu_sysrq_start(void)
  472. {
  473. if (!rcu_cpu_stall_suppress)
  474. rcu_cpu_stall_suppress = 2;
  475. }
  476. void rcu_sysrq_end(void)
  477. {
  478. if (rcu_cpu_stall_suppress == 2)
  479. rcu_cpu_stall_suppress = 0;
  480. }
  481. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  482. {
  483. rcu_cpu_stall_suppress = 1;
  484. return NOTIFY_DONE;
  485. }
  486. static struct notifier_block rcu_panic_block = {
  487. .notifier_call = rcu_panic,
  488. };
  489. static int __init check_cpu_stall_init(void)
  490. {
  491. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  492. return 0;
  493. }
  494. early_initcall(check_cpu_stall_init);
  495. #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
  496. #ifdef CONFIG_TASKS_RCU
  497. /*
  498. * Simple variant of RCU whose quiescent states are voluntary context switch,
  499. * user-space execution, and idle. As such, grace periods can take one good
  500. * long time. There are no read-side primitives similar to rcu_read_lock()
  501. * and rcu_read_unlock() because this implementation is intended to get
  502. * the system into a safe state for some of the manipulations involved in
  503. * tracing and the like. Finally, this implementation does not support
  504. * high call_rcu_tasks() rates from multiple CPUs. If this is required,
  505. * per-CPU callback lists will be needed.
  506. */
  507. /* Global list of callbacks and associated lock. */
  508. static struct rcu_head *rcu_tasks_cbs_head;
  509. static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  510. static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
  511. static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
  512. /* Track exiting tasks in order to allow them to be waited for. */
  513. DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
  514. /* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
  515. #define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
  516. static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
  517. module_param(rcu_task_stall_timeout, int, 0644);
  518. static struct task_struct *rcu_tasks_kthread_ptr;
  519. /**
  520. * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
  521. * @rhp: structure to be used for queueing the RCU updates.
  522. * @func: actual callback function to be invoked after the grace period
  523. *
  524. * The callback function will be invoked some time after a full grace
  525. * period elapses, in other words after all currently executing RCU
  526. * read-side critical sections have completed. call_rcu_tasks() assumes
  527. * that the read-side critical sections end at a voluntary context
  528. * switch (not a preemption!), entry into idle, or transition to usermode
  529. * execution. As such, there are no read-side primitives analogous to
  530. * rcu_read_lock() and rcu_read_unlock() because this primitive is intended
  531. * to determine that all tasks have passed through a safe state, not so
  532. * much for data-strcuture synchronization.
  533. *
  534. * See the description of call_rcu() for more detailed information on
  535. * memory ordering guarantees.
  536. */
  537. void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
  538. {
  539. unsigned long flags;
  540. bool needwake;
  541. rhp->next = NULL;
  542. rhp->func = func;
  543. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  544. needwake = !rcu_tasks_cbs_head;
  545. *rcu_tasks_cbs_tail = rhp;
  546. rcu_tasks_cbs_tail = &rhp->next;
  547. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  548. /* We can't create the thread unless interrupts are enabled. */
  549. if (needwake && READ_ONCE(rcu_tasks_kthread_ptr))
  550. wake_up(&rcu_tasks_cbs_wq);
  551. }
  552. EXPORT_SYMBOL_GPL(call_rcu_tasks);
  553. /**
  554. * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
  555. *
  556. * Control will return to the caller some time after a full rcu-tasks
  557. * grace period has elapsed, in other words after all currently
  558. * executing rcu-tasks read-side critical sections have elapsed. These
  559. * read-side critical sections are delimited by calls to schedule(),
  560. * cond_resched_rcu_qs(), idle execution, userspace execution, calls
  561. * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
  562. *
  563. * This is a very specialized primitive, intended only for a few uses in
  564. * tracing and other situations requiring manipulation of function
  565. * preambles and profiling hooks. The synchronize_rcu_tasks() function
  566. * is not (yet) intended for heavy use from multiple CPUs.
  567. *
  568. * Note that this guarantee implies further memory-ordering guarantees.
  569. * On systems with more than one CPU, when synchronize_rcu_tasks() returns,
  570. * each CPU is guaranteed to have executed a full memory barrier since the
  571. * end of its last RCU-tasks read-side critical section whose beginning
  572. * preceded the call to synchronize_rcu_tasks(). In addition, each CPU
  573. * having an RCU-tasks read-side critical section that extends beyond
  574. * the return from synchronize_rcu_tasks() is guaranteed to have executed
  575. * a full memory barrier after the beginning of synchronize_rcu_tasks()
  576. * and before the beginning of that RCU-tasks read-side critical section.
  577. * Note that these guarantees include CPUs that are offline, idle, or
  578. * executing in user mode, as well as CPUs that are executing in the kernel.
  579. *
  580. * Furthermore, if CPU A invoked synchronize_rcu_tasks(), which returned
  581. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  582. * to have executed a full memory barrier during the execution of
  583. * synchronize_rcu_tasks() -- even if CPU A and CPU B are the same CPU
  584. * (but again only if the system has more than one CPU).
  585. */
  586. void synchronize_rcu_tasks(void)
  587. {
  588. /* Complain if the scheduler has not started. */
  589. RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
  590. "synchronize_rcu_tasks called too soon");
  591. /* Wait for the grace period. */
  592. wait_rcu_gp(call_rcu_tasks);
  593. }
  594. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
  595. /**
  596. * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
  597. *
  598. * Although the current implementation is guaranteed to wait, it is not
  599. * obligated to, for example, if there are no pending callbacks.
  600. */
  601. void rcu_barrier_tasks(void)
  602. {
  603. /* There is only one callback queue, so this is easy. ;-) */
  604. synchronize_rcu_tasks();
  605. }
  606. EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
  607. /* See if tasks are still holding out, complain if so. */
  608. static void check_holdout_task(struct task_struct *t,
  609. bool needreport, bool *firstreport)
  610. {
  611. int cpu;
  612. if (!READ_ONCE(t->rcu_tasks_holdout) ||
  613. t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
  614. !READ_ONCE(t->on_rq) ||
  615. (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
  616. !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
  617. WRITE_ONCE(t->rcu_tasks_holdout, false);
  618. list_del_init(&t->rcu_tasks_holdout_list);
  619. put_task_struct(t);
  620. return;
  621. }
  622. rcu_request_urgent_qs_task(t);
  623. if (!needreport)
  624. return;
  625. if (*firstreport) {
  626. pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
  627. *firstreport = false;
  628. }
  629. cpu = task_cpu(t);
  630. pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
  631. t, ".I"[is_idle_task(t)],
  632. "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
  633. t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
  634. t->rcu_tasks_idle_cpu, cpu);
  635. sched_show_task(t);
  636. }
  637. /* RCU-tasks kthread that detects grace periods and invokes callbacks. */
  638. static int __noreturn rcu_tasks_kthread(void *arg)
  639. {
  640. unsigned long flags;
  641. struct task_struct *g, *t;
  642. unsigned long lastreport;
  643. struct rcu_head *list;
  644. struct rcu_head *next;
  645. LIST_HEAD(rcu_tasks_holdouts);
  646. /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
  647. housekeeping_affine(current, HK_FLAG_RCU);
  648. /*
  649. * Each pass through the following loop makes one check for
  650. * newly arrived callbacks, and, if there are some, waits for
  651. * one RCU-tasks grace period and then invokes the callbacks.
  652. * This loop is terminated by the system going down. ;-)
  653. */
  654. for (;;) {
  655. /* Pick up any new callbacks. */
  656. raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
  657. list = rcu_tasks_cbs_head;
  658. rcu_tasks_cbs_head = NULL;
  659. rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
  660. raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
  661. /* If there were none, wait a bit and start over. */
  662. if (!list) {
  663. wait_event_interruptible(rcu_tasks_cbs_wq,
  664. rcu_tasks_cbs_head);
  665. if (!rcu_tasks_cbs_head) {
  666. WARN_ON(signal_pending(current));
  667. schedule_timeout_interruptible(HZ/10);
  668. }
  669. continue;
  670. }
  671. /*
  672. * Wait for all pre-existing t->on_rq and t->nvcsw
  673. * transitions to complete. Invoking synchronize_sched()
  674. * suffices because all these transitions occur with
  675. * interrupts disabled. Without this synchronize_sched(),
  676. * a read-side critical section that started before the
  677. * grace period might be incorrectly seen as having started
  678. * after the grace period.
  679. *
  680. * This synchronize_sched() also dispenses with the
  681. * need for a memory barrier on the first store to
  682. * ->rcu_tasks_holdout, as it forces the store to happen
  683. * after the beginning of the grace period.
  684. */
  685. synchronize_sched();
  686. /*
  687. * There were callbacks, so we need to wait for an
  688. * RCU-tasks grace period. Start off by scanning
  689. * the task list for tasks that are not already
  690. * voluntarily blocked. Mark these tasks and make
  691. * a list of them in rcu_tasks_holdouts.
  692. */
  693. rcu_read_lock();
  694. for_each_process_thread(g, t) {
  695. if (t != current && READ_ONCE(t->on_rq) &&
  696. !is_idle_task(t)) {
  697. get_task_struct(t);
  698. t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
  699. WRITE_ONCE(t->rcu_tasks_holdout, true);
  700. list_add(&t->rcu_tasks_holdout_list,
  701. &rcu_tasks_holdouts);
  702. }
  703. }
  704. rcu_read_unlock();
  705. /*
  706. * Wait for tasks that are in the process of exiting.
  707. * This does only part of the job, ensuring that all
  708. * tasks that were previously exiting reach the point
  709. * where they have disabled preemption, allowing the
  710. * later synchronize_sched() to finish the job.
  711. */
  712. synchronize_srcu(&tasks_rcu_exit_srcu);
  713. /*
  714. * Each pass through the following loop scans the list
  715. * of holdout tasks, removing any that are no longer
  716. * holdouts. When the list is empty, we are done.
  717. */
  718. lastreport = jiffies;
  719. while (!list_empty(&rcu_tasks_holdouts)) {
  720. bool firstreport;
  721. bool needreport;
  722. int rtst;
  723. struct task_struct *t1;
  724. schedule_timeout_interruptible(HZ);
  725. rtst = READ_ONCE(rcu_task_stall_timeout);
  726. needreport = rtst > 0 &&
  727. time_after(jiffies, lastreport + rtst);
  728. if (needreport)
  729. lastreport = jiffies;
  730. firstreport = true;
  731. WARN_ON(signal_pending(current));
  732. list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
  733. rcu_tasks_holdout_list) {
  734. check_holdout_task(t, needreport, &firstreport);
  735. cond_resched();
  736. }
  737. }
  738. /*
  739. * Because ->on_rq and ->nvcsw are not guaranteed
  740. * to have a full memory barriers prior to them in the
  741. * schedule() path, memory reordering on other CPUs could
  742. * cause their RCU-tasks read-side critical sections to
  743. * extend past the end of the grace period. However,
  744. * because these ->nvcsw updates are carried out with
  745. * interrupts disabled, we can use synchronize_sched()
  746. * to force the needed ordering on all such CPUs.
  747. *
  748. * This synchronize_sched() also confines all
  749. * ->rcu_tasks_holdout accesses to be within the grace
  750. * period, avoiding the need for memory barriers for
  751. * ->rcu_tasks_holdout accesses.
  752. *
  753. * In addition, this synchronize_sched() waits for exiting
  754. * tasks to complete their final preempt_disable() region
  755. * of execution, cleaning up after the synchronize_srcu()
  756. * above.
  757. */
  758. synchronize_sched();
  759. /* Invoke the callbacks. */
  760. while (list) {
  761. next = list->next;
  762. local_bh_disable();
  763. list->func(list);
  764. local_bh_enable();
  765. list = next;
  766. cond_resched();
  767. }
  768. schedule_timeout_uninterruptible(HZ/10);
  769. }
  770. }
  771. /* Spawn rcu_tasks_kthread() at core_initcall() time. */
  772. static int __init rcu_spawn_tasks_kthread(void)
  773. {
  774. struct task_struct *t;
  775. t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
  776. BUG_ON(IS_ERR(t));
  777. smp_mb(); /* Ensure others see full kthread. */
  778. WRITE_ONCE(rcu_tasks_kthread_ptr, t);
  779. return 0;
  780. }
  781. core_initcall(rcu_spawn_tasks_kthread);
  782. /* Do the srcu_read_lock() for the above synchronize_srcu(). */
  783. void exit_tasks_rcu_start(void)
  784. {
  785. preempt_disable();
  786. current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
  787. preempt_enable();
  788. }
  789. /* Do the srcu_read_unlock() for the above synchronize_srcu(). */
  790. void exit_tasks_rcu_finish(void)
  791. {
  792. preempt_disable();
  793. __srcu_read_unlock(&tasks_rcu_exit_srcu, current->rcu_tasks_idx);
  794. preempt_enable();
  795. }
  796. #endif /* #ifdef CONFIG_TASKS_RCU */
  797. #ifndef CONFIG_TINY_RCU
  798. /*
  799. * Print any non-default Tasks RCU settings.
  800. */
  801. static void __init rcu_tasks_bootup_oddness(void)
  802. {
  803. #ifdef CONFIG_TASKS_RCU
  804. if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
  805. pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
  806. else
  807. pr_info("\tTasks RCU enabled.\n");
  808. #endif /* #ifdef CONFIG_TASKS_RCU */
  809. }
  810. #endif /* #ifndef CONFIG_TINY_RCU */
  811. #ifdef CONFIG_PROVE_RCU
  812. /*
  813. * Early boot self test parameters, one for each flavor
  814. */
  815. static bool rcu_self_test;
  816. static bool rcu_self_test_bh;
  817. static bool rcu_self_test_sched;
  818. module_param(rcu_self_test, bool, 0444);
  819. module_param(rcu_self_test_bh, bool, 0444);
  820. module_param(rcu_self_test_sched, bool, 0444);
  821. static int rcu_self_test_counter;
  822. static void test_callback(struct rcu_head *r)
  823. {
  824. rcu_self_test_counter++;
  825. pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
  826. }
  827. static void early_boot_test_call_rcu(void)
  828. {
  829. static struct rcu_head head;
  830. call_rcu(&head, test_callback);
  831. }
  832. static void early_boot_test_call_rcu_bh(void)
  833. {
  834. static struct rcu_head head;
  835. call_rcu_bh(&head, test_callback);
  836. }
  837. static void early_boot_test_call_rcu_sched(void)
  838. {
  839. static struct rcu_head head;
  840. call_rcu_sched(&head, test_callback);
  841. }
  842. void rcu_early_boot_tests(void)
  843. {
  844. pr_info("Running RCU self tests\n");
  845. if (rcu_self_test)
  846. early_boot_test_call_rcu();
  847. if (rcu_self_test_bh)
  848. early_boot_test_call_rcu_bh();
  849. if (rcu_self_test_sched)
  850. early_boot_test_call_rcu_sched();
  851. rcu_test_sync_prims();
  852. }
  853. static int rcu_verify_early_boot_tests(void)
  854. {
  855. int ret = 0;
  856. int early_boot_test_counter = 0;
  857. if (rcu_self_test) {
  858. early_boot_test_counter++;
  859. rcu_barrier();
  860. }
  861. if (rcu_self_test_bh) {
  862. early_boot_test_counter++;
  863. rcu_barrier_bh();
  864. }
  865. if (rcu_self_test_sched) {
  866. early_boot_test_counter++;
  867. rcu_barrier_sched();
  868. }
  869. if (rcu_self_test_counter != early_boot_test_counter) {
  870. WARN_ON(1);
  871. ret = -1;
  872. }
  873. return ret;
  874. }
  875. late_initcall(rcu_verify_early_boot_tests);
  876. #else
  877. void rcu_early_boot_tests(void) {}
  878. #endif /* CONFIG_PROVE_RCU */
  879. #ifndef CONFIG_TINY_RCU
  880. /*
  881. * Print any significant non-default boot-time settings.
  882. */
  883. void __init rcupdate_announce_bootup_oddness(void)
  884. {
  885. if (rcu_normal)
  886. pr_info("\tNo expedited grace period (rcu_normal).\n");
  887. else if (rcu_normal_after_boot)
  888. pr_info("\tNo expedited grace period (rcu_normal_after_boot).\n");
  889. else if (rcu_expedited)
  890. pr_info("\tAll grace periods are expedited (rcu_expedited).\n");
  891. if (rcu_cpu_stall_suppress)
  892. pr_info("\tRCU CPU stall warnings suppressed (rcu_cpu_stall_suppress).\n");
  893. if (rcu_cpu_stall_timeout != CONFIG_RCU_CPU_STALL_TIMEOUT)
  894. pr_info("\tRCU CPU stall warnings timeout set to %d (rcu_cpu_stall_timeout).\n", rcu_cpu_stall_timeout);
  895. rcu_tasks_bootup_oddness();
  896. }
  897. #endif /* #ifndef CONFIG_TINY_RCU */