tree_nocb.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  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. * Copyright SUSE, 2021
  10. *
  11. * Author: Ingo Molnar <mingo@elte.hu>
  12. * Paul E. McKenney <paulmck@linux.ibm.com>
  13. * Frederic Weisbecker <frederic@kernel.org>
  14. */
  15. #ifdef CONFIG_RCU_NOCB_CPU
  16. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  17. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  18. static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
  19. {
  20. /* Race on early boot between thread creation and assignment */
  21. if (!rdp->nocb_cb_kthread || !rdp->nocb_gp_kthread)
  22. return true;
  23. if (current == rdp->nocb_cb_kthread || current == rdp->nocb_gp_kthread)
  24. if (in_task())
  25. return true;
  26. return false;
  27. }
  28. /*
  29. * Offload callback processing from the boot-time-specified set of CPUs
  30. * specified by rcu_nocb_mask. For the CPUs in the set, there are kthreads
  31. * created that pull the callbacks from the corresponding CPU, wait for
  32. * a grace period to elapse, and invoke the callbacks. These kthreads
  33. * are organized into GP kthreads, which manage incoming callbacks, wait for
  34. * grace periods, and awaken CB kthreads, and the CB kthreads, which only
  35. * invoke callbacks. Each GP kthread invokes its own CBs. The no-CBs CPUs
  36. * do a wake_up() on their GP kthread when they insert a callback into any
  37. * empty list, unless the rcu_nocb_poll boot parameter has been specified,
  38. * in which case each kthread actively polls its CPU. (Which isn't so great
  39. * for energy efficiency, but which does reduce RCU's overhead on that CPU.)
  40. *
  41. * This is intended to be used in conjunction with Frederic Weisbecker's
  42. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  43. * running CPU-bound user-mode computations.
  44. *
  45. * Offloading of callbacks can also be used as an energy-efficiency
  46. * measure because CPUs with no RCU callbacks queued are more aggressive
  47. * about entering dyntick-idle mode.
  48. */
  49. /*
  50. * Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters.
  51. * If the list is invalid, a warning is emitted and all CPUs are offloaded.
  52. */
  53. static int __init rcu_nocb_setup(char *str)
  54. {
  55. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  56. if (*str == '=') {
  57. if (cpulist_parse(++str, rcu_nocb_mask)) {
  58. pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
  59. cpumask_setall(rcu_nocb_mask);
  60. }
  61. }
  62. rcu_state.nocb_is_setup = true;
  63. return 1;
  64. }
  65. __setup("rcu_nocbs", rcu_nocb_setup);
  66. static int __init parse_rcu_nocb_poll(char *arg)
  67. {
  68. rcu_nocb_poll = true;
  69. return 1;
  70. }
  71. __setup("rcu_nocb_poll", parse_rcu_nocb_poll);
  72. /*
  73. * Don't bother bypassing ->cblist if the call_rcu() rate is low.
  74. * After all, the main point of bypassing is to avoid lock contention
  75. * on ->nocb_lock, which only can happen at high call_rcu() rates.
  76. */
  77. static int nocb_nobypass_lim_per_jiffy = 16 * 1000 / HZ;
  78. module_param(nocb_nobypass_lim_per_jiffy, int, 0);
  79. /*
  80. * Acquire the specified rcu_data structure's ->nocb_bypass_lock. If the
  81. * lock isn't immediately available, perform minimal sanity check.
  82. */
  83. static void rcu_nocb_bypass_lock(struct rcu_data *rdp)
  84. __acquires(&rdp->nocb_bypass_lock)
  85. {
  86. lockdep_assert_irqs_disabled();
  87. if (raw_spin_trylock(&rdp->nocb_bypass_lock))
  88. return;
  89. /*
  90. * Contention expected only when local enqueue collide with
  91. * remote flush from kthreads.
  92. */
  93. WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
  94. raw_spin_lock(&rdp->nocb_bypass_lock);
  95. }
  96. /*
  97. * Conditionally acquire the specified rcu_data structure's
  98. * ->nocb_bypass_lock.
  99. */
  100. static bool rcu_nocb_bypass_trylock(struct rcu_data *rdp)
  101. {
  102. lockdep_assert_irqs_disabled();
  103. return raw_spin_trylock(&rdp->nocb_bypass_lock);
  104. }
  105. /*
  106. * Release the specified rcu_data structure's ->nocb_bypass_lock.
  107. */
  108. static void rcu_nocb_bypass_unlock(struct rcu_data *rdp)
  109. __releases(&rdp->nocb_bypass_lock)
  110. {
  111. lockdep_assert_irqs_disabled();
  112. raw_spin_unlock(&rdp->nocb_bypass_lock);
  113. }
  114. /*
  115. * Acquire the specified rcu_data structure's ->nocb_lock, but only
  116. * if it corresponds to a no-CBs CPU.
  117. */
  118. static void rcu_nocb_lock(struct rcu_data *rdp)
  119. {
  120. lockdep_assert_irqs_disabled();
  121. if (!rcu_rdp_is_offloaded(rdp))
  122. return;
  123. raw_spin_lock(&rdp->nocb_lock);
  124. }
  125. /*
  126. * Release the specified rcu_data structure's ->nocb_lock, but only
  127. * if it corresponds to a no-CBs CPU.
  128. */
  129. static void rcu_nocb_unlock(struct rcu_data *rdp)
  130. {
  131. if (rcu_rdp_is_offloaded(rdp)) {
  132. lockdep_assert_irqs_disabled();
  133. raw_spin_unlock(&rdp->nocb_lock);
  134. }
  135. }
  136. /*
  137. * Release the specified rcu_data structure's ->nocb_lock and restore
  138. * interrupts, but only if it corresponds to a no-CBs CPU.
  139. */
  140. static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
  141. unsigned long flags)
  142. {
  143. if (rcu_rdp_is_offloaded(rdp)) {
  144. lockdep_assert_irqs_disabled();
  145. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  146. } else {
  147. local_irq_restore(flags);
  148. }
  149. }
  150. /* Lockdep check that ->cblist may be safely accessed. */
  151. static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
  152. {
  153. lockdep_assert_irqs_disabled();
  154. if (rcu_rdp_is_offloaded(rdp))
  155. lockdep_assert_held(&rdp->nocb_lock);
  156. }
  157. /*
  158. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  159. * grace period.
  160. */
  161. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  162. {
  163. swake_up_all(sq);
  164. }
  165. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  166. {
  167. return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
  168. }
  169. static void rcu_init_one_nocb(struct rcu_node *rnp)
  170. {
  171. init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  172. init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  173. }
  174. static bool __wake_nocb_gp(struct rcu_data *rdp_gp,
  175. struct rcu_data *rdp,
  176. bool force, unsigned long flags)
  177. __releases(rdp_gp->nocb_gp_lock)
  178. {
  179. bool needwake = false;
  180. if (!READ_ONCE(rdp_gp->nocb_gp_kthread)) {
  181. raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
  182. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  183. TPS("AlreadyAwake"));
  184. return false;
  185. }
  186. if (rdp_gp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
  187. WRITE_ONCE(rdp_gp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  188. del_timer(&rdp_gp->nocb_timer);
  189. }
  190. if (force || READ_ONCE(rdp_gp->nocb_gp_sleep)) {
  191. WRITE_ONCE(rdp_gp->nocb_gp_sleep, false);
  192. needwake = true;
  193. }
  194. raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
  195. if (needwake) {
  196. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DoWake"));
  197. swake_up_one_online(&rdp_gp->nocb_gp_wq);
  198. }
  199. return needwake;
  200. }
  201. /*
  202. * Kick the GP kthread for this NOCB group.
  203. */
  204. static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
  205. {
  206. unsigned long flags;
  207. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  208. raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
  209. return __wake_nocb_gp(rdp_gp, rdp, force, flags);
  210. }
  211. #ifdef CONFIG_RCU_LAZY
  212. /*
  213. * LAZY_FLUSH_JIFFIES decides the maximum amount of time that
  214. * can elapse before lazy callbacks are flushed. Lazy callbacks
  215. * could be flushed much earlier for a number of other reasons
  216. * however, LAZY_FLUSH_JIFFIES will ensure no lazy callbacks are
  217. * left unsubmitted to RCU after those many jiffies.
  218. */
  219. #define LAZY_FLUSH_JIFFIES (10 * HZ)
  220. static unsigned long jiffies_lazy_flush = LAZY_FLUSH_JIFFIES;
  221. // To be called only from test code.
  222. void rcu_set_jiffies_lazy_flush(unsigned long jif)
  223. {
  224. jiffies_lazy_flush = jif;
  225. }
  226. EXPORT_SYMBOL(rcu_set_jiffies_lazy_flush);
  227. unsigned long rcu_get_jiffies_lazy_flush(void)
  228. {
  229. return jiffies_lazy_flush;
  230. }
  231. EXPORT_SYMBOL(rcu_get_jiffies_lazy_flush);
  232. #endif
  233. /*
  234. * Arrange to wake the GP kthread for this NOCB group at some future
  235. * time when it is safe to do so.
  236. */
  237. static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
  238. const char *reason)
  239. {
  240. unsigned long flags;
  241. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  242. raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
  243. /*
  244. * Bypass wakeup overrides previous deferments. In case of
  245. * callback storms, no need to wake up too early.
  246. */
  247. if (waketype == RCU_NOCB_WAKE_LAZY &&
  248. rdp_gp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT) {
  249. mod_timer(&rdp_gp->nocb_timer, jiffies + rcu_get_jiffies_lazy_flush());
  250. WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
  251. } else if (waketype == RCU_NOCB_WAKE_BYPASS) {
  252. mod_timer(&rdp_gp->nocb_timer, jiffies + 2);
  253. WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
  254. } else {
  255. if (rdp_gp->nocb_defer_wakeup < RCU_NOCB_WAKE)
  256. mod_timer(&rdp_gp->nocb_timer, jiffies + 1);
  257. if (rdp_gp->nocb_defer_wakeup < waketype)
  258. WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
  259. }
  260. raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
  261. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
  262. }
  263. /*
  264. * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
  265. * However, if there is a callback to be enqueued and if ->nocb_bypass
  266. * proves to be initially empty, just return false because the no-CB GP
  267. * kthread may need to be awakened in this case.
  268. *
  269. * Return true if there was something to be flushed and it succeeded, otherwise
  270. * false.
  271. *
  272. * Note that this function always returns true if rhp is NULL.
  273. */
  274. static bool rcu_nocb_do_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp_in,
  275. unsigned long j, bool lazy)
  276. {
  277. struct rcu_cblist rcl;
  278. struct rcu_head *rhp = rhp_in;
  279. WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
  280. rcu_lockdep_assert_cblist_protected(rdp);
  281. lockdep_assert_held(&rdp->nocb_bypass_lock);
  282. if (rhp && !rcu_cblist_n_cbs(&rdp->nocb_bypass)) {
  283. raw_spin_unlock(&rdp->nocb_bypass_lock);
  284. return false;
  285. }
  286. /* Note: ->cblist.len already accounts for ->nocb_bypass contents. */
  287. if (rhp)
  288. rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
  289. /*
  290. * If the new CB requested was a lazy one, queue it onto the main
  291. * ->cblist so that we can take advantage of the grace-period that will
  292. * happen regardless. But queue it onto the bypass list first so that
  293. * the lazy CB is ordered with the existing CBs in the bypass list.
  294. */
  295. if (lazy && rhp) {
  296. rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
  297. rhp = NULL;
  298. }
  299. rcu_cblist_flush_enqueue(&rcl, &rdp->nocb_bypass, rhp);
  300. WRITE_ONCE(rdp->lazy_len, 0);
  301. rcu_segcblist_insert_pend_cbs(&rdp->cblist, &rcl);
  302. WRITE_ONCE(rdp->nocb_bypass_first, j);
  303. rcu_nocb_bypass_unlock(rdp);
  304. return true;
  305. }
  306. /*
  307. * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
  308. * However, if there is a callback to be enqueued and if ->nocb_bypass
  309. * proves to be initially empty, just return false because the no-CB GP
  310. * kthread may need to be awakened in this case.
  311. *
  312. * Note that this function always returns true if rhp is NULL.
  313. */
  314. static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
  315. unsigned long j, bool lazy)
  316. {
  317. if (!rcu_rdp_is_offloaded(rdp))
  318. return true;
  319. rcu_lockdep_assert_cblist_protected(rdp);
  320. rcu_nocb_bypass_lock(rdp);
  321. return rcu_nocb_do_flush_bypass(rdp, rhp, j, lazy);
  322. }
  323. /*
  324. * If the ->nocb_bypass_lock is immediately available, flush the
  325. * ->nocb_bypass queue into ->cblist.
  326. */
  327. static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)
  328. {
  329. rcu_lockdep_assert_cblist_protected(rdp);
  330. if (!rcu_rdp_is_offloaded(rdp) ||
  331. !rcu_nocb_bypass_trylock(rdp))
  332. return;
  333. WARN_ON_ONCE(!rcu_nocb_do_flush_bypass(rdp, NULL, j, false));
  334. }
  335. /*
  336. * See whether it is appropriate to use the ->nocb_bypass list in order
  337. * to control contention on ->nocb_lock. A limited number of direct
  338. * enqueues are permitted into ->cblist per jiffy. If ->nocb_bypass
  339. * is non-empty, further callbacks must be placed into ->nocb_bypass,
  340. * otherwise rcu_barrier() breaks. Use rcu_nocb_flush_bypass() to switch
  341. * back to direct use of ->cblist. However, ->nocb_bypass should not be
  342. * used if ->cblist is empty, because otherwise callbacks can be stranded
  343. * on ->nocb_bypass because we cannot count on the current CPU ever again
  344. * invoking call_rcu(). The general rule is that if ->nocb_bypass is
  345. * non-empty, the corresponding no-CBs grace-period kthread must not be
  346. * in an indefinite sleep state.
  347. *
  348. * Finally, it is not permitted to use the bypass during early boot,
  349. * as doing so would confuse the auto-initialization code. Besides
  350. * which, there is no point in worrying about lock contention while
  351. * there is only one CPU in operation.
  352. */
  353. static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
  354. bool *was_alldone, unsigned long flags,
  355. bool lazy)
  356. {
  357. unsigned long c;
  358. unsigned long cur_gp_seq;
  359. unsigned long j = jiffies;
  360. long ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
  361. bool bypass_is_lazy = (ncbs == READ_ONCE(rdp->lazy_len));
  362. lockdep_assert_irqs_disabled();
  363. // Pure softirq/rcuc based processing: no bypassing, no
  364. // locking.
  365. if (!rcu_rdp_is_offloaded(rdp)) {
  366. *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
  367. return false;
  368. }
  369. // Don't use ->nocb_bypass during early boot.
  370. if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) {
  371. rcu_nocb_lock(rdp);
  372. WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
  373. *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
  374. return false;
  375. }
  376. // If we have advanced to a new jiffy, reset counts to allow
  377. // moving back from ->nocb_bypass to ->cblist.
  378. if (j == rdp->nocb_nobypass_last) {
  379. c = rdp->nocb_nobypass_count + 1;
  380. } else {
  381. WRITE_ONCE(rdp->nocb_nobypass_last, j);
  382. c = rdp->nocb_nobypass_count - nocb_nobypass_lim_per_jiffy;
  383. if (ULONG_CMP_LT(rdp->nocb_nobypass_count,
  384. nocb_nobypass_lim_per_jiffy))
  385. c = 0;
  386. else if (c > nocb_nobypass_lim_per_jiffy)
  387. c = nocb_nobypass_lim_per_jiffy;
  388. }
  389. WRITE_ONCE(rdp->nocb_nobypass_count, c);
  390. // If there hasn't yet been all that many ->cblist enqueues
  391. // this jiffy, tell the caller to enqueue onto ->cblist. But flush
  392. // ->nocb_bypass first.
  393. // Lazy CBs throttle this back and do immediate bypass queuing.
  394. if (rdp->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy && !lazy) {
  395. rcu_nocb_lock(rdp);
  396. *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
  397. if (*was_alldone)
  398. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  399. TPS("FirstQ"));
  400. WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, j, false));
  401. WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
  402. return false; // Caller must enqueue the callback.
  403. }
  404. // If ->nocb_bypass has been used too long or is too full,
  405. // flush ->nocb_bypass to ->cblist.
  406. if ((ncbs && !bypass_is_lazy && j != READ_ONCE(rdp->nocb_bypass_first)) ||
  407. (ncbs && bypass_is_lazy &&
  408. (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + rcu_get_jiffies_lazy_flush()))) ||
  409. ncbs >= qhimark) {
  410. rcu_nocb_lock(rdp);
  411. *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
  412. if (!rcu_nocb_flush_bypass(rdp, rhp, j, lazy)) {
  413. if (*was_alldone)
  414. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  415. TPS("FirstQ"));
  416. WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
  417. return false; // Caller must enqueue the callback.
  418. }
  419. if (j != rdp->nocb_gp_adv_time &&
  420. rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
  421. rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
  422. rcu_advance_cbs_nowake(rdp->mynode, rdp);
  423. rdp->nocb_gp_adv_time = j;
  424. }
  425. // The flush succeeded and we moved CBs into the regular list.
  426. // Don't wait for the wake up timer as it may be too far ahead.
  427. // Wake up the GP thread now instead, if the cblist was empty.
  428. __call_rcu_nocb_wake(rdp, *was_alldone, flags);
  429. return true; // Callback already enqueued.
  430. }
  431. // We need to use the bypass.
  432. rcu_nocb_bypass_lock(rdp);
  433. ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
  434. rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
  435. rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
  436. if (lazy)
  437. WRITE_ONCE(rdp->lazy_len, rdp->lazy_len + 1);
  438. if (!ncbs) {
  439. WRITE_ONCE(rdp->nocb_bypass_first, j);
  440. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FirstBQ"));
  441. }
  442. rcu_nocb_bypass_unlock(rdp);
  443. // A wake up of the grace period kthread or timer adjustment
  444. // needs to be done only if:
  445. // 1. Bypass list was fully empty before (this is the first
  446. // bypass list entry), or:
  447. // 2. Both of these conditions are met:
  448. // a. The bypass list previously had only lazy CBs, and:
  449. // b. The new CB is non-lazy.
  450. if (!ncbs || (bypass_is_lazy && !lazy)) {
  451. // No-CBs GP kthread might be indefinitely asleep, if so, wake.
  452. rcu_nocb_lock(rdp); // Rare during call_rcu() flood.
  453. if (!rcu_segcblist_pend_cbs(&rdp->cblist)) {
  454. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  455. TPS("FirstBQwake"));
  456. __call_rcu_nocb_wake(rdp, true, flags);
  457. } else {
  458. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  459. TPS("FirstBQnoWake"));
  460. rcu_nocb_unlock(rdp);
  461. }
  462. }
  463. return true; // Callback already enqueued.
  464. }
  465. /*
  466. * Awaken the no-CBs grace-period kthread if needed, either due to it
  467. * legitimately being asleep or due to overload conditions.
  468. *
  469. * If warranted, also wake up the kthread servicing this CPUs queues.
  470. */
  471. static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,
  472. unsigned long flags)
  473. __releases(rdp->nocb_lock)
  474. {
  475. long bypass_len;
  476. unsigned long cur_gp_seq;
  477. unsigned long j;
  478. long lazy_len;
  479. long len;
  480. struct task_struct *t;
  481. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  482. // If we are being polled or there is no kthread, just leave.
  483. t = READ_ONCE(rdp->nocb_gp_kthread);
  484. if (rcu_nocb_poll || !t) {
  485. rcu_nocb_unlock(rdp);
  486. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  487. TPS("WakeNotPoll"));
  488. return;
  489. }
  490. // Need to actually to a wakeup.
  491. len = rcu_segcblist_n_cbs(&rdp->cblist);
  492. bypass_len = rcu_cblist_n_cbs(&rdp->nocb_bypass);
  493. lazy_len = READ_ONCE(rdp->lazy_len);
  494. if (was_alldone) {
  495. rdp->qlen_last_fqs_check = len;
  496. // Only lazy CBs in bypass list
  497. if (lazy_len && bypass_len == lazy_len) {
  498. rcu_nocb_unlock(rdp);
  499. wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_LAZY,
  500. TPS("WakeLazy"));
  501. } else if (!irqs_disabled_flags(flags) && cpu_online(rdp->cpu)) {
  502. /* ... if queue was empty ... */
  503. rcu_nocb_unlock(rdp);
  504. wake_nocb_gp(rdp, false);
  505. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  506. TPS("WakeEmpty"));
  507. } else {
  508. /*
  509. * Don't do the wake-up upfront on fragile paths.
  510. * Also offline CPUs can't call swake_up_one_online() from
  511. * (soft-)IRQs. Rely on the final deferred wake-up from
  512. * rcutree_report_cpu_dead()
  513. */
  514. rcu_nocb_unlock(rdp);
  515. wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE,
  516. TPS("WakeEmptyIsDeferred"));
  517. }
  518. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  519. /* ... or if many callbacks queued. */
  520. rdp->qlen_last_fqs_check = len;
  521. j = jiffies;
  522. if (j != rdp->nocb_gp_adv_time &&
  523. rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
  524. rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
  525. rcu_advance_cbs_nowake(rdp->mynode, rdp);
  526. rdp->nocb_gp_adv_time = j;
  527. }
  528. smp_mb(); /* Enqueue before timer_pending(). */
  529. if ((rdp->nocb_cb_sleep ||
  530. !rcu_segcblist_ready_cbs(&rdp->cblist)) &&
  531. !timer_pending(&rdp_gp->nocb_timer)) {
  532. rcu_nocb_unlock(rdp);
  533. wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_FORCE,
  534. TPS("WakeOvfIsDeferred"));
  535. } else {
  536. rcu_nocb_unlock(rdp);
  537. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
  538. }
  539. } else {
  540. rcu_nocb_unlock(rdp);
  541. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
  542. }
  543. }
  544. static void call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *head,
  545. rcu_callback_t func, unsigned long flags, bool lazy)
  546. {
  547. bool was_alldone;
  548. if (!rcu_nocb_try_bypass(rdp, head, &was_alldone, flags, lazy)) {
  549. /* Not enqueued on bypass but locked, do regular enqueue */
  550. rcutree_enqueue(rdp, head, func);
  551. __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
  552. }
  553. }
  554. static void nocb_gp_toggle_rdp(struct rcu_data *rdp_gp, struct rcu_data *rdp)
  555. {
  556. struct rcu_segcblist *cblist = &rdp->cblist;
  557. unsigned long flags;
  558. /*
  559. * Locking orders future de-offloaded callbacks enqueue against previous
  560. * handling of this rdp. Ie: Make sure rcuog is done with this rdp before
  561. * deoffloaded callbacks can be enqueued.
  562. */
  563. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  564. if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
  565. /*
  566. * Offloading. Set our flag and notify the offload worker.
  567. * We will handle this rdp until it ever gets de-offloaded.
  568. */
  569. list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
  570. rcu_segcblist_set_flags(cblist, SEGCBLIST_OFFLOADED);
  571. } else {
  572. /*
  573. * De-offloading. Clear our flag and notify the de-offload worker.
  574. * We will ignore this rdp until it ever gets re-offloaded.
  575. */
  576. list_del(&rdp->nocb_entry_rdp);
  577. rcu_segcblist_clear_flags(cblist, SEGCBLIST_OFFLOADED);
  578. }
  579. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  580. }
  581. static void nocb_gp_sleep(struct rcu_data *my_rdp, int cpu)
  582. {
  583. trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Sleep"));
  584. swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
  585. !READ_ONCE(my_rdp->nocb_gp_sleep));
  586. trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("EndSleep"));
  587. }
  588. /*
  589. * No-CBs GP kthreads come here to wait for additional callbacks to show up
  590. * or for grace periods to end.
  591. */
  592. static void nocb_gp_wait(struct rcu_data *my_rdp)
  593. {
  594. bool bypass = false;
  595. int __maybe_unused cpu = my_rdp->cpu;
  596. unsigned long cur_gp_seq;
  597. unsigned long flags;
  598. bool gotcbs = false;
  599. unsigned long j = jiffies;
  600. bool lazy = false;
  601. bool needwait_gp = false; // This prevents actual uninitialized use.
  602. bool needwake;
  603. bool needwake_gp;
  604. struct rcu_data *rdp, *rdp_toggling = NULL;
  605. struct rcu_node *rnp;
  606. unsigned long wait_gp_seq = 0; // Suppress "use uninitialized" warning.
  607. bool wasempty = false;
  608. /*
  609. * Each pass through the following loop checks for CBs and for the
  610. * nearest grace period (if any) to wait for next. The CB kthreads
  611. * and the global grace-period kthread are awakened if needed.
  612. */
  613. WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
  614. /*
  615. * An rcu_data structure is removed from the list after its
  616. * CPU is de-offloaded and added to the list before that CPU is
  617. * (re-)offloaded. If the following loop happens to be referencing
  618. * that rcu_data structure during the time that the corresponding
  619. * CPU is de-offloaded and then immediately re-offloaded, this
  620. * loop's rdp pointer will be carried to the end of the list by
  621. * the resulting pair of list operations. This can cause the loop
  622. * to skip over some of the rcu_data structures that were supposed
  623. * to have been scanned. Fortunately a new iteration through the
  624. * entire loop is forced after a given CPU's rcu_data structure
  625. * is added to the list, so the skipped-over rcu_data structures
  626. * won't be ignored for long.
  627. */
  628. list_for_each_entry(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp) {
  629. long bypass_ncbs;
  630. bool flush_bypass = false;
  631. long lazy_ncbs;
  632. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
  633. rcu_nocb_lock_irqsave(rdp, flags);
  634. lockdep_assert_held(&rdp->nocb_lock);
  635. bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
  636. lazy_ncbs = READ_ONCE(rdp->lazy_len);
  637. if (bypass_ncbs && (lazy_ncbs == bypass_ncbs) &&
  638. (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + rcu_get_jiffies_lazy_flush()) ||
  639. bypass_ncbs > 2 * qhimark)) {
  640. flush_bypass = true;
  641. } else if (bypass_ncbs && (lazy_ncbs != bypass_ncbs) &&
  642. (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
  643. bypass_ncbs > 2 * qhimark)) {
  644. flush_bypass = true;
  645. } else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) {
  646. rcu_nocb_unlock_irqrestore(rdp, flags);
  647. continue; /* No callbacks here, try next. */
  648. }
  649. if (flush_bypass) {
  650. // Bypass full or old, so flush it.
  651. (void)rcu_nocb_try_flush_bypass(rdp, j);
  652. bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
  653. lazy_ncbs = READ_ONCE(rdp->lazy_len);
  654. }
  655. if (bypass_ncbs) {
  656. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  657. bypass_ncbs == lazy_ncbs ? TPS("Lazy") : TPS("Bypass"));
  658. if (bypass_ncbs == lazy_ncbs)
  659. lazy = true;
  660. else
  661. bypass = true;
  662. }
  663. rnp = rdp->mynode;
  664. // Advance callbacks if helpful and low contention.
  665. needwake_gp = false;
  666. if (!rcu_segcblist_restempty(&rdp->cblist,
  667. RCU_NEXT_READY_TAIL) ||
  668. (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
  669. rcu_seq_done(&rnp->gp_seq, cur_gp_seq))) {
  670. raw_spin_lock_rcu_node(rnp); /* irqs disabled. */
  671. needwake_gp = rcu_advance_cbs(rnp, rdp);
  672. wasempty = rcu_segcblist_restempty(&rdp->cblist,
  673. RCU_NEXT_READY_TAIL);
  674. raw_spin_unlock_rcu_node(rnp); /* irqs disabled. */
  675. }
  676. // Need to wait on some grace period?
  677. WARN_ON_ONCE(wasempty &&
  678. !rcu_segcblist_restempty(&rdp->cblist,
  679. RCU_NEXT_READY_TAIL));
  680. if (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq)) {
  681. if (!needwait_gp ||
  682. ULONG_CMP_LT(cur_gp_seq, wait_gp_seq))
  683. wait_gp_seq = cur_gp_seq;
  684. needwait_gp = true;
  685. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
  686. TPS("NeedWaitGP"));
  687. }
  688. if (rcu_segcblist_ready_cbs(&rdp->cblist)) {
  689. needwake = rdp->nocb_cb_sleep;
  690. WRITE_ONCE(rdp->nocb_cb_sleep, false);
  691. } else {
  692. needwake = false;
  693. }
  694. rcu_nocb_unlock_irqrestore(rdp, flags);
  695. if (needwake) {
  696. swake_up_one(&rdp->nocb_cb_wq);
  697. gotcbs = true;
  698. }
  699. if (needwake_gp)
  700. rcu_gp_kthread_wake();
  701. }
  702. my_rdp->nocb_gp_bypass = bypass;
  703. my_rdp->nocb_gp_gp = needwait_gp;
  704. my_rdp->nocb_gp_seq = needwait_gp ? wait_gp_seq : 0;
  705. // At least one child with non-empty ->nocb_bypass, so set
  706. // timer in order to avoid stranding its callbacks.
  707. if (!rcu_nocb_poll) {
  708. // If bypass list only has lazy CBs. Add a deferred lazy wake up.
  709. if (lazy && !bypass) {
  710. wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_LAZY,
  711. TPS("WakeLazyIsDeferred"));
  712. // Otherwise add a deferred bypass wake up.
  713. } else if (bypass) {
  714. wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_BYPASS,
  715. TPS("WakeBypassIsDeferred"));
  716. }
  717. }
  718. if (rcu_nocb_poll) {
  719. /* Polling, so trace if first poll in the series. */
  720. if (gotcbs)
  721. trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Poll"));
  722. if (list_empty(&my_rdp->nocb_head_rdp)) {
  723. raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
  724. if (!my_rdp->nocb_toggling_rdp)
  725. WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
  726. raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
  727. /* Wait for any offloading rdp */
  728. nocb_gp_sleep(my_rdp, cpu);
  729. } else {
  730. schedule_timeout_idle(1);
  731. }
  732. } else if (!needwait_gp) {
  733. /* Wait for callbacks to appear. */
  734. nocb_gp_sleep(my_rdp, cpu);
  735. } else {
  736. rnp = my_rdp->mynode;
  737. trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("StartWait"));
  738. swait_event_interruptible_exclusive(
  739. rnp->nocb_gp_wq[rcu_seq_ctr(wait_gp_seq) & 0x1],
  740. rcu_seq_done(&rnp->gp_seq, wait_gp_seq) ||
  741. !READ_ONCE(my_rdp->nocb_gp_sleep));
  742. trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("EndWait"));
  743. }
  744. if (!rcu_nocb_poll) {
  745. raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
  746. // (De-)queue an rdp to/from the group if its nocb state is changing
  747. rdp_toggling = my_rdp->nocb_toggling_rdp;
  748. if (rdp_toggling)
  749. my_rdp->nocb_toggling_rdp = NULL;
  750. if (my_rdp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
  751. WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  752. del_timer(&my_rdp->nocb_timer);
  753. }
  754. WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
  755. raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
  756. } else {
  757. rdp_toggling = READ_ONCE(my_rdp->nocb_toggling_rdp);
  758. if (rdp_toggling) {
  759. /*
  760. * Paranoid locking to make sure nocb_toggling_rdp is well
  761. * reset *before* we (re)set SEGCBLIST_KTHREAD_GP or we could
  762. * race with another round of nocb toggling for this rdp.
  763. * Nocb locking should prevent from that already but we stick
  764. * to paranoia, especially in rare path.
  765. */
  766. raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
  767. my_rdp->nocb_toggling_rdp = NULL;
  768. raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
  769. }
  770. }
  771. if (rdp_toggling) {
  772. nocb_gp_toggle_rdp(my_rdp, rdp_toggling);
  773. swake_up_one(&rdp_toggling->nocb_state_wq);
  774. }
  775. my_rdp->nocb_gp_seq = -1;
  776. WARN_ON(signal_pending(current));
  777. }
  778. /*
  779. * No-CBs grace-period-wait kthread. There is one of these per group
  780. * of CPUs, but only once at least one CPU in that group has come online
  781. * at least once since boot. This kthread checks for newly posted
  782. * callbacks from any of the CPUs it is responsible for, waits for a
  783. * grace period, then awakens all of the rcu_nocb_cb_kthread() instances
  784. * that then have callback-invocation work to do.
  785. */
  786. static int rcu_nocb_gp_kthread(void *arg)
  787. {
  788. struct rcu_data *rdp = arg;
  789. for (;;) {
  790. WRITE_ONCE(rdp->nocb_gp_loops, rdp->nocb_gp_loops + 1);
  791. nocb_gp_wait(rdp);
  792. cond_resched_tasks_rcu_qs();
  793. }
  794. return 0;
  795. }
  796. static inline bool nocb_cb_wait_cond(struct rcu_data *rdp)
  797. {
  798. return !READ_ONCE(rdp->nocb_cb_sleep) || kthread_should_park();
  799. }
  800. /*
  801. * Invoke any ready callbacks from the corresponding no-CBs CPU,
  802. * then, if there are no more, wait for more to appear.
  803. */
  804. static void nocb_cb_wait(struct rcu_data *rdp)
  805. {
  806. struct rcu_segcblist *cblist = &rdp->cblist;
  807. unsigned long cur_gp_seq;
  808. unsigned long flags;
  809. bool needwake_gp = false;
  810. struct rcu_node *rnp = rdp->mynode;
  811. swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
  812. nocb_cb_wait_cond(rdp));
  813. if (kthread_should_park()) {
  814. /*
  815. * kthread_park() must be preceded by an rcu_barrier().
  816. * But yet another rcu_barrier() might have sneaked in between
  817. * the barrier callback execution and the callbacks counter
  818. * decrement.
  819. */
  820. if (rdp->nocb_cb_sleep) {
  821. rcu_nocb_lock_irqsave(rdp, flags);
  822. WARN_ON_ONCE(rcu_segcblist_n_cbs(&rdp->cblist));
  823. rcu_nocb_unlock_irqrestore(rdp, flags);
  824. kthread_parkme();
  825. }
  826. } else if (READ_ONCE(rdp->nocb_cb_sleep)) {
  827. WARN_ON(signal_pending(current));
  828. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
  829. }
  830. WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
  831. local_irq_save(flags);
  832. rcu_momentary_eqs();
  833. local_irq_restore(flags);
  834. /*
  835. * Disable BH to provide the expected environment. Also, when
  836. * transitioning to/from NOCB mode, a self-requeuing callback might
  837. * be invoked from softirq. A short grace period could cause both
  838. * instances of this callback would execute concurrently.
  839. */
  840. local_bh_disable();
  841. rcu_do_batch(rdp);
  842. local_bh_enable();
  843. lockdep_assert_irqs_enabled();
  844. rcu_nocb_lock_irqsave(rdp, flags);
  845. if (rcu_segcblist_nextgp(cblist, &cur_gp_seq) &&
  846. rcu_seq_done(&rnp->gp_seq, cur_gp_seq) &&
  847. raw_spin_trylock_rcu_node(rnp)) { /* irqs already disabled. */
  848. needwake_gp = rcu_advance_cbs(rdp->mynode, rdp);
  849. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  850. }
  851. if (!rcu_segcblist_ready_cbs(cblist)) {
  852. WRITE_ONCE(rdp->nocb_cb_sleep, true);
  853. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("CBSleep"));
  854. } else {
  855. WRITE_ONCE(rdp->nocb_cb_sleep, false);
  856. }
  857. rcu_nocb_unlock_irqrestore(rdp, flags);
  858. if (needwake_gp)
  859. rcu_gp_kthread_wake();
  860. }
  861. /*
  862. * Per-rcu_data kthread, but only for no-CBs CPUs. Repeatedly invoke
  863. * nocb_cb_wait() to do the dirty work.
  864. */
  865. static int rcu_nocb_cb_kthread(void *arg)
  866. {
  867. struct rcu_data *rdp = arg;
  868. // Each pass through this loop does one callback batch, and,
  869. // if there are no more ready callbacks, waits for them.
  870. for (;;) {
  871. nocb_cb_wait(rdp);
  872. cond_resched_tasks_rcu_qs();
  873. }
  874. return 0;
  875. }
  876. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  877. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
  878. {
  879. return READ_ONCE(rdp->nocb_defer_wakeup) >= level;
  880. }
  881. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  882. static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,
  883. struct rcu_data *rdp, int level,
  884. unsigned long flags)
  885. __releases(rdp_gp->nocb_gp_lock)
  886. {
  887. int ndw;
  888. int ret;
  889. if (!rcu_nocb_need_deferred_wakeup(rdp_gp, level)) {
  890. raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
  891. return false;
  892. }
  893. ndw = rdp_gp->nocb_defer_wakeup;
  894. ret = __wake_nocb_gp(rdp_gp, rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
  895. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
  896. return ret;
  897. }
  898. /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
  899. static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
  900. {
  901. unsigned long flags;
  902. struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
  903. WARN_ON_ONCE(rdp->nocb_gp_rdp != rdp);
  904. trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Timer"));
  905. raw_spin_lock_irqsave(&rdp->nocb_gp_lock, flags);
  906. smp_mb__after_spinlock(); /* Timer expire before wakeup. */
  907. do_nocb_deferred_wakeup_common(rdp, rdp, RCU_NOCB_WAKE_BYPASS, flags);
  908. }
  909. /*
  910. * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
  911. * This means we do an inexact common-case check. Note that if
  912. * we miss, ->nocb_timer will eventually clean things up.
  913. */
  914. static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
  915. {
  916. unsigned long flags;
  917. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  918. if (!rdp_gp || !rcu_nocb_need_deferred_wakeup(rdp_gp, RCU_NOCB_WAKE))
  919. return false;
  920. raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
  921. return do_nocb_deferred_wakeup_common(rdp_gp, rdp, RCU_NOCB_WAKE, flags);
  922. }
  923. void rcu_nocb_flush_deferred_wakeup(void)
  924. {
  925. do_nocb_deferred_wakeup(this_cpu_ptr(&rcu_data));
  926. }
  927. EXPORT_SYMBOL_GPL(rcu_nocb_flush_deferred_wakeup);
  928. static int rcu_nocb_queue_toggle_rdp(struct rcu_data *rdp)
  929. {
  930. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  931. bool wake_gp = false;
  932. unsigned long flags;
  933. raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
  934. // Queue this rdp for add/del to/from the list to iterate on rcuog
  935. WRITE_ONCE(rdp_gp->nocb_toggling_rdp, rdp);
  936. if (rdp_gp->nocb_gp_sleep) {
  937. rdp_gp->nocb_gp_sleep = false;
  938. wake_gp = true;
  939. }
  940. raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
  941. return wake_gp;
  942. }
  943. static bool rcu_nocb_rdp_deoffload_wait_cond(struct rcu_data *rdp)
  944. {
  945. unsigned long flags;
  946. bool ret;
  947. /*
  948. * Locking makes sure rcuog is done handling this rdp before deoffloaded
  949. * enqueue can happen. Also it keeps the SEGCBLIST_OFFLOADED flag stable
  950. * while the ->nocb_lock is held.
  951. */
  952. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  953. ret = !rcu_segcblist_test_flags(&rdp->cblist, SEGCBLIST_OFFLOADED);
  954. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  955. return ret;
  956. }
  957. static int rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
  958. {
  959. unsigned long flags;
  960. int wake_gp;
  961. struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
  962. /* CPU must be offline, unless it's early boot */
  963. WARN_ON_ONCE(cpu_online(rdp->cpu) && rdp->cpu != raw_smp_processor_id());
  964. pr_info("De-offloading %d\n", rdp->cpu);
  965. /* Flush all callbacks from segcblist and bypass */
  966. rcu_barrier();
  967. /*
  968. * Make sure the rcuoc kthread isn't in the middle of a nocb locked
  969. * sequence while offloading is deactivated, along with nocb locking.
  970. */
  971. if (rdp->nocb_cb_kthread)
  972. kthread_park(rdp->nocb_cb_kthread);
  973. rcu_nocb_lock_irqsave(rdp, flags);
  974. WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
  975. WARN_ON_ONCE(rcu_segcblist_n_cbs(&rdp->cblist));
  976. rcu_nocb_unlock_irqrestore(rdp, flags);
  977. wake_gp = rcu_nocb_queue_toggle_rdp(rdp);
  978. mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
  979. if (rdp_gp->nocb_gp_kthread) {
  980. if (wake_gp)
  981. wake_up_process(rdp_gp->nocb_gp_kthread);
  982. swait_event_exclusive(rdp->nocb_state_wq,
  983. rcu_nocb_rdp_deoffload_wait_cond(rdp));
  984. } else {
  985. /*
  986. * No kthread to clear the flags for us or remove the rdp from the nocb list
  987. * to iterate. Do it here instead. Locking doesn't look stricly necessary
  988. * but we stick to paranoia in this rare path.
  989. */
  990. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  991. rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_OFFLOADED);
  992. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  993. list_del(&rdp->nocb_entry_rdp);
  994. }
  995. mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
  996. return 0;
  997. }
  998. int rcu_nocb_cpu_deoffload(int cpu)
  999. {
  1000. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1001. int ret = 0;
  1002. cpus_read_lock();
  1003. mutex_lock(&rcu_state.nocb_mutex);
  1004. if (rcu_rdp_is_offloaded(rdp)) {
  1005. if (!cpu_online(cpu)) {
  1006. ret = rcu_nocb_rdp_deoffload(rdp);
  1007. if (!ret)
  1008. cpumask_clear_cpu(cpu, rcu_nocb_mask);
  1009. } else {
  1010. pr_info("NOCB: Cannot CB-deoffload online CPU %d\n", rdp->cpu);
  1011. ret = -EINVAL;
  1012. }
  1013. }
  1014. mutex_unlock(&rcu_state.nocb_mutex);
  1015. cpus_read_unlock();
  1016. return ret;
  1017. }
  1018. EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
  1019. static bool rcu_nocb_rdp_offload_wait_cond(struct rcu_data *rdp)
  1020. {
  1021. unsigned long flags;
  1022. bool ret;
  1023. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  1024. ret = rcu_segcblist_test_flags(&rdp->cblist, SEGCBLIST_OFFLOADED);
  1025. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1026. return ret;
  1027. }
  1028. static int rcu_nocb_rdp_offload(struct rcu_data *rdp)
  1029. {
  1030. int wake_gp;
  1031. WARN_ON_ONCE(cpu_online(rdp->cpu));
  1032. /*
  1033. * For now we only support re-offload, ie: the rdp must have been
  1034. * offloaded on boot first.
  1035. */
  1036. if (!rdp->nocb_gp_rdp)
  1037. return -EINVAL;
  1038. if (WARN_ON_ONCE(!rdp->nocb_gp_kthread))
  1039. return -EINVAL;
  1040. pr_info("Offloading %d\n", rdp->cpu);
  1041. WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
  1042. WARN_ON_ONCE(rcu_segcblist_n_cbs(&rdp->cblist));
  1043. wake_gp = rcu_nocb_queue_toggle_rdp(rdp);
  1044. if (wake_gp)
  1045. wake_up_process(rdp->nocb_gp_kthread);
  1046. swait_event_exclusive(rdp->nocb_state_wq,
  1047. rcu_nocb_rdp_offload_wait_cond(rdp));
  1048. kthread_unpark(rdp->nocb_cb_kthread);
  1049. return 0;
  1050. }
  1051. int rcu_nocb_cpu_offload(int cpu)
  1052. {
  1053. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1054. int ret = 0;
  1055. cpus_read_lock();
  1056. mutex_lock(&rcu_state.nocb_mutex);
  1057. if (!rcu_rdp_is_offloaded(rdp)) {
  1058. if (!cpu_online(cpu)) {
  1059. ret = rcu_nocb_rdp_offload(rdp);
  1060. if (!ret)
  1061. cpumask_set_cpu(cpu, rcu_nocb_mask);
  1062. } else {
  1063. pr_info("NOCB: Cannot CB-offload online CPU %d\n", rdp->cpu);
  1064. ret = -EINVAL;
  1065. }
  1066. }
  1067. mutex_unlock(&rcu_state.nocb_mutex);
  1068. cpus_read_unlock();
  1069. return ret;
  1070. }
  1071. EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
  1072. #ifdef CONFIG_RCU_LAZY
  1073. static unsigned long
  1074. lazy_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  1075. {
  1076. int cpu;
  1077. unsigned long count = 0;
  1078. if (WARN_ON_ONCE(!cpumask_available(rcu_nocb_mask)))
  1079. return 0;
  1080. /* Protect rcu_nocb_mask against concurrent (de-)offloading. */
  1081. if (!mutex_trylock(&rcu_state.nocb_mutex))
  1082. return 0;
  1083. /* Snapshot count of all CPUs */
  1084. for_each_cpu(cpu, rcu_nocb_mask) {
  1085. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1086. count += READ_ONCE(rdp->lazy_len);
  1087. }
  1088. mutex_unlock(&rcu_state.nocb_mutex);
  1089. return count ? count : SHRINK_EMPTY;
  1090. }
  1091. static unsigned long
  1092. lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  1093. {
  1094. int cpu;
  1095. unsigned long flags;
  1096. unsigned long count = 0;
  1097. if (WARN_ON_ONCE(!cpumask_available(rcu_nocb_mask)))
  1098. return 0;
  1099. /*
  1100. * Protect against concurrent (de-)offloading. Otherwise nocb locking
  1101. * may be ignored or imbalanced.
  1102. */
  1103. if (!mutex_trylock(&rcu_state.nocb_mutex)) {
  1104. /*
  1105. * But really don't insist if nocb_mutex is contended since we
  1106. * can't guarantee that it will never engage in a dependency
  1107. * chain involving memory allocation. The lock is seldom contended
  1108. * anyway.
  1109. */
  1110. return 0;
  1111. }
  1112. /* Snapshot count of all CPUs */
  1113. for_each_cpu(cpu, rcu_nocb_mask) {
  1114. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1115. int _count;
  1116. if (WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp)))
  1117. continue;
  1118. if (!READ_ONCE(rdp->lazy_len))
  1119. continue;
  1120. rcu_nocb_lock_irqsave(rdp, flags);
  1121. /*
  1122. * Recheck under the nocb lock. Since we are not holding the bypass
  1123. * lock we may still race with increments from the enqueuer but still
  1124. * we know for sure if there is at least one lazy callback.
  1125. */
  1126. _count = READ_ONCE(rdp->lazy_len);
  1127. if (!_count) {
  1128. rcu_nocb_unlock_irqrestore(rdp, flags);
  1129. continue;
  1130. }
  1131. rcu_nocb_try_flush_bypass(rdp, jiffies);
  1132. rcu_nocb_unlock_irqrestore(rdp, flags);
  1133. wake_nocb_gp(rdp, false);
  1134. sc->nr_to_scan -= _count;
  1135. count += _count;
  1136. if (sc->nr_to_scan <= 0)
  1137. break;
  1138. }
  1139. mutex_unlock(&rcu_state.nocb_mutex);
  1140. return count ? count : SHRINK_STOP;
  1141. }
  1142. #endif // #ifdef CONFIG_RCU_LAZY
  1143. void __init rcu_init_nohz(void)
  1144. {
  1145. int cpu;
  1146. struct rcu_data *rdp;
  1147. const struct cpumask *cpumask = NULL;
  1148. struct shrinker * __maybe_unused lazy_rcu_shrinker;
  1149. #if defined(CONFIG_NO_HZ_FULL)
  1150. if (tick_nohz_full_running && !cpumask_empty(tick_nohz_full_mask))
  1151. cpumask = tick_nohz_full_mask;
  1152. #endif
  1153. if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_DEFAULT_ALL) &&
  1154. !rcu_state.nocb_is_setup && !cpumask)
  1155. cpumask = cpu_possible_mask;
  1156. if (cpumask) {
  1157. if (!cpumask_available(rcu_nocb_mask)) {
  1158. if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
  1159. pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
  1160. return;
  1161. }
  1162. }
  1163. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, cpumask);
  1164. rcu_state.nocb_is_setup = true;
  1165. }
  1166. if (!rcu_state.nocb_is_setup)
  1167. return;
  1168. #ifdef CONFIG_RCU_LAZY
  1169. lazy_rcu_shrinker = shrinker_alloc(0, "rcu-lazy");
  1170. if (!lazy_rcu_shrinker) {
  1171. pr_err("Failed to allocate lazy_rcu shrinker!\n");
  1172. } else {
  1173. lazy_rcu_shrinker->count_objects = lazy_rcu_shrink_count;
  1174. lazy_rcu_shrinker->scan_objects = lazy_rcu_shrink_scan;
  1175. shrinker_register(lazy_rcu_shrinker);
  1176. }
  1177. #endif // #ifdef CONFIG_RCU_LAZY
  1178. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  1179. pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
  1180. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  1181. rcu_nocb_mask);
  1182. }
  1183. if (cpumask_empty(rcu_nocb_mask))
  1184. pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
  1185. else
  1186. pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
  1187. cpumask_pr_args(rcu_nocb_mask));
  1188. if (rcu_nocb_poll)
  1189. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  1190. for_each_cpu(cpu, rcu_nocb_mask) {
  1191. rdp = per_cpu_ptr(&rcu_data, cpu);
  1192. if (rcu_segcblist_empty(&rdp->cblist))
  1193. rcu_segcblist_init(&rdp->cblist);
  1194. rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_OFFLOADED);
  1195. }
  1196. rcu_organize_nocb_kthreads();
  1197. }
  1198. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  1199. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  1200. {
  1201. init_swait_queue_head(&rdp->nocb_cb_wq);
  1202. init_swait_queue_head(&rdp->nocb_gp_wq);
  1203. init_swait_queue_head(&rdp->nocb_state_wq);
  1204. raw_spin_lock_init(&rdp->nocb_lock);
  1205. raw_spin_lock_init(&rdp->nocb_bypass_lock);
  1206. raw_spin_lock_init(&rdp->nocb_gp_lock);
  1207. timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
  1208. rcu_cblist_init(&rdp->nocb_bypass);
  1209. WRITE_ONCE(rdp->lazy_len, 0);
  1210. mutex_init(&rdp->nocb_gp_kthread_mutex);
  1211. }
  1212. /*
  1213. * If the specified CPU is a no-CBs CPU that does not already have its
  1214. * rcuo CB kthread, spawn it. Additionally, if the rcuo GP kthread
  1215. * for this CPU's group has not yet been created, spawn it as well.
  1216. */
  1217. static void rcu_spawn_cpu_nocb_kthread(int cpu)
  1218. {
  1219. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1220. struct rcu_data *rdp_gp;
  1221. struct task_struct *t;
  1222. struct sched_param sp;
  1223. if (!rcu_scheduler_fully_active || !rcu_state.nocb_is_setup)
  1224. return;
  1225. /* If there already is an rcuo kthread, then nothing to do. */
  1226. if (rdp->nocb_cb_kthread)
  1227. return;
  1228. /* If we didn't spawn the GP kthread first, reorganize! */
  1229. sp.sched_priority = kthread_prio;
  1230. rdp_gp = rdp->nocb_gp_rdp;
  1231. mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
  1232. if (!rdp_gp->nocb_gp_kthread) {
  1233. t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
  1234. "rcuog/%d", rdp_gp->cpu);
  1235. if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__)) {
  1236. mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
  1237. goto err;
  1238. }
  1239. WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
  1240. if (kthread_prio)
  1241. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1242. }
  1243. mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
  1244. /* Spawn the kthread for this CPU. */
  1245. t = kthread_create(rcu_nocb_cb_kthread, rdp,
  1246. "rcuo%c/%d", rcu_state.abbr, cpu);
  1247. if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
  1248. goto err;
  1249. if (rcu_rdp_is_offloaded(rdp))
  1250. wake_up_process(t);
  1251. else
  1252. kthread_park(t);
  1253. if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_CB_BOOST) && kthread_prio)
  1254. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1255. WRITE_ONCE(rdp->nocb_cb_kthread, t);
  1256. WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
  1257. return;
  1258. err:
  1259. /*
  1260. * No need to protect against concurrent rcu_barrier()
  1261. * because the number of callbacks should be 0 for a non-boot CPU,
  1262. * therefore rcu_barrier() shouldn't even try to grab the nocb_lock.
  1263. * But hold nocb_mutex to avoid nocb_lock imbalance from shrinker.
  1264. */
  1265. WARN_ON_ONCE(system_state > SYSTEM_BOOTING && rcu_segcblist_n_cbs(&rdp->cblist));
  1266. mutex_lock(&rcu_state.nocb_mutex);
  1267. if (rcu_rdp_is_offloaded(rdp)) {
  1268. rcu_nocb_rdp_deoffload(rdp);
  1269. cpumask_clear_cpu(cpu, rcu_nocb_mask);
  1270. }
  1271. mutex_unlock(&rcu_state.nocb_mutex);
  1272. }
  1273. /* How many CB CPU IDs per GP kthread? Default of -1 for sqrt(nr_cpu_ids). */
  1274. static int rcu_nocb_gp_stride = -1;
  1275. module_param(rcu_nocb_gp_stride, int, 0444);
  1276. /*
  1277. * Initialize GP-CB relationships for all no-CBs CPU.
  1278. */
  1279. static void __init rcu_organize_nocb_kthreads(void)
  1280. {
  1281. int cpu;
  1282. bool firsttime = true;
  1283. bool gotnocbs = false;
  1284. bool gotnocbscbs = true;
  1285. int ls = rcu_nocb_gp_stride;
  1286. int nl = 0; /* Next GP kthread. */
  1287. struct rcu_data *rdp;
  1288. struct rcu_data *rdp_gp = NULL; /* Suppress misguided gcc warn. */
  1289. if (!cpumask_available(rcu_nocb_mask))
  1290. return;
  1291. if (ls == -1) {
  1292. ls = nr_cpu_ids / int_sqrt(nr_cpu_ids);
  1293. rcu_nocb_gp_stride = ls;
  1294. }
  1295. /*
  1296. * Each pass through this loop sets up one rcu_data structure.
  1297. * Should the corresponding CPU come online in the future, then
  1298. * we will spawn the needed set of rcu_nocb_kthread() kthreads.
  1299. */
  1300. for_each_possible_cpu(cpu) {
  1301. rdp = per_cpu_ptr(&rcu_data, cpu);
  1302. if (rdp->cpu >= nl) {
  1303. /* New GP kthread, set up for CBs & next GP. */
  1304. gotnocbs = true;
  1305. nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
  1306. rdp_gp = rdp;
  1307. INIT_LIST_HEAD(&rdp->nocb_head_rdp);
  1308. if (dump_tree) {
  1309. if (!firsttime)
  1310. pr_cont("%s\n", gotnocbscbs
  1311. ? "" : " (self only)");
  1312. gotnocbscbs = false;
  1313. firsttime = false;
  1314. pr_alert("%s: No-CB GP kthread CPU %d:",
  1315. __func__, cpu);
  1316. }
  1317. } else {
  1318. /* Another CB kthread, link to previous GP kthread. */
  1319. gotnocbscbs = true;
  1320. if (dump_tree)
  1321. pr_cont(" %d", cpu);
  1322. }
  1323. rdp->nocb_gp_rdp = rdp_gp;
  1324. if (cpumask_test_cpu(cpu, rcu_nocb_mask))
  1325. list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
  1326. }
  1327. if (gotnocbs && dump_tree)
  1328. pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
  1329. }
  1330. /*
  1331. * Bind the current task to the offloaded CPUs. If there are no offloaded
  1332. * CPUs, leave the task unbound. Splat if the bind attempt fails.
  1333. */
  1334. void rcu_bind_current_to_nocb(void)
  1335. {
  1336. if (cpumask_available(rcu_nocb_mask) && !cpumask_empty(rcu_nocb_mask))
  1337. WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
  1338. }
  1339. EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
  1340. // The ->on_cpu field is available only in CONFIG_SMP=y, so...
  1341. #ifdef CONFIG_SMP
  1342. static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
  1343. {
  1344. return tsp && task_is_running(tsp) && !tsp->on_cpu ? "!" : "";
  1345. }
  1346. #else // #ifdef CONFIG_SMP
  1347. static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
  1348. {
  1349. return "";
  1350. }
  1351. #endif // #else #ifdef CONFIG_SMP
  1352. /*
  1353. * Dump out nocb grace-period kthread state for the specified rcu_data
  1354. * structure.
  1355. */
  1356. static void show_rcu_nocb_gp_state(struct rcu_data *rdp)
  1357. {
  1358. struct rcu_node *rnp = rdp->mynode;
  1359. pr_info("nocb GP %d %c%c%c%c%c %c[%c%c] %c%c:%ld rnp %d:%d %lu %c CPU %d%s\n",
  1360. rdp->cpu,
  1361. "kK"[!!rdp->nocb_gp_kthread],
  1362. "lL"[raw_spin_is_locked(&rdp->nocb_gp_lock)],
  1363. "dD"[!!rdp->nocb_defer_wakeup],
  1364. "tT"[timer_pending(&rdp->nocb_timer)],
  1365. "sS"[!!rdp->nocb_gp_sleep],
  1366. ".W"[swait_active(&rdp->nocb_gp_wq)],
  1367. ".W"[swait_active(&rnp->nocb_gp_wq[0])],
  1368. ".W"[swait_active(&rnp->nocb_gp_wq[1])],
  1369. ".B"[!!rdp->nocb_gp_bypass],
  1370. ".G"[!!rdp->nocb_gp_gp],
  1371. (long)rdp->nocb_gp_seq,
  1372. rnp->grplo, rnp->grphi, READ_ONCE(rdp->nocb_gp_loops),
  1373. rdp->nocb_gp_kthread ? task_state_to_char(rdp->nocb_gp_kthread) : '.',
  1374. rdp->nocb_gp_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
  1375. show_rcu_should_be_on_cpu(rdp->nocb_gp_kthread));
  1376. }
  1377. /* Dump out nocb kthread state for the specified rcu_data structure. */
  1378. static void show_rcu_nocb_state(struct rcu_data *rdp)
  1379. {
  1380. char bufw[20];
  1381. char bufr[20];
  1382. struct rcu_data *nocb_next_rdp;
  1383. struct rcu_segcblist *rsclp = &rdp->cblist;
  1384. bool waslocked;
  1385. bool wassleep;
  1386. if (rdp->nocb_gp_rdp == rdp)
  1387. show_rcu_nocb_gp_state(rdp);
  1388. nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
  1389. &rdp->nocb_entry_rdp,
  1390. typeof(*rdp),
  1391. nocb_entry_rdp);
  1392. sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
  1393. sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
  1394. pr_info(" CB %d^%d->%d %c%c%c%c%c F%ld L%ld C%d %c%c%s%c%s%c%c q%ld %c CPU %d%s\n",
  1395. rdp->cpu, rdp->nocb_gp_rdp->cpu,
  1396. nocb_next_rdp ? nocb_next_rdp->cpu : -1,
  1397. "kK"[!!rdp->nocb_cb_kthread],
  1398. "bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
  1399. "lL"[raw_spin_is_locked(&rdp->nocb_lock)],
  1400. "sS"[!!rdp->nocb_cb_sleep],
  1401. ".W"[swait_active(&rdp->nocb_cb_wq)],
  1402. jiffies - rdp->nocb_bypass_first,
  1403. jiffies - rdp->nocb_nobypass_last,
  1404. rdp->nocb_nobypass_count,
  1405. ".D"[rcu_segcblist_ready_cbs(rsclp)],
  1406. ".W"[!rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL)],
  1407. rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL) ? "" : bufw,
  1408. ".R"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL)],
  1409. rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL) ? "" : bufr,
  1410. ".N"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_TAIL)],
  1411. ".B"[!!rcu_cblist_n_cbs(&rdp->nocb_bypass)],
  1412. rcu_segcblist_n_cbs(&rdp->cblist),
  1413. rdp->nocb_cb_kthread ? task_state_to_char(rdp->nocb_cb_kthread) : '.',
  1414. rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_cb_kthread) : -1,
  1415. show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
  1416. /* It is OK for GP kthreads to have GP state. */
  1417. if (rdp->nocb_gp_rdp == rdp)
  1418. return;
  1419. waslocked = raw_spin_is_locked(&rdp->nocb_gp_lock);
  1420. wassleep = swait_active(&rdp->nocb_gp_wq);
  1421. if (!rdp->nocb_gp_sleep && !waslocked && !wassleep)
  1422. return; /* Nothing untoward. */
  1423. pr_info(" nocb GP activity on CB-only CPU!!! %c%c%c %c\n",
  1424. "lL"[waslocked],
  1425. "dD"[!!rdp->nocb_defer_wakeup],
  1426. "sS"[!!rdp->nocb_gp_sleep],
  1427. ".W"[wassleep]);
  1428. }
  1429. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  1430. /* No ->nocb_lock to acquire. */
  1431. static void rcu_nocb_lock(struct rcu_data *rdp)
  1432. {
  1433. }
  1434. /* No ->nocb_lock to release. */
  1435. static void rcu_nocb_unlock(struct rcu_data *rdp)
  1436. {
  1437. }
  1438. /* No ->nocb_lock to release. */
  1439. static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
  1440. unsigned long flags)
  1441. {
  1442. local_irq_restore(flags);
  1443. }
  1444. /* Lockdep check that ->cblist may be safely accessed. */
  1445. static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
  1446. {
  1447. lockdep_assert_irqs_disabled();
  1448. }
  1449. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  1450. {
  1451. }
  1452. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  1453. {
  1454. return NULL;
  1455. }
  1456. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1457. {
  1458. }
  1459. static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
  1460. {
  1461. return false;
  1462. }
  1463. static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
  1464. unsigned long j, bool lazy)
  1465. {
  1466. return true;
  1467. }
  1468. static void call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *head,
  1469. rcu_callback_t func, unsigned long flags, bool lazy)
  1470. {
  1471. WARN_ON_ONCE(1); /* Should be dead code! */
  1472. }
  1473. static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
  1474. unsigned long flags)
  1475. {
  1476. WARN_ON_ONCE(1); /* Should be dead code! */
  1477. }
  1478. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  1479. {
  1480. }
  1481. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
  1482. {
  1483. return false;
  1484. }
  1485. static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
  1486. {
  1487. return false;
  1488. }
  1489. static void rcu_spawn_cpu_nocb_kthread(int cpu)
  1490. {
  1491. }
  1492. static void show_rcu_nocb_state(struct rcu_data *rdp)
  1493. {
  1494. }
  1495. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */