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 "../time/tick-internal.h"
  31. #ifdef CONFIG_RCU_BOOST
  32. #include "../locking/rtmutex_common.h"
  33. /*
  34. * Control variables for per-CPU and per-rcu_node kthreads. These
  35. * handle all flavors of RCU.
  36. */
  37. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  38. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  39. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  40. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  41. #else /* #ifdef CONFIG_RCU_BOOST */
  42. /*
  43. * Some architectures do not define rt_mutexes, but if !CONFIG_RCU_BOOST,
  44. * all uses are in dead code. Provide a definition to keep the compiler
  45. * happy, but add WARN_ON_ONCE() to complain if used in the wrong place.
  46. * This probably needs to be excluded from -rt builds.
  47. */
  48. #define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
  49. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  50. #ifdef CONFIG_RCU_NOCB_CPU
  51. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  52. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  53. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  54. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  55. /*
  56. * Check the RCU kernel configuration parameters and print informative
  57. * messages about anything out of the ordinary.
  58. */
  59. static void __init rcu_bootup_announce_oddness(void)
  60. {
  61. if (IS_ENABLED(CONFIG_RCU_TRACE))
  62. pr_info("\tRCU debugfs-based tracing is enabled.\n");
  63. if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
  64. (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
  65. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  66. RCU_FANOUT);
  67. if (rcu_fanout_exact)
  68. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  69. if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
  70. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  71. if (IS_ENABLED(CONFIG_PROVE_RCU))
  72. pr_info("\tRCU lockdep checking is enabled.\n");
  73. if (RCU_NUM_LVLS >= 4)
  74. pr_info("\tFour(or more)-level hierarchy is enabled.\n");
  75. if (RCU_FANOUT_LEAF != 16)
  76. pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
  77. RCU_FANOUT_LEAF);
  78. if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
  79. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
  80. if (nr_cpu_ids != NR_CPUS)
  81. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
  82. if (IS_ENABLED(CONFIG_RCU_BOOST))
  83. pr_info("\tRCU kthread priority: %d.\n", kthread_prio);
  84. }
  85. #ifdef CONFIG_PREEMPT_RCU
  86. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  87. static struct rcu_state *const rcu_state_p = &rcu_preempt_state;
  88. static struct rcu_data __percpu *const rcu_data_p = &rcu_preempt_data;
  89. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  90. bool wake);
  91. /*
  92. * Tell them what RCU they are running.
  93. */
  94. static void __init rcu_bootup_announce(void)
  95. {
  96. pr_info("Preemptible hierarchical RCU implementation.\n");
  97. rcu_bootup_announce_oddness();
  98. }
  99. /* Flags for rcu_preempt_ctxt_queue() decision table. */
  100. #define RCU_GP_TASKS 0x8
  101. #define RCU_EXP_TASKS 0x4
  102. #define RCU_GP_BLKD 0x2
  103. #define RCU_EXP_BLKD 0x1
  104. /*
  105. * Queues a task preempted within an RCU-preempt read-side critical
  106. * section into the appropriate location within the ->blkd_tasks list,
  107. * depending on the states of any ongoing normal and expedited grace
  108. * periods. The ->gp_tasks pointer indicates which element the normal
  109. * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
  110. * indicates which element the expedited grace period is waiting on (again,
  111. * NULL if none). If a grace period is waiting on a given element in the
  112. * ->blkd_tasks list, it also waits on all subsequent elements. Thus,
  113. * adding a task to the tail of the list blocks any grace period that is
  114. * already waiting on one of the elements. In contrast, adding a task
  115. * to the head of the list won't block any grace period that is already
  116. * waiting on one of the elements.
  117. *
  118. * This queuing is imprecise, and can sometimes make an ongoing grace
  119. * period wait for a task that is not strictly speaking blocking it.
  120. * Given the choice, we needlessly block a normal grace period rather than
  121. * blocking an expedited grace period.
  122. *
  123. * Note that an endless sequence of expedited grace periods still cannot
  124. * indefinitely postpone a normal grace period. Eventually, all of the
  125. * fixed number of preempted tasks blocking the normal grace period that are
  126. * not also blocking the expedited grace period will resume and complete
  127. * their RCU read-side critical sections. At that point, the ->gp_tasks
  128. * pointer will equal the ->exp_tasks pointer, at which point the end of
  129. * the corresponding expedited grace period will also be the end of the
  130. * normal grace period.
  131. */
  132. static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
  133. __releases(rnp->lock) /* But leaves rrupts disabled. */
  134. {
  135. int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
  136. (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
  137. (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
  138. (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
  139. struct task_struct *t = current;
  140. /*
  141. * Decide where to queue the newly blocked task. In theory,
  142. * this could be an if-statement. In practice, when I tried
  143. * that, it was quite messy.
  144. */
  145. switch (blkd_state) {
  146. case 0:
  147. case RCU_EXP_TASKS:
  148. case RCU_EXP_TASKS + RCU_GP_BLKD:
  149. case RCU_GP_TASKS:
  150. case RCU_GP_TASKS + RCU_EXP_TASKS:
  151. /*
  152. * Blocking neither GP, or first task blocking the normal
  153. * GP but not blocking the already-waiting expedited GP.
  154. * Queue at the head of the list to avoid unnecessarily
  155. * blocking the already-waiting GPs.
  156. */
  157. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  158. break;
  159. case RCU_EXP_BLKD:
  160. case RCU_GP_BLKD:
  161. case RCU_GP_BLKD + RCU_EXP_BLKD:
  162. case RCU_GP_TASKS + RCU_EXP_BLKD:
  163. case RCU_GP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  164. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  165. /*
  166. * First task arriving that blocks either GP, or first task
  167. * arriving that blocks the expedited GP (with the normal
  168. * GP already waiting), or a task arriving that blocks
  169. * both GPs with both GPs already waiting. Queue at the
  170. * tail of the list to avoid any GP waiting on any of the
  171. * already queued tasks that are not blocking it.
  172. */
  173. list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
  174. break;
  175. case RCU_EXP_TASKS + RCU_EXP_BLKD:
  176. case RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  177. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_EXP_BLKD:
  178. /*
  179. * Second or subsequent task blocking the expedited GP.
  180. * The task either does not block the normal GP, or is the
  181. * first task blocking the normal GP. Queue just after
  182. * the first task blocking the expedited GP.
  183. */
  184. list_add(&t->rcu_node_entry, rnp->exp_tasks);
  185. break;
  186. case RCU_GP_TASKS + RCU_GP_BLKD:
  187. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
  188. /*
  189. * Second or subsequent task blocking the normal GP.
  190. * The task does not block the expedited GP. Queue just
  191. * after the first task blocking the normal GP.
  192. */
  193. list_add(&t->rcu_node_entry, rnp->gp_tasks);
  194. break;
  195. default:
  196. /* Yet another exercise in excessive paranoia. */
  197. WARN_ON_ONCE(1);
  198. break;
  199. }
  200. /*
  201. * We have now queued the task. If it was the first one to
  202. * block either grace period, update the ->gp_tasks and/or
  203. * ->exp_tasks pointers, respectively, to reference the newly
  204. * blocked tasks.
  205. */
  206. if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD))
  207. rnp->gp_tasks = &t->rcu_node_entry;
  208. if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
  209. rnp->exp_tasks = &t->rcu_node_entry;
  210. raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
  211. /*
  212. * Report the quiescent state for the expedited GP. This expedited
  213. * GP should not be able to end until we report, so there should be
  214. * no need to check for a subsequent expedited GP. (Though we are
  215. * still in a quiescent state in any case.)
  216. */
  217. if (blkd_state & RCU_EXP_BLKD &&
  218. t->rcu_read_unlock_special.b.exp_need_qs) {
  219. t->rcu_read_unlock_special.b.exp_need_qs = false;
  220. rcu_report_exp_rdp(rdp->rsp, rdp, true);
  221. } else {
  222. WARN_ON_ONCE(t->rcu_read_unlock_special.b.exp_need_qs);
  223. }
  224. }
  225. /*
  226. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  227. * that this just means that the task currently running on the CPU is
  228. * not in a quiescent state. There might be any number of tasks blocked
  229. * while in an RCU read-side critical section.
  230. *
  231. * As with the other rcu_*_qs() functions, callers to this function
  232. * must disable preemption.
  233. */
  234. static void rcu_preempt_qs(void)
  235. {
  236. if (__this_cpu_read(rcu_data_p->cpu_no_qs.s)) {
  237. trace_rcu_grace_period(TPS("rcu_preempt"),
  238. __this_cpu_read(rcu_data_p->gpnum),
  239. TPS("cpuqs"));
  240. __this_cpu_write(rcu_data_p->cpu_no_qs.b.norm, false);
  241. barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
  242. current->rcu_read_unlock_special.b.need_qs = false;
  243. }
  244. }
  245. /*
  246. * We have entered the scheduler, and the current task might soon be
  247. * context-switched away from. If this task is in an RCU read-side
  248. * critical section, we will no longer be able to rely on the CPU to
  249. * record that fact, so we enqueue the task on the blkd_tasks list.
  250. * The task will dequeue itself when it exits the outermost enclosing
  251. * RCU read-side critical section. Therefore, the current grace period
  252. * cannot be permitted to complete until the blkd_tasks list entries
  253. * predating the current grace period drain, in other words, until
  254. * rnp->gp_tasks becomes NULL.
  255. *
  256. * Caller must disable interrupts.
  257. */
  258. static void rcu_preempt_note_context_switch(void)
  259. {
  260. struct task_struct *t = current;
  261. struct rcu_data *rdp;
  262. struct rcu_node *rnp;
  263. if (t->rcu_read_lock_nesting > 0 &&
  264. !t->rcu_read_unlock_special.b.blocked) {
  265. /* Possibly blocking in an RCU read-side critical section. */
  266. rdp = this_cpu_ptr(rcu_state_p->rda);
  267. rnp = rdp->mynode;
  268. raw_spin_lock_rcu_node(rnp);
  269. t->rcu_read_unlock_special.b.blocked = true;
  270. t->rcu_blocked_node = rnp;
  271. /*
  272. * Verify the CPU's sanity, trace the preemption, and
  273. * then queue the task as required based on the states
  274. * of any ongoing and expedited grace periods.
  275. */
  276. WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
  277. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  278. trace_rcu_preempt_task(rdp->rsp->name,
  279. t->pid,
  280. (rnp->qsmask & rdp->grpmask)
  281. ? rnp->gpnum
  282. : rnp->gpnum + 1);
  283. rcu_preempt_ctxt_queue(rnp, rdp);
  284. } else if (t->rcu_read_lock_nesting < 0 &&
  285. t->rcu_read_unlock_special.s) {
  286. /*
  287. * Complete exit from RCU read-side critical section on
  288. * behalf of preempted instance of __rcu_read_unlock().
  289. */
  290. rcu_read_unlock_special(t);
  291. }
  292. /*
  293. * Either we were not in an RCU read-side critical section to
  294. * begin with, or we have now recorded that critical section
  295. * globally. Either way, we can now note a quiescent state
  296. * for this CPU. Again, if we were in an RCU read-side critical
  297. * section, and if that critical section was blocking the current
  298. * grace period, then the fact that the task has been enqueued
  299. * means that we continue to block the current grace period.
  300. */
  301. rcu_preempt_qs();
  302. }
  303. /*
  304. * Check for preempted RCU readers blocking the current grace period
  305. * for the specified rcu_node structure. If the caller needs a reliable
  306. * answer, it must hold the rcu_node's ->lock.
  307. */
  308. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  309. {
  310. return rnp->gp_tasks != NULL;
  311. }
  312. /*
  313. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  314. * returning NULL if at the end of the list.
  315. */
  316. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  317. struct rcu_node *rnp)
  318. {
  319. struct list_head *np;
  320. np = t->rcu_node_entry.next;
  321. if (np == &rnp->blkd_tasks)
  322. np = NULL;
  323. return np;
  324. }
  325. /*
  326. * Return true if the specified rcu_node structure has tasks that were
  327. * preempted within an RCU read-side critical section.
  328. */
  329. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  330. {
  331. return !list_empty(&rnp->blkd_tasks);
  332. }
  333. /*
  334. * Handle special cases during rcu_read_unlock(), such as needing to
  335. * notify RCU core processing or task having blocked during the RCU
  336. * read-side critical section.
  337. */
  338. void rcu_read_unlock_special(struct task_struct *t)
  339. {
  340. bool empty_exp;
  341. bool empty_norm;
  342. bool empty_exp_now;
  343. unsigned long flags;
  344. struct list_head *np;
  345. bool drop_boost_mutex = false;
  346. struct rcu_data *rdp;
  347. struct rcu_node *rnp;
  348. union rcu_special special;
  349. /* NMI handlers cannot block and cannot safely manipulate state. */
  350. if (in_nmi())
  351. return;
  352. local_irq_save(flags);
  353. /*
  354. * If RCU core is waiting for this CPU to exit its critical section,
  355. * report the fact that it has exited. Because irqs are disabled,
  356. * t->rcu_read_unlock_special cannot change.
  357. */
  358. special = t->rcu_read_unlock_special;
  359. if (special.b.need_qs) {
  360. rcu_preempt_qs();
  361. t->rcu_read_unlock_special.b.need_qs = false;
  362. if (!t->rcu_read_unlock_special.s) {
  363. local_irq_restore(flags);
  364. return;
  365. }
  366. }
  367. /*
  368. * Respond to a request for an expedited grace period, but only if
  369. * we were not preempted, meaning that we were running on the same
  370. * CPU throughout. If we were preempted, the exp_need_qs flag
  371. * would have been cleared at the time of the first preemption,
  372. * and the quiescent state would be reported when we were dequeued.
  373. */
  374. if (special.b.exp_need_qs) {
  375. WARN_ON_ONCE(special.b.blocked);
  376. t->rcu_read_unlock_special.b.exp_need_qs = false;
  377. rdp = this_cpu_ptr(rcu_state_p->rda);
  378. rcu_report_exp_rdp(rcu_state_p, rdp, true);
  379. if (!t->rcu_read_unlock_special.s) {
  380. local_irq_restore(flags);
  381. return;
  382. }
  383. }
  384. /* Hardware IRQ handlers cannot block, complain if they get here. */
  385. if (in_irq() || in_serving_softirq()) {
  386. lockdep_rcu_suspicious(__FILE__, __LINE__,
  387. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  388. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  389. t->rcu_read_unlock_special.s,
  390. t->rcu_read_unlock_special.b.blocked,
  391. t->rcu_read_unlock_special.b.exp_need_qs,
  392. t->rcu_read_unlock_special.b.need_qs);
  393. local_irq_restore(flags);
  394. return;
  395. }
  396. /* Clean up if blocked during RCU read-side critical section. */
  397. if (special.b.blocked) {
  398. t->rcu_read_unlock_special.b.blocked = false;
  399. /*
  400. * Remove this task from the list it blocked on. The task
  401. * now remains queued on the rcu_node corresponding to the
  402. * CPU it first blocked on, so there is no longer any need
  403. * to loop. Retain a WARN_ON_ONCE() out of sheer paranoia.
  404. */
  405. rnp = t->rcu_blocked_node;
  406. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  407. WARN_ON_ONCE(rnp != t->rcu_blocked_node);
  408. empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
  409. empty_exp = sync_rcu_preempt_exp_done(rnp);
  410. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  411. np = rcu_next_node_entry(t, rnp);
  412. list_del_init(&t->rcu_node_entry);
  413. t->rcu_blocked_node = NULL;
  414. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  415. rnp->gpnum, t->pid);
  416. if (&t->rcu_node_entry == rnp->gp_tasks)
  417. rnp->gp_tasks = np;
  418. if (&t->rcu_node_entry == rnp->exp_tasks)
  419. rnp->exp_tasks = np;
  420. if (IS_ENABLED(CONFIG_RCU_BOOST)) {
  421. if (&t->rcu_node_entry == rnp->boost_tasks)
  422. rnp->boost_tasks = np;
  423. /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
  424. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
  425. }
  426. /*
  427. * If this was the last task on the current list, and if
  428. * we aren't waiting on any CPUs, report the quiescent state.
  429. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  430. * so we must take a snapshot of the expedited state.
  431. */
  432. empty_exp_now = sync_rcu_preempt_exp_done(rnp);
  433. if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
  434. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  435. rnp->gpnum,
  436. 0, rnp->qsmask,
  437. rnp->level,
  438. rnp->grplo,
  439. rnp->grphi,
  440. !!rnp->gp_tasks);
  441. rcu_report_unblock_qs_rnp(rcu_state_p, rnp, flags);
  442. } else {
  443. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  444. }
  445. /* Unboost if we were boosted. */
  446. if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
  447. rt_mutex_unlock(&rnp->boost_mtx);
  448. /*
  449. * If this was the last task on the expedited lists,
  450. * then we need to report up the rcu_node hierarchy.
  451. */
  452. if (!empty_exp && empty_exp_now)
  453. rcu_report_exp_rnp(rcu_state_p, rnp, true);
  454. } else {
  455. local_irq_restore(flags);
  456. }
  457. }
  458. /*
  459. * Dump detailed information for all tasks blocking the current RCU
  460. * grace period on the specified rcu_node structure.
  461. */
  462. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  463. {
  464. unsigned long flags;
  465. struct task_struct *t;
  466. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  467. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  468. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  469. return;
  470. }
  471. t = list_entry(rnp->gp_tasks->prev,
  472. struct task_struct, rcu_node_entry);
  473. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  474. sched_show_task(t);
  475. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  476. }
  477. /*
  478. * Dump detailed information for all tasks blocking the current RCU
  479. * grace period.
  480. */
  481. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  482. {
  483. struct rcu_node *rnp = rcu_get_root(rsp);
  484. rcu_print_detail_task_stall_rnp(rnp);
  485. rcu_for_each_leaf_node(rsp, rnp)
  486. rcu_print_detail_task_stall_rnp(rnp);
  487. }
  488. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  489. {
  490. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  491. rnp->level, rnp->grplo, rnp->grphi);
  492. }
  493. static void rcu_print_task_stall_end(void)
  494. {
  495. pr_cont("\n");
  496. }
  497. /*
  498. * Scan the current list of tasks blocked within RCU read-side critical
  499. * sections, printing out the tid of each.
  500. */
  501. static int rcu_print_task_stall(struct rcu_node *rnp)
  502. {
  503. struct task_struct *t;
  504. int ndetected = 0;
  505. if (!rcu_preempt_blocked_readers_cgp(rnp))
  506. return 0;
  507. rcu_print_task_stall_begin(rnp);
  508. t = list_entry(rnp->gp_tasks->prev,
  509. struct task_struct, rcu_node_entry);
  510. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  511. pr_cont(" P%d", t->pid);
  512. ndetected++;
  513. }
  514. rcu_print_task_stall_end();
  515. return ndetected;
  516. }
  517. /*
  518. * Scan the current list of tasks blocked within RCU read-side critical
  519. * sections, printing out the tid of each that is blocking the current
  520. * expedited grace period.
  521. */
  522. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  523. {
  524. struct task_struct *t;
  525. int ndetected = 0;
  526. if (!rnp->exp_tasks)
  527. return 0;
  528. t = list_entry(rnp->exp_tasks->prev,
  529. struct task_struct, rcu_node_entry);
  530. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  531. pr_cont(" P%d", t->pid);
  532. ndetected++;
  533. }
  534. return ndetected;
  535. }
  536. /*
  537. * Check that the list of blocked tasks for the newly completed grace
  538. * period is in fact empty. It is a serious bug to complete a grace
  539. * period that still has RCU readers blocked! This function must be
  540. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  541. * must be held by the caller.
  542. *
  543. * Also, if there are blocked tasks on the list, they automatically
  544. * block the newly created grace period, so set up ->gp_tasks accordingly.
  545. */
  546. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  547. {
  548. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  549. if (rcu_preempt_has_tasks(rnp))
  550. rnp->gp_tasks = rnp->blkd_tasks.next;
  551. WARN_ON_ONCE(rnp->qsmask);
  552. }
  553. /*
  554. * Check for a quiescent state from the current CPU. When a task blocks,
  555. * the task is recorded in the corresponding CPU's rcu_node structure,
  556. * which is checked elsewhere.
  557. *
  558. * Caller must disable hard irqs.
  559. */
  560. static void rcu_preempt_check_callbacks(void)
  561. {
  562. struct task_struct *t = current;
  563. if (t->rcu_read_lock_nesting == 0) {
  564. rcu_preempt_qs();
  565. return;
  566. }
  567. if (t->rcu_read_lock_nesting > 0 &&
  568. __this_cpu_read(rcu_data_p->core_needs_qs) &&
  569. __this_cpu_read(rcu_data_p->cpu_no_qs.b.norm))
  570. t->rcu_read_unlock_special.b.need_qs = true;
  571. }
  572. #ifdef CONFIG_RCU_BOOST
  573. static void rcu_preempt_do_callbacks(void)
  574. {
  575. rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  576. }
  577. #endif /* #ifdef CONFIG_RCU_BOOST */
  578. /*
  579. * Queue a preemptible-RCU callback for invocation after a grace period.
  580. */
  581. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  582. {
  583. __call_rcu(head, func, rcu_state_p, -1, 0);
  584. }
  585. EXPORT_SYMBOL_GPL(call_rcu);
  586. /**
  587. * synchronize_rcu - wait until a grace period has elapsed.
  588. *
  589. * Control will return to the caller some time after a full grace
  590. * period has elapsed, in other words after all currently executing RCU
  591. * read-side critical sections have completed. Note, however, that
  592. * upon return from synchronize_rcu(), the caller might well be executing
  593. * concurrently with new RCU read-side critical sections that began while
  594. * synchronize_rcu() was waiting. RCU read-side critical sections are
  595. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  596. *
  597. * See the description of synchronize_sched() for more detailed information
  598. * on memory ordering guarantees.
  599. */
  600. void synchronize_rcu(void)
  601. {
  602. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  603. lock_is_held(&rcu_lock_map) ||
  604. lock_is_held(&rcu_sched_lock_map),
  605. "Illegal synchronize_rcu() in RCU read-side critical section");
  606. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  607. return;
  608. if (rcu_gp_is_expedited())
  609. synchronize_rcu_expedited();
  610. else
  611. wait_rcu_gp(call_rcu);
  612. }
  613. EXPORT_SYMBOL_GPL(synchronize_rcu);
  614. /**
  615. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  616. *
  617. * Note that this primitive does not necessarily wait for an RCU grace period
  618. * to complete. For example, if there are no RCU callbacks queued anywhere
  619. * in the system, then rcu_barrier() is within its rights to return
  620. * immediately, without waiting for anything, much less an RCU grace period.
  621. */
  622. void rcu_barrier(void)
  623. {
  624. _rcu_barrier(rcu_state_p);
  625. }
  626. EXPORT_SYMBOL_GPL(rcu_barrier);
  627. /*
  628. * Initialize preemptible RCU's state structures.
  629. */
  630. static void __init __rcu_init_preempt(void)
  631. {
  632. rcu_init_one(rcu_state_p);
  633. }
  634. /*
  635. * Check for a task exiting while in a preemptible-RCU read-side
  636. * critical section, clean up if so. No need to issue warnings,
  637. * as debug_check_no_locks_held() already does this if lockdep
  638. * is enabled.
  639. */
  640. void exit_rcu(void)
  641. {
  642. struct task_struct *t = current;
  643. if (likely(list_empty(&current->rcu_node_entry)))
  644. return;
  645. t->rcu_read_lock_nesting = 1;
  646. barrier();
  647. t->rcu_read_unlock_special.b.blocked = true;
  648. __rcu_read_unlock();
  649. }
  650. #else /* #ifdef CONFIG_PREEMPT_RCU */
  651. static struct rcu_state *const rcu_state_p = &rcu_sched_state;
  652. /*
  653. * Tell them what RCU they are running.
  654. */
  655. static void __init rcu_bootup_announce(void)
  656. {
  657. pr_info("Hierarchical RCU implementation.\n");
  658. rcu_bootup_announce_oddness();
  659. }
  660. /*
  661. * Because preemptible RCU does not exist, we never have to check for
  662. * CPUs being in quiescent states.
  663. */
  664. static void rcu_preempt_note_context_switch(void)
  665. {
  666. }
  667. /*
  668. * Because preemptible RCU does not exist, there are never any preempted
  669. * RCU readers.
  670. */
  671. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  672. {
  673. return 0;
  674. }
  675. /*
  676. * Because there is no preemptible RCU, there can be no readers blocked.
  677. */
  678. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  679. {
  680. return false;
  681. }
  682. /*
  683. * Because preemptible RCU does not exist, we never have to check for
  684. * tasks blocked within RCU read-side critical sections.
  685. */
  686. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  687. {
  688. }
  689. /*
  690. * Because preemptible RCU does not exist, we never have to check for
  691. * tasks blocked within RCU read-side critical sections.
  692. */
  693. static int rcu_print_task_stall(struct rcu_node *rnp)
  694. {
  695. return 0;
  696. }
  697. /*
  698. * Because preemptible RCU does not exist, we never have to check for
  699. * tasks blocked within RCU read-side critical sections that are
  700. * blocking the current expedited grace period.
  701. */
  702. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  703. {
  704. return 0;
  705. }
  706. /*
  707. * Because there is no preemptible RCU, there can be no readers blocked,
  708. * so there is no need to check for blocked tasks. So check only for
  709. * bogus qsmask values.
  710. */
  711. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  712. {
  713. WARN_ON_ONCE(rnp->qsmask);
  714. }
  715. /*
  716. * Because preemptible RCU does not exist, it never has any callbacks
  717. * to check.
  718. */
  719. static void rcu_preempt_check_callbacks(void)
  720. {
  721. }
  722. /*
  723. * Because preemptible RCU does not exist, rcu_barrier() is just
  724. * another name for rcu_barrier_sched().
  725. */
  726. void rcu_barrier(void)
  727. {
  728. rcu_barrier_sched();
  729. }
  730. EXPORT_SYMBOL_GPL(rcu_barrier);
  731. /*
  732. * Because preemptible RCU does not exist, it need not be initialized.
  733. */
  734. static void __init __rcu_init_preempt(void)
  735. {
  736. }
  737. /*
  738. * Because preemptible RCU does not exist, tasks cannot possibly exit
  739. * while in preemptible RCU read-side critical sections.
  740. */
  741. void exit_rcu(void)
  742. {
  743. }
  744. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  745. #ifdef CONFIG_RCU_BOOST
  746. #include "../locking/rtmutex_common.h"
  747. #ifdef CONFIG_RCU_TRACE
  748. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  749. {
  750. if (!rcu_preempt_has_tasks(rnp))
  751. rnp->n_balk_blkd_tasks++;
  752. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  753. rnp->n_balk_exp_gp_tasks++;
  754. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  755. rnp->n_balk_boost_tasks++;
  756. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  757. rnp->n_balk_notblocked++;
  758. else if (rnp->gp_tasks != NULL &&
  759. ULONG_CMP_LT(jiffies, rnp->boost_time))
  760. rnp->n_balk_notyet++;
  761. else
  762. rnp->n_balk_nos++;
  763. }
  764. #else /* #ifdef CONFIG_RCU_TRACE */
  765. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  766. {
  767. }
  768. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  769. static void rcu_wake_cond(struct task_struct *t, int status)
  770. {
  771. /*
  772. * If the thread is yielding, only wake it when this
  773. * is invoked from idle
  774. */
  775. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  776. wake_up_process(t);
  777. }
  778. /*
  779. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  780. * or ->boost_tasks, advancing the pointer to the next task in the
  781. * ->blkd_tasks list.
  782. *
  783. * Note that irqs must be enabled: boosting the task can block.
  784. * Returns 1 if there are more tasks needing to be boosted.
  785. */
  786. static int rcu_boost(struct rcu_node *rnp)
  787. {
  788. unsigned long flags;
  789. struct task_struct *t;
  790. struct list_head *tb;
  791. if (READ_ONCE(rnp->exp_tasks) == NULL &&
  792. READ_ONCE(rnp->boost_tasks) == NULL)
  793. return 0; /* Nothing left to boost. */
  794. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  795. /*
  796. * Recheck under the lock: all tasks in need of boosting
  797. * might exit their RCU read-side critical sections on their own.
  798. */
  799. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  800. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  801. return 0;
  802. }
  803. /*
  804. * Preferentially boost tasks blocking expedited grace periods.
  805. * This cannot starve the normal grace periods because a second
  806. * expedited grace period must boost all blocked tasks, including
  807. * those blocking the pre-existing normal grace period.
  808. */
  809. if (rnp->exp_tasks != NULL) {
  810. tb = rnp->exp_tasks;
  811. rnp->n_exp_boosts++;
  812. } else {
  813. tb = rnp->boost_tasks;
  814. rnp->n_normal_boosts++;
  815. }
  816. rnp->n_tasks_boosted++;
  817. /*
  818. * We boost task t by manufacturing an rt_mutex that appears to
  819. * be held by task t. We leave a pointer to that rt_mutex where
  820. * task t can find it, and task t will release the mutex when it
  821. * exits its outermost RCU read-side critical section. Then
  822. * simply acquiring this artificial rt_mutex will boost task
  823. * t's priority. (Thanks to tglx for suggesting this approach!)
  824. *
  825. * Note that task t must acquire rnp->lock to remove itself from
  826. * the ->blkd_tasks list, which it will do from exit() if from
  827. * nowhere else. We therefore are guaranteed that task t will
  828. * stay around at least until we drop rnp->lock. Note that
  829. * rnp->lock also resolves races between our priority boosting
  830. * and task t's exiting its outermost RCU read-side critical
  831. * section.
  832. */
  833. t = container_of(tb, struct task_struct, rcu_node_entry);
  834. rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
  835. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  836. /* Lock only for side effect: boosts task t's priority. */
  837. rt_mutex_lock(&rnp->boost_mtx);
  838. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  839. return READ_ONCE(rnp->exp_tasks) != NULL ||
  840. READ_ONCE(rnp->boost_tasks) != NULL;
  841. }
  842. /*
  843. * Priority-boosting kthread, one per leaf rcu_node.
  844. */
  845. static int rcu_boost_kthread(void *arg)
  846. {
  847. struct rcu_node *rnp = (struct rcu_node *)arg;
  848. int spincnt = 0;
  849. int more2boost;
  850. trace_rcu_utilization(TPS("Start boost kthread@init"));
  851. for (;;) {
  852. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  853. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  854. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  855. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  856. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  857. more2boost = rcu_boost(rnp);
  858. if (more2boost)
  859. spincnt++;
  860. else
  861. spincnt = 0;
  862. if (spincnt > 10) {
  863. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  864. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  865. schedule_timeout_interruptible(2);
  866. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  867. spincnt = 0;
  868. }
  869. }
  870. /* NOTREACHED */
  871. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  872. return 0;
  873. }
  874. /*
  875. * Check to see if it is time to start boosting RCU readers that are
  876. * blocking the current grace period, and, if so, tell the per-rcu_node
  877. * kthread to start boosting them. If there is an expedited grace
  878. * period in progress, it is always time to boost.
  879. *
  880. * The caller must hold rnp->lock, which this function releases.
  881. * The ->boost_kthread_task is immortal, so we don't need to worry
  882. * about it going away.
  883. */
  884. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  885. __releases(rnp->lock)
  886. {
  887. struct task_struct *t;
  888. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  889. rnp->n_balk_exp_gp_tasks++;
  890. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  891. return;
  892. }
  893. if (rnp->exp_tasks != NULL ||
  894. (rnp->gp_tasks != NULL &&
  895. rnp->boost_tasks == NULL &&
  896. rnp->qsmask == 0 &&
  897. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  898. if (rnp->exp_tasks == NULL)
  899. rnp->boost_tasks = rnp->gp_tasks;
  900. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  901. t = rnp->boost_kthread_task;
  902. if (t)
  903. rcu_wake_cond(t, rnp->boost_kthread_status);
  904. } else {
  905. rcu_initiate_boost_trace(rnp);
  906. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  907. }
  908. }
  909. /*
  910. * Wake up the per-CPU kthread to invoke RCU callbacks.
  911. */
  912. static void invoke_rcu_callbacks_kthread(void)
  913. {
  914. unsigned long flags;
  915. local_irq_save(flags);
  916. __this_cpu_write(rcu_cpu_has_work, 1);
  917. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  918. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  919. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  920. __this_cpu_read(rcu_cpu_kthread_status));
  921. }
  922. local_irq_restore(flags);
  923. }
  924. /*
  925. * Is the current CPU running the RCU-callbacks kthread?
  926. * Caller must have preemption disabled.
  927. */
  928. static bool rcu_is_callbacks_kthread(void)
  929. {
  930. return __this_cpu_read(rcu_cpu_kthread_task) == current;
  931. }
  932. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  933. /*
  934. * Do priority-boost accounting for the start of a new grace period.
  935. */
  936. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  937. {
  938. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  939. }
  940. /*
  941. * Create an RCU-boost kthread for the specified node if one does not
  942. * already exist. We only create this kthread for preemptible RCU.
  943. * Returns zero if all is well, a negated errno otherwise.
  944. */
  945. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  946. struct rcu_node *rnp)
  947. {
  948. int rnp_index = rnp - &rsp->node[0];
  949. unsigned long flags;
  950. struct sched_param sp;
  951. struct task_struct *t;
  952. if (rcu_state_p != rsp)
  953. return 0;
  954. if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
  955. return 0;
  956. rsp->boost = 1;
  957. if (rnp->boost_kthread_task != NULL)
  958. return 0;
  959. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  960. "rcub/%d", rnp_index);
  961. if (IS_ERR(t))
  962. return PTR_ERR(t);
  963. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  964. rnp->boost_kthread_task = t;
  965. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  966. sp.sched_priority = kthread_prio;
  967. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  968. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  969. return 0;
  970. }
  971. static void rcu_kthread_do_work(void)
  972. {
  973. rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  974. rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  975. rcu_preempt_do_callbacks();
  976. }
  977. static void rcu_cpu_kthread_setup(unsigned int cpu)
  978. {
  979. struct sched_param sp;
  980. sp.sched_priority = kthread_prio;
  981. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  982. }
  983. static void rcu_cpu_kthread_park(unsigned int cpu)
  984. {
  985. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  986. }
  987. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  988. {
  989. return __this_cpu_read(rcu_cpu_has_work);
  990. }
  991. /*
  992. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  993. * RCU softirq used in flavors and configurations of RCU that do not
  994. * support RCU priority boosting.
  995. */
  996. static void rcu_cpu_kthread(unsigned int cpu)
  997. {
  998. unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  999. char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  1000. int spincnt;
  1001. for (spincnt = 0; spincnt < 10; spincnt++) {
  1002. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1003. local_bh_disable();
  1004. *statusp = RCU_KTHREAD_RUNNING;
  1005. this_cpu_inc(rcu_cpu_kthread_loops);
  1006. local_irq_disable();
  1007. work = *workp;
  1008. *workp = 0;
  1009. local_irq_enable();
  1010. if (work)
  1011. rcu_kthread_do_work();
  1012. local_bh_enable();
  1013. if (*workp == 0) {
  1014. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1015. *statusp = RCU_KTHREAD_WAITING;
  1016. return;
  1017. }
  1018. }
  1019. *statusp = RCU_KTHREAD_YIELDING;
  1020. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1021. schedule_timeout_interruptible(2);
  1022. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1023. *statusp = RCU_KTHREAD_WAITING;
  1024. }
  1025. /*
  1026. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1027. * served by the rcu_node in question. The CPU hotplug lock is still
  1028. * held, so the value of rnp->qsmaskinit will be stable.
  1029. *
  1030. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1031. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1032. * this function allows the kthread to execute on any CPU.
  1033. */
  1034. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1035. {
  1036. struct task_struct *t = rnp->boost_kthread_task;
  1037. unsigned long mask = rcu_rnp_online_cpus(rnp);
  1038. cpumask_var_t cm;
  1039. int cpu;
  1040. if (!t)
  1041. return;
  1042. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1043. return;
  1044. for_each_leaf_node_possible_cpu(rnp, cpu)
  1045. if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
  1046. cpu != outgoingcpu)
  1047. cpumask_set_cpu(cpu, cm);
  1048. if (cpumask_weight(cm) == 0)
  1049. cpumask_setall(cm);
  1050. set_cpus_allowed_ptr(t, cm);
  1051. free_cpumask_var(cm);
  1052. }
  1053. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1054. .store = &rcu_cpu_kthread_task,
  1055. .thread_should_run = rcu_cpu_kthread_should_run,
  1056. .thread_fn = rcu_cpu_kthread,
  1057. .thread_comm = "rcuc/%u",
  1058. .setup = rcu_cpu_kthread_setup,
  1059. .park = rcu_cpu_kthread_park,
  1060. };
  1061. /*
  1062. * Spawn boost kthreads -- called as soon as the scheduler is running.
  1063. */
  1064. static void __init rcu_spawn_boost_kthreads(void)
  1065. {
  1066. struct rcu_node *rnp;
  1067. int cpu;
  1068. for_each_possible_cpu(cpu)
  1069. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1070. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1071. rcu_for_each_leaf_node(rcu_state_p, rnp)
  1072. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1073. }
  1074. static void rcu_prepare_kthreads(int cpu)
  1075. {
  1076. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  1077. struct rcu_node *rnp = rdp->mynode;
  1078. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1079. if (rcu_scheduler_fully_active)
  1080. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1081. }
  1082. #else /* #ifdef CONFIG_RCU_BOOST */
  1083. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1084. __releases(rnp->lock)
  1085. {
  1086. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1087. }
  1088. static void invoke_rcu_callbacks_kthread(void)
  1089. {
  1090. WARN_ON_ONCE(1);
  1091. }
  1092. static bool rcu_is_callbacks_kthread(void)
  1093. {
  1094. return false;
  1095. }
  1096. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1097. {
  1098. }
  1099. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1100. {
  1101. }
  1102. static void __init rcu_spawn_boost_kthreads(void)
  1103. {
  1104. }
  1105. static void rcu_prepare_kthreads(int cpu)
  1106. {
  1107. }
  1108. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1109. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1110. /*
  1111. * Check to see if any future RCU-related work will need to be done
  1112. * by the current CPU, even if none need be done immediately, returning
  1113. * 1 if so. This function is part of the RCU implementation; it is -not-
  1114. * an exported member of the RCU API.
  1115. *
  1116. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1117. * any flavor of RCU.
  1118. */
  1119. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1120. {
  1121. *nextevt = KTIME_MAX;
  1122. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  1123. ? 0 : rcu_cpu_has_callbacks(NULL);
  1124. }
  1125. /*
  1126. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1127. * after it.
  1128. */
  1129. static void rcu_cleanup_after_idle(void)
  1130. {
  1131. }
  1132. /*
  1133. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1134. * is nothing.
  1135. */
  1136. static void rcu_prepare_for_idle(void)
  1137. {
  1138. }
  1139. /*
  1140. * Don't bother keeping a running count of the number of RCU callbacks
  1141. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1142. */
  1143. static void rcu_idle_count_callbacks_posted(void)
  1144. {
  1145. }
  1146. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1147. /*
  1148. * This code is invoked when a CPU goes idle, at which point we want
  1149. * to have the CPU do everything required for RCU so that it can enter
  1150. * the energy-efficient dyntick-idle mode. This is handled by a
  1151. * state machine implemented by rcu_prepare_for_idle() below.
  1152. *
  1153. * The following three proprocessor symbols control this state machine:
  1154. *
  1155. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1156. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1157. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1158. * benchmarkers who might otherwise be tempted to set this to a large
  1159. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1160. * system. And if you are -that- concerned about energy efficiency,
  1161. * just power the system down and be done with it!
  1162. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1163. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1164. * callbacks pending. Setting this too high can OOM your system.
  1165. *
  1166. * The values below work well in practice. If future workloads require
  1167. * adjustment, they can be converted into kernel config parameters, though
  1168. * making the state machine smarter might be a better option.
  1169. */
  1170. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1171. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1172. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1173. module_param(rcu_idle_gp_delay, int, 0644);
  1174. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1175. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1176. /*
  1177. * Try to advance callbacks for all flavors of RCU on the current CPU, but
  1178. * only if it has been awhile since the last time we did so. Afterwards,
  1179. * if there are any callbacks ready for immediate invocation, return true.
  1180. */
  1181. static bool __maybe_unused rcu_try_advance_all_cbs(void)
  1182. {
  1183. bool cbs_ready = false;
  1184. struct rcu_data *rdp;
  1185. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1186. struct rcu_node *rnp;
  1187. struct rcu_state *rsp;
  1188. /* Exit early if we advanced recently. */
  1189. if (jiffies == rdtp->last_advance_all)
  1190. return false;
  1191. rdtp->last_advance_all = jiffies;
  1192. for_each_rcu_flavor(rsp) {
  1193. rdp = this_cpu_ptr(rsp->rda);
  1194. rnp = rdp->mynode;
  1195. /*
  1196. * Don't bother checking unless a grace period has
  1197. * completed since we last checked and there are
  1198. * callbacks not yet ready to invoke.
  1199. */
  1200. if ((rdp->completed != rnp->completed ||
  1201. unlikely(READ_ONCE(rdp->gpwrap))) &&
  1202. rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
  1203. note_gp_changes(rsp, rdp);
  1204. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1205. cbs_ready = true;
  1206. }
  1207. return cbs_ready;
  1208. }
  1209. /*
  1210. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1211. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1212. * caller to set the timeout based on whether or not there are non-lazy
  1213. * callbacks.
  1214. *
  1215. * The caller must have disabled interrupts.
  1216. */
  1217. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1218. {
  1219. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1220. unsigned long dj;
  1221. if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)) {
  1222. *nextevt = KTIME_MAX;
  1223. return 0;
  1224. }
  1225. /* Snapshot to detect later posting of non-lazy callback. */
  1226. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1227. /* If no callbacks, RCU doesn't need the CPU. */
  1228. if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
  1229. *nextevt = KTIME_MAX;
  1230. return 0;
  1231. }
  1232. /* Attempt to advance callbacks. */
  1233. if (rcu_try_advance_all_cbs()) {
  1234. /* Some ready to invoke, so initiate later invocation. */
  1235. invoke_rcu_core();
  1236. return 1;
  1237. }
  1238. rdtp->last_accelerate = jiffies;
  1239. /* Request timer delay depending on laziness, and round. */
  1240. if (!rdtp->all_lazy) {
  1241. dj = round_up(rcu_idle_gp_delay + jiffies,
  1242. rcu_idle_gp_delay) - jiffies;
  1243. } else {
  1244. dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1245. }
  1246. *nextevt = basemono + dj * TICK_NSEC;
  1247. return 0;
  1248. }
  1249. /*
  1250. * Prepare a CPU for idle from an RCU perspective. The first major task
  1251. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1252. * The second major task is to check to see if a non-lazy callback has
  1253. * arrived at a CPU that previously had only lazy callbacks. The third
  1254. * major task is to accelerate (that is, assign grace-period numbers to)
  1255. * any recently arrived callbacks.
  1256. *
  1257. * The caller must have disabled interrupts.
  1258. */
  1259. static void rcu_prepare_for_idle(void)
  1260. {
  1261. bool needwake;
  1262. struct rcu_data *rdp;
  1263. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1264. struct rcu_node *rnp;
  1265. struct rcu_state *rsp;
  1266. int tne;
  1267. if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL) ||
  1268. rcu_is_nocb_cpu(smp_processor_id()))
  1269. return;
  1270. /* Handle nohz enablement switches conservatively. */
  1271. tne = READ_ONCE(tick_nohz_active);
  1272. if (tne != rdtp->tick_nohz_enabled_snap) {
  1273. if (rcu_cpu_has_callbacks(NULL))
  1274. invoke_rcu_core(); /* force nohz to see update. */
  1275. rdtp->tick_nohz_enabled_snap = tne;
  1276. return;
  1277. }
  1278. if (!tne)
  1279. return;
  1280. /*
  1281. * If a non-lazy callback arrived at a CPU having only lazy
  1282. * callbacks, invoke RCU core for the side-effect of recalculating
  1283. * idle duration on re-entry to idle.
  1284. */
  1285. if (rdtp->all_lazy &&
  1286. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1287. rdtp->all_lazy = false;
  1288. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1289. invoke_rcu_core();
  1290. return;
  1291. }
  1292. /*
  1293. * If we have not yet accelerated this jiffy, accelerate all
  1294. * callbacks on this CPU.
  1295. */
  1296. if (rdtp->last_accelerate == jiffies)
  1297. return;
  1298. rdtp->last_accelerate = jiffies;
  1299. for_each_rcu_flavor(rsp) {
  1300. rdp = this_cpu_ptr(rsp->rda);
  1301. if (!*rdp->nxttail[RCU_DONE_TAIL])
  1302. continue;
  1303. rnp = rdp->mynode;
  1304. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1305. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  1306. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1307. if (needwake)
  1308. rcu_gp_kthread_wake(rsp);
  1309. }
  1310. }
  1311. /*
  1312. * Clean up for exit from idle. Attempt to advance callbacks based on
  1313. * any grace periods that elapsed while the CPU was idle, and if any
  1314. * callbacks are now ready to invoke, initiate invocation.
  1315. */
  1316. static void rcu_cleanup_after_idle(void)
  1317. {
  1318. if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL) ||
  1319. rcu_is_nocb_cpu(smp_processor_id()))
  1320. return;
  1321. if (rcu_try_advance_all_cbs())
  1322. invoke_rcu_core();
  1323. }
  1324. /*
  1325. * Keep a running count of the number of non-lazy callbacks posted
  1326. * on this CPU. This running counter (which is never decremented) allows
  1327. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1328. * posts a callback, even if an equal number of callbacks are invoked.
  1329. * Of course, callbacks should only be posted from within a trace event
  1330. * designed to be called from idle or from within RCU_NONIDLE().
  1331. */
  1332. static void rcu_idle_count_callbacks_posted(void)
  1333. {
  1334. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1335. }
  1336. /*
  1337. * Data for flushing lazy RCU callbacks at OOM time.
  1338. */
  1339. static atomic_t oom_callback_count;
  1340. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1341. /*
  1342. * RCU OOM callback -- decrement the outstanding count and deliver the
  1343. * wake-up if we are the last one.
  1344. */
  1345. static void rcu_oom_callback(struct rcu_head *rhp)
  1346. {
  1347. if (atomic_dec_and_test(&oom_callback_count))
  1348. wake_up(&oom_callback_wq);
  1349. }
  1350. /*
  1351. * Post an rcu_oom_notify callback on the current CPU if it has at
  1352. * least one lazy callback. This will unnecessarily post callbacks
  1353. * to CPUs that already have a non-lazy callback at the end of their
  1354. * callback list, but this is an infrequent operation, so accept some
  1355. * extra overhead to keep things simple.
  1356. */
  1357. static void rcu_oom_notify_cpu(void *unused)
  1358. {
  1359. struct rcu_state *rsp;
  1360. struct rcu_data *rdp;
  1361. for_each_rcu_flavor(rsp) {
  1362. rdp = raw_cpu_ptr(rsp->rda);
  1363. if (rdp->qlen_lazy != 0) {
  1364. atomic_inc(&oom_callback_count);
  1365. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1366. }
  1367. }
  1368. }
  1369. /*
  1370. * If low on memory, ensure that each CPU has a non-lazy callback.
  1371. * This will wake up CPUs that have only lazy callbacks, in turn
  1372. * ensuring that they free up the corresponding memory in a timely manner.
  1373. * Because an uncertain amount of memory will be freed in some uncertain
  1374. * timeframe, we do not claim to have freed anything.
  1375. */
  1376. static int rcu_oom_notify(struct notifier_block *self,
  1377. unsigned long notused, void *nfreed)
  1378. {
  1379. int cpu;
  1380. /* Wait for callbacks from earlier instance to complete. */
  1381. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1382. smp_mb(); /* Ensure callback reuse happens after callback invocation. */
  1383. /*
  1384. * Prevent premature wakeup: ensure that all increments happen
  1385. * before there is a chance of the counter reaching zero.
  1386. */
  1387. atomic_set(&oom_callback_count, 1);
  1388. for_each_online_cpu(cpu) {
  1389. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1390. cond_resched_rcu_qs();
  1391. }
  1392. /* Unconditionally decrement: no need to wake ourselves up. */
  1393. atomic_dec(&oom_callback_count);
  1394. return NOTIFY_OK;
  1395. }
  1396. static struct notifier_block rcu_oom_nb = {
  1397. .notifier_call = rcu_oom_notify
  1398. };
  1399. static int __init rcu_register_oom_notifier(void)
  1400. {
  1401. register_oom_notifier(&rcu_oom_nb);
  1402. return 0;
  1403. }
  1404. early_initcall(rcu_register_oom_notifier);
  1405. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1406. #ifdef CONFIG_RCU_FAST_NO_HZ
  1407. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1408. {
  1409. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1410. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1411. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1412. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1413. ulong2long(nlpd),
  1414. rdtp->all_lazy ? 'L' : '.',
  1415. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1416. }
  1417. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1418. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1419. {
  1420. *cp = '\0';
  1421. }
  1422. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1423. /* Initiate the stall-info list. */
  1424. static void print_cpu_stall_info_begin(void)
  1425. {
  1426. pr_cont("\n");
  1427. }
  1428. /*
  1429. * Print out diagnostic information for the specified stalled CPU.
  1430. *
  1431. * If the specified CPU is aware of the current RCU grace period
  1432. * (flavor specified by rsp), then print the number of scheduling
  1433. * clock interrupts the CPU has taken during the time that it has
  1434. * been aware. Otherwise, print the number of RCU grace periods
  1435. * that this CPU is ignorant of, for example, "1" if the CPU was
  1436. * aware of the previous grace period.
  1437. *
  1438. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1439. */
  1440. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1441. {
  1442. char fast_no_hz[72];
  1443. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1444. struct rcu_dynticks *rdtp = rdp->dynticks;
  1445. char *ticks_title;
  1446. unsigned long ticks_value;
  1447. if (rsp->gpnum == rdp->gpnum) {
  1448. ticks_title = "ticks this GP";
  1449. ticks_value = rdp->ticks_this_gp;
  1450. } else {
  1451. ticks_title = "GPs behind";
  1452. ticks_value = rsp->gpnum - rdp->gpnum;
  1453. }
  1454. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1455. pr_err("\t%d-%c%c%c: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u fqs=%ld %s\n",
  1456. cpu,
  1457. "O."[!!cpu_online(cpu)],
  1458. "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
  1459. "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
  1460. ticks_value, ticks_title,
  1461. atomic_read(&rdtp->dynticks) & 0xfff,
  1462. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1463. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1464. READ_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
  1465. fast_no_hz);
  1466. }
  1467. /* Terminate the stall-info list. */
  1468. static void print_cpu_stall_info_end(void)
  1469. {
  1470. pr_err("\t");
  1471. }
  1472. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1473. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1474. {
  1475. rdp->ticks_this_gp = 0;
  1476. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1477. }
  1478. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1479. static void increment_cpu_stall_ticks(void)
  1480. {
  1481. struct rcu_state *rsp;
  1482. for_each_rcu_flavor(rsp)
  1483. raw_cpu_inc(rsp->rda->ticks_this_gp);
  1484. }
  1485. #ifdef CONFIG_RCU_NOCB_CPU
  1486. /*
  1487. * Offload callback processing from the boot-time-specified set of CPUs
  1488. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1489. * kthread created that pulls the callbacks from the corresponding CPU,
  1490. * waits for a grace period to elapse, and invokes the callbacks.
  1491. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1492. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1493. * has been specified, in which case each kthread actively polls its
  1494. * CPU. (Which isn't so great for energy efficiency, but which does
  1495. * reduce RCU's overhead on that CPU.)
  1496. *
  1497. * This is intended to be used in conjunction with Frederic Weisbecker's
  1498. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1499. * running CPU-bound user-mode computations.
  1500. *
  1501. * Offloading of callback processing could also in theory be used as
  1502. * an energy-efficiency measure because CPUs with no RCU callbacks
  1503. * queued are more aggressive about entering dyntick-idle mode.
  1504. */
  1505. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1506. static int __init rcu_nocb_setup(char *str)
  1507. {
  1508. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1509. have_rcu_nocb_mask = true;
  1510. cpulist_parse(str, rcu_nocb_mask);
  1511. return 1;
  1512. }
  1513. __setup("rcu_nocbs=", rcu_nocb_setup);
  1514. static int __init parse_rcu_nocb_poll(char *arg)
  1515. {
  1516. rcu_nocb_poll = 1;
  1517. return 0;
  1518. }
  1519. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1520. /*
  1521. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1522. * grace period.
  1523. */
  1524. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  1525. {
  1526. swake_up_all(sq);
  1527. }
  1528. /*
  1529. * Set the root rcu_node structure's ->need_future_gp field
  1530. * based on the sum of those of all rcu_node structures. This does
  1531. * double-count the root rcu_node structure's requests, but this
  1532. * is necessary to handle the possibility of a rcu_nocb_kthread()
  1533. * having awakened during the time that the rcu_node structures
  1534. * were being updated for the end of the previous grace period.
  1535. */
  1536. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  1537. {
  1538. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  1539. }
  1540. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  1541. {
  1542. return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  1543. }
  1544. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1545. {
  1546. init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  1547. init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  1548. }
  1549. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1550. /* Is the specified CPU a no-CBs CPU? */
  1551. bool rcu_is_nocb_cpu(int cpu)
  1552. {
  1553. if (have_rcu_nocb_mask)
  1554. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1555. return false;
  1556. }
  1557. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1558. /*
  1559. * Kick the leader kthread for this NOCB group.
  1560. */
  1561. static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  1562. {
  1563. struct rcu_data *rdp_leader = rdp->nocb_leader;
  1564. if (!READ_ONCE(rdp_leader->nocb_kthread))
  1565. return;
  1566. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  1567. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  1568. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  1569. smp_mb(); /* ->nocb_leader_sleep before swake_up(). */
  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. smp_mb(); /* wakeup before ->nocb_head reads. */
  1803. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1804. rdp->nocb_gp_head = READ_ONCE(rdp->nocb_head);
  1805. if (!rdp->nocb_gp_head)
  1806. continue; /* No CBs here, try next follower. */
  1807. /* Move callbacks to wait-for-GP list, which is empty. */
  1808. WRITE_ONCE(rdp->nocb_head, NULL);
  1809. rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  1810. gotcbs = true;
  1811. }
  1812. /*
  1813. * If there were no callbacks, sleep a bit, rescan after a
  1814. * memory barrier, and go retry.
  1815. */
  1816. if (unlikely(!gotcbs)) {
  1817. if (!rcu_nocb_poll)
  1818. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
  1819. "WokeEmpty");
  1820. WARN_ON(signal_pending(current));
  1821. schedule_timeout_interruptible(1);
  1822. /* Rescan in case we were a victim of memory ordering. */
  1823. my_rdp->nocb_leader_sleep = true;
  1824. smp_mb(); /* Ensure _sleep true before scan. */
  1825. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower)
  1826. if (READ_ONCE(rdp->nocb_head)) {
  1827. /* Found CB, so short-circuit next wait. */
  1828. my_rdp->nocb_leader_sleep = false;
  1829. break;
  1830. }
  1831. goto wait_again;
  1832. }
  1833. /* Wait for one grace period. */
  1834. rcu_nocb_wait_gp(my_rdp);
  1835. /*
  1836. * We left ->nocb_leader_sleep unset to reduce cache thrashing.
  1837. * We set it now, but recheck for new callbacks while
  1838. * traversing our follower list.
  1839. */
  1840. my_rdp->nocb_leader_sleep = true;
  1841. smp_mb(); /* Ensure _sleep true before scan of ->nocb_head. */
  1842. /* Each pass through the following loop wakes a follower, if needed. */
  1843. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1844. if (READ_ONCE(rdp->nocb_head))
  1845. my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
  1846. if (!rdp->nocb_gp_head)
  1847. continue; /* No CBs, so no need to wake follower. */
  1848. /* Append callbacks to follower's "done" list. */
  1849. tail = xchg(&rdp->nocb_follower_tail, rdp->nocb_gp_tail);
  1850. *tail = rdp->nocb_gp_head;
  1851. smp_mb__after_atomic(); /* Store *tail before wakeup. */
  1852. if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
  1853. /*
  1854. * List was empty, wake up the follower.
  1855. * Memory barriers supplied by atomic_long_add().
  1856. */
  1857. swake_up(&rdp->nocb_wq);
  1858. }
  1859. }
  1860. /* If we (the leader) don't have CBs, go wait some more. */
  1861. if (!my_rdp->nocb_follower_head)
  1862. goto wait_again;
  1863. }
  1864. /*
  1865. * Followers come here to wait for additional callbacks to show up.
  1866. * This function does not return until callbacks appear.
  1867. */
  1868. static void nocb_follower_wait(struct rcu_data *rdp)
  1869. {
  1870. bool firsttime = true;
  1871. for (;;) {
  1872. if (!rcu_nocb_poll) {
  1873. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1874. "FollowerSleep");
  1875. swait_event_interruptible(rdp->nocb_wq,
  1876. READ_ONCE(rdp->nocb_follower_head));
  1877. } else if (firsttime) {
  1878. /* Don't drown trace log with "Poll"! */
  1879. firsttime = false;
  1880. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "Poll");
  1881. }
  1882. if (smp_load_acquire(&rdp->nocb_follower_head)) {
  1883. /* ^^^ Ensure CB invocation follows _head test. */
  1884. return;
  1885. }
  1886. if (!rcu_nocb_poll)
  1887. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1888. "WokeEmpty");
  1889. WARN_ON(signal_pending(current));
  1890. schedule_timeout_interruptible(1);
  1891. }
  1892. }
  1893. /*
  1894. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  1895. * callbacks queued by the corresponding no-CBs CPU, however, there is
  1896. * an optional leader-follower relationship so that the grace-period
  1897. * kthreads don't have to do quite so many wakeups.
  1898. */
  1899. static int rcu_nocb_kthread(void *arg)
  1900. {
  1901. int c, cl;
  1902. struct rcu_head *list;
  1903. struct rcu_head *next;
  1904. struct rcu_head **tail;
  1905. struct rcu_data *rdp = arg;
  1906. /* Each pass through this loop invokes one batch of callbacks */
  1907. for (;;) {
  1908. /* Wait for callbacks. */
  1909. if (rdp->nocb_leader == rdp)
  1910. nocb_leader_wait(rdp);
  1911. else
  1912. nocb_follower_wait(rdp);
  1913. /* Pull the ready-to-invoke callbacks onto local list. */
  1914. list = READ_ONCE(rdp->nocb_follower_head);
  1915. BUG_ON(!list);
  1916. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "WokeNonEmpty");
  1917. WRITE_ONCE(rdp->nocb_follower_head, NULL);
  1918. tail = xchg(&rdp->nocb_follower_tail, &rdp->nocb_follower_head);
  1919. /* Each pass through the following loop invokes a callback. */
  1920. trace_rcu_batch_start(rdp->rsp->name,
  1921. atomic_long_read(&rdp->nocb_q_count_lazy),
  1922. atomic_long_read(&rdp->nocb_q_count), -1);
  1923. c = cl = 0;
  1924. while (list) {
  1925. next = list->next;
  1926. /* Wait for enqueuing to complete, if needed. */
  1927. while (next == NULL && &list->next != tail) {
  1928. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1929. TPS("WaitQueue"));
  1930. schedule_timeout_interruptible(1);
  1931. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1932. TPS("WokeQueue"));
  1933. next = list->next;
  1934. }
  1935. debug_rcu_head_unqueue(list);
  1936. local_bh_disable();
  1937. if (__rcu_reclaim(rdp->rsp->name, list))
  1938. cl++;
  1939. c++;
  1940. local_bh_enable();
  1941. cond_resched_rcu_qs();
  1942. list = next;
  1943. }
  1944. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  1945. smp_mb__before_atomic(); /* _add after CB invocation. */
  1946. atomic_long_add(-c, &rdp->nocb_q_count);
  1947. atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
  1948. rdp->n_nocbs_invoked += c;
  1949. }
  1950. return 0;
  1951. }
  1952. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  1953. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  1954. {
  1955. return READ_ONCE(rdp->nocb_defer_wakeup);
  1956. }
  1957. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  1958. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  1959. {
  1960. int ndw;
  1961. if (!rcu_nocb_need_deferred_wakeup(rdp))
  1962. return;
  1963. ndw = READ_ONCE(rdp->nocb_defer_wakeup);
  1964. WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOGP_WAKE_NOT);
  1965. wake_nocb_leader(rdp, ndw == RCU_NOGP_WAKE_FORCE);
  1966. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
  1967. }
  1968. void __init rcu_init_nohz(void)
  1969. {
  1970. int cpu;
  1971. bool need_rcu_nocb_mask = true;
  1972. struct rcu_state *rsp;
  1973. #ifdef CONFIG_RCU_NOCB_CPU_NONE
  1974. need_rcu_nocb_mask = false;
  1975. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
  1976. #if defined(CONFIG_NO_HZ_FULL)
  1977. if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
  1978. need_rcu_nocb_mask = true;
  1979. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  1980. if (!have_rcu_nocb_mask && need_rcu_nocb_mask) {
  1981. if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
  1982. pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
  1983. return;
  1984. }
  1985. have_rcu_nocb_mask = true;
  1986. }
  1987. if (!have_rcu_nocb_mask)
  1988. return;
  1989. #ifdef CONFIG_RCU_NOCB_CPU_ZERO
  1990. pr_info("\tOffload RCU callbacks from CPU 0\n");
  1991. cpumask_set_cpu(0, rcu_nocb_mask);
  1992. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
  1993. #ifdef CONFIG_RCU_NOCB_CPU_ALL
  1994. pr_info("\tOffload RCU callbacks from all CPUs\n");
  1995. cpumask_copy(rcu_nocb_mask, cpu_possible_mask);
  1996. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
  1997. #if defined(CONFIG_NO_HZ_FULL)
  1998. if (tick_nohz_full_running)
  1999. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
  2000. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2001. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  2002. pr_info("\tNote: kernel parameter 'rcu_nocbs=' contains nonexistent CPUs.\n");
  2003. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  2004. rcu_nocb_mask);
  2005. }
  2006. pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
  2007. cpumask_pr_args(rcu_nocb_mask));
  2008. if (rcu_nocb_poll)
  2009. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  2010. for_each_rcu_flavor(rsp) {
  2011. for_each_cpu(cpu, rcu_nocb_mask)
  2012. init_nocb_callback_list(per_cpu_ptr(rsp->rda, cpu));
  2013. rcu_organize_nocb_kthreads(rsp);
  2014. }
  2015. }
  2016. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2017. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2018. {
  2019. rdp->nocb_tail = &rdp->nocb_head;
  2020. init_swait_queue_head(&rdp->nocb_wq);
  2021. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2022. }
  2023. /*
  2024. * If the specified CPU is a no-CBs CPU that does not already have its
  2025. * rcuo kthread for the specified RCU flavor, spawn it. If the CPUs are
  2026. * brought online out of order, this can require re-organizing the
  2027. * leader-follower relationships.
  2028. */
  2029. static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
  2030. {
  2031. struct rcu_data *rdp;
  2032. struct rcu_data *rdp_last;
  2033. struct rcu_data *rdp_old_leader;
  2034. struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
  2035. struct task_struct *t;
  2036. /*
  2037. * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
  2038. * then nothing to do.
  2039. */
  2040. if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
  2041. return;
  2042. /* If we didn't spawn the leader first, reorganize! */
  2043. rdp_old_leader = rdp_spawn->nocb_leader;
  2044. if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
  2045. rdp_last = NULL;
  2046. rdp = rdp_old_leader;
  2047. do {
  2048. rdp->nocb_leader = rdp_spawn;
  2049. if (rdp_last && rdp != rdp_spawn)
  2050. rdp_last->nocb_next_follower = rdp;
  2051. if (rdp == rdp_spawn) {
  2052. rdp = rdp->nocb_next_follower;
  2053. } else {
  2054. rdp_last = rdp;
  2055. rdp = rdp->nocb_next_follower;
  2056. rdp_last->nocb_next_follower = NULL;
  2057. }
  2058. } while (rdp);
  2059. rdp_spawn->nocb_next_follower = rdp_old_leader;
  2060. }
  2061. /* Spawn the kthread for this CPU and RCU flavor. */
  2062. t = kthread_run(rcu_nocb_kthread, rdp_spawn,
  2063. "rcuo%c/%d", rsp->abbr, cpu);
  2064. BUG_ON(IS_ERR(t));
  2065. WRITE_ONCE(rdp_spawn->nocb_kthread, t);
  2066. }
  2067. /*
  2068. * If the specified CPU is a no-CBs CPU that does not already have its
  2069. * rcuo kthreads, spawn them.
  2070. */
  2071. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2072. {
  2073. struct rcu_state *rsp;
  2074. if (rcu_scheduler_fully_active)
  2075. for_each_rcu_flavor(rsp)
  2076. rcu_spawn_one_nocb_kthread(rsp, cpu);
  2077. }
  2078. /*
  2079. * Once the scheduler is running, spawn rcuo kthreads for all online
  2080. * no-CBs CPUs. This assumes that the early_initcall()s happen before
  2081. * non-boot CPUs come online -- if this changes, we will need to add
  2082. * some mutual exclusion.
  2083. */
  2084. static void __init rcu_spawn_nocb_kthreads(void)
  2085. {
  2086. int cpu;
  2087. for_each_online_cpu(cpu)
  2088. rcu_spawn_all_nocb_kthreads(cpu);
  2089. }
  2090. /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
  2091. static int rcu_nocb_leader_stride = -1;
  2092. module_param(rcu_nocb_leader_stride, int, 0444);
  2093. /*
  2094. * Initialize leader-follower relationships for all no-CBs CPU.
  2095. */
  2096. static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
  2097. {
  2098. int cpu;
  2099. int ls = rcu_nocb_leader_stride;
  2100. int nl = 0; /* Next leader. */
  2101. struct rcu_data *rdp;
  2102. struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
  2103. struct rcu_data *rdp_prev = NULL;
  2104. if (!have_rcu_nocb_mask)
  2105. return;
  2106. if (ls == -1) {
  2107. ls = int_sqrt(nr_cpu_ids);
  2108. rcu_nocb_leader_stride = ls;
  2109. }
  2110. /*
  2111. * Each pass through this loop sets up one rcu_data structure and
  2112. * spawns one rcu_nocb_kthread().
  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. }