tree_plugin.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  4. * Internal non-public definitions that provide either classic
  5. * or preemptible semantics.
  6. *
  7. * Copyright Red Hat, 2009
  8. * Copyright IBM Corporation, 2009
  9. *
  10. * Author: Ingo Molnar <mingo@elte.hu>
  11. * Paul E. McKenney <paulmck@linux.ibm.com>
  12. */
  13. #include "../locking/rtmutex_common.h"
  14. static bool rcu_rdp_is_offloaded(struct rcu_data *rdp)
  15. {
  16. /*
  17. * In order to read the offloaded state of an rdp in a safe
  18. * and stable way and prevent from its value to be changed
  19. * under us, we must either hold the barrier mutex, the cpu
  20. * hotplug lock (read or write) or the nocb lock. Local
  21. * non-preemptible reads are also safe. NOCB kthreads and
  22. * timers have their own means of synchronization against the
  23. * offloaded state updaters.
  24. */
  25. RCU_NOCB_LOCKDEP_WARN(
  26. !(lockdep_is_held(&rcu_state.barrier_mutex) ||
  27. (IS_ENABLED(CONFIG_HOTPLUG_CPU) && lockdep_is_cpus_held()) ||
  28. lockdep_is_held(&rdp->nocb_lock) ||
  29. lockdep_is_held(&rcu_state.nocb_mutex) ||
  30. (!(IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible()) &&
  31. rdp == this_cpu_ptr(&rcu_data)) ||
  32. rcu_current_is_nocb_kthread(rdp)),
  33. "Unsafe read of RCU_NOCB offloaded state"
  34. );
  35. return rcu_segcblist_is_offloaded(&rdp->cblist);
  36. }
  37. /*
  38. * Check the RCU kernel configuration parameters and print informative
  39. * messages about anything out of the ordinary.
  40. */
  41. static void __init rcu_bootup_announce_oddness(void)
  42. {
  43. if (IS_ENABLED(CONFIG_RCU_TRACE))
  44. pr_info("\tRCU event tracing is enabled.\n");
  45. if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
  46. (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
  47. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
  48. RCU_FANOUT);
  49. if (rcu_fanout_exact)
  50. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  51. if (IS_ENABLED(CONFIG_PROVE_RCU))
  52. pr_info("\tRCU lockdep checking is enabled.\n");
  53. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  54. pr_info("\tRCU strict (and thus non-scalable) grace periods are enabled.\n");
  55. if (RCU_NUM_LVLS >= 4)
  56. pr_info("\tFour(or more)-level hierarchy is enabled.\n");
  57. if (RCU_FANOUT_LEAF != 16)
  58. pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
  59. RCU_FANOUT_LEAF);
  60. if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
  61. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
  62. rcu_fanout_leaf);
  63. if (nr_cpu_ids != NR_CPUS)
  64. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
  65. #ifdef CONFIG_RCU_BOOST
  66. pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
  67. kthread_prio, CONFIG_RCU_BOOST_DELAY);
  68. #endif
  69. if (blimit != DEFAULT_RCU_BLIMIT)
  70. pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit);
  71. if (qhimark != DEFAULT_RCU_QHIMARK)
  72. pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark);
  73. if (qlowmark != DEFAULT_RCU_QLOMARK)
  74. pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark);
  75. if (qovld != DEFAULT_RCU_QOVLD)
  76. pr_info("\tBoot-time adjustment of callback overload level to %ld.\n", qovld);
  77. if (jiffies_till_first_fqs != ULONG_MAX)
  78. pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs);
  79. if (jiffies_till_next_fqs != ULONG_MAX)
  80. pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs);
  81. if (jiffies_till_sched_qs != ULONG_MAX)
  82. pr_info("\tBoot-time adjustment of scheduler-enlistment delay to %ld jiffies.\n", jiffies_till_sched_qs);
  83. if (rcu_kick_kthreads)
  84. pr_info("\tKick kthreads if too-long grace period.\n");
  85. if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
  86. pr_info("\tRCU callback double-/use-after-free debug is enabled.\n");
  87. if (gp_preinit_delay)
  88. pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
  89. if (gp_init_delay)
  90. pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
  91. if (gp_cleanup_delay)
  92. pr_info("\tRCU debug GP cleanup slowdown %d jiffies.\n", gp_cleanup_delay);
  93. if (nohz_full_patience_delay < 0) {
  94. pr_info("\tRCU NOCB CPU patience negative (%d), resetting to zero.\n", nohz_full_patience_delay);
  95. nohz_full_patience_delay = 0;
  96. } else if (nohz_full_patience_delay > 5 * MSEC_PER_SEC) {
  97. pr_info("\tRCU NOCB CPU patience too large (%d), resetting to %ld.\n", nohz_full_patience_delay, 5 * MSEC_PER_SEC);
  98. nohz_full_patience_delay = 5 * MSEC_PER_SEC;
  99. } else if (nohz_full_patience_delay) {
  100. pr_info("\tRCU NOCB CPU patience set to %d milliseconds.\n", nohz_full_patience_delay);
  101. }
  102. nohz_full_patience_delay_jiffies = msecs_to_jiffies(nohz_full_patience_delay);
  103. if (!use_softirq)
  104. pr_info("\tRCU_SOFTIRQ processing moved to rcuc kthreads.\n");
  105. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG))
  106. pr_info("\tRCU debug extended QS entry/exit.\n");
  107. rcupdate_announce_bootup_oddness();
  108. }
  109. #ifdef CONFIG_PREEMPT_RCU
  110. static void rcu_report_exp_rnp(struct rcu_node *rnp, bool wake);
  111. static void rcu_read_unlock_special(struct task_struct *t);
  112. /*
  113. * Tell them what RCU they are running.
  114. */
  115. static void __init rcu_bootup_announce(void)
  116. {
  117. pr_info("Preemptible hierarchical RCU implementation.\n");
  118. rcu_bootup_announce_oddness();
  119. }
  120. /* Flags for rcu_preempt_ctxt_queue() decision table. */
  121. #define RCU_GP_TASKS 0x8
  122. #define RCU_EXP_TASKS 0x4
  123. #define RCU_GP_BLKD 0x2
  124. #define RCU_EXP_BLKD 0x1
  125. /*
  126. * Queues a task preempted within an RCU-preempt read-side critical
  127. * section into the appropriate location within the ->blkd_tasks list,
  128. * depending on the states of any ongoing normal and expedited grace
  129. * periods. The ->gp_tasks pointer indicates which element the normal
  130. * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
  131. * indicates which element the expedited grace period is waiting on (again,
  132. * NULL if none). If a grace period is waiting on a given element in the
  133. * ->blkd_tasks list, it also waits on all subsequent elements. Thus,
  134. * adding a task to the tail of the list blocks any grace period that is
  135. * already waiting on one of the elements. In contrast, adding a task
  136. * to the head of the list won't block any grace period that is already
  137. * waiting on one of the elements.
  138. *
  139. * This queuing is imprecise, and can sometimes make an ongoing grace
  140. * period wait for a task that is not strictly speaking blocking it.
  141. * Given the choice, we needlessly block a normal grace period rather than
  142. * blocking an expedited grace period.
  143. *
  144. * Note that an endless sequence of expedited grace periods still cannot
  145. * indefinitely postpone a normal grace period. Eventually, all of the
  146. * fixed number of preempted tasks blocking the normal grace period that are
  147. * not also blocking the expedited grace period will resume and complete
  148. * their RCU read-side critical sections. At that point, the ->gp_tasks
  149. * pointer will equal the ->exp_tasks pointer, at which point the end of
  150. * the corresponding expedited grace period will also be the end of the
  151. * normal grace period.
  152. */
  153. static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
  154. __releases(rnp->lock) /* But leaves rrupts disabled. */
  155. {
  156. int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
  157. (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
  158. (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
  159. (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
  160. struct task_struct *t = current;
  161. raw_lockdep_assert_held_rcu_node(rnp);
  162. WARN_ON_ONCE(rdp->mynode != rnp);
  163. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  164. /* RCU better not be waiting on newly onlined CPUs! */
  165. WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
  166. rdp->grpmask);
  167. /*
  168. * Decide where to queue the newly blocked task. In theory,
  169. * this could be an if-statement. In practice, when I tried
  170. * that, it was quite messy.
  171. */
  172. switch (blkd_state) {
  173. case 0:
  174. case RCU_EXP_TASKS:
  175. case RCU_EXP_TASKS + RCU_GP_BLKD:
  176. case RCU_GP_TASKS:
  177. case RCU_GP_TASKS + RCU_EXP_TASKS:
  178. /*
  179. * Blocking neither GP, or first task blocking the normal
  180. * GP but not blocking the already-waiting expedited GP.
  181. * Queue at the head of the list to avoid unnecessarily
  182. * blocking the already-waiting GPs.
  183. */
  184. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  185. break;
  186. case RCU_EXP_BLKD:
  187. case RCU_GP_BLKD:
  188. case RCU_GP_BLKD + RCU_EXP_BLKD:
  189. case RCU_GP_TASKS + RCU_EXP_BLKD:
  190. case RCU_GP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  191. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  192. /*
  193. * First task arriving that blocks either GP, or first task
  194. * arriving that blocks the expedited GP (with the normal
  195. * GP already waiting), or a task arriving that blocks
  196. * both GPs with both GPs already waiting. Queue at the
  197. * tail of the list to avoid any GP waiting on any of the
  198. * already queued tasks that are not blocking it.
  199. */
  200. list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
  201. break;
  202. case RCU_EXP_TASKS + RCU_EXP_BLKD:
  203. case RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  204. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_EXP_BLKD:
  205. /*
  206. * Second or subsequent task blocking the expedited GP.
  207. * The task either does not block the normal GP, or is the
  208. * first task blocking the normal GP. Queue just after
  209. * the first task blocking the expedited GP.
  210. */
  211. list_add(&t->rcu_node_entry, rnp->exp_tasks);
  212. break;
  213. case RCU_GP_TASKS + RCU_GP_BLKD:
  214. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
  215. /*
  216. * Second or subsequent task blocking the normal GP.
  217. * The task does not block the expedited GP. Queue just
  218. * after the first task blocking the normal GP.
  219. */
  220. list_add(&t->rcu_node_entry, rnp->gp_tasks);
  221. break;
  222. default:
  223. /* Yet another exercise in excessive paranoia. */
  224. WARN_ON_ONCE(1);
  225. break;
  226. }
  227. /*
  228. * We have now queued the task. If it was the first one to
  229. * block either grace period, update the ->gp_tasks and/or
  230. * ->exp_tasks pointers, respectively, to reference the newly
  231. * blocked tasks.
  232. */
  233. if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
  234. WRITE_ONCE(rnp->gp_tasks, &t->rcu_node_entry);
  235. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
  236. }
  237. if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
  238. WRITE_ONCE(rnp->exp_tasks, &t->rcu_node_entry);
  239. WARN_ON_ONCE(!(blkd_state & RCU_GP_BLKD) !=
  240. !(rnp->qsmask & rdp->grpmask));
  241. WARN_ON_ONCE(!(blkd_state & RCU_EXP_BLKD) !=
  242. !(rnp->expmask & rdp->grpmask));
  243. raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
  244. /*
  245. * Report the quiescent state for the expedited GP. This expedited
  246. * GP should not be able to end until we report, so there should be
  247. * no need to check for a subsequent expedited GP. (Though we are
  248. * still in a quiescent state in any case.)
  249. *
  250. * Interrupts are disabled, so ->cpu_no_qs.b.exp cannot change.
  251. */
  252. if (blkd_state & RCU_EXP_BLKD && rdp->cpu_no_qs.b.exp)
  253. rcu_report_exp_rdp(rdp);
  254. else
  255. WARN_ON_ONCE(rdp->cpu_no_qs.b.exp);
  256. }
  257. /*
  258. * Record a preemptible-RCU quiescent state for the specified CPU.
  259. * Note that this does not necessarily mean that the task currently running
  260. * on the CPU is in a quiescent state: Instead, it means that the current
  261. * grace period need not wait on any RCU read-side critical section that
  262. * starts later on this CPU. It also means that if the current task is
  263. * in an RCU read-side critical section, it has already added itself to
  264. * some leaf rcu_node structure's ->blkd_tasks list. In addition to the
  265. * current task, there might be any number of other tasks blocked while
  266. * in an RCU read-side critical section.
  267. *
  268. * Unlike non-preemptible-RCU, quiescent state reports for expedited
  269. * grace periods are handled separately via deferred quiescent states
  270. * and context switch events.
  271. *
  272. * Callers to this function must disable preemption.
  273. */
  274. static void rcu_qs(void)
  275. {
  276. RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!\n");
  277. if (__this_cpu_read(rcu_data.cpu_no_qs.b.norm)) {
  278. trace_rcu_grace_period(TPS("rcu_preempt"),
  279. __this_cpu_read(rcu_data.gp_seq),
  280. TPS("cpuqs"));
  281. __this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
  282. barrier(); /* Coordinate with rcu_flavor_sched_clock_irq(). */
  283. WRITE_ONCE(current->rcu_read_unlock_special.b.need_qs, false);
  284. }
  285. }
  286. /*
  287. * We have entered the scheduler, and the current task might soon be
  288. * context-switched away from. If this task is in an RCU read-side
  289. * critical section, we will no longer be able to rely on the CPU to
  290. * record that fact, so we enqueue the task on the blkd_tasks list.
  291. * The task will dequeue itself when it exits the outermost enclosing
  292. * RCU read-side critical section. Therefore, the current grace period
  293. * cannot be permitted to complete until the blkd_tasks list entries
  294. * predating the current grace period drain, in other words, until
  295. * rnp->gp_tasks becomes NULL.
  296. *
  297. * Caller must disable interrupts.
  298. */
  299. void rcu_note_context_switch(bool preempt)
  300. {
  301. struct task_struct *t = current;
  302. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  303. struct rcu_node *rnp;
  304. trace_rcu_utilization(TPS("Start context switch"));
  305. lockdep_assert_irqs_disabled();
  306. WARN_ONCE(!preempt && rcu_preempt_depth() > 0, "Voluntary context switch within RCU read-side critical section!");
  307. if (rcu_preempt_depth() > 0 &&
  308. !t->rcu_read_unlock_special.b.blocked) {
  309. /* Possibly blocking in an RCU read-side critical section. */
  310. rnp = rdp->mynode;
  311. raw_spin_lock_rcu_node(rnp);
  312. t->rcu_read_unlock_special.b.blocked = true;
  313. t->rcu_blocked_node = rnp;
  314. /*
  315. * Verify the CPU's sanity, trace the preemption, and
  316. * then queue the task as required based on the states
  317. * of any ongoing and expedited grace periods.
  318. */
  319. WARN_ON_ONCE(!rcu_rdp_cpu_online(rdp));
  320. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  321. trace_rcu_preempt_task(rcu_state.name,
  322. t->pid,
  323. (rnp->qsmask & rdp->grpmask)
  324. ? rnp->gp_seq
  325. : rcu_seq_snap(&rnp->gp_seq));
  326. rcu_preempt_ctxt_queue(rnp, rdp);
  327. } else {
  328. rcu_preempt_deferred_qs(t);
  329. }
  330. /*
  331. * Either we were not in an RCU read-side critical section to
  332. * begin with, or we have now recorded that critical section
  333. * globally. Either way, we can now note a quiescent state
  334. * for this CPU. Again, if we were in an RCU read-side critical
  335. * section, and if that critical section was blocking the current
  336. * grace period, then the fact that the task has been enqueued
  337. * means that we continue to block the current grace period.
  338. */
  339. rcu_qs();
  340. if (rdp->cpu_no_qs.b.exp)
  341. rcu_report_exp_rdp(rdp);
  342. rcu_tasks_qs(current, preempt);
  343. trace_rcu_utilization(TPS("End context switch"));
  344. }
  345. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  346. /*
  347. * Check for preempted RCU readers blocking the current grace period
  348. * for the specified rcu_node structure. If the caller needs a reliable
  349. * answer, it must hold the rcu_node's ->lock.
  350. */
  351. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  352. {
  353. return READ_ONCE(rnp->gp_tasks) != NULL;
  354. }
  355. /* limit value for ->rcu_read_lock_nesting. */
  356. #define RCU_NEST_PMAX (INT_MAX / 2)
  357. static void rcu_preempt_read_enter(void)
  358. {
  359. WRITE_ONCE(current->rcu_read_lock_nesting, READ_ONCE(current->rcu_read_lock_nesting) + 1);
  360. }
  361. static int rcu_preempt_read_exit(void)
  362. {
  363. int ret = READ_ONCE(current->rcu_read_lock_nesting) - 1;
  364. WRITE_ONCE(current->rcu_read_lock_nesting, ret);
  365. return ret;
  366. }
  367. static void rcu_preempt_depth_set(int val)
  368. {
  369. WRITE_ONCE(current->rcu_read_lock_nesting, val);
  370. }
  371. /*
  372. * Preemptible RCU implementation for rcu_read_lock().
  373. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  374. * if we block.
  375. */
  376. void __rcu_read_lock(void)
  377. {
  378. rcu_preempt_read_enter();
  379. if (IS_ENABLED(CONFIG_PROVE_LOCKING))
  380. WARN_ON_ONCE(rcu_preempt_depth() > RCU_NEST_PMAX);
  381. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) && rcu_state.gp_kthread)
  382. WRITE_ONCE(current->rcu_read_unlock_special.b.need_qs, true);
  383. barrier(); /* critical section after entry code. */
  384. }
  385. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  386. /*
  387. * Preemptible RCU implementation for rcu_read_unlock().
  388. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  389. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  390. * invoke rcu_read_unlock_special() to clean up after a context switch
  391. * in an RCU read-side critical section and other special cases.
  392. */
  393. void __rcu_read_unlock(void)
  394. {
  395. struct task_struct *t = current;
  396. barrier(); // critical section before exit code.
  397. if (rcu_preempt_read_exit() == 0) {
  398. barrier(); // critical-section exit before .s check.
  399. if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
  400. rcu_read_unlock_special(t);
  401. }
  402. if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
  403. int rrln = rcu_preempt_depth();
  404. WARN_ON_ONCE(rrln < 0 || rrln > RCU_NEST_PMAX);
  405. }
  406. }
  407. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  408. /*
  409. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  410. * returning NULL if at the end of the list.
  411. */
  412. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  413. struct rcu_node *rnp)
  414. {
  415. struct list_head *np;
  416. np = t->rcu_node_entry.next;
  417. if (np == &rnp->blkd_tasks)
  418. np = NULL;
  419. return np;
  420. }
  421. /*
  422. * Return true if the specified rcu_node structure has tasks that were
  423. * preempted within an RCU read-side critical section.
  424. */
  425. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  426. {
  427. return !list_empty(&rnp->blkd_tasks);
  428. }
  429. /*
  430. * Report deferred quiescent states. The deferral time can
  431. * be quite short, for example, in the case of the call from
  432. * rcu_read_unlock_special().
  433. */
  434. static notrace void
  435. rcu_preempt_deferred_qs_irqrestore(struct task_struct *t, unsigned long flags)
  436. {
  437. bool empty_exp;
  438. bool empty_norm;
  439. bool empty_exp_now;
  440. struct list_head *np;
  441. bool drop_boost_mutex = false;
  442. struct rcu_data *rdp;
  443. struct rcu_node *rnp;
  444. union rcu_special special;
  445. rdp = this_cpu_ptr(&rcu_data);
  446. if (rdp->defer_qs_iw_pending == DEFER_QS_PENDING)
  447. rdp->defer_qs_iw_pending = DEFER_QS_IDLE;
  448. /*
  449. * If RCU core is waiting for this CPU to exit its critical section,
  450. * report the fact that it has exited. Because irqs are disabled,
  451. * t->rcu_read_unlock_special cannot change.
  452. */
  453. special = t->rcu_read_unlock_special;
  454. if (!special.s && !rdp->cpu_no_qs.b.exp) {
  455. local_irq_restore(flags);
  456. return;
  457. }
  458. t->rcu_read_unlock_special.s = 0;
  459. if (special.b.need_qs) {
  460. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) {
  461. rdp->cpu_no_qs.b.norm = false;
  462. rcu_report_qs_rdp(rdp);
  463. udelay(rcu_unlock_delay);
  464. } else {
  465. rcu_qs();
  466. }
  467. }
  468. /*
  469. * Respond to a request by an expedited grace period for a
  470. * quiescent state from this CPU. Note that requests from
  471. * tasks are handled when removing the task from the
  472. * blocked-tasks list below.
  473. */
  474. if (rdp->cpu_no_qs.b.exp)
  475. rcu_report_exp_rdp(rdp);
  476. /* Clean up if blocked during RCU read-side critical section. */
  477. if (special.b.blocked) {
  478. /*
  479. * Remove this task from the list it blocked on. The task
  480. * now remains queued on the rcu_node corresponding to the
  481. * CPU it first blocked on, so there is no longer any need
  482. * to loop. Retain a WARN_ON_ONCE() out of sheer paranoia.
  483. */
  484. rnp = t->rcu_blocked_node;
  485. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  486. WARN_ON_ONCE(rnp != t->rcu_blocked_node);
  487. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  488. empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
  489. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
  490. (!empty_norm || rnp->qsmask));
  491. empty_exp = sync_rcu_exp_done(rnp);
  492. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  493. np = rcu_next_node_entry(t, rnp);
  494. list_del_init(&t->rcu_node_entry);
  495. t->rcu_blocked_node = NULL;
  496. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  497. rnp->gp_seq, t->pid);
  498. if (&t->rcu_node_entry == rnp->gp_tasks)
  499. WRITE_ONCE(rnp->gp_tasks, np);
  500. if (&t->rcu_node_entry == rnp->exp_tasks)
  501. WRITE_ONCE(rnp->exp_tasks, np);
  502. if (IS_ENABLED(CONFIG_RCU_BOOST)) {
  503. /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
  504. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx.rtmutex) == t;
  505. if (&t->rcu_node_entry == rnp->boost_tasks)
  506. WRITE_ONCE(rnp->boost_tasks, np);
  507. }
  508. /*
  509. * If this was the last task on the current list, and if
  510. * we aren't waiting on any CPUs, report the quiescent state.
  511. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  512. * so we must take a snapshot of the expedited state.
  513. */
  514. empty_exp_now = sync_rcu_exp_done(rnp);
  515. if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
  516. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  517. rnp->gp_seq,
  518. 0, rnp->qsmask,
  519. rnp->level,
  520. rnp->grplo,
  521. rnp->grphi,
  522. !!rnp->gp_tasks);
  523. rcu_report_unblock_qs_rnp(rnp, flags);
  524. } else {
  525. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  526. }
  527. /*
  528. * If this was the last task on the expedited lists,
  529. * then we need to report up the rcu_node hierarchy.
  530. */
  531. if (!empty_exp && empty_exp_now)
  532. rcu_report_exp_rnp(rnp, true);
  533. /* Unboost if we were boosted. */
  534. if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
  535. rt_mutex_futex_unlock(&rnp->boost_mtx.rtmutex);
  536. } else {
  537. local_irq_restore(flags);
  538. }
  539. }
  540. /*
  541. * Is a deferred quiescent-state pending, and are we also not in
  542. * an RCU read-side critical section? It is the caller's responsibility
  543. * to ensure it is otherwise safe to report any deferred quiescent
  544. * states. The reason for this is that it is safe to report a
  545. * quiescent state during context switch even though preemption
  546. * is disabled. This function cannot be expected to understand these
  547. * nuances, so the caller must handle them.
  548. */
  549. static notrace bool rcu_preempt_need_deferred_qs(struct task_struct *t)
  550. {
  551. return (__this_cpu_read(rcu_data.cpu_no_qs.b.exp) ||
  552. READ_ONCE(t->rcu_read_unlock_special.s)) &&
  553. rcu_preempt_depth() == 0;
  554. }
  555. /*
  556. * Report a deferred quiescent state if needed and safe to do so.
  557. * As with rcu_preempt_need_deferred_qs(), "safe" involves only
  558. * not being in an RCU read-side critical section. The caller must
  559. * evaluate safety in terms of interrupt, softirq, and preemption
  560. * disabling.
  561. */
  562. notrace void rcu_preempt_deferred_qs(struct task_struct *t)
  563. {
  564. unsigned long flags;
  565. if (!rcu_preempt_need_deferred_qs(t))
  566. return;
  567. local_irq_save(flags);
  568. rcu_preempt_deferred_qs_irqrestore(t, flags);
  569. }
  570. /*
  571. * Minimal handler to give the scheduler a chance to re-evaluate.
  572. */
  573. static void rcu_preempt_deferred_qs_handler(struct irq_work *iwp)
  574. {
  575. unsigned long flags;
  576. struct rcu_data *rdp;
  577. rdp = container_of(iwp, struct rcu_data, defer_qs_iw);
  578. local_irq_save(flags);
  579. /*
  580. * If the IRQ work handler happens to run in the middle of RCU read-side
  581. * critical section, it could be ineffective in getting the scheduler's
  582. * attention to report a deferred quiescent state (the whole point of the
  583. * IRQ work). For this reason, requeue the IRQ work.
  584. *
  585. * Basically, we want to avoid following situation:
  586. * 1. rcu_read_unlock() queues IRQ work (state -> DEFER_QS_PENDING)
  587. * 2. CPU enters new rcu_read_lock()
  588. * 3. IRQ work runs but cannot report QS due to rcu_preempt_depth() > 0
  589. * 4. rcu_read_unlock() does not re-queue work (state still PENDING)
  590. * 5. Deferred QS reporting does not happen.
  591. */
  592. if (rcu_preempt_depth() > 0)
  593. WRITE_ONCE(rdp->defer_qs_iw_pending, DEFER_QS_IDLE);
  594. local_irq_restore(flags);
  595. }
  596. /*
  597. * Handle special cases during rcu_read_unlock(), such as needing to
  598. * notify RCU core processing or task having blocked during the RCU
  599. * read-side critical section.
  600. */
  601. static void rcu_read_unlock_special(struct task_struct *t)
  602. {
  603. unsigned long flags;
  604. bool irqs_were_disabled;
  605. bool preempt_bh_were_disabled =
  606. !!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
  607. /* NMI handlers cannot block and cannot safely manipulate state. */
  608. if (in_nmi())
  609. return;
  610. local_irq_save(flags);
  611. irqs_were_disabled = irqs_disabled_flags(flags);
  612. if (preempt_bh_were_disabled || irqs_were_disabled) {
  613. bool expboost; // Expedited GP in flight or possible boosting.
  614. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  615. struct rcu_node *rnp = rdp->mynode;
  616. expboost = (t->rcu_blocked_node && READ_ONCE(t->rcu_blocked_node->exp_tasks)) ||
  617. (rdp->grpmask & READ_ONCE(rnp->expmask)) ||
  618. (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) &&
  619. ((rdp->grpmask & READ_ONCE(rnp->qsmask)) || t->rcu_blocked_node)) ||
  620. (IS_ENABLED(CONFIG_RCU_BOOST) && irqs_were_disabled &&
  621. t->rcu_blocked_node);
  622. // Need to defer quiescent state until everything is enabled.
  623. if (use_softirq && (in_hardirq() || (expboost && !irqs_were_disabled))) {
  624. // Using softirq, safe to awaken, and either the
  625. // wakeup is free or there is either an expedited
  626. // GP in flight or a potential need to deboost.
  627. raise_softirq_irqoff(RCU_SOFTIRQ);
  628. } else {
  629. // Enabling BH or preempt does reschedule, so...
  630. // Also if no expediting and no possible deboosting,
  631. // slow is OK. Plus nohz_full CPUs eventually get
  632. // tick enabled.
  633. set_tsk_need_resched(current);
  634. set_preempt_need_resched();
  635. if (IS_ENABLED(CONFIG_IRQ_WORK) && irqs_were_disabled &&
  636. expboost && rdp->defer_qs_iw_pending != DEFER_QS_PENDING &&
  637. cpu_online(rdp->cpu)) {
  638. // Get scheduler to re-evaluate and call hooks.
  639. // If !IRQ_WORK, FQS scan will eventually IPI.
  640. rdp->defer_qs_iw_pending = DEFER_QS_PENDING;
  641. irq_work_queue_on(&rdp->defer_qs_iw, rdp->cpu);
  642. }
  643. }
  644. local_irq_restore(flags);
  645. return;
  646. }
  647. rcu_preempt_deferred_qs_irqrestore(t, flags);
  648. }
  649. /*
  650. * Check that the list of blocked tasks for the newly completed grace
  651. * period is in fact empty. It is a serious bug to complete a grace
  652. * period that still has RCU readers blocked! This function must be
  653. * invoked -before- updating this rnp's ->gp_seq.
  654. *
  655. * Also, if there are blocked tasks on the list, they automatically
  656. * block the newly created grace period, so set up ->gp_tasks accordingly.
  657. */
  658. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  659. {
  660. struct task_struct *t;
  661. RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
  662. raw_lockdep_assert_held_rcu_node(rnp);
  663. if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
  664. dump_blkd_tasks(rnp, 10);
  665. if (rcu_preempt_has_tasks(rnp) &&
  666. (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
  667. WRITE_ONCE(rnp->gp_tasks, rnp->blkd_tasks.next);
  668. t = container_of(rnp->gp_tasks, struct task_struct,
  669. rcu_node_entry);
  670. trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
  671. rnp->gp_seq, t->pid);
  672. }
  673. WARN_ON_ONCE(rnp->qsmask);
  674. }
  675. /*
  676. * Check for a quiescent state from the current CPU, including voluntary
  677. * context switches for Tasks RCU. When a task blocks, the task is
  678. * recorded in the corresponding CPU's rcu_node structure, which is checked
  679. * elsewhere, hence this function need only check for quiescent states
  680. * related to the current CPU, not to those related to tasks.
  681. */
  682. static void rcu_flavor_sched_clock_irq(int user)
  683. {
  684. struct task_struct *t = current;
  685. lockdep_assert_irqs_disabled();
  686. if (rcu_preempt_depth() > 0 ||
  687. (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
  688. /* No QS, force context switch if deferred. */
  689. if (rcu_preempt_need_deferred_qs(t)) {
  690. set_tsk_need_resched(t);
  691. set_preempt_need_resched();
  692. }
  693. } else if (rcu_preempt_need_deferred_qs(t)) {
  694. rcu_preempt_deferred_qs(t); /* Report deferred QS. */
  695. return;
  696. } else if (!WARN_ON_ONCE(rcu_preempt_depth())) {
  697. rcu_qs(); /* Report immediate QS. */
  698. return;
  699. }
  700. /* If GP is oldish, ask for help from rcu_read_unlock_special(). */
  701. if (rcu_preempt_depth() > 0 &&
  702. __this_cpu_read(rcu_data.core_needs_qs) &&
  703. __this_cpu_read(rcu_data.cpu_no_qs.b.norm) &&
  704. !t->rcu_read_unlock_special.b.need_qs &&
  705. time_after(jiffies, rcu_state.gp_start + HZ))
  706. t->rcu_read_unlock_special.b.need_qs = true;
  707. }
  708. /*
  709. * Check for a task exiting while in a preemptible-RCU read-side
  710. * critical section, clean up if so. No need to issue warnings, as
  711. * debug_check_no_locks_held() already does this if lockdep is enabled.
  712. * Besides, if this function does anything other than just immediately
  713. * return, there was a bug of some sort. Spewing warnings from this
  714. * function is like as not to simply obscure important prior warnings.
  715. */
  716. void exit_rcu(void)
  717. {
  718. struct task_struct *t = current;
  719. if (unlikely(!list_empty(&current->rcu_node_entry))) {
  720. rcu_preempt_depth_set(1);
  721. barrier();
  722. WRITE_ONCE(t->rcu_read_unlock_special.b.blocked, true);
  723. } else if (unlikely(rcu_preempt_depth())) {
  724. rcu_preempt_depth_set(1);
  725. } else {
  726. return;
  727. }
  728. __rcu_read_unlock();
  729. rcu_preempt_deferred_qs(current);
  730. }
  731. /*
  732. * Dump the blocked-tasks state, but limit the list dump to the
  733. * specified number of elements.
  734. */
  735. static void
  736. dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
  737. {
  738. int cpu;
  739. int i;
  740. struct list_head *lhp;
  741. struct rcu_data *rdp;
  742. struct rcu_node *rnp1;
  743. raw_lockdep_assert_held_rcu_node(rnp);
  744. pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
  745. __func__, rnp->grplo, rnp->grphi, rnp->level,
  746. (long)READ_ONCE(rnp->gp_seq), (long)rnp->completedqs);
  747. for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
  748. pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
  749. __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
  750. pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
  751. __func__, READ_ONCE(rnp->gp_tasks), data_race(rnp->boost_tasks),
  752. READ_ONCE(rnp->exp_tasks));
  753. pr_info("%s: ->blkd_tasks", __func__);
  754. i = 0;
  755. list_for_each(lhp, &rnp->blkd_tasks) {
  756. pr_cont(" %p", lhp);
  757. if (++i >= ncheck)
  758. break;
  759. }
  760. pr_cont("\n");
  761. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
  762. rdp = per_cpu_ptr(&rcu_data, cpu);
  763. pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
  764. cpu, ".o"[rcu_rdp_cpu_online(rdp)],
  765. (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_state,
  766. (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_state);
  767. }
  768. }
  769. static void rcu_preempt_deferred_qs_init(struct rcu_data *rdp)
  770. {
  771. rdp->defer_qs_iw = IRQ_WORK_INIT_HARD(rcu_preempt_deferred_qs_handler);
  772. }
  773. #else /* #ifdef CONFIG_PREEMPT_RCU */
  774. /*
  775. * If strict grace periods are enabled, and if the calling
  776. * __rcu_read_unlock() marks the beginning of a quiescent state, immediately
  777. * report that quiescent state and, if requested, spin for a bit.
  778. */
  779. void rcu_read_unlock_strict(void)
  780. {
  781. struct rcu_data *rdp;
  782. if (irqs_disabled() || in_atomic_preempt_off() || !rcu_state.gp_kthread)
  783. return;
  784. /*
  785. * rcu_report_qs_rdp() can only be invoked with a stable rdp and
  786. * from the local CPU.
  787. *
  788. * The in_atomic_preempt_off() check ensures that we come here holding
  789. * the last preempt_count (which will get dropped once we return to
  790. * __rcu_read_unlock().
  791. */
  792. rdp = this_cpu_ptr(&rcu_data);
  793. rdp->cpu_no_qs.b.norm = false;
  794. rcu_report_qs_rdp(rdp);
  795. udelay(rcu_unlock_delay);
  796. }
  797. EXPORT_SYMBOL_GPL(rcu_read_unlock_strict);
  798. /*
  799. * Tell them what RCU they are running.
  800. */
  801. static void __init rcu_bootup_announce(void)
  802. {
  803. pr_info("Hierarchical RCU implementation.\n");
  804. rcu_bootup_announce_oddness();
  805. }
  806. /*
  807. * Note a quiescent state for PREEMPTION=n. Because we do not need to know
  808. * how many quiescent states passed, just if there was at least one since
  809. * the start of the grace period, this just sets a flag. The caller must
  810. * have disabled preemption.
  811. */
  812. static void rcu_qs(void)
  813. {
  814. RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!");
  815. if (!__this_cpu_read(rcu_data.cpu_no_qs.s))
  816. return;
  817. trace_rcu_grace_period(TPS("rcu_sched"),
  818. __this_cpu_read(rcu_data.gp_seq), TPS("cpuqs"));
  819. __this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
  820. if (__this_cpu_read(rcu_data.cpu_no_qs.b.exp))
  821. rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
  822. }
  823. /*
  824. * Register an urgently needed quiescent state. If there is an
  825. * emergency, invoke rcu_momentary_eqs() to do a heavy-weight
  826. * dyntick-idle quiescent state visible to other CPUs, which will in
  827. * some cases serve for expedited as well as normal grace periods.
  828. * Either way, register a lightweight quiescent state.
  829. */
  830. void rcu_all_qs(void)
  831. {
  832. unsigned long flags;
  833. if (!raw_cpu_read(rcu_data.rcu_urgent_qs))
  834. return;
  835. preempt_disable(); // For CONFIG_PREEMPT_COUNT=y kernels
  836. /* Load rcu_urgent_qs before other flags. */
  837. if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
  838. preempt_enable();
  839. return;
  840. }
  841. this_cpu_write(rcu_data.rcu_urgent_qs, false);
  842. if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs))) {
  843. local_irq_save(flags);
  844. rcu_momentary_eqs();
  845. local_irq_restore(flags);
  846. }
  847. rcu_qs();
  848. preempt_enable();
  849. }
  850. EXPORT_SYMBOL_GPL(rcu_all_qs);
  851. /*
  852. * Note a PREEMPTION=n context switch. The caller must have disabled interrupts.
  853. */
  854. void rcu_note_context_switch(bool preempt)
  855. {
  856. trace_rcu_utilization(TPS("Start context switch"));
  857. rcu_qs();
  858. /* Load rcu_urgent_qs before other flags. */
  859. if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs)))
  860. goto out;
  861. this_cpu_write(rcu_data.rcu_urgent_qs, false);
  862. if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs)))
  863. rcu_momentary_eqs();
  864. out:
  865. rcu_tasks_qs(current, preempt);
  866. trace_rcu_utilization(TPS("End context switch"));
  867. }
  868. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  869. /*
  870. * Because preemptible RCU does not exist, there are never any preempted
  871. * RCU readers.
  872. */
  873. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  874. {
  875. return 0;
  876. }
  877. /*
  878. * Because there is no preemptible RCU, there can be no readers blocked.
  879. */
  880. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  881. {
  882. return false;
  883. }
  884. /*
  885. * Because there is no preemptible RCU, there can be no deferred quiescent
  886. * states.
  887. */
  888. static notrace bool rcu_preempt_need_deferred_qs(struct task_struct *t)
  889. {
  890. return false;
  891. }
  892. // Except that we do need to respond to a request by an expedited
  893. // grace period for a quiescent state from this CPU. Note that in
  894. // non-preemptible kernels, there can be no context switches within RCU
  895. // read-side critical sections, which in turn means that the leaf rcu_node
  896. // structure's blocked-tasks list is always empty. is therefore no need to
  897. // actually check it. Instead, a quiescent state from this CPU suffices,
  898. // and this function is only called from such a quiescent state.
  899. notrace void rcu_preempt_deferred_qs(struct task_struct *t)
  900. {
  901. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  902. if (READ_ONCE(rdp->cpu_no_qs.b.exp))
  903. rcu_report_exp_rdp(rdp);
  904. }
  905. /*
  906. * Because there is no preemptible RCU, there can be no readers blocked,
  907. * so there is no need to check for blocked tasks. So check only for
  908. * bogus qsmask values.
  909. */
  910. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  911. {
  912. WARN_ON_ONCE(rnp->qsmask);
  913. }
  914. /*
  915. * Check to see if this CPU is in a non-context-switch quiescent state,
  916. * namely user mode and idle loop.
  917. */
  918. static void rcu_flavor_sched_clock_irq(int user)
  919. {
  920. if (user || rcu_is_cpu_rrupt_from_idle() ||
  921. (IS_ENABLED(CONFIG_PREEMPT_COUNT) &&
  922. (preempt_count() == HARDIRQ_OFFSET))) {
  923. /*
  924. * Get here if this CPU took its interrupt from user
  925. * mode, from the idle loop without this being a nested
  926. * interrupt, or while not holding the task preempt count
  927. * (with PREEMPT_COUNT=y). In this case, the CPU is in a
  928. * quiescent state, so note it.
  929. *
  930. * No memory barrier is required here because rcu_qs()
  931. * references only CPU-local variables that other CPUs
  932. * neither access nor modify, at least not while the
  933. * corresponding CPU is online.
  934. */
  935. rcu_qs();
  936. }
  937. }
  938. /*
  939. * Because preemptible RCU does not exist, tasks cannot possibly exit
  940. * while in preemptible RCU read-side critical sections.
  941. */
  942. void exit_rcu(void)
  943. {
  944. }
  945. /*
  946. * Dump the guaranteed-empty blocked-tasks state. Trust but verify.
  947. */
  948. static void
  949. dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
  950. {
  951. WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
  952. }
  953. static void rcu_preempt_deferred_qs_init(struct rcu_data *rdp) { }
  954. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  955. /*
  956. * If boosting, set rcuc kthreads to realtime priority.
  957. */
  958. static void rcu_cpu_kthread_setup(unsigned int cpu)
  959. {
  960. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  961. #ifdef CONFIG_RCU_BOOST
  962. struct sched_param sp;
  963. sp.sched_priority = kthread_prio;
  964. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  965. #endif /* #ifdef CONFIG_RCU_BOOST */
  966. WRITE_ONCE(rdp->rcuc_activity, jiffies);
  967. }
  968. static bool rcu_is_callbacks_nocb_kthread(struct rcu_data *rdp)
  969. {
  970. #ifdef CONFIG_RCU_NOCB_CPU
  971. return rdp->nocb_cb_kthread == current;
  972. #else
  973. return false;
  974. #endif
  975. }
  976. /*
  977. * Is the current CPU running the RCU-callbacks kthread?
  978. * Caller must have preemption disabled.
  979. */
  980. static bool rcu_is_callbacks_kthread(struct rcu_data *rdp)
  981. {
  982. return rdp->rcu_cpu_kthread_task == current ||
  983. rcu_is_callbacks_nocb_kthread(rdp);
  984. }
  985. #ifdef CONFIG_RCU_BOOST
  986. /*
  987. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  988. * or ->boost_tasks, advancing the pointer to the next task in the
  989. * ->blkd_tasks list.
  990. *
  991. * Note that irqs must be enabled: boosting the task can block.
  992. * Returns 1 if there are more tasks needing to be boosted.
  993. */
  994. static int rcu_boost(struct rcu_node *rnp)
  995. {
  996. unsigned long flags;
  997. struct task_struct *t;
  998. struct list_head *tb;
  999. if (READ_ONCE(rnp->exp_tasks) == NULL &&
  1000. READ_ONCE(rnp->boost_tasks) == NULL)
  1001. return 0; /* Nothing left to boost. */
  1002. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1003. /*
  1004. * Recheck under the lock: all tasks in need of boosting
  1005. * might exit their RCU read-side critical sections on their own.
  1006. */
  1007. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  1008. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1009. return 0;
  1010. }
  1011. /*
  1012. * Preferentially boost tasks blocking expedited grace periods.
  1013. * This cannot starve the normal grace periods because a second
  1014. * expedited grace period must boost all blocked tasks, including
  1015. * those blocking the pre-existing normal grace period.
  1016. */
  1017. if (rnp->exp_tasks != NULL)
  1018. tb = rnp->exp_tasks;
  1019. else
  1020. tb = rnp->boost_tasks;
  1021. /*
  1022. * We boost task t by manufacturing an rt_mutex that appears to
  1023. * be held by task t. We leave a pointer to that rt_mutex where
  1024. * task t can find it, and task t will release the mutex when it
  1025. * exits its outermost RCU read-side critical section. Then
  1026. * simply acquiring this artificial rt_mutex will boost task
  1027. * t's priority. (Thanks to tglx for suggesting this approach!)
  1028. *
  1029. * Note that task t must acquire rnp->lock to remove itself from
  1030. * the ->blkd_tasks list, which it will do from exit() if from
  1031. * nowhere else. We therefore are guaranteed that task t will
  1032. * stay around at least until we drop rnp->lock. Note that
  1033. * rnp->lock also resolves races between our priority boosting
  1034. * and task t's exiting its outermost RCU read-side critical
  1035. * section.
  1036. */
  1037. t = container_of(tb, struct task_struct, rcu_node_entry);
  1038. rt_mutex_init_proxy_locked(&rnp->boost_mtx.rtmutex, t);
  1039. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1040. /* Lock only for side effect: boosts task t's priority. */
  1041. rt_mutex_lock(&rnp->boost_mtx);
  1042. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  1043. rnp->n_boosts++;
  1044. return READ_ONCE(rnp->exp_tasks) != NULL ||
  1045. READ_ONCE(rnp->boost_tasks) != NULL;
  1046. }
  1047. /*
  1048. * Priority-boosting kthread, one per leaf rcu_node.
  1049. */
  1050. static int rcu_boost_kthread(void *arg)
  1051. {
  1052. struct rcu_node *rnp = (struct rcu_node *)arg;
  1053. int spincnt = 0;
  1054. int more2boost;
  1055. trace_rcu_utilization(TPS("Start boost kthread@init"));
  1056. for (;;) {
  1057. WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_WAITING);
  1058. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  1059. rcu_wait(READ_ONCE(rnp->boost_tasks) ||
  1060. READ_ONCE(rnp->exp_tasks));
  1061. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  1062. WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_RUNNING);
  1063. more2boost = rcu_boost(rnp);
  1064. if (more2boost)
  1065. spincnt++;
  1066. else
  1067. spincnt = 0;
  1068. if (spincnt > 10) {
  1069. WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_YIELDING);
  1070. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  1071. schedule_timeout_idle(2);
  1072. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  1073. spincnt = 0;
  1074. }
  1075. }
  1076. /* NOTREACHED */
  1077. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  1078. return 0;
  1079. }
  1080. /*
  1081. * Check to see if it is time to start boosting RCU readers that are
  1082. * blocking the current grace period, and, if so, tell the per-rcu_node
  1083. * kthread to start boosting them. If there is an expedited grace
  1084. * period in progress, it is always time to boost.
  1085. *
  1086. * The caller must hold rnp->lock, which this function releases.
  1087. * The ->boost_kthread_task is immortal, so we don't need to worry
  1088. * about it going away.
  1089. */
  1090. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1091. __releases(rnp->lock)
  1092. {
  1093. raw_lockdep_assert_held_rcu_node(rnp);
  1094. if (!rnp->boost_kthread_task ||
  1095. (!rcu_preempt_blocked_readers_cgp(rnp) && !rnp->exp_tasks)) {
  1096. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1097. return;
  1098. }
  1099. if (rnp->exp_tasks != NULL ||
  1100. (rnp->gp_tasks != NULL &&
  1101. rnp->boost_tasks == NULL &&
  1102. rnp->qsmask == 0 &&
  1103. (!time_after(rnp->boost_time, jiffies) || rcu_state.cbovld ||
  1104. IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)))) {
  1105. if (rnp->exp_tasks == NULL)
  1106. WRITE_ONCE(rnp->boost_tasks, rnp->gp_tasks);
  1107. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1108. rcu_wake_cond(rnp->boost_kthread_task,
  1109. READ_ONCE(rnp->boost_kthread_status));
  1110. } else {
  1111. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1112. }
  1113. }
  1114. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1115. /*
  1116. * Do priority-boost accounting for the start of a new grace period.
  1117. */
  1118. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1119. {
  1120. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1121. }
  1122. /*
  1123. * Create an RCU-boost kthread for the specified node if one does not
  1124. * already exist. We only create this kthread for preemptible RCU.
  1125. */
  1126. static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
  1127. {
  1128. unsigned long flags;
  1129. int rnp_index = rnp - rcu_get_root();
  1130. struct sched_param sp;
  1131. struct task_struct *t;
  1132. if (rnp->boost_kthread_task)
  1133. return;
  1134. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1135. "rcub/%d", rnp_index);
  1136. if (WARN_ON_ONCE(IS_ERR(t)))
  1137. return;
  1138. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1139. rnp->boost_kthread_task = t;
  1140. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1141. sp.sched_priority = kthread_prio;
  1142. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1143. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1144. }
  1145. static struct task_struct *rcu_boost_task(struct rcu_node *rnp)
  1146. {
  1147. return READ_ONCE(rnp->boost_kthread_task);
  1148. }
  1149. #else /* #ifdef CONFIG_RCU_BOOST */
  1150. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1151. __releases(rnp->lock)
  1152. {
  1153. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1154. }
  1155. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1156. {
  1157. }
  1158. static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
  1159. {
  1160. }
  1161. static struct task_struct *rcu_boost_task(struct rcu_node *rnp)
  1162. {
  1163. return NULL;
  1164. }
  1165. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1166. /*
  1167. * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
  1168. * grace-period kthread will do force_quiescent_state() processing?
  1169. * The idea is to avoid waking up RCU core processing on such a
  1170. * CPU unless the grace period has extended for too long.
  1171. *
  1172. * This code relies on the fact that all NO_HZ_FULL CPUs are also
  1173. * RCU_NOCB_CPU CPUs.
  1174. */
  1175. static bool rcu_nohz_full_cpu(void)
  1176. {
  1177. #ifdef CONFIG_NO_HZ_FULL
  1178. if (tick_nohz_full_cpu(smp_processor_id()) &&
  1179. (!rcu_gp_in_progress() ||
  1180. time_before(jiffies, READ_ONCE(rcu_state.gp_start) + HZ)))
  1181. return true;
  1182. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  1183. return false;
  1184. }
  1185. /*
  1186. * Bind the RCU grace-period kthreads to the housekeeping CPU.
  1187. */
  1188. static void rcu_bind_gp_kthread(void)
  1189. {
  1190. if (!tick_nohz_full_enabled())
  1191. return;
  1192. housekeeping_affine(current, HK_TYPE_RCU);
  1193. }