tree_plugin.h 84 KB

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