update.c 32 KB

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