tree_plugin.h 90 KB

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