tree_plugin.h 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  3. * Internal non-public definitions that provide either classic
  4. * or preemptible semantics.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you can access it online at
  18. * http://www.gnu.org/licenses/gpl-2.0.html.
  19. *
  20. * Copyright Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/gfp.h>
  28. #include <linux/oom.h>
  29. #include <linux/sched/debug.h>
  30. #include <linux/smpboot.h>
  31. #include <linux/sched/isolation.h>
  32. #include <uapi/linux/sched/types.h>
  33. #include "../time/tick-internal.h"
  34. #ifdef CONFIG_RCU_BOOST
  35. #include "../locking/rtmutex_common.h"
  36. /*
  37. * Control variables for per-CPU and per-rcu_node kthreads. These
  38. * handle all flavors of RCU.
  39. */
  40. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  41. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  42. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  43. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  44. #else /* #ifdef CONFIG_RCU_BOOST */
  45. /*
  46. * Some architectures do not define rt_mutexes, but if !CONFIG_RCU_BOOST,
  47. * all uses are in dead code. Provide a definition to keep the compiler
  48. * happy, but add WARN_ON_ONCE() to complain if used in the wrong place.
  49. * This probably needs to be excluded from -rt builds.
  50. */
  51. #define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
  52. #define rt_mutex_futex_unlock(x) WARN_ON_ONCE(1)
  53. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  54. #ifdef CONFIG_RCU_NOCB_CPU
  55. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  56. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  57. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  58. /*
  59. * Check the RCU kernel configuration parameters and print informative
  60. * messages about anything out of the ordinary.
  61. */
  62. static void __init rcu_bootup_announce_oddness(void)
  63. {
  64. if (IS_ENABLED(CONFIG_RCU_TRACE))
  65. pr_info("\tRCU event tracing is enabled.\n");
  66. if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
  67. (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
  68. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
  69. RCU_FANOUT);
  70. if (rcu_fanout_exact)
  71. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  72. if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
  73. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  74. if (IS_ENABLED(CONFIG_PROVE_RCU))
  75. pr_info("\tRCU lockdep checking is enabled.\n");
  76. if (RCU_NUM_LVLS >= 4)
  77. pr_info("\tFour(or more)-level hierarchy is enabled.\n");
  78. if (RCU_FANOUT_LEAF != 16)
  79. pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
  80. RCU_FANOUT_LEAF);
  81. if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
  82. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
  83. rcu_fanout_leaf);
  84. if (nr_cpu_ids != NR_CPUS)
  85. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
  86. #ifdef CONFIG_RCU_BOOST
  87. pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
  88. kthread_prio, CONFIG_RCU_BOOST_DELAY);
  89. #endif
  90. if (blimit != DEFAULT_RCU_BLIMIT)
  91. pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit);
  92. if (qhimark != DEFAULT_RCU_QHIMARK)
  93. pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark);
  94. if (qlowmark != DEFAULT_RCU_QLOMARK)
  95. pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark);
  96. if (jiffies_till_first_fqs != ULONG_MAX)
  97. pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs);
  98. if (jiffies_till_next_fqs != ULONG_MAX)
  99. pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs);
  100. if (rcu_kick_kthreads)
  101. pr_info("\tKick kthreads if too-long grace period.\n");
  102. if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
  103. pr_info("\tRCU callback double-/use-after-free debug enabled.\n");
  104. if (gp_preinit_delay)
  105. pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
  106. if (gp_init_delay)
  107. pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
  108. if (gp_cleanup_delay)
  109. pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_cleanup_delay);
  110. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG))
  111. pr_info("\tRCU debug extended QS entry/exit.\n");
  112. rcupdate_announce_bootup_oddness();
  113. }
  114. #ifdef CONFIG_PREEMPT_RCU
  115. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  116. static struct rcu_state *const rcu_state_p = &rcu_preempt_state;
  117. static struct rcu_data __percpu *const rcu_data_p = &rcu_preempt_data;
  118. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  119. bool wake);
  120. static void rcu_read_unlock_special(struct task_struct *t);
  121. /*
  122. * Tell them what RCU they are running.
  123. */
  124. static void __init rcu_bootup_announce(void)
  125. {
  126. pr_info("Preemptible hierarchical RCU implementation.\n");
  127. rcu_bootup_announce_oddness();
  128. }
  129. /* Flags for rcu_preempt_ctxt_queue() decision table. */
  130. #define RCU_GP_TASKS 0x8
  131. #define RCU_EXP_TASKS 0x4
  132. #define RCU_GP_BLKD 0x2
  133. #define RCU_EXP_BLKD 0x1
  134. /*
  135. * Queues a task preempted within an RCU-preempt read-side critical
  136. * section into the appropriate location within the ->blkd_tasks list,
  137. * depending on the states of any ongoing normal and expedited grace
  138. * periods. The ->gp_tasks pointer indicates which element the normal
  139. * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
  140. * indicates which element the expedited grace period is waiting on (again,
  141. * NULL if none). If a grace period is waiting on a given element in the
  142. * ->blkd_tasks list, it also waits on all subsequent elements. Thus,
  143. * adding a task to the tail of the list blocks any grace period that is
  144. * already waiting on one of the elements. In contrast, adding a task
  145. * to the head of the list won't block any grace period that is already
  146. * waiting on one of the elements.
  147. *
  148. * This queuing is imprecise, and can sometimes make an ongoing grace
  149. * period wait for a task that is not strictly speaking blocking it.
  150. * Given the choice, we needlessly block a normal grace period rather than
  151. * blocking an expedited grace period.
  152. *
  153. * Note that an endless sequence of expedited grace periods still cannot
  154. * indefinitely postpone a normal grace period. Eventually, all of the
  155. * fixed number of preempted tasks blocking the normal grace period that are
  156. * not also blocking the expedited grace period will resume and complete
  157. * their RCU read-side critical sections. At that point, the ->gp_tasks
  158. * pointer will equal the ->exp_tasks pointer, at which point the end of
  159. * the corresponding expedited grace period will also be the end of the
  160. * normal grace period.
  161. */
  162. static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
  163. __releases(rnp->lock) /* But leaves rrupts disabled. */
  164. {
  165. int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
  166. (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
  167. (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
  168. (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
  169. struct task_struct *t = current;
  170. raw_lockdep_assert_held_rcu_node(rnp);
  171. WARN_ON_ONCE(rdp->mynode != rnp);
  172. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  173. /* RCU better not be waiting on newly onlined CPUs! */
  174. WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
  175. rdp->grpmask);
  176. /*
  177. * Decide where to queue the newly blocked task. In theory,
  178. * this could be an if-statement. In practice, when I tried
  179. * that, it was quite messy.
  180. */
  181. switch (blkd_state) {
  182. case 0:
  183. case RCU_EXP_TASKS:
  184. case RCU_EXP_TASKS + RCU_GP_BLKD:
  185. case RCU_GP_TASKS:
  186. case RCU_GP_TASKS + RCU_EXP_TASKS:
  187. /*
  188. * Blocking neither GP, or first task blocking the normal
  189. * GP but not blocking the already-waiting expedited GP.
  190. * Queue at the head of the list to avoid unnecessarily
  191. * blocking the already-waiting GPs.
  192. */
  193. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  194. break;
  195. case RCU_EXP_BLKD:
  196. case RCU_GP_BLKD:
  197. case RCU_GP_BLKD + RCU_EXP_BLKD:
  198. case RCU_GP_TASKS + RCU_EXP_BLKD:
  199. case RCU_GP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  200. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  201. /*
  202. * First task arriving that blocks either GP, or first task
  203. * arriving that blocks the expedited GP (with the normal
  204. * GP already waiting), or a task arriving that blocks
  205. * both GPs with both GPs already waiting. Queue at the
  206. * tail of the list to avoid any GP waiting on any of the
  207. * already queued tasks that are not blocking it.
  208. */
  209. list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
  210. break;
  211. case RCU_EXP_TASKS + RCU_EXP_BLKD:
  212. case RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  213. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_EXP_BLKD:
  214. /*
  215. * Second or subsequent task blocking the expedited GP.
  216. * The task either does not block the normal GP, or is the
  217. * first task blocking the normal GP. Queue just after
  218. * the first task blocking the expedited GP.
  219. */
  220. list_add(&t->rcu_node_entry, rnp->exp_tasks);
  221. break;
  222. case RCU_GP_TASKS + RCU_GP_BLKD:
  223. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
  224. /*
  225. * Second or subsequent task blocking the normal GP.
  226. * The task does not block the expedited GP. Queue just
  227. * after the first task blocking the normal GP.
  228. */
  229. list_add(&t->rcu_node_entry, rnp->gp_tasks);
  230. break;
  231. default:
  232. /* Yet another exercise in excessive paranoia. */
  233. WARN_ON_ONCE(1);
  234. break;
  235. }
  236. /*
  237. * We have now queued the task. If it was the first one to
  238. * block either grace period, update the ->gp_tasks and/or
  239. * ->exp_tasks pointers, respectively, to reference the newly
  240. * blocked tasks.
  241. */
  242. if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
  243. rnp->gp_tasks = &t->rcu_node_entry;
  244. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
  245. }
  246. if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
  247. rnp->exp_tasks = &t->rcu_node_entry;
  248. WARN_ON_ONCE(!(blkd_state & RCU_GP_BLKD) !=
  249. !(rnp->qsmask & rdp->grpmask));
  250. WARN_ON_ONCE(!(blkd_state & RCU_EXP_BLKD) !=
  251. !(rnp->expmask & rdp->grpmask));
  252. raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
  253. /*
  254. * Report the quiescent state for the expedited GP. This expedited
  255. * GP should not be able to end until we report, so there should be
  256. * no need to check for a subsequent expedited GP. (Though we are
  257. * still in a quiescent state in any case.)
  258. */
  259. if (blkd_state & RCU_EXP_BLKD &&
  260. t->rcu_read_unlock_special.b.exp_need_qs) {
  261. t->rcu_read_unlock_special.b.exp_need_qs = false;
  262. rcu_report_exp_rdp(rdp->rsp, rdp, true);
  263. } else {
  264. WARN_ON_ONCE(t->rcu_read_unlock_special.b.exp_need_qs);
  265. }
  266. }
  267. /*
  268. * Record a preemptible-RCU quiescent state for the specified CPU.
  269. * Note that this does not necessarily mean that the task currently running
  270. * on the CPU is in a quiescent state: Instead, it means that the current
  271. * grace period need not wait on any RCU read-side critical section that
  272. * starts later on this CPU. It also means that if the current task is
  273. * in an RCU read-side critical section, it has already added itself to
  274. * some leaf rcu_node structure's ->blkd_tasks list. In addition to the
  275. * current task, there might be any number of other tasks blocked while
  276. * in an RCU read-side critical section.
  277. *
  278. * Callers to this function must disable preemption.
  279. */
  280. static void rcu_preempt_qs(void)
  281. {
  282. RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_qs() invoked with preemption enabled!!!\n");
  283. if (__this_cpu_read(rcu_data_p->cpu_no_qs.s)) {
  284. trace_rcu_grace_period(TPS("rcu_preempt"),
  285. __this_cpu_read(rcu_data_p->gp_seq),
  286. TPS("cpuqs"));
  287. __this_cpu_write(rcu_data_p->cpu_no_qs.b.norm, false);
  288. barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
  289. current->rcu_read_unlock_special.b.need_qs = false;
  290. }
  291. }
  292. /*
  293. * We have entered the scheduler, and the current task might soon be
  294. * context-switched away from. If this task is in an RCU read-side
  295. * critical section, we will no longer be able to rely on the CPU to
  296. * record that fact, so we enqueue the task on the blkd_tasks list.
  297. * The task will dequeue itself when it exits the outermost enclosing
  298. * RCU read-side critical section. Therefore, the current grace period
  299. * cannot be permitted to complete until the blkd_tasks list entries
  300. * predating the current grace period drain, in other words, until
  301. * rnp->gp_tasks becomes NULL.
  302. *
  303. * Caller must disable interrupts.
  304. */
  305. static void rcu_preempt_note_context_switch(bool preempt)
  306. {
  307. struct task_struct *t = current;
  308. struct rcu_data *rdp;
  309. struct rcu_node *rnp;
  310. lockdep_assert_irqs_disabled();
  311. WARN_ON_ONCE(!preempt && t->rcu_read_lock_nesting > 0);
  312. if (t->rcu_read_lock_nesting > 0 &&
  313. !t->rcu_read_unlock_special.b.blocked) {
  314. /* Possibly blocking in an RCU read-side critical section. */
  315. rdp = this_cpu_ptr(rcu_state_p->rda);
  316. rnp = rdp->mynode;
  317. raw_spin_lock_rcu_node(rnp);
  318. t->rcu_read_unlock_special.b.blocked = true;
  319. t->rcu_blocked_node = rnp;
  320. /*
  321. * Verify the CPU's sanity, trace the preemption, and
  322. * then queue the task as required based on the states
  323. * of any ongoing and expedited grace periods.
  324. */
  325. WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
  326. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  327. trace_rcu_preempt_task(rdp->rsp->name,
  328. t->pid,
  329. (rnp->qsmask & rdp->grpmask)
  330. ? rnp->gp_seq
  331. : rcu_seq_snap(&rnp->gp_seq));
  332. rcu_preempt_ctxt_queue(rnp, rdp);
  333. } else if (t->rcu_read_lock_nesting < 0 &&
  334. t->rcu_read_unlock_special.s) {
  335. /*
  336. * Complete exit from RCU read-side critical section on
  337. * behalf of preempted instance of __rcu_read_unlock().
  338. */
  339. rcu_read_unlock_special(t);
  340. rcu_preempt_deferred_qs(t);
  341. } else {
  342. rcu_preempt_deferred_qs(t);
  343. }
  344. /*
  345. * Either we were not in an RCU read-side critical section to
  346. * begin with, or we have now recorded that critical section
  347. * globally. Either way, we can now note a quiescent state
  348. * for this CPU. Again, if we were in an RCU read-side critical
  349. * section, and if that critical section was blocking the current
  350. * grace period, then the fact that the task has been enqueued
  351. * means that we continue to block the current grace period.
  352. */
  353. rcu_preempt_qs();
  354. }
  355. /*
  356. * Check for preempted RCU readers blocking the current grace period
  357. * for the specified rcu_node structure. If the caller needs a reliable
  358. * answer, it must hold the rcu_node's ->lock.
  359. */
  360. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  361. {
  362. return rnp->gp_tasks != NULL;
  363. }
  364. /*
  365. * Preemptible RCU implementation for rcu_read_lock().
  366. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  367. * if we block.
  368. */
  369. void __rcu_read_lock(void)
  370. {
  371. current->rcu_read_lock_nesting++;
  372. barrier(); /* critical section after entry code. */
  373. }
  374. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  375. /*
  376. * Preemptible RCU implementation for rcu_read_unlock().
  377. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  378. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  379. * invoke rcu_read_unlock_special() to clean up after a context switch
  380. * in an RCU read-side critical section and other special cases.
  381. */
  382. void __rcu_read_unlock(void)
  383. {
  384. struct task_struct *t = current;
  385. if (t->rcu_read_lock_nesting != 1) {
  386. --t->rcu_read_lock_nesting;
  387. } else {
  388. barrier(); /* critical section before exit code. */
  389. t->rcu_read_lock_nesting = INT_MIN;
  390. barrier(); /* assign before ->rcu_read_unlock_special load */
  391. if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
  392. rcu_read_unlock_special(t);
  393. barrier(); /* ->rcu_read_unlock_special load before assign */
  394. t->rcu_read_lock_nesting = 0;
  395. }
  396. #ifdef CONFIG_PROVE_LOCKING
  397. {
  398. int rrln = READ_ONCE(t->rcu_read_lock_nesting);
  399. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  400. }
  401. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  402. }
  403. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  404. /*
  405. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  406. * returning NULL if at the end of the list.
  407. */
  408. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  409. struct rcu_node *rnp)
  410. {
  411. struct list_head *np;
  412. np = t->rcu_node_entry.next;
  413. if (np == &rnp->blkd_tasks)
  414. np = NULL;
  415. return np;
  416. }
  417. /*
  418. * Return true if the specified rcu_node structure has tasks that were
  419. * preempted within an RCU read-side critical section.
  420. */
  421. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  422. {
  423. return !list_empty(&rnp->blkd_tasks);
  424. }
  425. /*
  426. * Report deferred quiescent states. The deferral time can
  427. * be quite short, for example, in the case of the call from
  428. * rcu_read_unlock_special().
  429. */
  430. static void
  431. rcu_preempt_deferred_qs_irqrestore(struct task_struct *t, unsigned long flags)
  432. {
  433. bool empty_exp;
  434. bool empty_norm;
  435. bool empty_exp_now;
  436. struct list_head *np;
  437. bool drop_boost_mutex = false;
  438. struct rcu_data *rdp;
  439. struct rcu_node *rnp;
  440. union rcu_special special;
  441. /*
  442. * If RCU core is waiting for this CPU to exit its critical section,
  443. * report the fact that it has exited. Because irqs are disabled,
  444. * t->rcu_read_unlock_special cannot change.
  445. */
  446. special = t->rcu_read_unlock_special;
  447. rdp = this_cpu_ptr(rcu_state_p->rda);
  448. if (!special.s && !rdp->deferred_qs) {
  449. local_irq_restore(flags);
  450. return;
  451. }
  452. if (special.b.need_qs) {
  453. rcu_preempt_qs();
  454. t->rcu_read_unlock_special.b.need_qs = false;
  455. if (!t->rcu_read_unlock_special.s && !rdp->deferred_qs) {
  456. local_irq_restore(flags);
  457. return;
  458. }
  459. }
  460. /*
  461. * Respond to a request by an expedited grace period for a
  462. * quiescent state from this CPU. Note that requests from
  463. * tasks are handled when removing the task from the
  464. * blocked-tasks list below.
  465. */
  466. if (special.b.exp_need_qs || rdp->deferred_qs) {
  467. t->rcu_read_unlock_special.b.exp_need_qs = false;
  468. rdp->deferred_qs = false;
  469. rcu_report_exp_rdp(rcu_state_p, rdp, true);
  470. if (!t->rcu_read_unlock_special.s) {
  471. local_irq_restore(flags);
  472. return;
  473. }
  474. }
  475. /* Clean up if blocked during RCU read-side critical section. */
  476. if (special.b.blocked) {
  477. t->rcu_read_unlock_special.b.blocked = false;
  478. /*
  479. * Remove this task from the list it blocked on. The task
  480. * now remains queued on the rcu_node corresponding to the
  481. * CPU it first blocked on, so there is no longer any need
  482. * to loop. Retain a WARN_ON_ONCE() out of sheer paranoia.
  483. */
  484. rnp = t->rcu_blocked_node;
  485. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  486. WARN_ON_ONCE(rnp != t->rcu_blocked_node);
  487. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  488. empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
  489. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
  490. (!empty_norm || rnp->qsmask));
  491. empty_exp = sync_rcu_preempt_exp_done(rnp);
  492. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  493. np = rcu_next_node_entry(t, rnp);
  494. list_del_init(&t->rcu_node_entry);
  495. t->rcu_blocked_node = NULL;
  496. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  497. rnp->gp_seq, t->pid);
  498. if (&t->rcu_node_entry == rnp->gp_tasks)
  499. rnp->gp_tasks = np;
  500. if (&t->rcu_node_entry == rnp->exp_tasks)
  501. rnp->exp_tasks = np;
  502. if (IS_ENABLED(CONFIG_RCU_BOOST)) {
  503. /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
  504. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
  505. if (&t->rcu_node_entry == rnp->boost_tasks)
  506. rnp->boost_tasks = np;
  507. }
  508. /*
  509. * If this was the last task on the current list, and if
  510. * we aren't waiting on any CPUs, report the quiescent state.
  511. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  512. * so we must take a snapshot of the expedited state.
  513. */
  514. empty_exp_now = sync_rcu_preempt_exp_done(rnp);
  515. if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
  516. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  517. rnp->gp_seq,
  518. 0, rnp->qsmask,
  519. rnp->level,
  520. rnp->grplo,
  521. rnp->grphi,
  522. !!rnp->gp_tasks);
  523. rcu_report_unblock_qs_rnp(rcu_state_p, rnp, flags);
  524. } else {
  525. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  526. }
  527. /* Unboost if we were boosted. */
  528. if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
  529. rt_mutex_futex_unlock(&rnp->boost_mtx);
  530. /*
  531. * If this was the last task on the expedited lists,
  532. * then we need to report up the rcu_node hierarchy.
  533. */
  534. if (!empty_exp && empty_exp_now)
  535. rcu_report_exp_rnp(rcu_state_p, rnp, true);
  536. } else {
  537. local_irq_restore(flags);
  538. }
  539. }
  540. /*
  541. * Is a deferred quiescent-state pending, and are we also not in
  542. * an RCU read-side critical section? It is the caller's responsibility
  543. * to ensure it is otherwise safe to report any deferred quiescent
  544. * states. The reason for this is that it is safe to report a
  545. * quiescent state during context switch even though preemption
  546. * is disabled. This function cannot be expected to understand these
  547. * nuances, so the caller must handle them.
  548. */
  549. static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
  550. {
  551. return (this_cpu_ptr(&rcu_preempt_data)->deferred_qs ||
  552. READ_ONCE(t->rcu_read_unlock_special.s)) &&
  553. !t->rcu_read_lock_nesting;
  554. }
  555. /*
  556. * Report a deferred quiescent state if needed and safe to do so.
  557. * As with rcu_preempt_need_deferred_qs(), "safe" involves only
  558. * not being in an RCU read-side critical section. The caller must
  559. * evaluate safety in terms of interrupt, softirq, and preemption
  560. * disabling.
  561. */
  562. static void rcu_preempt_deferred_qs(struct task_struct *t)
  563. {
  564. unsigned long flags;
  565. bool couldrecurse = t->rcu_read_lock_nesting >= 0;
  566. if (!rcu_preempt_need_deferred_qs(t))
  567. return;
  568. if (couldrecurse)
  569. t->rcu_read_lock_nesting -= INT_MIN;
  570. local_irq_save(flags);
  571. rcu_preempt_deferred_qs_irqrestore(t, flags);
  572. if (couldrecurse)
  573. t->rcu_read_lock_nesting += INT_MIN;
  574. }
  575. /*
  576. * Handle special cases during rcu_read_unlock(), such as needing to
  577. * notify RCU core processing or task having blocked during the RCU
  578. * read-side critical section.
  579. */
  580. static void rcu_read_unlock_special(struct task_struct *t)
  581. {
  582. unsigned long flags;
  583. bool preempt_bh_were_disabled =
  584. !!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
  585. bool irqs_were_disabled;
  586. /* NMI handlers cannot block and cannot safely manipulate state. */
  587. if (in_nmi())
  588. return;
  589. local_irq_save(flags);
  590. irqs_were_disabled = irqs_disabled_flags(flags);
  591. if ((preempt_bh_were_disabled || irqs_were_disabled) &&
  592. t->rcu_read_unlock_special.b.blocked) {
  593. /* Need to defer quiescent state until everything is enabled. */
  594. raise_softirq_irqoff(RCU_SOFTIRQ);
  595. local_irq_restore(flags);
  596. return;
  597. }
  598. rcu_preempt_deferred_qs_irqrestore(t, flags);
  599. }
  600. /*
  601. * Dump detailed information for all tasks blocking the current RCU
  602. * grace period on the specified rcu_node structure.
  603. */
  604. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  605. {
  606. unsigned long flags;
  607. struct task_struct *t;
  608. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  609. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  610. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  611. return;
  612. }
  613. t = list_entry(rnp->gp_tasks->prev,
  614. struct task_struct, rcu_node_entry);
  615. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  616. /*
  617. * We could be printing a lot while holding a spinlock.
  618. * Avoid triggering hard lockup.
  619. */
  620. touch_nmi_watchdog();
  621. sched_show_task(t);
  622. }
  623. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  624. }
  625. /*
  626. * Dump detailed information for all tasks blocking the current RCU
  627. * grace period.
  628. */
  629. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  630. {
  631. struct rcu_node *rnp = rcu_get_root(rsp);
  632. rcu_print_detail_task_stall_rnp(rnp);
  633. rcu_for_each_leaf_node(rsp, rnp)
  634. rcu_print_detail_task_stall_rnp(rnp);
  635. }
  636. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  637. {
  638. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  639. rnp->level, rnp->grplo, rnp->grphi);
  640. }
  641. static void rcu_print_task_stall_end(void)
  642. {
  643. pr_cont("\n");
  644. }
  645. /*
  646. * Scan the current list of tasks blocked within RCU read-side critical
  647. * sections, printing out the tid of each.
  648. */
  649. static int rcu_print_task_stall(struct rcu_node *rnp)
  650. {
  651. struct task_struct *t;
  652. int ndetected = 0;
  653. if (!rcu_preempt_blocked_readers_cgp(rnp))
  654. return 0;
  655. rcu_print_task_stall_begin(rnp);
  656. t = list_entry(rnp->gp_tasks->prev,
  657. struct task_struct, rcu_node_entry);
  658. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  659. pr_cont(" P%d", t->pid);
  660. ndetected++;
  661. }
  662. rcu_print_task_stall_end();
  663. return ndetected;
  664. }
  665. /*
  666. * Scan the current list of tasks blocked within RCU read-side critical
  667. * sections, printing out the tid of each that is blocking the current
  668. * expedited grace period.
  669. */
  670. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  671. {
  672. struct task_struct *t;
  673. int ndetected = 0;
  674. if (!rnp->exp_tasks)
  675. return 0;
  676. t = list_entry(rnp->exp_tasks->prev,
  677. struct task_struct, rcu_node_entry);
  678. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  679. pr_cont(" P%d", t->pid);
  680. ndetected++;
  681. }
  682. return ndetected;
  683. }
  684. /*
  685. * Check that the list of blocked tasks for the newly completed grace
  686. * period is in fact empty. It is a serious bug to complete a grace
  687. * period that still has RCU readers blocked! This function must be
  688. * invoked -before- updating this rnp's ->gp_seq, and the rnp's ->lock
  689. * must be held by the caller.
  690. *
  691. * Also, if there are blocked tasks on the list, they automatically
  692. * block the newly created grace period, so set up ->gp_tasks accordingly.
  693. */
  694. static void
  695. rcu_preempt_check_blocked_tasks(struct rcu_state *rsp, struct rcu_node *rnp)
  696. {
  697. struct task_struct *t;
  698. RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
  699. if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
  700. dump_blkd_tasks(rsp, rnp, 10);
  701. if (rcu_preempt_has_tasks(rnp) &&
  702. (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
  703. rnp->gp_tasks = rnp->blkd_tasks.next;
  704. t = container_of(rnp->gp_tasks, struct task_struct,
  705. rcu_node_entry);
  706. trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
  707. rnp->gp_seq, t->pid);
  708. }
  709. WARN_ON_ONCE(rnp->qsmask);
  710. }
  711. /*
  712. * Check for a quiescent state from the current CPU. When a task blocks,
  713. * the task is recorded in the corresponding CPU's rcu_node structure,
  714. * which is checked elsewhere.
  715. *
  716. * Caller must disable hard irqs.
  717. */
  718. static void rcu_preempt_check_callbacks(void)
  719. {
  720. struct rcu_state *rsp = &rcu_preempt_state;
  721. struct task_struct *t = current;
  722. if (t->rcu_read_lock_nesting > 0 ||
  723. (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
  724. /* No QS, force context switch if deferred. */
  725. if (rcu_preempt_need_deferred_qs(t))
  726. resched_cpu(smp_processor_id());
  727. } else if (rcu_preempt_need_deferred_qs(t)) {
  728. rcu_preempt_deferred_qs(t); /* Report deferred QS. */
  729. return;
  730. } else if (!t->rcu_read_lock_nesting) {
  731. rcu_preempt_qs(); /* Report immediate QS. */
  732. return;
  733. }
  734. /* If GP is oldish, ask for help from rcu_read_unlock_special(). */
  735. if (t->rcu_read_lock_nesting > 0 &&
  736. __this_cpu_read(rcu_data_p->core_needs_qs) &&
  737. __this_cpu_read(rcu_data_p->cpu_no_qs.b.norm) &&
  738. !t->rcu_read_unlock_special.b.need_qs &&
  739. time_after(jiffies, rsp->gp_start + HZ))
  740. t->rcu_read_unlock_special.b.need_qs = true;
  741. }
  742. /**
  743. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  744. * @head: structure to be used for queueing the RCU updates.
  745. * @func: actual callback function to be invoked after the grace period
  746. *
  747. * The callback function will be invoked some time after a full grace
  748. * period elapses, in other words after all pre-existing RCU read-side
  749. * critical sections have completed. However, the callback function
  750. * might well execute concurrently with RCU read-side critical sections
  751. * that started after call_rcu() was invoked. RCU read-side critical
  752. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  753. * and may be nested.
  754. *
  755. * Note that all CPUs must agree that the grace period extended beyond
  756. * all pre-existing RCU read-side critical section. On systems with more
  757. * than one CPU, this means that when "func()" is invoked, each CPU is
  758. * guaranteed to have executed a full memory barrier since the end of its
  759. * last RCU read-side critical section whose beginning preceded the call
  760. * to call_rcu(). It also means that each CPU executing an RCU read-side
  761. * critical section that continues beyond the start of "func()" must have
  762. * executed a memory barrier after the call_rcu() but before the beginning
  763. * of that RCU read-side critical section. Note that these guarantees
  764. * include CPUs that are offline, idle, or executing in user mode, as
  765. * well as CPUs that are executing in the kernel.
  766. *
  767. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  768. * resulting RCU callback function "func()", then both CPU A and CPU B are
  769. * guaranteed to execute a full memory barrier during the time interval
  770. * between the call to call_rcu() and the invocation of "func()" -- even
  771. * if CPU A and CPU B are the same CPU (but again only if the system has
  772. * more than one CPU).
  773. */
  774. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  775. {
  776. __call_rcu(head, func, rcu_state_p, -1, 0);
  777. }
  778. EXPORT_SYMBOL_GPL(call_rcu);
  779. /**
  780. * synchronize_rcu - wait until a grace period has elapsed.
  781. *
  782. * Control will return to the caller some time after a full grace
  783. * period has elapsed, in other words after all currently executing RCU
  784. * read-side critical sections have completed. Note, however, that
  785. * upon return from synchronize_rcu(), the caller might well be executing
  786. * concurrently with new RCU read-side critical sections that began while
  787. * synchronize_rcu() was waiting. RCU read-side critical sections are
  788. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  789. *
  790. * See the description of synchronize_sched() for more detailed
  791. * information on memory-ordering guarantees. However, please note
  792. * that -only- the memory-ordering guarantees apply. For example,
  793. * synchronize_rcu() is -not- guaranteed to wait on things like code
  794. * protected by preempt_disable(), instead, synchronize_rcu() is -only-
  795. * guaranteed to wait on RCU read-side critical sections, that is, sections
  796. * of code protected by rcu_read_lock().
  797. */
  798. void synchronize_rcu(void)
  799. {
  800. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  801. lock_is_held(&rcu_lock_map) ||
  802. lock_is_held(&rcu_sched_lock_map),
  803. "Illegal synchronize_rcu() in RCU read-side critical section");
  804. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  805. return;
  806. if (rcu_gp_is_expedited())
  807. synchronize_rcu_expedited();
  808. else
  809. wait_rcu_gp(call_rcu);
  810. }
  811. EXPORT_SYMBOL_GPL(synchronize_rcu);
  812. /**
  813. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  814. *
  815. * Note that this primitive does not necessarily wait for an RCU grace period
  816. * to complete. For example, if there are no RCU callbacks queued anywhere
  817. * in the system, then rcu_barrier() is within its rights to return
  818. * immediately, without waiting for anything, much less an RCU grace period.
  819. */
  820. void rcu_barrier(void)
  821. {
  822. _rcu_barrier(rcu_state_p);
  823. }
  824. EXPORT_SYMBOL_GPL(rcu_barrier);
  825. /*
  826. * Initialize preemptible RCU's state structures.
  827. */
  828. static void __init __rcu_init_preempt(void)
  829. {
  830. rcu_init_one(rcu_state_p);
  831. }
  832. /*
  833. * Check for a task exiting while in a preemptible-RCU read-side
  834. * critical section, clean up if so. No need to issue warnings,
  835. * as debug_check_no_locks_held() already does this if lockdep
  836. * is enabled.
  837. */
  838. void exit_rcu(void)
  839. {
  840. struct task_struct *t = current;
  841. if (likely(list_empty(&current->rcu_node_entry)))
  842. return;
  843. t->rcu_read_lock_nesting = 1;
  844. barrier();
  845. t->rcu_read_unlock_special.b.blocked = true;
  846. __rcu_read_unlock();
  847. rcu_preempt_deferred_qs(current);
  848. }
  849. /*
  850. * Dump the blocked-tasks state, but limit the list dump to the
  851. * specified number of elements.
  852. */
  853. static void
  854. dump_blkd_tasks(struct rcu_state *rsp, struct rcu_node *rnp, int ncheck)
  855. {
  856. int cpu;
  857. int i;
  858. struct list_head *lhp;
  859. bool onl;
  860. struct rcu_data *rdp;
  861. struct rcu_node *rnp1;
  862. raw_lockdep_assert_held_rcu_node(rnp);
  863. pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
  864. __func__, rnp->grplo, rnp->grphi, rnp->level,
  865. (long)rnp->gp_seq, (long)rnp->completedqs);
  866. for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
  867. pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
  868. __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
  869. pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
  870. __func__, rnp->gp_tasks, rnp->boost_tasks, rnp->exp_tasks);
  871. pr_info("%s: ->blkd_tasks", __func__);
  872. i = 0;
  873. list_for_each(lhp, &rnp->blkd_tasks) {
  874. pr_cont(" %p", lhp);
  875. if (++i >= 10)
  876. break;
  877. }
  878. pr_cont("\n");
  879. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
  880. rdp = per_cpu_ptr(rsp->rda, cpu);
  881. onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
  882. pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
  883. cpu, ".o"[onl],
  884. (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
  885. (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
  886. }
  887. }
  888. #else /* #ifdef CONFIG_PREEMPT_RCU */
  889. static struct rcu_state *const rcu_state_p = &rcu_sched_state;
  890. /*
  891. * Tell them what RCU they are running.
  892. */
  893. static void __init rcu_bootup_announce(void)
  894. {
  895. pr_info("Hierarchical RCU implementation.\n");
  896. rcu_bootup_announce_oddness();
  897. }
  898. /*
  899. * Because preemptible RCU does not exist, we never have to check for
  900. * CPUs being in quiescent states.
  901. */
  902. static void rcu_preempt_note_context_switch(bool preempt)
  903. {
  904. }
  905. /*
  906. * Because preemptible RCU does not exist, there are never any preempted
  907. * RCU readers.
  908. */
  909. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  910. {
  911. return 0;
  912. }
  913. /*
  914. * Because there is no preemptible RCU, there can be no readers blocked.
  915. */
  916. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  917. {
  918. return false;
  919. }
  920. /*
  921. * Because there is no preemptible RCU, there can be no deferred quiescent
  922. * states.
  923. */
  924. static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
  925. {
  926. return false;
  927. }
  928. static void rcu_preempt_deferred_qs(struct task_struct *t) { }
  929. /*
  930. * Because preemptible RCU does not exist, we never have to check for
  931. * tasks blocked within RCU read-side critical sections.
  932. */
  933. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  934. {
  935. }
  936. /*
  937. * Because preemptible RCU does not exist, we never have to check for
  938. * tasks blocked within RCU read-side critical sections.
  939. */
  940. static int rcu_print_task_stall(struct rcu_node *rnp)
  941. {
  942. return 0;
  943. }
  944. /*
  945. * Because preemptible RCU does not exist, we never have to check for
  946. * tasks blocked within RCU read-side critical sections that are
  947. * blocking the current expedited grace period.
  948. */
  949. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  950. {
  951. return 0;
  952. }
  953. /*
  954. * Because there is no preemptible RCU, there can be no readers blocked,
  955. * so there is no need to check for blocked tasks. So check only for
  956. * bogus qsmask values.
  957. */
  958. static void
  959. rcu_preempt_check_blocked_tasks(struct rcu_state *rsp, struct rcu_node *rnp)
  960. {
  961. WARN_ON_ONCE(rnp->qsmask);
  962. }
  963. /*
  964. * Because preemptible RCU does not exist, it never has any callbacks
  965. * to check.
  966. */
  967. static void rcu_preempt_check_callbacks(void)
  968. {
  969. }
  970. /*
  971. * Because preemptible RCU does not exist, rcu_barrier() is just
  972. * another name for rcu_barrier_sched().
  973. */
  974. void rcu_barrier(void)
  975. {
  976. rcu_barrier_sched();
  977. }
  978. EXPORT_SYMBOL_GPL(rcu_barrier);
  979. /*
  980. * Because preemptible RCU does not exist, it need not be initialized.
  981. */
  982. static void __init __rcu_init_preempt(void)
  983. {
  984. }
  985. /*
  986. * Because preemptible RCU does not exist, tasks cannot possibly exit
  987. * while in preemptible RCU read-side critical sections.
  988. */
  989. void exit_rcu(void)
  990. {
  991. }
  992. /*
  993. * Dump the guaranteed-empty blocked-tasks state. Trust but verify.
  994. */
  995. static void
  996. dump_blkd_tasks(struct rcu_state *rsp, struct rcu_node *rnp, int ncheck)
  997. {
  998. WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
  999. }
  1000. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  1001. #ifdef CONFIG_RCU_BOOST
  1002. static void rcu_wake_cond(struct task_struct *t, int status)
  1003. {
  1004. /*
  1005. * If the thread is yielding, only wake it when this
  1006. * is invoked from idle
  1007. */
  1008. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  1009. wake_up_process(t);
  1010. }
  1011. /*
  1012. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  1013. * or ->boost_tasks, advancing the pointer to the next task in the
  1014. * ->blkd_tasks list.
  1015. *
  1016. * Note that irqs must be enabled: boosting the task can block.
  1017. * Returns 1 if there are more tasks needing to be boosted.
  1018. */
  1019. static int rcu_boost(struct rcu_node *rnp)
  1020. {
  1021. unsigned long flags;
  1022. struct task_struct *t;
  1023. struct list_head *tb;
  1024. if (READ_ONCE(rnp->exp_tasks) == NULL &&
  1025. READ_ONCE(rnp->boost_tasks) == NULL)
  1026. return 0; /* Nothing left to boost. */
  1027. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1028. /*
  1029. * Recheck under the lock: all tasks in need of boosting
  1030. * might exit their RCU read-side critical sections on their own.
  1031. */
  1032. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  1033. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1034. return 0;
  1035. }
  1036. /*
  1037. * Preferentially boost tasks blocking expedited grace periods.
  1038. * This cannot starve the normal grace periods because a second
  1039. * expedited grace period must boost all blocked tasks, including
  1040. * those blocking the pre-existing normal grace period.
  1041. */
  1042. if (rnp->exp_tasks != NULL)
  1043. tb = rnp->exp_tasks;
  1044. else
  1045. tb = rnp->boost_tasks;
  1046. /*
  1047. * We boost task t by manufacturing an rt_mutex that appears to
  1048. * be held by task t. We leave a pointer to that rt_mutex where
  1049. * task t can find it, and task t will release the mutex when it
  1050. * exits its outermost RCU read-side critical section. Then
  1051. * simply acquiring this artificial rt_mutex will boost task
  1052. * t's priority. (Thanks to tglx for suggesting this approach!)
  1053. *
  1054. * Note that task t must acquire rnp->lock to remove itself from
  1055. * the ->blkd_tasks list, which it will do from exit() if from
  1056. * nowhere else. We therefore are guaranteed that task t will
  1057. * stay around at least until we drop rnp->lock. Note that
  1058. * rnp->lock also resolves races between our priority boosting
  1059. * and task t's exiting its outermost RCU read-side critical
  1060. * section.
  1061. */
  1062. t = container_of(tb, struct task_struct, rcu_node_entry);
  1063. rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
  1064. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1065. /* Lock only for side effect: boosts task t's priority. */
  1066. rt_mutex_lock(&rnp->boost_mtx);
  1067. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  1068. return READ_ONCE(rnp->exp_tasks) != NULL ||
  1069. READ_ONCE(rnp->boost_tasks) != NULL;
  1070. }
  1071. /*
  1072. * Priority-boosting kthread, one per leaf rcu_node.
  1073. */
  1074. static int rcu_boost_kthread(void *arg)
  1075. {
  1076. struct rcu_node *rnp = (struct rcu_node *)arg;
  1077. int spincnt = 0;
  1078. int more2boost;
  1079. trace_rcu_utilization(TPS("Start boost kthread@init"));
  1080. for (;;) {
  1081. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1082. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  1083. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  1084. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  1085. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1086. more2boost = rcu_boost(rnp);
  1087. if (more2boost)
  1088. spincnt++;
  1089. else
  1090. spincnt = 0;
  1091. if (spincnt > 10) {
  1092. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  1093. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  1094. schedule_timeout_interruptible(2);
  1095. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  1096. spincnt = 0;
  1097. }
  1098. }
  1099. /* NOTREACHED */
  1100. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  1101. return 0;
  1102. }
  1103. /*
  1104. * Check to see if it is time to start boosting RCU readers that are
  1105. * blocking the current grace period, and, if so, tell the per-rcu_node
  1106. * kthread to start boosting them. If there is an expedited grace
  1107. * period in progress, it is always time to boost.
  1108. *
  1109. * The caller must hold rnp->lock, which this function releases.
  1110. * The ->boost_kthread_task is immortal, so we don't need to worry
  1111. * about it going away.
  1112. */
  1113. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1114. __releases(rnp->lock)
  1115. {
  1116. struct task_struct *t;
  1117. raw_lockdep_assert_held_rcu_node(rnp);
  1118. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1119. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1120. return;
  1121. }
  1122. if (rnp->exp_tasks != NULL ||
  1123. (rnp->gp_tasks != NULL &&
  1124. rnp->boost_tasks == NULL &&
  1125. rnp->qsmask == 0 &&
  1126. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1127. if (rnp->exp_tasks == NULL)
  1128. rnp->boost_tasks = rnp->gp_tasks;
  1129. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1130. t = rnp->boost_kthread_task;
  1131. if (t)
  1132. rcu_wake_cond(t, rnp->boost_kthread_status);
  1133. } else {
  1134. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1135. }
  1136. }
  1137. /*
  1138. * Wake up the per-CPU kthread to invoke RCU callbacks.
  1139. */
  1140. static void invoke_rcu_callbacks_kthread(void)
  1141. {
  1142. unsigned long flags;
  1143. local_irq_save(flags);
  1144. __this_cpu_write(rcu_cpu_has_work, 1);
  1145. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  1146. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  1147. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  1148. __this_cpu_read(rcu_cpu_kthread_status));
  1149. }
  1150. local_irq_restore(flags);
  1151. }
  1152. /*
  1153. * Is the current CPU running the RCU-callbacks kthread?
  1154. * Caller must have preemption disabled.
  1155. */
  1156. static bool rcu_is_callbacks_kthread(void)
  1157. {
  1158. return __this_cpu_read(rcu_cpu_kthread_task) == current;
  1159. }
  1160. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1161. /*
  1162. * Do priority-boost accounting for the start of a new grace period.
  1163. */
  1164. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1165. {
  1166. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1167. }
  1168. /*
  1169. * Create an RCU-boost kthread for the specified node if one does not
  1170. * already exist. We only create this kthread for preemptible RCU.
  1171. * Returns zero if all is well, a negated errno otherwise.
  1172. */
  1173. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1174. struct rcu_node *rnp)
  1175. {
  1176. int rnp_index = rnp - &rsp->node[0];
  1177. unsigned long flags;
  1178. struct sched_param sp;
  1179. struct task_struct *t;
  1180. if (rcu_state_p != rsp)
  1181. return 0;
  1182. if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
  1183. return 0;
  1184. rsp->boost = 1;
  1185. if (rnp->boost_kthread_task != NULL)
  1186. return 0;
  1187. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1188. "rcub/%d", rnp_index);
  1189. if (IS_ERR(t))
  1190. return PTR_ERR(t);
  1191. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1192. rnp->boost_kthread_task = t;
  1193. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1194. sp.sched_priority = kthread_prio;
  1195. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1196. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1197. return 0;
  1198. }
  1199. static void rcu_kthread_do_work(void)
  1200. {
  1201. rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  1202. rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  1203. rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  1204. }
  1205. static void rcu_cpu_kthread_setup(unsigned int cpu)
  1206. {
  1207. struct sched_param sp;
  1208. sp.sched_priority = kthread_prio;
  1209. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1210. }
  1211. static void rcu_cpu_kthread_park(unsigned int cpu)
  1212. {
  1213. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1214. }
  1215. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  1216. {
  1217. return __this_cpu_read(rcu_cpu_has_work);
  1218. }
  1219. /*
  1220. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1221. * RCU softirq used in flavors and configurations of RCU that do not
  1222. * support RCU priority boosting.
  1223. */
  1224. static void rcu_cpu_kthread(unsigned int cpu)
  1225. {
  1226. unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  1227. char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  1228. int spincnt;
  1229. for (spincnt = 0; spincnt < 10; spincnt++) {
  1230. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1231. local_bh_disable();
  1232. *statusp = RCU_KTHREAD_RUNNING;
  1233. this_cpu_inc(rcu_cpu_kthread_loops);
  1234. local_irq_disable();
  1235. work = *workp;
  1236. *workp = 0;
  1237. local_irq_enable();
  1238. if (work)
  1239. rcu_kthread_do_work();
  1240. local_bh_enable();
  1241. if (*workp == 0) {
  1242. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1243. *statusp = RCU_KTHREAD_WAITING;
  1244. return;
  1245. }
  1246. }
  1247. *statusp = RCU_KTHREAD_YIELDING;
  1248. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1249. schedule_timeout_interruptible(2);
  1250. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1251. *statusp = RCU_KTHREAD_WAITING;
  1252. }
  1253. /*
  1254. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1255. * served by the rcu_node in question. The CPU hotplug lock is still
  1256. * held, so the value of rnp->qsmaskinit will be stable.
  1257. *
  1258. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1259. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1260. * this function allows the kthread to execute on any CPU.
  1261. */
  1262. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1263. {
  1264. struct task_struct *t = rnp->boost_kthread_task;
  1265. unsigned long mask = rcu_rnp_online_cpus(rnp);
  1266. cpumask_var_t cm;
  1267. int cpu;
  1268. if (!t)
  1269. return;
  1270. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1271. return;
  1272. for_each_leaf_node_possible_cpu(rnp, cpu)
  1273. if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
  1274. cpu != outgoingcpu)
  1275. cpumask_set_cpu(cpu, cm);
  1276. if (cpumask_weight(cm) == 0)
  1277. cpumask_setall(cm);
  1278. set_cpus_allowed_ptr(t, cm);
  1279. free_cpumask_var(cm);
  1280. }
  1281. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1282. .store = &rcu_cpu_kthread_task,
  1283. .thread_should_run = rcu_cpu_kthread_should_run,
  1284. .thread_fn = rcu_cpu_kthread,
  1285. .thread_comm = "rcuc/%u",
  1286. .setup = rcu_cpu_kthread_setup,
  1287. .park = rcu_cpu_kthread_park,
  1288. };
  1289. /*
  1290. * Spawn boost kthreads -- called as soon as the scheduler is running.
  1291. */
  1292. static void __init rcu_spawn_boost_kthreads(void)
  1293. {
  1294. struct rcu_node *rnp;
  1295. int cpu;
  1296. for_each_possible_cpu(cpu)
  1297. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1298. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1299. rcu_for_each_leaf_node(rcu_state_p, rnp)
  1300. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1301. }
  1302. static void rcu_prepare_kthreads(int cpu)
  1303. {
  1304. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  1305. struct rcu_node *rnp = rdp->mynode;
  1306. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1307. if (rcu_scheduler_fully_active)
  1308. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1309. }
  1310. #else /* #ifdef CONFIG_RCU_BOOST */
  1311. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1312. __releases(rnp->lock)
  1313. {
  1314. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1315. }
  1316. static void invoke_rcu_callbacks_kthread(void)
  1317. {
  1318. WARN_ON_ONCE(1);
  1319. }
  1320. static bool rcu_is_callbacks_kthread(void)
  1321. {
  1322. return false;
  1323. }
  1324. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1325. {
  1326. }
  1327. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1328. {
  1329. }
  1330. static void __init rcu_spawn_boost_kthreads(void)
  1331. {
  1332. }
  1333. static void rcu_prepare_kthreads(int cpu)
  1334. {
  1335. }
  1336. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1337. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1338. /*
  1339. * Check to see if any future RCU-related work will need to be done
  1340. * by the current CPU, even if none need be done immediately, returning
  1341. * 1 if so. This function is part of the RCU implementation; it is -not-
  1342. * an exported member of the RCU API.
  1343. *
  1344. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1345. * any flavor of RCU.
  1346. */
  1347. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1348. {
  1349. *nextevt = KTIME_MAX;
  1350. return rcu_cpu_has_callbacks(NULL);
  1351. }
  1352. /*
  1353. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1354. * after it.
  1355. */
  1356. static void rcu_cleanup_after_idle(void)
  1357. {
  1358. }
  1359. /*
  1360. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1361. * is nothing.
  1362. */
  1363. static void rcu_prepare_for_idle(void)
  1364. {
  1365. }
  1366. /*
  1367. * Don't bother keeping a running count of the number of RCU callbacks
  1368. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1369. */
  1370. static void rcu_idle_count_callbacks_posted(void)
  1371. {
  1372. }
  1373. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1374. /*
  1375. * This code is invoked when a CPU goes idle, at which point we want
  1376. * to have the CPU do everything required for RCU so that it can enter
  1377. * the energy-efficient dyntick-idle mode. This is handled by a
  1378. * state machine implemented by rcu_prepare_for_idle() below.
  1379. *
  1380. * The following three proprocessor symbols control this state machine:
  1381. *
  1382. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1383. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1384. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1385. * benchmarkers who might otherwise be tempted to set this to a large
  1386. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1387. * system. And if you are -that- concerned about energy efficiency,
  1388. * just power the system down and be done with it!
  1389. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1390. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1391. * callbacks pending. Setting this too high can OOM your system.
  1392. *
  1393. * The values below work well in practice. If future workloads require
  1394. * adjustment, they can be converted into kernel config parameters, though
  1395. * making the state machine smarter might be a better option.
  1396. */
  1397. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1398. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1399. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1400. module_param(rcu_idle_gp_delay, int, 0644);
  1401. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1402. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1403. /*
  1404. * Try to advance callbacks for all flavors of RCU on the current CPU, but
  1405. * only if it has been awhile since the last time we did so. Afterwards,
  1406. * if there are any callbacks ready for immediate invocation, return true.
  1407. */
  1408. static bool __maybe_unused rcu_try_advance_all_cbs(void)
  1409. {
  1410. bool cbs_ready = false;
  1411. struct rcu_data *rdp;
  1412. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1413. struct rcu_node *rnp;
  1414. struct rcu_state *rsp;
  1415. /* Exit early if we advanced recently. */
  1416. if (jiffies == rdtp->last_advance_all)
  1417. return false;
  1418. rdtp->last_advance_all = jiffies;
  1419. for_each_rcu_flavor(rsp) {
  1420. rdp = this_cpu_ptr(rsp->rda);
  1421. rnp = rdp->mynode;
  1422. /*
  1423. * Don't bother checking unless a grace period has
  1424. * completed since we last checked and there are
  1425. * callbacks not yet ready to invoke.
  1426. */
  1427. if ((rcu_seq_completed_gp(rdp->gp_seq,
  1428. rcu_seq_current(&rnp->gp_seq)) ||
  1429. unlikely(READ_ONCE(rdp->gpwrap))) &&
  1430. rcu_segcblist_pend_cbs(&rdp->cblist))
  1431. note_gp_changes(rsp, rdp);
  1432. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  1433. cbs_ready = true;
  1434. }
  1435. return cbs_ready;
  1436. }
  1437. /*
  1438. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1439. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1440. * caller to set the timeout based on whether or not there are non-lazy
  1441. * callbacks.
  1442. *
  1443. * The caller must have disabled interrupts.
  1444. */
  1445. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1446. {
  1447. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1448. unsigned long dj;
  1449. lockdep_assert_irqs_disabled();
  1450. /* Snapshot to detect later posting of non-lazy callback. */
  1451. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1452. /* If no callbacks, RCU doesn't need the CPU. */
  1453. if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
  1454. *nextevt = KTIME_MAX;
  1455. return 0;
  1456. }
  1457. /* Attempt to advance callbacks. */
  1458. if (rcu_try_advance_all_cbs()) {
  1459. /* Some ready to invoke, so initiate later invocation. */
  1460. invoke_rcu_core();
  1461. return 1;
  1462. }
  1463. rdtp->last_accelerate = jiffies;
  1464. /* Request timer delay depending on laziness, and round. */
  1465. if (!rdtp->all_lazy) {
  1466. dj = round_up(rcu_idle_gp_delay + jiffies,
  1467. rcu_idle_gp_delay) - jiffies;
  1468. } else {
  1469. dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1470. }
  1471. *nextevt = basemono + dj * TICK_NSEC;
  1472. return 0;
  1473. }
  1474. /*
  1475. * Prepare a CPU for idle from an RCU perspective. The first major task
  1476. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1477. * The second major task is to check to see if a non-lazy callback has
  1478. * arrived at a CPU that previously had only lazy callbacks. The third
  1479. * major task is to accelerate (that is, assign grace-period numbers to)
  1480. * any recently arrived callbacks.
  1481. *
  1482. * The caller must have disabled interrupts.
  1483. */
  1484. static void rcu_prepare_for_idle(void)
  1485. {
  1486. bool needwake;
  1487. struct rcu_data *rdp;
  1488. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1489. struct rcu_node *rnp;
  1490. struct rcu_state *rsp;
  1491. int tne;
  1492. lockdep_assert_irqs_disabled();
  1493. if (rcu_is_nocb_cpu(smp_processor_id()))
  1494. return;
  1495. /* Handle nohz enablement switches conservatively. */
  1496. tne = READ_ONCE(tick_nohz_active);
  1497. if (tne != rdtp->tick_nohz_enabled_snap) {
  1498. if (rcu_cpu_has_callbacks(NULL))
  1499. invoke_rcu_core(); /* force nohz to see update. */
  1500. rdtp->tick_nohz_enabled_snap = tne;
  1501. return;
  1502. }
  1503. if (!tne)
  1504. return;
  1505. /*
  1506. * If a non-lazy callback arrived at a CPU having only lazy
  1507. * callbacks, invoke RCU core for the side-effect of recalculating
  1508. * idle duration on re-entry to idle.
  1509. */
  1510. if (rdtp->all_lazy &&
  1511. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1512. rdtp->all_lazy = false;
  1513. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1514. invoke_rcu_core();
  1515. return;
  1516. }
  1517. /*
  1518. * If we have not yet accelerated this jiffy, accelerate all
  1519. * callbacks on this CPU.
  1520. */
  1521. if (rdtp->last_accelerate == jiffies)
  1522. return;
  1523. rdtp->last_accelerate = jiffies;
  1524. for_each_rcu_flavor(rsp) {
  1525. rdp = this_cpu_ptr(rsp->rda);
  1526. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1527. continue;
  1528. rnp = rdp->mynode;
  1529. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1530. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  1531. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1532. if (needwake)
  1533. rcu_gp_kthread_wake(rsp);
  1534. }
  1535. }
  1536. /*
  1537. * Clean up for exit from idle. Attempt to advance callbacks based on
  1538. * any grace periods that elapsed while the CPU was idle, and if any
  1539. * callbacks are now ready to invoke, initiate invocation.
  1540. */
  1541. static void rcu_cleanup_after_idle(void)
  1542. {
  1543. lockdep_assert_irqs_disabled();
  1544. if (rcu_is_nocb_cpu(smp_processor_id()))
  1545. return;
  1546. if (rcu_try_advance_all_cbs())
  1547. invoke_rcu_core();
  1548. }
  1549. /*
  1550. * Keep a running count of the number of non-lazy callbacks posted
  1551. * on this CPU. This running counter (which is never decremented) allows
  1552. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1553. * posts a callback, even if an equal number of callbacks are invoked.
  1554. * Of course, callbacks should only be posted from within a trace event
  1555. * designed to be called from idle or from within RCU_NONIDLE().
  1556. */
  1557. static void rcu_idle_count_callbacks_posted(void)
  1558. {
  1559. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1560. }
  1561. /*
  1562. * Data for flushing lazy RCU callbacks at OOM time.
  1563. */
  1564. static atomic_t oom_callback_count;
  1565. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1566. /*
  1567. * RCU OOM callback -- decrement the outstanding count and deliver the
  1568. * wake-up if we are the last one.
  1569. */
  1570. static void rcu_oom_callback(struct rcu_head *rhp)
  1571. {
  1572. if (atomic_dec_and_test(&oom_callback_count))
  1573. wake_up(&oom_callback_wq);
  1574. }
  1575. /*
  1576. * Post an rcu_oom_notify callback on the current CPU if it has at
  1577. * least one lazy callback. This will unnecessarily post callbacks
  1578. * to CPUs that already have a non-lazy callback at the end of their
  1579. * callback list, but this is an infrequent operation, so accept some
  1580. * extra overhead to keep things simple.
  1581. */
  1582. static void rcu_oom_notify_cpu(void *unused)
  1583. {
  1584. struct rcu_state *rsp;
  1585. struct rcu_data *rdp;
  1586. for_each_rcu_flavor(rsp) {
  1587. rdp = raw_cpu_ptr(rsp->rda);
  1588. if (rcu_segcblist_n_lazy_cbs(&rdp->cblist)) {
  1589. atomic_inc(&oom_callback_count);
  1590. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1591. }
  1592. }
  1593. }
  1594. /*
  1595. * If low on memory, ensure that each CPU has a non-lazy callback.
  1596. * This will wake up CPUs that have only lazy callbacks, in turn
  1597. * ensuring that they free up the corresponding memory in a timely manner.
  1598. * Because an uncertain amount of memory will be freed in some uncertain
  1599. * timeframe, we do not claim to have freed anything.
  1600. */
  1601. static int rcu_oom_notify(struct notifier_block *self,
  1602. unsigned long notused, void *nfreed)
  1603. {
  1604. int cpu;
  1605. /* Wait for callbacks from earlier instance to complete. */
  1606. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1607. smp_mb(); /* Ensure callback reuse happens after callback invocation. */
  1608. /*
  1609. * Prevent premature wakeup: ensure that all increments happen
  1610. * before there is a chance of the counter reaching zero.
  1611. */
  1612. atomic_set(&oom_callback_count, 1);
  1613. for_each_online_cpu(cpu) {
  1614. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1615. cond_resched_tasks_rcu_qs();
  1616. }
  1617. /* Unconditionally decrement: no need to wake ourselves up. */
  1618. atomic_dec(&oom_callback_count);
  1619. return NOTIFY_OK;
  1620. }
  1621. static struct notifier_block rcu_oom_nb = {
  1622. .notifier_call = rcu_oom_notify
  1623. };
  1624. static int __init rcu_register_oom_notifier(void)
  1625. {
  1626. register_oom_notifier(&rcu_oom_nb);
  1627. return 0;
  1628. }
  1629. early_initcall(rcu_register_oom_notifier);
  1630. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1631. #ifdef CONFIG_RCU_FAST_NO_HZ
  1632. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1633. {
  1634. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1635. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1636. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1637. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1638. ulong2long(nlpd),
  1639. rdtp->all_lazy ? 'L' : '.',
  1640. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1641. }
  1642. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1643. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1644. {
  1645. *cp = '\0';
  1646. }
  1647. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1648. /* Initiate the stall-info list. */
  1649. static void print_cpu_stall_info_begin(void)
  1650. {
  1651. pr_cont("\n");
  1652. }
  1653. /*
  1654. * Print out diagnostic information for the specified stalled CPU.
  1655. *
  1656. * If the specified CPU is aware of the current RCU grace period
  1657. * (flavor specified by rsp), then print the number of scheduling
  1658. * clock interrupts the CPU has taken during the time that it has
  1659. * been aware. Otherwise, print the number of RCU grace periods
  1660. * that this CPU is ignorant of, for example, "1" if the CPU was
  1661. * aware of the previous grace period.
  1662. *
  1663. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1664. */
  1665. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1666. {
  1667. unsigned long delta;
  1668. char fast_no_hz[72];
  1669. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1670. struct rcu_dynticks *rdtp = rdp->dynticks;
  1671. char *ticks_title;
  1672. unsigned long ticks_value;
  1673. /*
  1674. * We could be printing a lot while holding a spinlock. Avoid
  1675. * triggering hard lockup.
  1676. */
  1677. touch_nmi_watchdog();
  1678. ticks_value = rcu_seq_ctr(rsp->gp_seq - rdp->gp_seq);
  1679. if (ticks_value) {
  1680. ticks_title = "GPs behind";
  1681. } else {
  1682. ticks_title = "ticks this GP";
  1683. ticks_value = rdp->ticks_this_gp;
  1684. }
  1685. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1686. delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
  1687. pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s\n",
  1688. cpu,
  1689. "O."[!!cpu_online(cpu)],
  1690. "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
  1691. "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
  1692. !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
  1693. rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
  1694. "!."[!delta],
  1695. ticks_value, ticks_title,
  1696. rcu_dynticks_snap(rdtp) & 0xfff,
  1697. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1698. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1699. READ_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
  1700. fast_no_hz);
  1701. }
  1702. /* Terminate the stall-info list. */
  1703. static void print_cpu_stall_info_end(void)
  1704. {
  1705. pr_err("\t");
  1706. }
  1707. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1708. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1709. {
  1710. rdp->ticks_this_gp = 0;
  1711. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1712. }
  1713. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1714. static void increment_cpu_stall_ticks(void)
  1715. {
  1716. struct rcu_state *rsp;
  1717. for_each_rcu_flavor(rsp)
  1718. raw_cpu_inc(rsp->rda->ticks_this_gp);
  1719. }
  1720. #ifdef CONFIG_RCU_NOCB_CPU
  1721. /*
  1722. * Offload callback processing from the boot-time-specified set of CPUs
  1723. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1724. * kthread created that pulls the callbacks from the corresponding CPU,
  1725. * waits for a grace period to elapse, and invokes the callbacks.
  1726. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1727. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1728. * has been specified, in which case each kthread actively polls its
  1729. * CPU. (Which isn't so great for energy efficiency, but which does
  1730. * reduce RCU's overhead on that CPU.)
  1731. *
  1732. * This is intended to be used in conjunction with Frederic Weisbecker's
  1733. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1734. * running CPU-bound user-mode computations.
  1735. *
  1736. * Offloading of callback processing could also in theory be used as
  1737. * an energy-efficiency measure because CPUs with no RCU callbacks
  1738. * queued are more aggressive about entering dyntick-idle mode.
  1739. */
  1740. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1741. static int __init rcu_nocb_setup(char *str)
  1742. {
  1743. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1744. cpulist_parse(str, rcu_nocb_mask);
  1745. return 1;
  1746. }
  1747. __setup("rcu_nocbs=", rcu_nocb_setup);
  1748. static int __init parse_rcu_nocb_poll(char *arg)
  1749. {
  1750. rcu_nocb_poll = true;
  1751. return 0;
  1752. }
  1753. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1754. /*
  1755. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1756. * grace period.
  1757. */
  1758. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  1759. {
  1760. swake_up_all(sq);
  1761. }
  1762. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  1763. {
  1764. return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
  1765. }
  1766. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1767. {
  1768. init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  1769. init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  1770. }
  1771. /* Is the specified CPU a no-CBs CPU? */
  1772. bool rcu_is_nocb_cpu(int cpu)
  1773. {
  1774. if (cpumask_available(rcu_nocb_mask))
  1775. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1776. return false;
  1777. }
  1778. /*
  1779. * Kick the leader kthread for this NOCB group. Caller holds ->nocb_lock
  1780. * and this function releases it.
  1781. */
  1782. static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
  1783. unsigned long flags)
  1784. __releases(rdp->nocb_lock)
  1785. {
  1786. struct rcu_data *rdp_leader = rdp->nocb_leader;
  1787. lockdep_assert_held(&rdp->nocb_lock);
  1788. if (!READ_ONCE(rdp_leader->nocb_kthread)) {
  1789. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1790. return;
  1791. }
  1792. if (rdp_leader->nocb_leader_sleep || force) {
  1793. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  1794. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  1795. del_timer(&rdp->nocb_timer);
  1796. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1797. smp_mb(); /* ->nocb_leader_sleep before swake_up_one(). */
  1798. swake_up_one(&rdp_leader->nocb_wq);
  1799. } else {
  1800. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1801. }
  1802. }
  1803. /*
  1804. * Kick the leader kthread for this NOCB group, but caller has not
  1805. * acquired locks.
  1806. */
  1807. static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  1808. {
  1809. unsigned long flags;
  1810. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  1811. __wake_nocb_leader(rdp, force, flags);
  1812. }
  1813. /*
  1814. * Arrange to wake the leader kthread for this NOCB group at some
  1815. * future time when it is safe to do so.
  1816. */
  1817. static void wake_nocb_leader_defer(struct rcu_data *rdp, int waketype,
  1818. const char *reason)
  1819. {
  1820. unsigned long flags;
  1821. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  1822. if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT)
  1823. mod_timer(&rdp->nocb_timer, jiffies + 1);
  1824. WRITE_ONCE(rdp->nocb_defer_wakeup, waketype);
  1825. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, reason);
  1826. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1827. }
  1828. /*
  1829. * Does the specified CPU need an RCU callback for the specified flavor
  1830. * of rcu_barrier()?
  1831. */
  1832. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  1833. {
  1834. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1835. unsigned long ret;
  1836. #ifdef CONFIG_PROVE_RCU
  1837. struct rcu_head *rhp;
  1838. #endif /* #ifdef CONFIG_PROVE_RCU */
  1839. /*
  1840. * Check count of all no-CBs callbacks awaiting invocation.
  1841. * There needs to be a barrier before this function is called,
  1842. * but associated with a prior determination that no more
  1843. * callbacks would be posted. In the worst case, the first
  1844. * barrier in _rcu_barrier() suffices (but the caller cannot
  1845. * necessarily rely on this, not a substitute for the caller
  1846. * getting the concurrency design right!). There must also be
  1847. * a barrier between the following load an posting of a callback
  1848. * (if a callback is in fact needed). This is associated with an
  1849. * atomic_inc() in the caller.
  1850. */
  1851. ret = atomic_long_read(&rdp->nocb_q_count);
  1852. #ifdef CONFIG_PROVE_RCU
  1853. rhp = READ_ONCE(rdp->nocb_head);
  1854. if (!rhp)
  1855. rhp = READ_ONCE(rdp->nocb_gp_head);
  1856. if (!rhp)
  1857. rhp = READ_ONCE(rdp->nocb_follower_head);
  1858. /* Having no rcuo kthread but CBs after scheduler starts is bad! */
  1859. if (!READ_ONCE(rdp->nocb_kthread) && rhp &&
  1860. rcu_scheduler_fully_active) {
  1861. /* RCU callback enqueued before CPU first came online??? */
  1862. pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
  1863. cpu, rhp->func);
  1864. WARN_ON_ONCE(1);
  1865. }
  1866. #endif /* #ifdef CONFIG_PROVE_RCU */
  1867. return !!ret;
  1868. }
  1869. /*
  1870. * Enqueue the specified string of rcu_head structures onto the specified
  1871. * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
  1872. * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
  1873. * counts are supplied by rhcount and rhcount_lazy.
  1874. *
  1875. * If warranted, also wake up the kthread servicing this CPUs queues.
  1876. */
  1877. static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
  1878. struct rcu_head *rhp,
  1879. struct rcu_head **rhtp,
  1880. int rhcount, int rhcount_lazy,
  1881. unsigned long flags)
  1882. {
  1883. int len;
  1884. struct rcu_head **old_rhpp;
  1885. struct task_struct *t;
  1886. /* Enqueue the callback on the nocb list and update counts. */
  1887. atomic_long_add(rhcount, &rdp->nocb_q_count);
  1888. /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
  1889. old_rhpp = xchg(&rdp->nocb_tail, rhtp);
  1890. WRITE_ONCE(*old_rhpp, rhp);
  1891. atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
  1892. smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
  1893. /* If we are not being polled and there is a kthread, awaken it ... */
  1894. t = READ_ONCE(rdp->nocb_kthread);
  1895. if (rcu_nocb_poll || !t) {
  1896. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1897. TPS("WakeNotPoll"));
  1898. return;
  1899. }
  1900. len = atomic_long_read(&rdp->nocb_q_count);
  1901. if (old_rhpp == &rdp->nocb_head) {
  1902. if (!irqs_disabled_flags(flags)) {
  1903. /* ... if queue was empty ... */
  1904. wake_nocb_leader(rdp, false);
  1905. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1906. TPS("WakeEmpty"));
  1907. } else {
  1908. wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE,
  1909. TPS("WakeEmptyIsDeferred"));
  1910. }
  1911. rdp->qlen_last_fqs_check = 0;
  1912. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  1913. /* ... or if many callbacks queued. */
  1914. if (!irqs_disabled_flags(flags)) {
  1915. wake_nocb_leader(rdp, true);
  1916. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1917. TPS("WakeOvf"));
  1918. } else {
  1919. wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE_FORCE,
  1920. TPS("WakeOvfIsDeferred"));
  1921. }
  1922. rdp->qlen_last_fqs_check = LONG_MAX / 2;
  1923. } else {
  1924. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
  1925. }
  1926. return;
  1927. }
  1928. /*
  1929. * This is a helper for __call_rcu(), which invokes this when the normal
  1930. * callback queue is inoperable. If this is not a no-CBs CPU, this
  1931. * function returns failure back to __call_rcu(), which can complain
  1932. * appropriately.
  1933. *
  1934. * Otherwise, this function queues the callback where the corresponding
  1935. * "rcuo" kthread can find it.
  1936. */
  1937. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  1938. bool lazy, unsigned long flags)
  1939. {
  1940. if (!rcu_is_nocb_cpu(rdp->cpu))
  1941. return false;
  1942. __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
  1943. if (__is_kfree_rcu_offset((unsigned long)rhp->func))
  1944. trace_rcu_kfree_callback(rdp->rsp->name, rhp,
  1945. (unsigned long)rhp->func,
  1946. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1947. -atomic_long_read(&rdp->nocb_q_count));
  1948. else
  1949. trace_rcu_callback(rdp->rsp->name, rhp,
  1950. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1951. -atomic_long_read(&rdp->nocb_q_count));
  1952. /*
  1953. * If called from an extended quiescent state with interrupts
  1954. * disabled, invoke the RCU core in order to allow the idle-entry
  1955. * deferred-wakeup check to function.
  1956. */
  1957. if (irqs_disabled_flags(flags) &&
  1958. !rcu_is_watching() &&
  1959. cpu_online(smp_processor_id()))
  1960. invoke_rcu_core();
  1961. return true;
  1962. }
  1963. /*
  1964. * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
  1965. * not a no-CBs CPU.
  1966. */
  1967. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
  1968. struct rcu_data *rdp,
  1969. unsigned long flags)
  1970. {
  1971. lockdep_assert_irqs_disabled();
  1972. if (!rcu_is_nocb_cpu(smp_processor_id()))
  1973. return false; /* Not NOCBs CPU, caller must migrate CBs. */
  1974. __call_rcu_nocb_enqueue(my_rdp, rcu_segcblist_head(&rdp->cblist),
  1975. rcu_segcblist_tail(&rdp->cblist),
  1976. rcu_segcblist_n_cbs(&rdp->cblist),
  1977. rcu_segcblist_n_lazy_cbs(&rdp->cblist), flags);
  1978. rcu_segcblist_init(&rdp->cblist);
  1979. rcu_segcblist_disable(&rdp->cblist);
  1980. return true;
  1981. }
  1982. /*
  1983. * If necessary, kick off a new grace period, and either way wait
  1984. * for a subsequent grace period to complete.
  1985. */
  1986. static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  1987. {
  1988. unsigned long c;
  1989. bool d;
  1990. unsigned long flags;
  1991. bool needwake;
  1992. struct rcu_node *rnp = rdp->mynode;
  1993. local_irq_save(flags);
  1994. c = rcu_seq_snap(&rdp->rsp->gp_seq);
  1995. if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
  1996. local_irq_restore(flags);
  1997. } else {
  1998. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1999. needwake = rcu_start_this_gp(rnp, rdp, c);
  2000. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2001. if (needwake)
  2002. rcu_gp_kthread_wake(rdp->rsp);
  2003. }
  2004. /*
  2005. * Wait for the grace period. Do so interruptibly to avoid messing
  2006. * up the load average.
  2007. */
  2008. trace_rcu_this_gp(rnp, rdp, c, TPS("StartWait"));
  2009. for (;;) {
  2010. swait_event_interruptible_exclusive(
  2011. rnp->nocb_gp_wq[rcu_seq_ctr(c) & 0x1],
  2012. (d = rcu_seq_done(&rnp->gp_seq, c)));
  2013. if (likely(d))
  2014. break;
  2015. WARN_ON(signal_pending(current));
  2016. trace_rcu_this_gp(rnp, rdp, c, TPS("ResumeWait"));
  2017. }
  2018. trace_rcu_this_gp(rnp, rdp, c, TPS("EndWait"));
  2019. smp_mb(); /* Ensure that CB invocation happens after GP end. */
  2020. }
  2021. /*
  2022. * Leaders come here to wait for additional callbacks to show up.
  2023. * This function does not return until callbacks appear.
  2024. */
  2025. static void nocb_leader_wait(struct rcu_data *my_rdp)
  2026. {
  2027. bool firsttime = true;
  2028. unsigned long flags;
  2029. bool gotcbs;
  2030. struct rcu_data *rdp;
  2031. struct rcu_head **tail;
  2032. wait_again:
  2033. /* Wait for callbacks to appear. */
  2034. if (!rcu_nocb_poll) {
  2035. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, TPS("Sleep"));
  2036. swait_event_interruptible_exclusive(my_rdp->nocb_wq,
  2037. !READ_ONCE(my_rdp->nocb_leader_sleep));
  2038. raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
  2039. my_rdp->nocb_leader_sleep = true;
  2040. WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  2041. del_timer(&my_rdp->nocb_timer);
  2042. raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
  2043. } else if (firsttime) {
  2044. firsttime = false; /* Don't drown trace log with "Poll"! */
  2045. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, TPS("Poll"));
  2046. }
  2047. /*
  2048. * Each pass through the following loop checks a follower for CBs.
  2049. * We are our own first follower. Any CBs found are moved to
  2050. * nocb_gp_head, where they await a grace period.
  2051. */
  2052. gotcbs = false;
  2053. smp_mb(); /* wakeup and _sleep before ->nocb_head reads. */
  2054. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  2055. rdp->nocb_gp_head = READ_ONCE(rdp->nocb_head);
  2056. if (!rdp->nocb_gp_head)
  2057. continue; /* No CBs here, try next follower. */
  2058. /* Move callbacks to wait-for-GP list, which is empty. */
  2059. WRITE_ONCE(rdp->nocb_head, NULL);
  2060. rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  2061. gotcbs = true;
  2062. }
  2063. /* No callbacks? Sleep a bit if polling, and go retry. */
  2064. if (unlikely(!gotcbs)) {
  2065. WARN_ON(signal_pending(current));
  2066. if (rcu_nocb_poll) {
  2067. schedule_timeout_interruptible(1);
  2068. } else {
  2069. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
  2070. TPS("WokeEmpty"));
  2071. }
  2072. goto wait_again;
  2073. }
  2074. /* Wait for one grace period. */
  2075. rcu_nocb_wait_gp(my_rdp);
  2076. /* Each pass through the following loop wakes a follower, if needed. */
  2077. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  2078. if (!rcu_nocb_poll &&
  2079. READ_ONCE(rdp->nocb_head) &&
  2080. READ_ONCE(my_rdp->nocb_leader_sleep)) {
  2081. raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
  2082. my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
  2083. raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
  2084. }
  2085. if (!rdp->nocb_gp_head)
  2086. continue; /* No CBs, so no need to wake follower. */
  2087. /* Append callbacks to follower's "done" list. */
  2088. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2089. tail = rdp->nocb_follower_tail;
  2090. rdp->nocb_follower_tail = rdp->nocb_gp_tail;
  2091. *tail = rdp->nocb_gp_head;
  2092. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2093. if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
  2094. /* List was empty, so wake up the follower. */
  2095. swake_up_one(&rdp->nocb_wq);
  2096. }
  2097. }
  2098. /* If we (the leader) don't have CBs, go wait some more. */
  2099. if (!my_rdp->nocb_follower_head)
  2100. goto wait_again;
  2101. }
  2102. /*
  2103. * Followers come here to wait for additional callbacks to show up.
  2104. * This function does not return until callbacks appear.
  2105. */
  2106. static void nocb_follower_wait(struct rcu_data *rdp)
  2107. {
  2108. for (;;) {
  2109. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("FollowerSleep"));
  2110. swait_event_interruptible_exclusive(rdp->nocb_wq,
  2111. READ_ONCE(rdp->nocb_follower_head));
  2112. if (smp_load_acquire(&rdp->nocb_follower_head)) {
  2113. /* ^^^ Ensure CB invocation follows _head test. */
  2114. return;
  2115. }
  2116. WARN_ON(signal_pending(current));
  2117. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WokeEmpty"));
  2118. }
  2119. }
  2120. /*
  2121. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  2122. * callbacks queued by the corresponding no-CBs CPU, however, there is
  2123. * an optional leader-follower relationship so that the grace-period
  2124. * kthreads don't have to do quite so many wakeups.
  2125. */
  2126. static int rcu_nocb_kthread(void *arg)
  2127. {
  2128. int c, cl;
  2129. unsigned long flags;
  2130. struct rcu_head *list;
  2131. struct rcu_head *next;
  2132. struct rcu_head **tail;
  2133. struct rcu_data *rdp = arg;
  2134. /* Each pass through this loop invokes one batch of callbacks */
  2135. for (;;) {
  2136. /* Wait for callbacks. */
  2137. if (rdp->nocb_leader == rdp)
  2138. nocb_leader_wait(rdp);
  2139. else
  2140. nocb_follower_wait(rdp);
  2141. /* Pull the ready-to-invoke callbacks onto local list. */
  2142. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2143. list = rdp->nocb_follower_head;
  2144. rdp->nocb_follower_head = NULL;
  2145. tail = rdp->nocb_follower_tail;
  2146. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2147. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2148. BUG_ON(!list);
  2149. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WokeNonEmpty"));
  2150. /* Each pass through the following loop invokes a callback. */
  2151. trace_rcu_batch_start(rdp->rsp->name,
  2152. atomic_long_read(&rdp->nocb_q_count_lazy),
  2153. atomic_long_read(&rdp->nocb_q_count), -1);
  2154. c = cl = 0;
  2155. while (list) {
  2156. next = list->next;
  2157. /* Wait for enqueuing to complete, if needed. */
  2158. while (next == NULL && &list->next != tail) {
  2159. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2160. TPS("WaitQueue"));
  2161. schedule_timeout_interruptible(1);
  2162. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2163. TPS("WokeQueue"));
  2164. next = list->next;
  2165. }
  2166. debug_rcu_head_unqueue(list);
  2167. local_bh_disable();
  2168. if (__rcu_reclaim(rdp->rsp->name, list))
  2169. cl++;
  2170. c++;
  2171. local_bh_enable();
  2172. cond_resched_tasks_rcu_qs();
  2173. list = next;
  2174. }
  2175. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  2176. smp_mb__before_atomic(); /* _add after CB invocation. */
  2177. atomic_long_add(-c, &rdp->nocb_q_count);
  2178. atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
  2179. }
  2180. return 0;
  2181. }
  2182. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  2183. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2184. {
  2185. return READ_ONCE(rdp->nocb_defer_wakeup);
  2186. }
  2187. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  2188. static void do_nocb_deferred_wakeup_common(struct rcu_data *rdp)
  2189. {
  2190. unsigned long flags;
  2191. int ndw;
  2192. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2193. if (!rcu_nocb_need_deferred_wakeup(rdp)) {
  2194. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2195. return;
  2196. }
  2197. ndw = READ_ONCE(rdp->nocb_defer_wakeup);
  2198. WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  2199. __wake_nocb_leader(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
  2200. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
  2201. }
  2202. /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
  2203. static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
  2204. {
  2205. struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
  2206. do_nocb_deferred_wakeup_common(rdp);
  2207. }
  2208. /*
  2209. * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
  2210. * This means we do an inexact common-case check. Note that if
  2211. * we miss, ->nocb_timer will eventually clean things up.
  2212. */
  2213. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2214. {
  2215. if (rcu_nocb_need_deferred_wakeup(rdp))
  2216. do_nocb_deferred_wakeup_common(rdp);
  2217. }
  2218. void __init rcu_init_nohz(void)
  2219. {
  2220. int cpu;
  2221. bool need_rcu_nocb_mask = false;
  2222. struct rcu_state *rsp;
  2223. #if defined(CONFIG_NO_HZ_FULL)
  2224. if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
  2225. need_rcu_nocb_mask = true;
  2226. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2227. if (!cpumask_available(rcu_nocb_mask) && need_rcu_nocb_mask) {
  2228. if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
  2229. pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
  2230. return;
  2231. }
  2232. }
  2233. if (!cpumask_available(rcu_nocb_mask))
  2234. return;
  2235. #if defined(CONFIG_NO_HZ_FULL)
  2236. if (tick_nohz_full_running)
  2237. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
  2238. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2239. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  2240. pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
  2241. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  2242. rcu_nocb_mask);
  2243. }
  2244. if (cpumask_empty(rcu_nocb_mask))
  2245. pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
  2246. else
  2247. pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
  2248. cpumask_pr_args(rcu_nocb_mask));
  2249. if (rcu_nocb_poll)
  2250. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  2251. for_each_rcu_flavor(rsp) {
  2252. for_each_cpu(cpu, rcu_nocb_mask)
  2253. init_nocb_callback_list(per_cpu_ptr(rsp->rda, cpu));
  2254. rcu_organize_nocb_kthreads(rsp);
  2255. }
  2256. }
  2257. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2258. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2259. {
  2260. rdp->nocb_tail = &rdp->nocb_head;
  2261. init_swait_queue_head(&rdp->nocb_wq);
  2262. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2263. raw_spin_lock_init(&rdp->nocb_lock);
  2264. timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
  2265. }
  2266. /*
  2267. * If the specified CPU is a no-CBs CPU that does not already have its
  2268. * rcuo kthread for the specified RCU flavor, spawn it. If the CPUs are
  2269. * brought online out of order, this can require re-organizing the
  2270. * leader-follower relationships.
  2271. */
  2272. static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
  2273. {
  2274. struct rcu_data *rdp;
  2275. struct rcu_data *rdp_last;
  2276. struct rcu_data *rdp_old_leader;
  2277. struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
  2278. struct task_struct *t;
  2279. /*
  2280. * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
  2281. * then nothing to do.
  2282. */
  2283. if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
  2284. return;
  2285. /* If we didn't spawn the leader first, reorganize! */
  2286. rdp_old_leader = rdp_spawn->nocb_leader;
  2287. if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
  2288. rdp_last = NULL;
  2289. rdp = rdp_old_leader;
  2290. do {
  2291. rdp->nocb_leader = rdp_spawn;
  2292. if (rdp_last && rdp != rdp_spawn)
  2293. rdp_last->nocb_next_follower = rdp;
  2294. if (rdp == rdp_spawn) {
  2295. rdp = rdp->nocb_next_follower;
  2296. } else {
  2297. rdp_last = rdp;
  2298. rdp = rdp->nocb_next_follower;
  2299. rdp_last->nocb_next_follower = NULL;
  2300. }
  2301. } while (rdp);
  2302. rdp_spawn->nocb_next_follower = rdp_old_leader;
  2303. }
  2304. /* Spawn the kthread for this CPU and RCU flavor. */
  2305. t = kthread_run(rcu_nocb_kthread, rdp_spawn,
  2306. "rcuo%c/%d", rsp->abbr, cpu);
  2307. BUG_ON(IS_ERR(t));
  2308. WRITE_ONCE(rdp_spawn->nocb_kthread, t);
  2309. }
  2310. /*
  2311. * If the specified CPU is a no-CBs CPU that does not already have its
  2312. * rcuo kthreads, spawn them.
  2313. */
  2314. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2315. {
  2316. struct rcu_state *rsp;
  2317. if (rcu_scheduler_fully_active)
  2318. for_each_rcu_flavor(rsp)
  2319. rcu_spawn_one_nocb_kthread(rsp, cpu);
  2320. }
  2321. /*
  2322. * Once the scheduler is running, spawn rcuo kthreads for all online
  2323. * no-CBs CPUs. This assumes that the early_initcall()s happen before
  2324. * non-boot CPUs come online -- if this changes, we will need to add
  2325. * some mutual exclusion.
  2326. */
  2327. static void __init rcu_spawn_nocb_kthreads(void)
  2328. {
  2329. int cpu;
  2330. for_each_online_cpu(cpu)
  2331. rcu_spawn_all_nocb_kthreads(cpu);
  2332. }
  2333. /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
  2334. static int rcu_nocb_leader_stride = -1;
  2335. module_param(rcu_nocb_leader_stride, int, 0444);
  2336. /*
  2337. * Initialize leader-follower relationships for all no-CBs CPU.
  2338. */
  2339. static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
  2340. {
  2341. int cpu;
  2342. int ls = rcu_nocb_leader_stride;
  2343. int nl = 0; /* Next leader. */
  2344. struct rcu_data *rdp;
  2345. struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
  2346. struct rcu_data *rdp_prev = NULL;
  2347. if (!cpumask_available(rcu_nocb_mask))
  2348. return;
  2349. if (ls == -1) {
  2350. ls = int_sqrt(nr_cpu_ids);
  2351. rcu_nocb_leader_stride = ls;
  2352. }
  2353. /*
  2354. * Each pass through this loop sets up one rcu_data structure.
  2355. * Should the corresponding CPU come online in the future, then
  2356. * we will spawn the needed set of rcu_nocb_kthread() kthreads.
  2357. */
  2358. for_each_cpu(cpu, rcu_nocb_mask) {
  2359. rdp = per_cpu_ptr(rsp->rda, cpu);
  2360. if (rdp->cpu >= nl) {
  2361. /* New leader, set up for followers & next leader. */
  2362. nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
  2363. rdp->nocb_leader = rdp;
  2364. rdp_leader = rdp;
  2365. } else {
  2366. /* Another follower, link to previous leader. */
  2367. rdp->nocb_leader = rdp_leader;
  2368. rdp_prev->nocb_next_follower = rdp;
  2369. }
  2370. rdp_prev = rdp;
  2371. }
  2372. }
  2373. /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
  2374. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2375. {
  2376. if (!rcu_is_nocb_cpu(rdp->cpu))
  2377. return false;
  2378. /* If there are early-boot callbacks, move them to nocb lists. */
  2379. if (!rcu_segcblist_empty(&rdp->cblist)) {
  2380. rdp->nocb_head = rcu_segcblist_head(&rdp->cblist);
  2381. rdp->nocb_tail = rcu_segcblist_tail(&rdp->cblist);
  2382. atomic_long_set(&rdp->nocb_q_count,
  2383. rcu_segcblist_n_cbs(&rdp->cblist));
  2384. atomic_long_set(&rdp->nocb_q_count_lazy,
  2385. rcu_segcblist_n_lazy_cbs(&rdp->cblist));
  2386. rcu_segcblist_init(&rdp->cblist);
  2387. }
  2388. rcu_segcblist_disable(&rdp->cblist);
  2389. return true;
  2390. }
  2391. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  2392. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  2393. {
  2394. WARN_ON_ONCE(1); /* Should be dead code. */
  2395. return false;
  2396. }
  2397. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  2398. {
  2399. }
  2400. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  2401. {
  2402. return NULL;
  2403. }
  2404. static void rcu_init_one_nocb(struct rcu_node *rnp)
  2405. {
  2406. }
  2407. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  2408. bool lazy, unsigned long flags)
  2409. {
  2410. return false;
  2411. }
  2412. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
  2413. struct rcu_data *rdp,
  2414. unsigned long flags)
  2415. {
  2416. return false;
  2417. }
  2418. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2419. {
  2420. }
  2421. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2422. {
  2423. return false;
  2424. }
  2425. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2426. {
  2427. }
  2428. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2429. {
  2430. }
  2431. static void __init rcu_spawn_nocb_kthreads(void)
  2432. {
  2433. }
  2434. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2435. {
  2436. return false;
  2437. }
  2438. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  2439. /*
  2440. * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
  2441. * grace-period kthread will do force_quiescent_state() processing?
  2442. * The idea is to avoid waking up RCU core processing on such a
  2443. * CPU unless the grace period has extended for too long.
  2444. *
  2445. * This code relies on the fact that all NO_HZ_FULL CPUs are also
  2446. * CONFIG_RCU_NOCB_CPU CPUs.
  2447. */
  2448. static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
  2449. {
  2450. #ifdef CONFIG_NO_HZ_FULL
  2451. if (tick_nohz_full_cpu(smp_processor_id()) &&
  2452. (!rcu_gp_in_progress(rsp) ||
  2453. ULONG_CMP_LT(jiffies, READ_ONCE(rsp->gp_start) + HZ)))
  2454. return true;
  2455. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2456. return false;
  2457. }
  2458. /*
  2459. * Bind the RCU grace-period kthreads to the housekeeping CPU.
  2460. */
  2461. static void rcu_bind_gp_kthread(void)
  2462. {
  2463. if (!tick_nohz_full_enabled())
  2464. return;
  2465. housekeeping_affine(current, HK_FLAG_RCU);
  2466. }
  2467. /* Record the current task on dyntick-idle entry. */
  2468. static void rcu_dynticks_task_enter(void)
  2469. {
  2470. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2471. WRITE_ONCE(current->rcu_tasks_idle_cpu, smp_processor_id());
  2472. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2473. }
  2474. /* Record no current task on dyntick-idle exit. */
  2475. static void rcu_dynticks_task_exit(void)
  2476. {
  2477. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2478. WRITE_ONCE(current->rcu_tasks_idle_cpu, -1);
  2479. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2480. }