tree_plugin.h 93 KB

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