tree_plugin.h 90 KB

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