tree_plugin.h 90 KB

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