tree_exp.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * RCU expedited grace periods
  4. *
  5. * Copyright IBM Corporation, 2016
  6. *
  7. * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
  8. */
  9. #include <linux/lockdep.h>
  10. static void rcu_exp_handler(void *unused);
  11. static int rcu_print_task_exp_stall(struct rcu_node *rnp);
  12. /*
  13. * Record the start of an expedited grace period.
  14. */
  15. static void rcu_exp_gp_seq_start(void)
  16. {
  17. rcu_seq_start(&rcu_state.expedited_sequence);
  18. }
  19. /*
  20. * Return the value that the expedited-grace-period counter will have
  21. * at the end of the current grace period.
  22. */
  23. static __maybe_unused unsigned long rcu_exp_gp_seq_endval(void)
  24. {
  25. return rcu_seq_endval(&rcu_state.expedited_sequence);
  26. }
  27. /*
  28. * Record the end of an expedited grace period.
  29. */
  30. static void rcu_exp_gp_seq_end(void)
  31. {
  32. rcu_seq_end(&rcu_state.expedited_sequence);
  33. smp_mb(); /* Ensure that consecutive grace periods serialize. */
  34. }
  35. /*
  36. * Take a snapshot of the expedited-grace-period counter, which is the
  37. * earliest value that will indicate that a full grace period has
  38. * elapsed since the current time.
  39. */
  40. static unsigned long rcu_exp_gp_seq_snap(void)
  41. {
  42. unsigned long s;
  43. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  44. s = rcu_seq_snap(&rcu_state.expedited_sequence);
  45. trace_rcu_exp_grace_period(rcu_state.name, s, TPS("snap"));
  46. return s;
  47. }
  48. /*
  49. * Given a counter snapshot from rcu_exp_gp_seq_snap(), return true
  50. * if a full expedited grace period has elapsed since that snapshot
  51. * was taken.
  52. */
  53. static bool rcu_exp_gp_seq_done(unsigned long s)
  54. {
  55. return rcu_seq_done(&rcu_state.expedited_sequence, s);
  56. }
  57. /*
  58. * Reset the ->expmaskinit values in the rcu_node tree to reflect any
  59. * recent CPU-online activity. Note that these masks are not cleared
  60. * when CPUs go offline, so they reflect the union of all CPUs that have
  61. * ever been online. This means that this function normally takes its
  62. * no-work-to-do fastpath.
  63. */
  64. static void sync_exp_reset_tree_hotplug(void)
  65. {
  66. bool done;
  67. unsigned long flags;
  68. unsigned long mask;
  69. unsigned long oldmask;
  70. int ncpus = smp_load_acquire(&rcu_state.ncpus); /* Order vs. locking. */
  71. struct rcu_node *rnp;
  72. struct rcu_node *rnp_up;
  73. /* If no new CPUs onlined since last time, nothing to do. */
  74. if (likely(ncpus == rcu_state.ncpus_snap))
  75. return;
  76. rcu_state.ncpus_snap = ncpus;
  77. /*
  78. * Each pass through the following loop propagates newly onlined
  79. * CPUs for the current rcu_node structure up the rcu_node tree.
  80. */
  81. rcu_for_each_leaf_node(rnp) {
  82. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  83. if (rnp->expmaskinit == rnp->expmaskinitnext) {
  84. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  85. continue; /* No new CPUs, nothing to do. */
  86. }
  87. /* Update this node's mask, track old value for propagation. */
  88. oldmask = rnp->expmaskinit;
  89. rnp->expmaskinit = rnp->expmaskinitnext;
  90. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  91. /* If was already nonzero, nothing to propagate. */
  92. if (oldmask)
  93. continue;
  94. /* Propagate the new CPU up the tree. */
  95. mask = rnp->grpmask;
  96. rnp_up = rnp->parent;
  97. done = false;
  98. while (rnp_up) {
  99. raw_spin_lock_irqsave_rcu_node(rnp_up, flags);
  100. if (rnp_up->expmaskinit)
  101. done = true;
  102. rnp_up->expmaskinit |= mask;
  103. raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags);
  104. if (done)
  105. break;
  106. mask = rnp_up->grpmask;
  107. rnp_up = rnp_up->parent;
  108. }
  109. }
  110. }
  111. /*
  112. * Reset the ->expmask values in the rcu_node tree in preparation for
  113. * a new expedited grace period.
  114. */
  115. static void __maybe_unused sync_exp_reset_tree(void)
  116. {
  117. unsigned long flags;
  118. struct rcu_node *rnp;
  119. sync_exp_reset_tree_hotplug();
  120. rcu_for_each_node_breadth_first(rnp) {
  121. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  122. WARN_ON_ONCE(rnp->expmask);
  123. WRITE_ONCE(rnp->expmask, rnp->expmaskinit);
  124. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  125. }
  126. }
  127. /*
  128. * Return non-zero if there is no RCU expedited grace period in progress
  129. * for the specified rcu_node structure, in other words, if all CPUs and
  130. * tasks covered by the specified rcu_node structure have done their bit
  131. * for the current expedited grace period.
  132. */
  133. static bool sync_rcu_exp_done(struct rcu_node *rnp)
  134. {
  135. raw_lockdep_assert_held_rcu_node(rnp);
  136. return READ_ONCE(rnp->exp_tasks) == NULL &&
  137. READ_ONCE(rnp->expmask) == 0;
  138. }
  139. /*
  140. * Like sync_rcu_exp_done(), but where the caller does not hold the
  141. * rcu_node's ->lock.
  142. */
  143. static bool sync_rcu_exp_done_unlocked(struct rcu_node *rnp)
  144. {
  145. unsigned long flags;
  146. bool ret;
  147. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  148. ret = sync_rcu_exp_done(rnp);
  149. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  150. return ret;
  151. }
  152. /*
  153. * Report the exit from RCU read-side critical section for the last task
  154. * that queued itself during or before the current expedited preemptible-RCU
  155. * grace period. This event is reported either to the rcu_node structure on
  156. * which the task was queued or to one of that rcu_node structure's ancestors,
  157. * recursively up the tree. (Calm down, calm down, we do the recursion
  158. * iteratively!)
  159. */
  160. static void __rcu_report_exp_rnp(struct rcu_node *rnp,
  161. bool wake, unsigned long flags)
  162. __releases(rnp->lock)
  163. {
  164. unsigned long mask;
  165. raw_lockdep_assert_held_rcu_node(rnp);
  166. for (;;) {
  167. if (!sync_rcu_exp_done(rnp)) {
  168. if (!rnp->expmask)
  169. rcu_initiate_boost(rnp, flags);
  170. else
  171. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  172. break;
  173. }
  174. if (rnp->parent == NULL) {
  175. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  176. if (wake) {
  177. smp_mb(); /* EGP done before wake_up(). */
  178. swake_up_one(&rcu_state.expedited_wq);
  179. }
  180. break;
  181. }
  182. mask = rnp->grpmask;
  183. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */
  184. rnp = rnp->parent;
  185. raw_spin_lock_rcu_node(rnp); /* irqs already disabled */
  186. WARN_ON_ONCE(!(rnp->expmask & mask));
  187. WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask);
  188. }
  189. }
  190. /*
  191. * Report expedited quiescent state for specified node. This is a
  192. * lock-acquisition wrapper function for __rcu_report_exp_rnp().
  193. */
  194. static void __maybe_unused rcu_report_exp_rnp(struct rcu_node *rnp, bool wake)
  195. {
  196. unsigned long flags;
  197. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  198. __rcu_report_exp_rnp(rnp, wake, flags);
  199. }
  200. /*
  201. * Report expedited quiescent state for multiple CPUs, all covered by the
  202. * specified leaf rcu_node structure.
  203. */
  204. static void rcu_report_exp_cpu_mult(struct rcu_node *rnp,
  205. unsigned long mask, bool wake)
  206. {
  207. int cpu;
  208. unsigned long flags;
  209. struct rcu_data *rdp;
  210. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  211. if (!(rnp->expmask & mask)) {
  212. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  213. return;
  214. }
  215. WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask);
  216. for_each_leaf_node_cpu_mask(rnp, cpu, mask) {
  217. rdp = per_cpu_ptr(&rcu_data, cpu);
  218. if (!IS_ENABLED(CONFIG_NO_HZ_FULL) || !rdp->rcu_forced_tick_exp)
  219. continue;
  220. rdp->rcu_forced_tick_exp = false;
  221. tick_dep_clear_cpu(cpu, TICK_DEP_BIT_RCU_EXP);
  222. }
  223. __rcu_report_exp_rnp(rnp, wake, flags); /* Releases rnp->lock. */
  224. }
  225. /*
  226. * Report expedited quiescent state for specified rcu_data (CPU).
  227. */
  228. static void rcu_report_exp_rdp(struct rcu_data *rdp)
  229. {
  230. WRITE_ONCE(rdp->exp_deferred_qs, false);
  231. rcu_report_exp_cpu_mult(rdp->mynode, rdp->grpmask, true);
  232. }
  233. /* Common code for work-done checking. */
  234. static bool sync_exp_work_done(unsigned long s)
  235. {
  236. if (rcu_exp_gp_seq_done(s)) {
  237. trace_rcu_exp_grace_period(rcu_state.name, s, TPS("done"));
  238. smp_mb(); /* Ensure test happens before caller kfree(). */
  239. return true;
  240. }
  241. return false;
  242. }
  243. /*
  244. * Funnel-lock acquisition for expedited grace periods. Returns true
  245. * if some other task completed an expedited grace period that this task
  246. * can piggy-back on, and with no mutex held. Otherwise, returns false
  247. * with the mutex held, indicating that the caller must actually do the
  248. * expedited grace period.
  249. */
  250. static bool exp_funnel_lock(unsigned long s)
  251. {
  252. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id());
  253. struct rcu_node *rnp = rdp->mynode;
  254. struct rcu_node *rnp_root = rcu_get_root();
  255. /* Low-contention fastpath. */
  256. if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s) &&
  257. (rnp == rnp_root ||
  258. ULONG_CMP_LT(READ_ONCE(rnp_root->exp_seq_rq), s)) &&
  259. mutex_trylock(&rcu_state.exp_mutex))
  260. goto fastpath;
  261. /*
  262. * Each pass through the following loop works its way up
  263. * the rcu_node tree, returning if others have done the work or
  264. * otherwise falls through to acquire ->exp_mutex. The mapping
  265. * from CPU to rcu_node structure can be inexact, as it is just
  266. * promoting locality and is not strictly needed for correctness.
  267. */
  268. for (; rnp != NULL; rnp = rnp->parent) {
  269. if (sync_exp_work_done(s))
  270. return true;
  271. /* Work not done, either wait here or go up. */
  272. spin_lock(&rnp->exp_lock);
  273. if (ULONG_CMP_GE(rnp->exp_seq_rq, s)) {
  274. /* Someone else doing GP, so wait for them. */
  275. spin_unlock(&rnp->exp_lock);
  276. trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,
  277. rnp->grplo, rnp->grphi,
  278. TPS("wait"));
  279. wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
  280. sync_exp_work_done(s));
  281. return true;
  282. }
  283. WRITE_ONCE(rnp->exp_seq_rq, s); /* Followers can wait on us. */
  284. spin_unlock(&rnp->exp_lock);
  285. trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,
  286. rnp->grplo, rnp->grphi, TPS("nxtlvl"));
  287. }
  288. mutex_lock(&rcu_state.exp_mutex);
  289. fastpath:
  290. if (sync_exp_work_done(s)) {
  291. mutex_unlock(&rcu_state.exp_mutex);
  292. return true;
  293. }
  294. rcu_exp_gp_seq_start();
  295. trace_rcu_exp_grace_period(rcu_state.name, s, TPS("start"));
  296. return false;
  297. }
  298. /*
  299. * Select the CPUs within the specified rcu_node that the upcoming
  300. * expedited grace period needs to wait for.
  301. */
  302. static void sync_rcu_exp_select_node_cpus(struct work_struct *wp)
  303. {
  304. int cpu;
  305. unsigned long flags;
  306. unsigned long mask_ofl_test;
  307. unsigned long mask_ofl_ipi;
  308. int ret;
  309. struct rcu_exp_work *rewp =
  310. container_of(wp, struct rcu_exp_work, rew_work);
  311. struct rcu_node *rnp = container_of(rewp, struct rcu_node, rew);
  312. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  313. /* Each pass checks a CPU for identity, offline, and idle. */
  314. mask_ofl_test = 0;
  315. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
  316. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  317. unsigned long mask = rdp->grpmask;
  318. int snap;
  319. if (raw_smp_processor_id() == cpu ||
  320. !(rnp->qsmaskinitnext & mask)) {
  321. mask_ofl_test |= mask;
  322. } else {
  323. snap = rcu_dynticks_snap(rdp);
  324. if (rcu_dynticks_in_eqs(snap))
  325. mask_ofl_test |= mask;
  326. else
  327. rdp->exp_dynticks_snap = snap;
  328. }
  329. }
  330. mask_ofl_ipi = rnp->expmask & ~mask_ofl_test;
  331. /*
  332. * Need to wait for any blocked tasks as well. Note that
  333. * additional blocking tasks will also block the expedited GP
  334. * until such time as the ->expmask bits are cleared.
  335. */
  336. if (rcu_preempt_has_tasks(rnp))
  337. WRITE_ONCE(rnp->exp_tasks, rnp->blkd_tasks.next);
  338. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  339. /* IPI the remaining CPUs for expedited quiescent state. */
  340. for_each_leaf_node_cpu_mask(rnp, cpu, mask_ofl_ipi) {
  341. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  342. unsigned long mask = rdp->grpmask;
  343. retry_ipi:
  344. if (rcu_dynticks_in_eqs_since(rdp, rdp->exp_dynticks_snap)) {
  345. mask_ofl_test |= mask;
  346. continue;
  347. }
  348. if (get_cpu() == cpu) {
  349. mask_ofl_test |= mask;
  350. put_cpu();
  351. continue;
  352. }
  353. ret = smp_call_function_single(cpu, rcu_exp_handler, NULL, 0);
  354. put_cpu();
  355. /* The CPU will report the QS in response to the IPI. */
  356. if (!ret)
  357. continue;
  358. /* Failed, raced with CPU hotplug operation. */
  359. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  360. if ((rnp->qsmaskinitnext & mask) &&
  361. (rnp->expmask & mask)) {
  362. /* Online, so delay for a bit and try again. */
  363. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  364. trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("selectofl"));
  365. schedule_timeout_idle(1);
  366. goto retry_ipi;
  367. }
  368. /* CPU really is offline, so we must report its QS. */
  369. if (rnp->expmask & mask)
  370. mask_ofl_test |= mask;
  371. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  372. }
  373. /* Report quiescent states for those that went offline. */
  374. if (mask_ofl_test)
  375. rcu_report_exp_cpu_mult(rnp, mask_ofl_test, false);
  376. }
  377. /*
  378. * Select the nodes that the upcoming expedited grace period needs
  379. * to wait for.
  380. */
  381. static void sync_rcu_exp_select_cpus(void)
  382. {
  383. int cpu;
  384. struct rcu_node *rnp;
  385. trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("reset"));
  386. sync_exp_reset_tree();
  387. trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("select"));
  388. /* Schedule work for each leaf rcu_node structure. */
  389. rcu_for_each_leaf_node(rnp) {
  390. rnp->exp_need_flush = false;
  391. if (!READ_ONCE(rnp->expmask))
  392. continue; /* Avoid early boot non-existent wq. */
  393. if (!READ_ONCE(rcu_par_gp_wq) ||
  394. rcu_scheduler_active != RCU_SCHEDULER_RUNNING ||
  395. rcu_is_last_leaf_node(rnp)) {
  396. /* No workqueues yet or last leaf, do direct call. */
  397. sync_rcu_exp_select_node_cpus(&rnp->rew.rew_work);
  398. continue;
  399. }
  400. INIT_WORK(&rnp->rew.rew_work, sync_rcu_exp_select_node_cpus);
  401. cpu = find_next_bit(&rnp->ffmask, BITS_PER_LONG, -1);
  402. /* If all offline, queue the work on an unbound CPU. */
  403. if (unlikely(cpu > rnp->grphi - rnp->grplo))
  404. cpu = WORK_CPU_UNBOUND;
  405. else
  406. cpu += rnp->grplo;
  407. queue_work_on(cpu, rcu_par_gp_wq, &rnp->rew.rew_work);
  408. rnp->exp_need_flush = true;
  409. }
  410. /* Wait for workqueue jobs (if any) to complete. */
  411. rcu_for_each_leaf_node(rnp)
  412. if (rnp->exp_need_flush)
  413. flush_work(&rnp->rew.rew_work);
  414. }
  415. /*
  416. * Wait for the expedited grace period to elapse, within time limit.
  417. * If the time limit is exceeded without the grace period elapsing,
  418. * return false, otherwise return true.
  419. */
  420. static bool synchronize_rcu_expedited_wait_once(long tlimit)
  421. {
  422. int t;
  423. struct rcu_node *rnp_root = rcu_get_root();
  424. t = swait_event_timeout_exclusive(rcu_state.expedited_wq,
  425. sync_rcu_exp_done_unlocked(rnp_root),
  426. tlimit);
  427. // Workqueues should not be signaled.
  428. if (t > 0 || sync_rcu_exp_done_unlocked(rnp_root))
  429. return true;
  430. WARN_ON(t < 0); /* workqueues should not be signaled. */
  431. return false;
  432. }
  433. /*
  434. * Wait for the expedited grace period to elapse, issuing any needed
  435. * RCU CPU stall warnings along the way.
  436. */
  437. static void synchronize_rcu_expedited_wait(void)
  438. {
  439. int cpu;
  440. unsigned long j;
  441. unsigned long jiffies_stall;
  442. unsigned long jiffies_start;
  443. unsigned long mask;
  444. int ndetected;
  445. struct rcu_data *rdp;
  446. struct rcu_node *rnp;
  447. struct rcu_node *rnp_root = rcu_get_root();
  448. trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("startwait"));
  449. jiffies_stall = rcu_jiffies_till_stall_check();
  450. jiffies_start = jiffies;
  451. if (tick_nohz_full_enabled() && rcu_inkernel_boot_has_ended()) {
  452. if (synchronize_rcu_expedited_wait_once(1))
  453. return;
  454. rcu_for_each_leaf_node(rnp) {
  455. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
  456. rdp = per_cpu_ptr(&rcu_data, cpu);
  457. if (rdp->rcu_forced_tick_exp)
  458. continue;
  459. rdp->rcu_forced_tick_exp = true;
  460. tick_dep_set_cpu(cpu, TICK_DEP_BIT_RCU_EXP);
  461. }
  462. }
  463. j = READ_ONCE(jiffies_till_first_fqs);
  464. if (synchronize_rcu_expedited_wait_once(j + HZ))
  465. return;
  466. WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT));
  467. }
  468. for (;;) {
  469. if (synchronize_rcu_expedited_wait_once(jiffies_stall))
  470. return;
  471. if (rcu_stall_is_suppressed())
  472. continue;
  473. panic_on_rcu_stall();
  474. trace_rcu_stall_warning(rcu_state.name, TPS("ExpeditedStall"));
  475. pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {",
  476. rcu_state.name);
  477. ndetected = 0;
  478. rcu_for_each_leaf_node(rnp) {
  479. ndetected += rcu_print_task_exp_stall(rnp);
  480. for_each_leaf_node_possible_cpu(rnp, cpu) {
  481. struct rcu_data *rdp;
  482. mask = leaf_node_cpu_bit(rnp, cpu);
  483. if (!(READ_ONCE(rnp->expmask) & mask))
  484. continue;
  485. ndetected++;
  486. rdp = per_cpu_ptr(&rcu_data, cpu);
  487. pr_cont(" %d-%c%c%c", cpu,
  488. "O."[!!cpu_online(cpu)],
  489. "o."[!!(rdp->grpmask & rnp->expmaskinit)],
  490. "N."[!!(rdp->grpmask & rnp->expmaskinitnext)]);
  491. }
  492. }
  493. pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n",
  494. jiffies - jiffies_start, rcu_state.expedited_sequence,
  495. data_race(rnp_root->expmask),
  496. ".T"[!!data_race(rnp_root->exp_tasks)]);
  497. if (ndetected) {
  498. pr_err("blocking rcu_node structures:");
  499. rcu_for_each_node_breadth_first(rnp) {
  500. if (rnp == rnp_root)
  501. continue; /* printed unconditionally */
  502. if (sync_rcu_exp_done_unlocked(rnp))
  503. continue;
  504. pr_cont(" l=%u:%d-%d:%#lx/%c",
  505. rnp->level, rnp->grplo, rnp->grphi,
  506. data_race(rnp->expmask),
  507. ".T"[!!data_race(rnp->exp_tasks)]);
  508. }
  509. pr_cont("\n");
  510. }
  511. rcu_for_each_leaf_node(rnp) {
  512. for_each_leaf_node_possible_cpu(rnp, cpu) {
  513. mask = leaf_node_cpu_bit(rnp, cpu);
  514. if (!(READ_ONCE(rnp->expmask) & mask))
  515. continue;
  516. dump_cpu_task(cpu);
  517. }
  518. }
  519. jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
  520. }
  521. }
  522. /*
  523. * Wait for the current expedited grace period to complete, and then
  524. * wake up everyone who piggybacked on the just-completed expedited
  525. * grace period. Also update all the ->exp_seq_rq counters as needed
  526. * in order to avoid counter-wrap problems.
  527. */
  528. static void rcu_exp_wait_wake(unsigned long s)
  529. {
  530. struct rcu_node *rnp;
  531. synchronize_rcu_expedited_wait();
  532. // Switch over to wakeup mode, allowing the next GP to proceed.
  533. // End the previous grace period only after acquiring the mutex
  534. // to ensure that only one GP runs concurrently with wakeups.
  535. mutex_lock(&rcu_state.exp_wake_mutex);
  536. rcu_exp_gp_seq_end();
  537. trace_rcu_exp_grace_period(rcu_state.name, s, TPS("end"));
  538. rcu_for_each_node_breadth_first(rnp) {
  539. if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s)) {
  540. spin_lock(&rnp->exp_lock);
  541. /* Recheck, avoid hang in case someone just arrived. */
  542. if (ULONG_CMP_LT(rnp->exp_seq_rq, s))
  543. WRITE_ONCE(rnp->exp_seq_rq, s);
  544. spin_unlock(&rnp->exp_lock);
  545. }
  546. smp_mb(); /* All above changes before wakeup. */
  547. wake_up_all(&rnp->exp_wq[rcu_seq_ctr(s) & 0x3]);
  548. }
  549. trace_rcu_exp_grace_period(rcu_state.name, s, TPS("endwake"));
  550. mutex_unlock(&rcu_state.exp_wake_mutex);
  551. }
  552. /*
  553. * Common code to drive an expedited grace period forward, used by
  554. * workqueues and mid-boot-time tasks.
  555. */
  556. static void rcu_exp_sel_wait_wake(unsigned long s)
  557. {
  558. /* Initialize the rcu_node tree in preparation for the wait. */
  559. sync_rcu_exp_select_cpus();
  560. /* Wait and clean up, including waking everyone. */
  561. rcu_exp_wait_wake(s);
  562. }
  563. /*
  564. * Work-queue handler to drive an expedited grace period forward.
  565. */
  566. static void wait_rcu_exp_gp(struct work_struct *wp)
  567. {
  568. struct rcu_exp_work *rewp;
  569. rewp = container_of(wp, struct rcu_exp_work, rew_work);
  570. rcu_exp_sel_wait_wake(rewp->rew_s);
  571. }
  572. #ifdef CONFIG_PREEMPT_RCU
  573. /*
  574. * Remote handler for smp_call_function_single(). If there is an
  575. * RCU read-side critical section in effect, request that the
  576. * next rcu_read_unlock() record the quiescent state up the
  577. * ->expmask fields in the rcu_node tree. Otherwise, immediately
  578. * report the quiescent state.
  579. */
  580. static void rcu_exp_handler(void *unused)
  581. {
  582. int depth = rcu_preempt_depth();
  583. unsigned long flags;
  584. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  585. struct rcu_node *rnp = rdp->mynode;
  586. struct task_struct *t = current;
  587. /*
  588. * First, the common case of not being in an RCU read-side
  589. * critical section. If also enabled or idle, immediately
  590. * report the quiescent state, otherwise defer.
  591. */
  592. if (!depth) {
  593. if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
  594. rcu_dynticks_curr_cpu_in_eqs()) {
  595. rcu_report_exp_rdp(rdp);
  596. } else {
  597. rdp->exp_deferred_qs = true;
  598. set_tsk_need_resched(t);
  599. set_preempt_need_resched();
  600. }
  601. return;
  602. }
  603. /*
  604. * Second, the less-common case of being in an RCU read-side
  605. * critical section. In this case we can count on a future
  606. * rcu_read_unlock(). However, this rcu_read_unlock() might
  607. * execute on some other CPU, but in that case there will be
  608. * a future context switch. Either way, if the expedited
  609. * grace period is still waiting on this CPU, set ->deferred_qs
  610. * so that the eventual quiescent state will be reported.
  611. * Note that there is a large group of race conditions that
  612. * can have caused this quiescent state to already have been
  613. * reported, so we really do need to check ->expmask.
  614. */
  615. if (depth > 0) {
  616. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  617. if (rnp->expmask & rdp->grpmask) {
  618. rdp->exp_deferred_qs = true;
  619. t->rcu_read_unlock_special.b.exp_hint = true;
  620. }
  621. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  622. return;
  623. }
  624. // Finally, negative nesting depth should not happen.
  625. WARN_ON_ONCE(1);
  626. }
  627. /* PREEMPTION=y, so no PREEMPTION=n expedited grace period to clean up after. */
  628. static void sync_sched_exp_online_cleanup(int cpu)
  629. {
  630. }
  631. /*
  632. * Scan the current list of tasks blocked within RCU read-side critical
  633. * sections, printing out the tid of each that is blocking the current
  634. * expedited grace period.
  635. */
  636. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  637. {
  638. unsigned long flags;
  639. int ndetected = 0;
  640. struct task_struct *t;
  641. if (!READ_ONCE(rnp->exp_tasks))
  642. return 0;
  643. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  644. t = list_entry(rnp->exp_tasks->prev,
  645. struct task_struct, rcu_node_entry);
  646. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  647. pr_cont(" P%d", t->pid);
  648. ndetected++;
  649. }
  650. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  651. return ndetected;
  652. }
  653. #else /* #ifdef CONFIG_PREEMPT_RCU */
  654. /* Request an expedited quiescent state. */
  655. static void rcu_exp_need_qs(void)
  656. {
  657. __this_cpu_write(rcu_data.cpu_no_qs.b.exp, true);
  658. /* Store .exp before .rcu_urgent_qs. */
  659. smp_store_release(this_cpu_ptr(&rcu_data.rcu_urgent_qs), true);
  660. set_tsk_need_resched(current);
  661. set_preempt_need_resched();
  662. }
  663. /* Invoked on each online non-idle CPU for expedited quiescent state. */
  664. static void rcu_exp_handler(void *unused)
  665. {
  666. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  667. struct rcu_node *rnp = rdp->mynode;
  668. if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
  669. __this_cpu_read(rcu_data.cpu_no_qs.b.exp))
  670. return;
  671. if (rcu_is_cpu_rrupt_from_idle()) {
  672. rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
  673. return;
  674. }
  675. rcu_exp_need_qs();
  676. }
  677. /* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */
  678. static void sync_sched_exp_online_cleanup(int cpu)
  679. {
  680. unsigned long flags;
  681. int my_cpu;
  682. struct rcu_data *rdp;
  683. int ret;
  684. struct rcu_node *rnp;
  685. rdp = per_cpu_ptr(&rcu_data, cpu);
  686. rnp = rdp->mynode;
  687. my_cpu = get_cpu();
  688. /* Quiescent state either not needed or already requested, leave. */
  689. if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
  690. rdp->cpu_no_qs.b.exp) {
  691. put_cpu();
  692. return;
  693. }
  694. /* Quiescent state needed on current CPU, so set it up locally. */
  695. if (my_cpu == cpu) {
  696. local_irq_save(flags);
  697. rcu_exp_need_qs();
  698. local_irq_restore(flags);
  699. put_cpu();
  700. return;
  701. }
  702. /* Quiescent state needed on some other CPU, send IPI. */
  703. ret = smp_call_function_single(cpu, rcu_exp_handler, NULL, 0);
  704. put_cpu();
  705. WARN_ON_ONCE(ret);
  706. }
  707. /*
  708. * Because preemptible RCU does not exist, we never have to check for
  709. * tasks blocked within RCU read-side critical sections that are
  710. * blocking the current expedited grace period.
  711. */
  712. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  713. {
  714. return 0;
  715. }
  716. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  717. /**
  718. * synchronize_rcu_expedited - Brute-force RCU grace period
  719. *
  720. * Wait for an RCU grace period, but expedite it. The basic idea is to
  721. * IPI all non-idle non-nohz online CPUs. The IPI handler checks whether
  722. * the CPU is in an RCU critical section, and if so, it sets a flag that
  723. * causes the outermost rcu_read_unlock() to report the quiescent state
  724. * for RCU-preempt or asks the scheduler for help for RCU-sched. On the
  725. * other hand, if the CPU is not in an RCU read-side critical section,
  726. * the IPI handler reports the quiescent state immediately.
  727. *
  728. * Although this is a great improvement over previous expedited
  729. * implementations, it is still unfriendly to real-time workloads, so is
  730. * thus not recommended for any sort of common-case code. In fact, if
  731. * you are using synchronize_rcu_expedited() in a loop, please restructure
  732. * your code to batch your updates, and then use a single synchronize_rcu()
  733. * instead.
  734. *
  735. * This has the same semantics as (but is more brutal than) synchronize_rcu().
  736. */
  737. void synchronize_rcu_expedited(void)
  738. {
  739. bool no_wq;
  740. struct rcu_exp_work rew;
  741. struct rcu_node *rnp;
  742. unsigned long s;
  743. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  744. lock_is_held(&rcu_lock_map) ||
  745. lock_is_held(&rcu_sched_lock_map),
  746. "Illegal synchronize_rcu_expedited() in RCU read-side critical section");
  747. /* Is the state is such that the call is a grace period? */
  748. if (rcu_blocking_is_gp())
  749. return;
  750. /* If expedited grace periods are prohibited, fall back to normal. */
  751. if (rcu_gp_is_normal()) {
  752. wait_rcu_gp(call_rcu);
  753. return;
  754. }
  755. /* Take a snapshot of the sequence number. */
  756. s = rcu_exp_gp_seq_snap();
  757. if (exp_funnel_lock(s))
  758. return; /* Someone else did our work for us. */
  759. /* Don't use workqueue during boot or from an incoming CPU. */
  760. preempt_disable();
  761. no_wq = rcu_scheduler_active == RCU_SCHEDULER_INIT ||
  762. !cpumask_test_cpu(smp_processor_id(), cpu_active_mask);
  763. preempt_enable();
  764. /* Ensure that load happens before action based on it. */
  765. if (unlikely(no_wq)) {
  766. /* Direct call for scheduler init, early_initcall()s, and incoming CPUs. */
  767. rcu_exp_sel_wait_wake(s);
  768. } else {
  769. /* Marshall arguments & schedule the expedited grace period. */
  770. rew.rew_s = s;
  771. INIT_WORK_ONSTACK(&rew.rew_work, wait_rcu_exp_gp);
  772. queue_work(rcu_gp_wq, &rew.rew_work);
  773. }
  774. /* Wait for expedited grace period to complete. */
  775. rnp = rcu_get_root();
  776. wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
  777. sync_exp_work_done(s));
  778. smp_mb(); /* Workqueue actions happen before return. */
  779. /* Let the next expedited grace period start. */
  780. mutex_unlock(&rcu_state.exp_mutex);
  781. if (likely(!no_wq))
  782. destroy_work_on_stack(&rew.rew_work);
  783. }
  784. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);