tree_plugin.h 89 KB

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