tree_plugin.h 90 KB

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