tree_plugin.h 87 KB

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