tree_plugin.h 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  3. * Internal non-public definitions that provide either classic
  4. * or preemptible semantics.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you can access it online at
  18. * http://www.gnu.org/licenses/gpl-2.0.html.
  19. *
  20. * Copyright Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/gfp.h>
  28. #include <linux/oom.h>
  29. #include <linux/sched/debug.h>
  30. #include <linux/smpboot.h>
  31. #include <linux/sched/isolation.h>
  32. #include <uapi/linux/sched/types.h>
  33. #include "../time/tick-internal.h"
  34. #ifdef CONFIG_RCU_BOOST
  35. #include "../locking/rtmutex_common.h"
  36. /*
  37. * Control variables for per-CPU and per-rcu_node kthreads. These
  38. * handle all flavors of RCU.
  39. */
  40. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  41. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  42. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  43. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  44. #else /* #ifdef CONFIG_RCU_BOOST */
  45. /*
  46. * Some architectures do not define rt_mutexes, but if !CONFIG_RCU_BOOST,
  47. * all uses are in dead code. Provide a definition to keep the compiler
  48. * happy, but add WARN_ON_ONCE() to complain if used in the wrong place.
  49. * This probably needs to be excluded from -rt builds.
  50. */
  51. #define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
  52. #define rt_mutex_futex_unlock(x) WARN_ON_ONCE(1)
  53. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  54. #ifdef CONFIG_RCU_NOCB_CPU
  55. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  56. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  57. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  58. /*
  59. * Check the RCU kernel configuration parameters and print informative
  60. * messages about anything out of the ordinary.
  61. */
  62. static void __init rcu_bootup_announce_oddness(void)
  63. {
  64. if (IS_ENABLED(CONFIG_RCU_TRACE))
  65. pr_info("\tRCU event tracing is enabled.\n");
  66. if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
  67. (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
  68. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
  69. RCU_FANOUT);
  70. if (rcu_fanout_exact)
  71. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  72. if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
  73. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  74. if (IS_ENABLED(CONFIG_PROVE_RCU))
  75. pr_info("\tRCU lockdep checking is enabled.\n");
  76. if (RCU_NUM_LVLS >= 4)
  77. pr_info("\tFour(or more)-level hierarchy is enabled.\n");
  78. if (RCU_FANOUT_LEAF != 16)
  79. pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
  80. RCU_FANOUT_LEAF);
  81. if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
  82. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
  83. rcu_fanout_leaf);
  84. if (nr_cpu_ids != NR_CPUS)
  85. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
  86. #ifdef CONFIG_RCU_BOOST
  87. pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
  88. kthread_prio, CONFIG_RCU_BOOST_DELAY);
  89. #endif
  90. if (blimit != DEFAULT_RCU_BLIMIT)
  91. pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit);
  92. if (qhimark != DEFAULT_RCU_QHIMARK)
  93. pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark);
  94. if (qlowmark != DEFAULT_RCU_QLOMARK)
  95. pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark);
  96. if (jiffies_till_first_fqs != ULONG_MAX)
  97. pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs);
  98. if (jiffies_till_next_fqs != ULONG_MAX)
  99. pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs);
  100. if (rcu_kick_kthreads)
  101. pr_info("\tKick kthreads if too-long grace period.\n");
  102. if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
  103. pr_info("\tRCU callback double-/use-after-free debug enabled.\n");
  104. if (gp_preinit_delay)
  105. pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
  106. if (gp_init_delay)
  107. pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
  108. if (gp_cleanup_delay)
  109. pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_cleanup_delay);
  110. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG))
  111. pr_info("\tRCU debug extended QS entry/exit.\n");
  112. rcupdate_announce_bootup_oddness();
  113. }
  114. #ifdef CONFIG_PREEMPT_RCU
  115. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  116. static struct rcu_state *const rcu_state_p = &rcu_preempt_state;
  117. static struct rcu_data __percpu *const rcu_data_p = &rcu_preempt_data;
  118. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  119. bool wake);
  120. static void rcu_read_unlock_special(struct task_struct *t);
  121. /*
  122. * Tell them what RCU they are running.
  123. */
  124. static void __init rcu_bootup_announce(void)
  125. {
  126. pr_info("Preemptible hierarchical RCU implementation.\n");
  127. rcu_bootup_announce_oddness();
  128. }
  129. /* Flags for rcu_preempt_ctxt_queue() decision table. */
  130. #define RCU_GP_TASKS 0x8
  131. #define RCU_EXP_TASKS 0x4
  132. #define RCU_GP_BLKD 0x2
  133. #define RCU_EXP_BLKD 0x1
  134. /*
  135. * Queues a task preempted within an RCU-preempt read-side critical
  136. * section into the appropriate location within the ->blkd_tasks list,
  137. * depending on the states of any ongoing normal and expedited grace
  138. * periods. The ->gp_tasks pointer indicates which element the normal
  139. * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
  140. * indicates which element the expedited grace period is waiting on (again,
  141. * NULL if none). If a grace period is waiting on a given element in the
  142. * ->blkd_tasks list, it also waits on all subsequent elements. Thus,
  143. * adding a task to the tail of the list blocks any grace period that is
  144. * already waiting on one of the elements. In contrast, adding a task
  145. * to the head of the list won't block any grace period that is already
  146. * waiting on one of the elements.
  147. *
  148. * This queuing is imprecise, and can sometimes make an ongoing grace
  149. * period wait for a task that is not strictly speaking blocking it.
  150. * Given the choice, we needlessly block a normal grace period rather than
  151. * blocking an expedited grace period.
  152. *
  153. * Note that an endless sequence of expedited grace periods still cannot
  154. * indefinitely postpone a normal grace period. Eventually, all of the
  155. * fixed number of preempted tasks blocking the normal grace period that are
  156. * not also blocking the expedited grace period will resume and complete
  157. * their RCU read-side critical sections. At that point, the ->gp_tasks
  158. * pointer will equal the ->exp_tasks pointer, at which point the end of
  159. * the corresponding expedited grace period will also be the end of the
  160. * normal grace period.
  161. */
  162. static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
  163. __releases(rnp->lock) /* But leaves rrupts disabled. */
  164. {
  165. int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
  166. (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
  167. (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
  168. (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
  169. struct task_struct *t = current;
  170. raw_lockdep_assert_held_rcu_node(rnp);
  171. WARN_ON_ONCE(rdp->mynode != rnp);
  172. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  173. /* RCU better not be waiting on newly onlined CPUs! */
  174. WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
  175. rdp->grpmask);
  176. /*
  177. * Decide where to queue the newly blocked task. In theory,
  178. * this could be an if-statement. In practice, when I tried
  179. * that, it was quite messy.
  180. */
  181. switch (blkd_state) {
  182. case 0:
  183. case RCU_EXP_TASKS:
  184. case RCU_EXP_TASKS + RCU_GP_BLKD:
  185. case RCU_GP_TASKS:
  186. case RCU_GP_TASKS + RCU_EXP_TASKS:
  187. /*
  188. * Blocking neither GP, or first task blocking the normal
  189. * GP but not blocking the already-waiting expedited GP.
  190. * Queue at the head of the list to avoid unnecessarily
  191. * blocking the already-waiting GPs.
  192. */
  193. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  194. break;
  195. case RCU_EXP_BLKD:
  196. case RCU_GP_BLKD:
  197. case RCU_GP_BLKD + RCU_EXP_BLKD:
  198. case RCU_GP_TASKS + RCU_EXP_BLKD:
  199. case RCU_GP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  200. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  201. /*
  202. * First task arriving that blocks either GP, or first task
  203. * arriving that blocks the expedited GP (with the normal
  204. * GP already waiting), or a task arriving that blocks
  205. * both GPs with both GPs already waiting. Queue at the
  206. * tail of the list to avoid any GP waiting on any of the
  207. * already queued tasks that are not blocking it.
  208. */
  209. list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
  210. break;
  211. case RCU_EXP_TASKS + RCU_EXP_BLKD:
  212. case RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
  213. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_EXP_BLKD:
  214. /*
  215. * Second or subsequent task blocking the expedited GP.
  216. * The task either does not block the normal GP, or is the
  217. * first task blocking the normal GP. Queue just after
  218. * the first task blocking the expedited GP.
  219. */
  220. list_add(&t->rcu_node_entry, rnp->exp_tasks);
  221. break;
  222. case RCU_GP_TASKS + RCU_GP_BLKD:
  223. case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
  224. /*
  225. * Second or subsequent task blocking the normal GP.
  226. * The task does not block the expedited GP. Queue just
  227. * after the first task blocking the normal GP.
  228. */
  229. list_add(&t->rcu_node_entry, rnp->gp_tasks);
  230. break;
  231. default:
  232. /* Yet another exercise in excessive paranoia. */
  233. WARN_ON_ONCE(1);
  234. break;
  235. }
  236. /*
  237. * We have now queued the task. If it was the first one to
  238. * block either grace period, update the ->gp_tasks and/or
  239. * ->exp_tasks pointers, respectively, to reference the newly
  240. * blocked tasks.
  241. */
  242. if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
  243. WRITE_ONCE(rnp->gp_tasks, &t->rcu_node_entry);
  244. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
  245. }
  246. if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
  247. rnp->exp_tasks = &t->rcu_node_entry;
  248. WARN_ON_ONCE(!(blkd_state & RCU_GP_BLKD) !=
  249. !(rnp->qsmask & rdp->grpmask));
  250. WARN_ON_ONCE(!(blkd_state & RCU_EXP_BLKD) !=
  251. !(rnp->expmask & rdp->grpmask));
  252. raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
  253. /*
  254. * Report the quiescent state for the expedited GP. This expedited
  255. * GP should not be able to end until we report, so there should be
  256. * no need to check for a subsequent expedited GP. (Though we are
  257. * still in a quiescent state in any case.)
  258. */
  259. if (blkd_state & RCU_EXP_BLKD &&
  260. t->rcu_read_unlock_special.b.exp_need_qs) {
  261. t->rcu_read_unlock_special.b.exp_need_qs = false;
  262. rcu_report_exp_rdp(rdp->rsp, rdp, true);
  263. } else {
  264. WARN_ON_ONCE(t->rcu_read_unlock_special.b.exp_need_qs);
  265. }
  266. }
  267. /*
  268. * Record a preemptible-RCU quiescent state for the specified CPU.
  269. * Note that this does not necessarily mean that the task currently running
  270. * on the CPU is in a quiescent state: Instead, it means that the current
  271. * grace period need not wait on any RCU read-side critical section that
  272. * starts later on this CPU. It also means that if the current task is
  273. * in an RCU read-side critical section, it has already added itself to
  274. * some leaf rcu_node structure's ->blkd_tasks list. In addition to the
  275. * current task, there might be any number of other tasks blocked while
  276. * in an RCU read-side critical section.
  277. *
  278. * Callers to this function must disable preemption.
  279. */
  280. static void rcu_preempt_qs(void)
  281. {
  282. RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_qs() invoked with preemption enabled!!!\n");
  283. if (__this_cpu_read(rcu_data_p->cpu_no_qs.s)) {
  284. trace_rcu_grace_period(TPS("rcu_preempt"),
  285. __this_cpu_read(rcu_data_p->gp_seq),
  286. TPS("cpuqs"));
  287. __this_cpu_write(rcu_data_p->cpu_no_qs.b.norm, false);
  288. barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
  289. current->rcu_read_unlock_special.b.need_qs = false;
  290. }
  291. }
  292. /*
  293. * We have entered the scheduler, and the current task might soon be
  294. * context-switched away from. If this task is in an RCU read-side
  295. * critical section, we will no longer be able to rely on the CPU to
  296. * record that fact, so we enqueue the task on the blkd_tasks list.
  297. * The task will dequeue itself when it exits the outermost enclosing
  298. * RCU read-side critical section. Therefore, the current grace period
  299. * cannot be permitted to complete until the blkd_tasks list entries
  300. * predating the current grace period drain, in other words, until
  301. * rnp->gp_tasks becomes NULL.
  302. *
  303. * Caller must disable interrupts.
  304. */
  305. static void rcu_preempt_note_context_switch(bool preempt)
  306. {
  307. struct task_struct *t = current;
  308. struct rcu_data *rdp;
  309. struct rcu_node *rnp;
  310. lockdep_assert_irqs_disabled();
  311. WARN_ON_ONCE(!preempt && t->rcu_read_lock_nesting > 0);
  312. if (t->rcu_read_lock_nesting > 0 &&
  313. !t->rcu_read_unlock_special.b.blocked) {
  314. /* Possibly blocking in an RCU read-side critical section. */
  315. rdp = this_cpu_ptr(rcu_state_p->rda);
  316. rnp = rdp->mynode;
  317. raw_spin_lock_rcu_node(rnp);
  318. t->rcu_read_unlock_special.b.blocked = true;
  319. t->rcu_blocked_node = rnp;
  320. /*
  321. * Verify the CPU's sanity, trace the preemption, and
  322. * then queue the task as required based on the states
  323. * of any ongoing and expedited grace periods.
  324. */
  325. WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
  326. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  327. trace_rcu_preempt_task(rdp->rsp->name,
  328. t->pid,
  329. (rnp->qsmask & rdp->grpmask)
  330. ? rnp->gp_seq
  331. : rcu_seq_snap(&rnp->gp_seq));
  332. rcu_preempt_ctxt_queue(rnp, rdp);
  333. } else if (t->rcu_read_lock_nesting < 0 &&
  334. t->rcu_read_unlock_special.s) {
  335. /*
  336. * Complete exit from RCU read-side critical section on
  337. * behalf of preempted instance of __rcu_read_unlock().
  338. */
  339. rcu_read_unlock_special(t);
  340. }
  341. /*
  342. * Either we were not in an RCU read-side critical section to
  343. * begin with, or we have now recorded that critical section
  344. * globally. Either way, we can now note a quiescent state
  345. * for this CPU. Again, if we were in an RCU read-side critical
  346. * section, and if that critical section was blocking the current
  347. * grace period, then the fact that the task has been enqueued
  348. * means that we continue to block the current grace period.
  349. */
  350. rcu_preempt_qs();
  351. }
  352. /*
  353. * Check for preempted RCU readers blocking the current grace period
  354. * for the specified rcu_node structure. If the caller needs a reliable
  355. * answer, it must hold the rcu_node's ->lock.
  356. */
  357. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  358. {
  359. return READ_ONCE(rnp->gp_tasks) != NULL;
  360. }
  361. /*
  362. * Preemptible RCU implementation for rcu_read_lock().
  363. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  364. * if we block.
  365. */
  366. void __rcu_read_lock(void)
  367. {
  368. current->rcu_read_lock_nesting++;
  369. barrier(); /* critical section after entry code. */
  370. }
  371. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  372. /*
  373. * Preemptible RCU implementation for rcu_read_unlock().
  374. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  375. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  376. * invoke rcu_read_unlock_special() to clean up after a context switch
  377. * in an RCU read-side critical section and other special cases.
  378. */
  379. void __rcu_read_unlock(void)
  380. {
  381. struct task_struct *t = current;
  382. if (t->rcu_read_lock_nesting != 1) {
  383. --t->rcu_read_lock_nesting;
  384. } else {
  385. barrier(); /* critical section before exit code. */
  386. t->rcu_read_lock_nesting = INT_MIN;
  387. barrier(); /* assign before ->rcu_read_unlock_special load */
  388. if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
  389. rcu_read_unlock_special(t);
  390. barrier(); /* ->rcu_read_unlock_special load before assign */
  391. t->rcu_read_lock_nesting = 0;
  392. }
  393. #ifdef CONFIG_PROVE_LOCKING
  394. {
  395. int rrln = READ_ONCE(t->rcu_read_lock_nesting);
  396. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  397. }
  398. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  399. }
  400. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  401. /*
  402. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  403. * returning NULL if at the end of the list.
  404. */
  405. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  406. struct rcu_node *rnp)
  407. {
  408. struct list_head *np;
  409. np = t->rcu_node_entry.next;
  410. if (np == &rnp->blkd_tasks)
  411. np = NULL;
  412. return np;
  413. }
  414. /*
  415. * Return true if the specified rcu_node structure has tasks that were
  416. * preempted within an RCU read-side critical section.
  417. */
  418. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  419. {
  420. return !list_empty(&rnp->blkd_tasks);
  421. }
  422. /*
  423. * Handle special cases during rcu_read_unlock(), such as needing to
  424. * notify RCU core processing or task having blocked during the RCU
  425. * read-side critical section.
  426. */
  427. static void rcu_read_unlock_special(struct task_struct *t)
  428. {
  429. bool empty_exp;
  430. bool empty_norm;
  431. bool empty_exp_now;
  432. unsigned long flags;
  433. struct list_head *np;
  434. bool drop_boost_mutex = false;
  435. struct rcu_data *rdp;
  436. struct rcu_node *rnp;
  437. union rcu_special special;
  438. /* NMI handlers cannot block and cannot safely manipulate state. */
  439. if (in_nmi())
  440. return;
  441. local_irq_save(flags);
  442. /*
  443. * If RCU core is waiting for this CPU to exit its critical section,
  444. * report the fact that it has exited. Because irqs are disabled,
  445. * t->rcu_read_unlock_special cannot change.
  446. */
  447. special = t->rcu_read_unlock_special;
  448. if (special.b.need_qs) {
  449. rcu_preempt_qs();
  450. t->rcu_read_unlock_special.b.need_qs = false;
  451. if (!t->rcu_read_unlock_special.s) {
  452. local_irq_restore(flags);
  453. return;
  454. }
  455. }
  456. /*
  457. * Respond to a request for an expedited grace period, but only if
  458. * we were not preempted, meaning that we were running on the same
  459. * CPU throughout. If we were preempted, the exp_need_qs flag
  460. * would have been cleared at the time of the first preemption,
  461. * and the quiescent state would be reported when we were dequeued.
  462. */
  463. if (special.b.exp_need_qs) {
  464. WARN_ON_ONCE(special.b.blocked);
  465. t->rcu_read_unlock_special.b.exp_need_qs = false;
  466. rdp = this_cpu_ptr(rcu_state_p->rda);
  467. rcu_report_exp_rdp(rcu_state_p, rdp, true);
  468. if (!t->rcu_read_unlock_special.s) {
  469. local_irq_restore(flags);
  470. return;
  471. }
  472. }
  473. /* Hardware IRQ handlers cannot block, complain if they get here. */
  474. if (in_irq() || in_serving_softirq()) {
  475. lockdep_rcu_suspicious(__FILE__, __LINE__,
  476. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  477. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  478. t->rcu_read_unlock_special.s,
  479. t->rcu_read_unlock_special.b.blocked,
  480. t->rcu_read_unlock_special.b.exp_need_qs,
  481. t->rcu_read_unlock_special.b.need_qs);
  482. local_irq_restore(flags);
  483. return;
  484. }
  485. /* Clean up if blocked during RCU read-side critical section. */
  486. if (special.b.blocked) {
  487. t->rcu_read_unlock_special.b.blocked = false;
  488. /*
  489. * Remove this task from the list it blocked on. The task
  490. * now remains queued on the rcu_node corresponding to the
  491. * CPU it first blocked on, so there is no longer any need
  492. * to loop. Retain a WARN_ON_ONCE() out of sheer paranoia.
  493. */
  494. rnp = t->rcu_blocked_node;
  495. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  496. WARN_ON_ONCE(rnp != t->rcu_blocked_node);
  497. WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
  498. empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
  499. WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
  500. (!empty_norm || rnp->qsmask));
  501. empty_exp = sync_rcu_preempt_exp_done(rnp);
  502. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  503. np = rcu_next_node_entry(t, rnp);
  504. list_del_init(&t->rcu_node_entry);
  505. t->rcu_blocked_node = NULL;
  506. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  507. rnp->gp_seq, t->pid);
  508. if (&t->rcu_node_entry == rnp->gp_tasks)
  509. WRITE_ONCE(rnp->gp_tasks, np);
  510. if (&t->rcu_node_entry == rnp->exp_tasks)
  511. rnp->exp_tasks = np;
  512. if (IS_ENABLED(CONFIG_RCU_BOOST)) {
  513. /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
  514. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
  515. if (&t->rcu_node_entry == rnp->boost_tasks)
  516. rnp->boost_tasks = np;
  517. }
  518. /*
  519. * If this was the last task on the current list, and if
  520. * we aren't waiting on any CPUs, report the quiescent state.
  521. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  522. * so we must take a snapshot of the expedited state.
  523. */
  524. empty_exp_now = sync_rcu_preempt_exp_done(rnp);
  525. if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
  526. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  527. rnp->gp_seq,
  528. 0, rnp->qsmask,
  529. rnp->level,
  530. rnp->grplo,
  531. rnp->grphi,
  532. !!rnp->gp_tasks);
  533. rcu_report_unblock_qs_rnp(rcu_state_p, rnp, flags);
  534. } else {
  535. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  536. }
  537. /* Unboost if we were boosted. */
  538. if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
  539. rt_mutex_futex_unlock(&rnp->boost_mtx);
  540. /*
  541. * If this was the last task on the expedited lists,
  542. * then we need to report up the rcu_node hierarchy.
  543. */
  544. if (!empty_exp && empty_exp_now)
  545. rcu_report_exp_rnp(rcu_state_p, rnp, true);
  546. } else {
  547. local_irq_restore(flags);
  548. }
  549. }
  550. /*
  551. * Dump detailed information for all tasks blocking the current RCU
  552. * grace period on the specified rcu_node structure.
  553. */
  554. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  555. {
  556. unsigned long flags;
  557. struct task_struct *t;
  558. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  559. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  560. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  561. return;
  562. }
  563. t = list_entry(rnp->gp_tasks->prev,
  564. struct task_struct, rcu_node_entry);
  565. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  566. /*
  567. * We could be printing a lot while holding a spinlock.
  568. * Avoid triggering hard lockup.
  569. */
  570. touch_nmi_watchdog();
  571. sched_show_task(t);
  572. }
  573. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  574. }
  575. /*
  576. * Dump detailed information for all tasks blocking the current RCU
  577. * grace period.
  578. */
  579. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  580. {
  581. struct rcu_node *rnp = rcu_get_root(rsp);
  582. rcu_print_detail_task_stall_rnp(rnp);
  583. rcu_for_each_leaf_node(rsp, rnp)
  584. rcu_print_detail_task_stall_rnp(rnp);
  585. }
  586. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  587. {
  588. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  589. rnp->level, rnp->grplo, rnp->grphi);
  590. }
  591. static void rcu_print_task_stall_end(void)
  592. {
  593. pr_cont("\n");
  594. }
  595. /*
  596. * Scan the current list of tasks blocked within RCU read-side critical
  597. * sections, printing out the tid of each.
  598. */
  599. static int rcu_print_task_stall(struct rcu_node *rnp)
  600. {
  601. struct task_struct *t;
  602. int ndetected = 0;
  603. if (!rcu_preempt_blocked_readers_cgp(rnp))
  604. return 0;
  605. rcu_print_task_stall_begin(rnp);
  606. t = list_entry(rnp->gp_tasks->prev,
  607. struct task_struct, rcu_node_entry);
  608. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  609. pr_cont(" P%d", t->pid);
  610. ndetected++;
  611. }
  612. rcu_print_task_stall_end();
  613. return ndetected;
  614. }
  615. /*
  616. * Scan the current list of tasks blocked within RCU read-side critical
  617. * sections, printing out the tid of each that is blocking the current
  618. * expedited grace period.
  619. */
  620. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  621. {
  622. struct task_struct *t;
  623. int ndetected = 0;
  624. if (!rnp->exp_tasks)
  625. return 0;
  626. t = list_entry(rnp->exp_tasks->prev,
  627. struct task_struct, rcu_node_entry);
  628. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  629. pr_cont(" P%d", t->pid);
  630. ndetected++;
  631. }
  632. return ndetected;
  633. }
  634. /*
  635. * Check that the list of blocked tasks for the newly completed grace
  636. * period is in fact empty. It is a serious bug to complete a grace
  637. * period that still has RCU readers blocked! This function must be
  638. * invoked -before- updating this rnp's ->gp_seq, and the rnp's ->lock
  639. * must be held by the caller.
  640. *
  641. * Also, if there are blocked tasks on the list, they automatically
  642. * block the newly created grace period, so set up ->gp_tasks accordingly.
  643. */
  644. static void
  645. rcu_preempt_check_blocked_tasks(struct rcu_state *rsp, struct rcu_node *rnp)
  646. {
  647. struct task_struct *t;
  648. RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
  649. if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
  650. dump_blkd_tasks(rsp, rnp, 10);
  651. if (rcu_preempt_has_tasks(rnp) &&
  652. (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
  653. WRITE_ONCE(rnp->gp_tasks, rnp->blkd_tasks.next);
  654. t = container_of(rnp->gp_tasks, struct task_struct,
  655. rcu_node_entry);
  656. trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
  657. rnp->gp_seq, t->pid);
  658. }
  659. WARN_ON_ONCE(rnp->qsmask);
  660. }
  661. /*
  662. * Check for a quiescent state from the current CPU. When a task blocks,
  663. * the task is recorded in the corresponding CPU's rcu_node structure,
  664. * which is checked elsewhere.
  665. *
  666. * Caller must disable hard irqs.
  667. */
  668. static void rcu_preempt_check_callbacks(void)
  669. {
  670. struct rcu_state *rsp = &rcu_preempt_state;
  671. struct task_struct *t = current;
  672. if (t->rcu_read_lock_nesting == 0) {
  673. rcu_preempt_qs();
  674. return;
  675. }
  676. if (t->rcu_read_lock_nesting > 0 &&
  677. __this_cpu_read(rcu_data_p->core_needs_qs) &&
  678. __this_cpu_read(rcu_data_p->cpu_no_qs.b.norm) &&
  679. !t->rcu_read_unlock_special.b.need_qs &&
  680. time_after(jiffies, rsp->gp_start + HZ))
  681. t->rcu_read_unlock_special.b.need_qs = true;
  682. }
  683. /**
  684. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  685. * @head: structure to be used for queueing the RCU updates.
  686. * @func: actual callback function to be invoked after the grace period
  687. *
  688. * The callback function will be invoked some time after a full grace
  689. * period elapses, in other words after all pre-existing RCU read-side
  690. * critical sections have completed. However, the callback function
  691. * might well execute concurrently with RCU read-side critical sections
  692. * that started after call_rcu() was invoked. RCU read-side critical
  693. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  694. * and may be nested.
  695. *
  696. * Note that all CPUs must agree that the grace period extended beyond
  697. * all pre-existing RCU read-side critical section. On systems with more
  698. * than one CPU, this means that when "func()" is invoked, each CPU is
  699. * guaranteed to have executed a full memory barrier since the end of its
  700. * last RCU read-side critical section whose beginning preceded the call
  701. * to call_rcu(). It also means that each CPU executing an RCU read-side
  702. * critical section that continues beyond the start of "func()" must have
  703. * executed a memory barrier after the call_rcu() but before the beginning
  704. * of that RCU read-side critical section. Note that these guarantees
  705. * include CPUs that are offline, idle, or executing in user mode, as
  706. * well as CPUs that are executing in the kernel.
  707. *
  708. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  709. * resulting RCU callback function "func()", then both CPU A and CPU B are
  710. * guaranteed to execute a full memory barrier during the time interval
  711. * between the call to call_rcu() and the invocation of "func()" -- even
  712. * if CPU A and CPU B are the same CPU (but again only if the system has
  713. * more than one CPU).
  714. */
  715. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  716. {
  717. __call_rcu(head, func, rcu_state_p, -1, 0);
  718. }
  719. EXPORT_SYMBOL_GPL(call_rcu);
  720. /**
  721. * synchronize_rcu - wait until a grace period has elapsed.
  722. *
  723. * Control will return to the caller some time after a full grace
  724. * period has elapsed, in other words after all currently executing RCU
  725. * read-side critical sections have completed. Note, however, that
  726. * upon return from synchronize_rcu(), the caller might well be executing
  727. * concurrently with new RCU read-side critical sections that began while
  728. * synchronize_rcu() was waiting. RCU read-side critical sections are
  729. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  730. *
  731. * See the description of synchronize_sched() for more detailed
  732. * information on memory-ordering guarantees. However, please note
  733. * that -only- the memory-ordering guarantees apply. For example,
  734. * synchronize_rcu() is -not- guaranteed to wait on things like code
  735. * protected by preempt_disable(), instead, synchronize_rcu() is -only-
  736. * guaranteed to wait on RCU read-side critical sections, that is, sections
  737. * of code protected by rcu_read_lock().
  738. */
  739. void synchronize_rcu(void)
  740. {
  741. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  742. lock_is_held(&rcu_lock_map) ||
  743. lock_is_held(&rcu_sched_lock_map),
  744. "Illegal synchronize_rcu() in RCU read-side critical section");
  745. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  746. return;
  747. if (rcu_gp_is_expedited())
  748. synchronize_rcu_expedited();
  749. else
  750. wait_rcu_gp(call_rcu);
  751. }
  752. EXPORT_SYMBOL_GPL(synchronize_rcu);
  753. /**
  754. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  755. *
  756. * Note that this primitive does not necessarily wait for an RCU grace period
  757. * to complete. For example, if there are no RCU callbacks queued anywhere
  758. * in the system, then rcu_barrier() is within its rights to return
  759. * immediately, without waiting for anything, much less an RCU grace period.
  760. */
  761. void rcu_barrier(void)
  762. {
  763. _rcu_barrier(rcu_state_p);
  764. }
  765. EXPORT_SYMBOL_GPL(rcu_barrier);
  766. /*
  767. * Initialize preemptible RCU's state structures.
  768. */
  769. static void __init __rcu_init_preempt(void)
  770. {
  771. rcu_init_one(rcu_state_p);
  772. }
  773. /*
  774. * Check for a task exiting while in a preemptible-RCU read-side
  775. * critical section, clean up if so. No need to issue warnings,
  776. * as debug_check_no_locks_held() already does this if lockdep
  777. * is enabled.
  778. */
  779. void exit_rcu(void)
  780. {
  781. struct task_struct *t = current;
  782. if (likely(list_empty(&current->rcu_node_entry)))
  783. return;
  784. t->rcu_read_lock_nesting = 1;
  785. barrier();
  786. t->rcu_read_unlock_special.b.blocked = true;
  787. __rcu_read_unlock();
  788. }
  789. /*
  790. * Dump the blocked-tasks state, but limit the list dump to the
  791. * specified number of elements.
  792. */
  793. static void
  794. dump_blkd_tasks(struct rcu_state *rsp, struct rcu_node *rnp, int ncheck)
  795. {
  796. int cpu;
  797. int i;
  798. struct list_head *lhp;
  799. bool onl;
  800. struct rcu_data *rdp;
  801. struct rcu_node *rnp1;
  802. raw_lockdep_assert_held_rcu_node(rnp);
  803. pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
  804. __func__, rnp->grplo, rnp->grphi, rnp->level,
  805. (long)rnp->gp_seq, (long)rnp->completedqs);
  806. for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
  807. pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
  808. __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
  809. pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
  810. __func__, READ_ONCE(rnp->gp_tasks), rnp->boost_tasks,
  811. rnp->exp_tasks);
  812. pr_info("%s: ->blkd_tasks", __func__);
  813. i = 0;
  814. list_for_each(lhp, &rnp->blkd_tasks) {
  815. pr_cont(" %p", lhp);
  816. if (++i >= 10)
  817. break;
  818. }
  819. pr_cont("\n");
  820. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
  821. rdp = per_cpu_ptr(rsp->rda, cpu);
  822. onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
  823. pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
  824. cpu, ".o"[onl],
  825. (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
  826. (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
  827. }
  828. }
  829. #else /* #ifdef CONFIG_PREEMPT_RCU */
  830. static struct rcu_state *const rcu_state_p = &rcu_sched_state;
  831. /*
  832. * Tell them what RCU they are running.
  833. */
  834. static void __init rcu_bootup_announce(void)
  835. {
  836. pr_info("Hierarchical RCU implementation.\n");
  837. rcu_bootup_announce_oddness();
  838. }
  839. /*
  840. * Because preemptible RCU does not exist, we never have to check for
  841. * CPUs being in quiescent states.
  842. */
  843. static void rcu_preempt_note_context_switch(bool preempt)
  844. {
  845. }
  846. /*
  847. * Because preemptible RCU does not exist, there are never any preempted
  848. * RCU readers.
  849. */
  850. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  851. {
  852. return 0;
  853. }
  854. /*
  855. * Because there is no preemptible RCU, there can be no readers blocked.
  856. */
  857. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  858. {
  859. return false;
  860. }
  861. /*
  862. * Because preemptible RCU does not exist, we never have to check for
  863. * tasks blocked within RCU read-side critical sections.
  864. */
  865. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  866. {
  867. }
  868. /*
  869. * Because preemptible RCU does not exist, we never have to check for
  870. * tasks blocked within RCU read-side critical sections.
  871. */
  872. static int rcu_print_task_stall(struct rcu_node *rnp)
  873. {
  874. return 0;
  875. }
  876. /*
  877. * Because preemptible RCU does not exist, we never have to check for
  878. * tasks blocked within RCU read-side critical sections that are
  879. * blocking the current expedited grace period.
  880. */
  881. static int rcu_print_task_exp_stall(struct rcu_node *rnp)
  882. {
  883. return 0;
  884. }
  885. /*
  886. * Because there is no preemptible RCU, there can be no readers blocked,
  887. * so there is no need to check for blocked tasks. So check only for
  888. * bogus qsmask values.
  889. */
  890. static void
  891. rcu_preempt_check_blocked_tasks(struct rcu_state *rsp, struct rcu_node *rnp)
  892. {
  893. WARN_ON_ONCE(rnp->qsmask);
  894. }
  895. /*
  896. * Because preemptible RCU does not exist, it never has any callbacks
  897. * to check.
  898. */
  899. static void rcu_preempt_check_callbacks(void)
  900. {
  901. }
  902. /*
  903. * Because preemptible RCU does not exist, rcu_barrier() is just
  904. * another name for rcu_barrier_sched().
  905. */
  906. void rcu_barrier(void)
  907. {
  908. rcu_barrier_sched();
  909. }
  910. EXPORT_SYMBOL_GPL(rcu_barrier);
  911. /*
  912. * Because preemptible RCU does not exist, it need not be initialized.
  913. */
  914. static void __init __rcu_init_preempt(void)
  915. {
  916. }
  917. /*
  918. * Because preemptible RCU does not exist, tasks cannot possibly exit
  919. * while in preemptible RCU read-side critical sections.
  920. */
  921. void exit_rcu(void)
  922. {
  923. }
  924. /*
  925. * Dump the guaranteed-empty blocked-tasks state. Trust but verify.
  926. */
  927. static void
  928. dump_blkd_tasks(struct rcu_state *rsp, struct rcu_node *rnp, int ncheck)
  929. {
  930. WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
  931. }
  932. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  933. #ifdef CONFIG_RCU_BOOST
  934. static void rcu_wake_cond(struct task_struct *t, int status)
  935. {
  936. /*
  937. * If the thread is yielding, only wake it when this
  938. * is invoked from idle
  939. */
  940. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  941. wake_up_process(t);
  942. }
  943. /*
  944. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  945. * or ->boost_tasks, advancing the pointer to the next task in the
  946. * ->blkd_tasks list.
  947. *
  948. * Note that irqs must be enabled: boosting the task can block.
  949. * Returns 1 if there are more tasks needing to be boosted.
  950. */
  951. static int rcu_boost(struct rcu_node *rnp)
  952. {
  953. unsigned long flags;
  954. struct task_struct *t;
  955. struct list_head *tb;
  956. if (READ_ONCE(rnp->exp_tasks) == NULL &&
  957. READ_ONCE(rnp->boost_tasks) == NULL)
  958. return 0; /* Nothing left to boost. */
  959. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  960. /*
  961. * Recheck under the lock: all tasks in need of boosting
  962. * might exit their RCU read-side critical sections on their own.
  963. */
  964. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  965. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  966. return 0;
  967. }
  968. /*
  969. * Preferentially boost tasks blocking expedited grace periods.
  970. * This cannot starve the normal grace periods because a second
  971. * expedited grace period must boost all blocked tasks, including
  972. * those blocking the pre-existing normal grace period.
  973. */
  974. if (rnp->exp_tasks != NULL)
  975. tb = rnp->exp_tasks;
  976. else
  977. tb = rnp->boost_tasks;
  978. /*
  979. * We boost task t by manufacturing an rt_mutex that appears to
  980. * be held by task t. We leave a pointer to that rt_mutex where
  981. * task t can find it, and task t will release the mutex when it
  982. * exits its outermost RCU read-side critical section. Then
  983. * simply acquiring this artificial rt_mutex will boost task
  984. * t's priority. (Thanks to tglx for suggesting this approach!)
  985. *
  986. * Note that task t must acquire rnp->lock to remove itself from
  987. * the ->blkd_tasks list, which it will do from exit() if from
  988. * nowhere else. We therefore are guaranteed that task t will
  989. * stay around at least until we drop rnp->lock. Note that
  990. * rnp->lock also resolves races between our priority boosting
  991. * and task t's exiting its outermost RCU read-side critical
  992. * section.
  993. */
  994. t = container_of(tb, struct task_struct, rcu_node_entry);
  995. rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
  996. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  997. /* Lock only for side effect: boosts task t's priority. */
  998. rt_mutex_lock(&rnp->boost_mtx);
  999. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  1000. return READ_ONCE(rnp->exp_tasks) != NULL ||
  1001. READ_ONCE(rnp->boost_tasks) != NULL;
  1002. }
  1003. /*
  1004. * Priority-boosting kthread, one per leaf rcu_node.
  1005. */
  1006. static int rcu_boost_kthread(void *arg)
  1007. {
  1008. struct rcu_node *rnp = (struct rcu_node *)arg;
  1009. int spincnt = 0;
  1010. int more2boost;
  1011. trace_rcu_utilization(TPS("Start boost kthread@init"));
  1012. for (;;) {
  1013. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1014. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  1015. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  1016. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  1017. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1018. more2boost = rcu_boost(rnp);
  1019. if (more2boost)
  1020. spincnt++;
  1021. else
  1022. spincnt = 0;
  1023. if (spincnt > 10) {
  1024. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  1025. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  1026. schedule_timeout_interruptible(2);
  1027. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  1028. spincnt = 0;
  1029. }
  1030. }
  1031. /* NOTREACHED */
  1032. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  1033. return 0;
  1034. }
  1035. /*
  1036. * Check to see if it is time to start boosting RCU readers that are
  1037. * blocking the current grace period, and, if so, tell the per-rcu_node
  1038. * kthread to start boosting them. If there is an expedited grace
  1039. * period in progress, it is always time to boost.
  1040. *
  1041. * The caller must hold rnp->lock, which this function releases.
  1042. * The ->boost_kthread_task is immortal, so we don't need to worry
  1043. * about it going away.
  1044. */
  1045. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1046. __releases(rnp->lock)
  1047. {
  1048. struct task_struct *t;
  1049. raw_lockdep_assert_held_rcu_node(rnp);
  1050. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1051. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1052. return;
  1053. }
  1054. if (rnp->exp_tasks != NULL ||
  1055. (rnp->gp_tasks != NULL &&
  1056. rnp->boost_tasks == NULL &&
  1057. rnp->qsmask == 0 &&
  1058. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1059. if (rnp->exp_tasks == NULL)
  1060. rnp->boost_tasks = rnp->gp_tasks;
  1061. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1062. t = rnp->boost_kthread_task;
  1063. if (t)
  1064. rcu_wake_cond(t, rnp->boost_kthread_status);
  1065. } else {
  1066. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1067. }
  1068. }
  1069. /*
  1070. * Wake up the per-CPU kthread to invoke RCU callbacks.
  1071. */
  1072. static void invoke_rcu_callbacks_kthread(void)
  1073. {
  1074. unsigned long flags;
  1075. local_irq_save(flags);
  1076. __this_cpu_write(rcu_cpu_has_work, 1);
  1077. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  1078. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  1079. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  1080. __this_cpu_read(rcu_cpu_kthread_status));
  1081. }
  1082. local_irq_restore(flags);
  1083. }
  1084. /*
  1085. * Is the current CPU running the RCU-callbacks kthread?
  1086. * Caller must have preemption disabled.
  1087. */
  1088. static bool rcu_is_callbacks_kthread(void)
  1089. {
  1090. return __this_cpu_read(rcu_cpu_kthread_task) == current;
  1091. }
  1092. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1093. /*
  1094. * Do priority-boost accounting for the start of a new grace period.
  1095. */
  1096. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1097. {
  1098. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1099. }
  1100. /*
  1101. * Create an RCU-boost kthread for the specified node if one does not
  1102. * already exist. We only create this kthread for preemptible RCU.
  1103. * Returns zero if all is well, a negated errno otherwise.
  1104. */
  1105. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1106. struct rcu_node *rnp)
  1107. {
  1108. int rnp_index = rnp - &rsp->node[0];
  1109. unsigned long flags;
  1110. struct sched_param sp;
  1111. struct task_struct *t;
  1112. if (rcu_state_p != rsp)
  1113. return 0;
  1114. if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
  1115. return 0;
  1116. rsp->boost = 1;
  1117. if (rnp->boost_kthread_task != NULL)
  1118. return 0;
  1119. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1120. "rcub/%d", rnp_index);
  1121. if (IS_ERR(t))
  1122. return PTR_ERR(t);
  1123. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1124. rnp->boost_kthread_task = t;
  1125. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1126. sp.sched_priority = kthread_prio;
  1127. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1128. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1129. return 0;
  1130. }
  1131. static void rcu_kthread_do_work(void)
  1132. {
  1133. rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  1134. rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  1135. rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  1136. }
  1137. static void rcu_cpu_kthread_setup(unsigned int cpu)
  1138. {
  1139. struct sched_param sp;
  1140. sp.sched_priority = kthread_prio;
  1141. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1142. }
  1143. static void rcu_cpu_kthread_park(unsigned int cpu)
  1144. {
  1145. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1146. }
  1147. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  1148. {
  1149. return __this_cpu_read(rcu_cpu_has_work);
  1150. }
  1151. /*
  1152. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1153. * RCU softirq used in flavors and configurations of RCU that do not
  1154. * support RCU priority boosting.
  1155. */
  1156. static void rcu_cpu_kthread(unsigned int cpu)
  1157. {
  1158. unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  1159. char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  1160. int spincnt;
  1161. for (spincnt = 0; spincnt < 10; spincnt++) {
  1162. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1163. local_bh_disable();
  1164. *statusp = RCU_KTHREAD_RUNNING;
  1165. this_cpu_inc(rcu_cpu_kthread_loops);
  1166. local_irq_disable();
  1167. work = *workp;
  1168. *workp = 0;
  1169. local_irq_enable();
  1170. if (work)
  1171. rcu_kthread_do_work();
  1172. local_bh_enable();
  1173. if (*workp == 0) {
  1174. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1175. *statusp = RCU_KTHREAD_WAITING;
  1176. return;
  1177. }
  1178. }
  1179. *statusp = RCU_KTHREAD_YIELDING;
  1180. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1181. schedule_timeout_interruptible(2);
  1182. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1183. *statusp = RCU_KTHREAD_WAITING;
  1184. }
  1185. /*
  1186. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1187. * served by the rcu_node in question. The CPU hotplug lock is still
  1188. * held, so the value of rnp->qsmaskinit will be stable.
  1189. *
  1190. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1191. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1192. * this function allows the kthread to execute on any CPU.
  1193. */
  1194. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1195. {
  1196. struct task_struct *t = rnp->boost_kthread_task;
  1197. unsigned long mask = rcu_rnp_online_cpus(rnp);
  1198. cpumask_var_t cm;
  1199. int cpu;
  1200. if (!t)
  1201. return;
  1202. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1203. return;
  1204. for_each_leaf_node_possible_cpu(rnp, cpu)
  1205. if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
  1206. cpu != outgoingcpu)
  1207. cpumask_set_cpu(cpu, cm);
  1208. if (cpumask_weight(cm) == 0)
  1209. cpumask_setall(cm);
  1210. set_cpus_allowed_ptr(t, cm);
  1211. free_cpumask_var(cm);
  1212. }
  1213. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1214. .store = &rcu_cpu_kthread_task,
  1215. .thread_should_run = rcu_cpu_kthread_should_run,
  1216. .thread_fn = rcu_cpu_kthread,
  1217. .thread_comm = "rcuc/%u",
  1218. .setup = rcu_cpu_kthread_setup,
  1219. .park = rcu_cpu_kthread_park,
  1220. };
  1221. /*
  1222. * Spawn boost kthreads -- called as soon as the scheduler is running.
  1223. */
  1224. static void __init rcu_spawn_boost_kthreads(void)
  1225. {
  1226. struct rcu_node *rnp;
  1227. int cpu;
  1228. for_each_possible_cpu(cpu)
  1229. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1230. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1231. rcu_for_each_leaf_node(rcu_state_p, rnp)
  1232. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1233. }
  1234. static void rcu_prepare_kthreads(int cpu)
  1235. {
  1236. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  1237. struct rcu_node *rnp = rdp->mynode;
  1238. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1239. if (rcu_scheduler_fully_active)
  1240. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1241. }
  1242. #else /* #ifdef CONFIG_RCU_BOOST */
  1243. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1244. __releases(rnp->lock)
  1245. {
  1246. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1247. }
  1248. static void invoke_rcu_callbacks_kthread(void)
  1249. {
  1250. WARN_ON_ONCE(1);
  1251. }
  1252. static bool rcu_is_callbacks_kthread(void)
  1253. {
  1254. return false;
  1255. }
  1256. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1257. {
  1258. }
  1259. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1260. {
  1261. }
  1262. static void __init rcu_spawn_boost_kthreads(void)
  1263. {
  1264. }
  1265. static void rcu_prepare_kthreads(int cpu)
  1266. {
  1267. }
  1268. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1269. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1270. /*
  1271. * Check to see if any future RCU-related work will need to be done
  1272. * by the current CPU, even if none need be done immediately, returning
  1273. * 1 if so. This function is part of the RCU implementation; it is -not-
  1274. * an exported member of the RCU API.
  1275. *
  1276. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1277. * any flavor of RCU.
  1278. */
  1279. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1280. {
  1281. *nextevt = KTIME_MAX;
  1282. return rcu_cpu_has_callbacks(NULL);
  1283. }
  1284. /*
  1285. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1286. * after it.
  1287. */
  1288. static void rcu_cleanup_after_idle(void)
  1289. {
  1290. }
  1291. /*
  1292. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1293. * is nothing.
  1294. */
  1295. static void rcu_prepare_for_idle(void)
  1296. {
  1297. }
  1298. /*
  1299. * Don't bother keeping a running count of the number of RCU callbacks
  1300. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1301. */
  1302. static void rcu_idle_count_callbacks_posted(void)
  1303. {
  1304. }
  1305. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1306. /*
  1307. * This code is invoked when a CPU goes idle, at which point we want
  1308. * to have the CPU do everything required for RCU so that it can enter
  1309. * the energy-efficient dyntick-idle mode. This is handled by a
  1310. * state machine implemented by rcu_prepare_for_idle() below.
  1311. *
  1312. * The following three proprocessor symbols control this state machine:
  1313. *
  1314. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1315. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1316. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1317. * benchmarkers who might otherwise be tempted to set this to a large
  1318. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1319. * system. And if you are -that- concerned about energy efficiency,
  1320. * just power the system down and be done with it!
  1321. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1322. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1323. * callbacks pending. Setting this too high can OOM your system.
  1324. *
  1325. * The values below work well in practice. If future workloads require
  1326. * adjustment, they can be converted into kernel config parameters, though
  1327. * making the state machine smarter might be a better option.
  1328. */
  1329. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1330. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1331. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1332. module_param(rcu_idle_gp_delay, int, 0644);
  1333. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1334. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1335. /*
  1336. * Try to advance callbacks for all flavors of RCU on the current CPU, but
  1337. * only if it has been awhile since the last time we did so. Afterwards,
  1338. * if there are any callbacks ready for immediate invocation, return true.
  1339. */
  1340. static bool __maybe_unused rcu_try_advance_all_cbs(void)
  1341. {
  1342. bool cbs_ready = false;
  1343. struct rcu_data *rdp;
  1344. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1345. struct rcu_node *rnp;
  1346. struct rcu_state *rsp;
  1347. /* Exit early if we advanced recently. */
  1348. if (jiffies == rdtp->last_advance_all)
  1349. return false;
  1350. rdtp->last_advance_all = jiffies;
  1351. for_each_rcu_flavor(rsp) {
  1352. rdp = this_cpu_ptr(rsp->rda);
  1353. rnp = rdp->mynode;
  1354. /*
  1355. * Don't bother checking unless a grace period has
  1356. * completed since we last checked and there are
  1357. * callbacks not yet ready to invoke.
  1358. */
  1359. if ((rcu_seq_completed_gp(rdp->gp_seq,
  1360. rcu_seq_current(&rnp->gp_seq)) ||
  1361. unlikely(READ_ONCE(rdp->gpwrap))) &&
  1362. rcu_segcblist_pend_cbs(&rdp->cblist))
  1363. note_gp_changes(rsp, rdp);
  1364. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  1365. cbs_ready = true;
  1366. }
  1367. return cbs_ready;
  1368. }
  1369. /*
  1370. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1371. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1372. * caller to set the timeout based on whether or not there are non-lazy
  1373. * callbacks.
  1374. *
  1375. * The caller must have disabled interrupts.
  1376. */
  1377. int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  1378. {
  1379. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1380. unsigned long dj;
  1381. lockdep_assert_irqs_disabled();
  1382. /* Snapshot to detect later posting of non-lazy callback. */
  1383. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1384. /* If no callbacks, RCU doesn't need the CPU. */
  1385. if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
  1386. *nextevt = KTIME_MAX;
  1387. return 0;
  1388. }
  1389. /* Attempt to advance callbacks. */
  1390. if (rcu_try_advance_all_cbs()) {
  1391. /* Some ready to invoke, so initiate later invocation. */
  1392. invoke_rcu_core();
  1393. return 1;
  1394. }
  1395. rdtp->last_accelerate = jiffies;
  1396. /* Request timer delay depending on laziness, and round. */
  1397. if (!rdtp->all_lazy) {
  1398. dj = round_up(rcu_idle_gp_delay + jiffies,
  1399. rcu_idle_gp_delay) - jiffies;
  1400. } else {
  1401. dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1402. }
  1403. *nextevt = basemono + dj * TICK_NSEC;
  1404. return 0;
  1405. }
  1406. /*
  1407. * Prepare a CPU for idle from an RCU perspective. The first major task
  1408. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1409. * The second major task is to check to see if a non-lazy callback has
  1410. * arrived at a CPU that previously had only lazy callbacks. The third
  1411. * major task is to accelerate (that is, assign grace-period numbers to)
  1412. * any recently arrived callbacks.
  1413. *
  1414. * The caller must have disabled interrupts.
  1415. */
  1416. static void rcu_prepare_for_idle(void)
  1417. {
  1418. bool needwake;
  1419. struct rcu_data *rdp;
  1420. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1421. struct rcu_node *rnp;
  1422. struct rcu_state *rsp;
  1423. int tne;
  1424. lockdep_assert_irqs_disabled();
  1425. if (rcu_is_nocb_cpu(smp_processor_id()))
  1426. return;
  1427. /* Handle nohz enablement switches conservatively. */
  1428. tne = READ_ONCE(tick_nohz_active);
  1429. if (tne != rdtp->tick_nohz_enabled_snap) {
  1430. if (rcu_cpu_has_callbacks(NULL))
  1431. invoke_rcu_core(); /* force nohz to see update. */
  1432. rdtp->tick_nohz_enabled_snap = tne;
  1433. return;
  1434. }
  1435. if (!tne)
  1436. return;
  1437. /*
  1438. * If a non-lazy callback arrived at a CPU having only lazy
  1439. * callbacks, invoke RCU core for the side-effect of recalculating
  1440. * idle duration on re-entry to idle.
  1441. */
  1442. if (rdtp->all_lazy &&
  1443. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1444. rdtp->all_lazy = false;
  1445. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1446. invoke_rcu_core();
  1447. return;
  1448. }
  1449. /*
  1450. * If we have not yet accelerated this jiffy, accelerate all
  1451. * callbacks on this CPU.
  1452. */
  1453. if (rdtp->last_accelerate == jiffies)
  1454. return;
  1455. rdtp->last_accelerate = jiffies;
  1456. for_each_rcu_flavor(rsp) {
  1457. rdp = this_cpu_ptr(rsp->rda);
  1458. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1459. continue;
  1460. rnp = rdp->mynode;
  1461. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1462. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  1463. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1464. if (needwake)
  1465. rcu_gp_kthread_wake(rsp);
  1466. }
  1467. }
  1468. /*
  1469. * Clean up for exit from idle. Attempt to advance callbacks based on
  1470. * any grace periods that elapsed while the CPU was idle, and if any
  1471. * callbacks are now ready to invoke, initiate invocation.
  1472. */
  1473. static void rcu_cleanup_after_idle(void)
  1474. {
  1475. lockdep_assert_irqs_disabled();
  1476. if (rcu_is_nocb_cpu(smp_processor_id()))
  1477. return;
  1478. if (rcu_try_advance_all_cbs())
  1479. invoke_rcu_core();
  1480. }
  1481. /*
  1482. * Keep a running count of the number of non-lazy callbacks posted
  1483. * on this CPU. This running counter (which is never decremented) allows
  1484. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1485. * posts a callback, even if an equal number of callbacks are invoked.
  1486. * Of course, callbacks should only be posted from within a trace event
  1487. * designed to be called from idle or from within RCU_NONIDLE().
  1488. */
  1489. static void rcu_idle_count_callbacks_posted(void)
  1490. {
  1491. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1492. }
  1493. /*
  1494. * Data for flushing lazy RCU callbacks at OOM time.
  1495. */
  1496. static atomic_t oom_callback_count;
  1497. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1498. /*
  1499. * RCU OOM callback -- decrement the outstanding count and deliver the
  1500. * wake-up if we are the last one.
  1501. */
  1502. static void rcu_oom_callback(struct rcu_head *rhp)
  1503. {
  1504. if (atomic_dec_and_test(&oom_callback_count))
  1505. wake_up(&oom_callback_wq);
  1506. }
  1507. /*
  1508. * Post an rcu_oom_notify callback on the current CPU if it has at
  1509. * least one lazy callback. This will unnecessarily post callbacks
  1510. * to CPUs that already have a non-lazy callback at the end of their
  1511. * callback list, but this is an infrequent operation, so accept some
  1512. * extra overhead to keep things simple.
  1513. */
  1514. static void rcu_oom_notify_cpu(void *unused)
  1515. {
  1516. struct rcu_state *rsp;
  1517. struct rcu_data *rdp;
  1518. for_each_rcu_flavor(rsp) {
  1519. rdp = raw_cpu_ptr(rsp->rda);
  1520. if (rcu_segcblist_n_lazy_cbs(&rdp->cblist)) {
  1521. atomic_inc(&oom_callback_count);
  1522. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1523. }
  1524. }
  1525. }
  1526. /*
  1527. * If low on memory, ensure that each CPU has a non-lazy callback.
  1528. * This will wake up CPUs that have only lazy callbacks, in turn
  1529. * ensuring that they free up the corresponding memory in a timely manner.
  1530. * Because an uncertain amount of memory will be freed in some uncertain
  1531. * timeframe, we do not claim to have freed anything.
  1532. */
  1533. static int rcu_oom_notify(struct notifier_block *self,
  1534. unsigned long notused, void *nfreed)
  1535. {
  1536. int cpu;
  1537. /* Wait for callbacks from earlier instance to complete. */
  1538. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1539. smp_mb(); /* Ensure callback reuse happens after callback invocation. */
  1540. /*
  1541. * Prevent premature wakeup: ensure that all increments happen
  1542. * before there is a chance of the counter reaching zero.
  1543. */
  1544. atomic_set(&oom_callback_count, 1);
  1545. for_each_online_cpu(cpu) {
  1546. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1547. cond_resched_tasks_rcu_qs();
  1548. }
  1549. /* Unconditionally decrement: no need to wake ourselves up. */
  1550. atomic_dec(&oom_callback_count);
  1551. return NOTIFY_OK;
  1552. }
  1553. static struct notifier_block rcu_oom_nb = {
  1554. .notifier_call = rcu_oom_notify
  1555. };
  1556. static int __init rcu_register_oom_notifier(void)
  1557. {
  1558. register_oom_notifier(&rcu_oom_nb);
  1559. return 0;
  1560. }
  1561. early_initcall(rcu_register_oom_notifier);
  1562. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1563. #ifdef CONFIG_RCU_FAST_NO_HZ
  1564. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1565. {
  1566. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1567. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1568. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1569. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1570. ulong2long(nlpd),
  1571. rdtp->all_lazy ? 'L' : '.',
  1572. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1573. }
  1574. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1575. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1576. {
  1577. *cp = '\0';
  1578. }
  1579. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1580. /* Initiate the stall-info list. */
  1581. static void print_cpu_stall_info_begin(void)
  1582. {
  1583. pr_cont("\n");
  1584. }
  1585. /*
  1586. * Print out diagnostic information for the specified stalled CPU.
  1587. *
  1588. * If the specified CPU is aware of the current RCU grace period
  1589. * (flavor specified by rsp), then print the number of scheduling
  1590. * clock interrupts the CPU has taken during the time that it has
  1591. * been aware. Otherwise, print the number of RCU grace periods
  1592. * that this CPU is ignorant of, for example, "1" if the CPU was
  1593. * aware of the previous grace period.
  1594. *
  1595. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1596. */
  1597. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1598. {
  1599. unsigned long delta;
  1600. char fast_no_hz[72];
  1601. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1602. struct rcu_dynticks *rdtp = rdp->dynticks;
  1603. char *ticks_title;
  1604. unsigned long ticks_value;
  1605. /*
  1606. * We could be printing a lot while holding a spinlock. Avoid
  1607. * triggering hard lockup.
  1608. */
  1609. touch_nmi_watchdog();
  1610. ticks_value = rcu_seq_ctr(rsp->gp_seq - rdp->gp_seq);
  1611. if (ticks_value) {
  1612. ticks_title = "GPs behind";
  1613. } else {
  1614. ticks_title = "ticks this GP";
  1615. ticks_value = rdp->ticks_this_gp;
  1616. }
  1617. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1618. delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
  1619. pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s\n",
  1620. cpu,
  1621. "O."[!!cpu_online(cpu)],
  1622. "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
  1623. "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
  1624. !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
  1625. rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
  1626. "!."[!delta],
  1627. ticks_value, ticks_title,
  1628. rcu_dynticks_snap(rdtp) & 0xfff,
  1629. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1630. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1631. READ_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
  1632. fast_no_hz);
  1633. }
  1634. /* Terminate the stall-info list. */
  1635. static void print_cpu_stall_info_end(void)
  1636. {
  1637. pr_err("\t");
  1638. }
  1639. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1640. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1641. {
  1642. rdp->ticks_this_gp = 0;
  1643. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1644. }
  1645. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1646. static void increment_cpu_stall_ticks(void)
  1647. {
  1648. struct rcu_state *rsp;
  1649. for_each_rcu_flavor(rsp)
  1650. raw_cpu_inc(rsp->rda->ticks_this_gp);
  1651. }
  1652. #ifdef CONFIG_RCU_NOCB_CPU
  1653. /*
  1654. * Offload callback processing from the boot-time-specified set of CPUs
  1655. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1656. * kthread created that pulls the callbacks from the corresponding CPU,
  1657. * waits for a grace period to elapse, and invokes the callbacks.
  1658. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1659. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1660. * has been specified, in which case each kthread actively polls its
  1661. * CPU. (Which isn't so great for energy efficiency, but which does
  1662. * reduce RCU's overhead on that CPU.)
  1663. *
  1664. * This is intended to be used in conjunction with Frederic Weisbecker's
  1665. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1666. * running CPU-bound user-mode computations.
  1667. *
  1668. * Offloading of callback processing could also in theory be used as
  1669. * an energy-efficiency measure because CPUs with no RCU callbacks
  1670. * queued are more aggressive about entering dyntick-idle mode.
  1671. */
  1672. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1673. static int __init rcu_nocb_setup(char *str)
  1674. {
  1675. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1676. cpulist_parse(str, rcu_nocb_mask);
  1677. return 1;
  1678. }
  1679. __setup("rcu_nocbs=", rcu_nocb_setup);
  1680. static int __init parse_rcu_nocb_poll(char *arg)
  1681. {
  1682. rcu_nocb_poll = true;
  1683. return 0;
  1684. }
  1685. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1686. /*
  1687. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1688. * grace period.
  1689. */
  1690. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  1691. {
  1692. swake_up_all(sq);
  1693. }
  1694. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  1695. {
  1696. return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
  1697. }
  1698. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1699. {
  1700. init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  1701. init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  1702. }
  1703. /* Is the specified CPU a no-CBs CPU? */
  1704. bool rcu_is_nocb_cpu(int cpu)
  1705. {
  1706. if (cpumask_available(rcu_nocb_mask))
  1707. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1708. return false;
  1709. }
  1710. /*
  1711. * Kick the leader kthread for this NOCB group. Caller holds ->nocb_lock
  1712. * and this function releases it.
  1713. */
  1714. static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
  1715. unsigned long flags)
  1716. __releases(rdp->nocb_lock)
  1717. {
  1718. struct rcu_data *rdp_leader = rdp->nocb_leader;
  1719. lockdep_assert_held(&rdp->nocb_lock);
  1720. if (!READ_ONCE(rdp_leader->nocb_kthread)) {
  1721. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1722. return;
  1723. }
  1724. if (rdp_leader->nocb_leader_sleep || force) {
  1725. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  1726. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  1727. del_timer(&rdp->nocb_timer);
  1728. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1729. smp_mb(); /* ->nocb_leader_sleep before swake_up_one(). */
  1730. swake_up_one(&rdp_leader->nocb_wq);
  1731. } else {
  1732. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1733. }
  1734. }
  1735. /*
  1736. * Kick the leader kthread for this NOCB group, but caller has not
  1737. * acquired locks.
  1738. */
  1739. static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  1740. {
  1741. unsigned long flags;
  1742. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  1743. __wake_nocb_leader(rdp, force, flags);
  1744. }
  1745. /*
  1746. * Arrange to wake the leader kthread for this NOCB group at some
  1747. * future time when it is safe to do so.
  1748. */
  1749. static void wake_nocb_leader_defer(struct rcu_data *rdp, int waketype,
  1750. const char *reason)
  1751. {
  1752. unsigned long flags;
  1753. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  1754. if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT)
  1755. mod_timer(&rdp->nocb_timer, jiffies + 1);
  1756. WRITE_ONCE(rdp->nocb_defer_wakeup, waketype);
  1757. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, reason);
  1758. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  1759. }
  1760. /*
  1761. * Does the specified CPU need an RCU callback for the specified flavor
  1762. * of rcu_barrier()?
  1763. */
  1764. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  1765. {
  1766. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1767. unsigned long ret;
  1768. #ifdef CONFIG_PROVE_RCU
  1769. struct rcu_head *rhp;
  1770. #endif /* #ifdef CONFIG_PROVE_RCU */
  1771. /*
  1772. * Check count of all no-CBs callbacks awaiting invocation.
  1773. * There needs to be a barrier before this function is called,
  1774. * but associated with a prior determination that no more
  1775. * callbacks would be posted. In the worst case, the first
  1776. * barrier in _rcu_barrier() suffices (but the caller cannot
  1777. * necessarily rely on this, not a substitute for the caller
  1778. * getting the concurrency design right!). There must also be
  1779. * a barrier between the following load an posting of a callback
  1780. * (if a callback is in fact needed). This is associated with an
  1781. * atomic_inc() in the caller.
  1782. */
  1783. ret = atomic_long_read(&rdp->nocb_q_count);
  1784. #ifdef CONFIG_PROVE_RCU
  1785. rhp = READ_ONCE(rdp->nocb_head);
  1786. if (!rhp)
  1787. rhp = READ_ONCE(rdp->nocb_gp_head);
  1788. if (!rhp)
  1789. rhp = READ_ONCE(rdp->nocb_follower_head);
  1790. /* Having no rcuo kthread but CBs after scheduler starts is bad! */
  1791. if (!READ_ONCE(rdp->nocb_kthread) && rhp &&
  1792. rcu_scheduler_fully_active) {
  1793. /* RCU callback enqueued before CPU first came online??? */
  1794. pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
  1795. cpu, rhp->func);
  1796. WARN_ON_ONCE(1);
  1797. }
  1798. #endif /* #ifdef CONFIG_PROVE_RCU */
  1799. return !!ret;
  1800. }
  1801. /*
  1802. * Enqueue the specified string of rcu_head structures onto the specified
  1803. * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
  1804. * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
  1805. * counts are supplied by rhcount and rhcount_lazy.
  1806. *
  1807. * If warranted, also wake up the kthread servicing this CPUs queues.
  1808. */
  1809. static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
  1810. struct rcu_head *rhp,
  1811. struct rcu_head **rhtp,
  1812. int rhcount, int rhcount_lazy,
  1813. unsigned long flags)
  1814. {
  1815. int len;
  1816. struct rcu_head **old_rhpp;
  1817. struct task_struct *t;
  1818. /* Enqueue the callback on the nocb list and update counts. */
  1819. atomic_long_add(rhcount, &rdp->nocb_q_count);
  1820. /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
  1821. old_rhpp = xchg(&rdp->nocb_tail, rhtp);
  1822. WRITE_ONCE(*old_rhpp, rhp);
  1823. atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
  1824. smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
  1825. /* If we are not being polled and there is a kthread, awaken it ... */
  1826. t = READ_ONCE(rdp->nocb_kthread);
  1827. if (rcu_nocb_poll || !t) {
  1828. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1829. TPS("WakeNotPoll"));
  1830. return;
  1831. }
  1832. len = atomic_long_read(&rdp->nocb_q_count);
  1833. if (old_rhpp == &rdp->nocb_head) {
  1834. if (!irqs_disabled_flags(flags)) {
  1835. /* ... if queue was empty ... */
  1836. wake_nocb_leader(rdp, false);
  1837. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1838. TPS("WakeEmpty"));
  1839. } else {
  1840. wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE,
  1841. TPS("WakeEmptyIsDeferred"));
  1842. }
  1843. rdp->qlen_last_fqs_check = 0;
  1844. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  1845. /* ... or if many callbacks queued. */
  1846. if (!irqs_disabled_flags(flags)) {
  1847. wake_nocb_leader(rdp, true);
  1848. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1849. TPS("WakeOvf"));
  1850. } else {
  1851. wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE_FORCE,
  1852. TPS("WakeOvfIsDeferred"));
  1853. }
  1854. rdp->qlen_last_fqs_check = LONG_MAX / 2;
  1855. } else {
  1856. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
  1857. }
  1858. return;
  1859. }
  1860. /*
  1861. * This is a helper for __call_rcu(), which invokes this when the normal
  1862. * callback queue is inoperable. If this is not a no-CBs CPU, this
  1863. * function returns failure back to __call_rcu(), which can complain
  1864. * appropriately.
  1865. *
  1866. * Otherwise, this function queues the callback where the corresponding
  1867. * "rcuo" kthread can find it.
  1868. */
  1869. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  1870. bool lazy, unsigned long flags)
  1871. {
  1872. if (!rcu_is_nocb_cpu(rdp->cpu))
  1873. return false;
  1874. __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
  1875. if (__is_kfree_rcu_offset((unsigned long)rhp->func))
  1876. trace_rcu_kfree_callback(rdp->rsp->name, rhp,
  1877. (unsigned long)rhp->func,
  1878. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1879. -atomic_long_read(&rdp->nocb_q_count));
  1880. else
  1881. trace_rcu_callback(rdp->rsp->name, rhp,
  1882. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1883. -atomic_long_read(&rdp->nocb_q_count));
  1884. /*
  1885. * If called from an extended quiescent state with interrupts
  1886. * disabled, invoke the RCU core in order to allow the idle-entry
  1887. * deferred-wakeup check to function.
  1888. */
  1889. if (irqs_disabled_flags(flags) &&
  1890. !rcu_is_watching() &&
  1891. cpu_online(smp_processor_id()))
  1892. invoke_rcu_core();
  1893. return true;
  1894. }
  1895. /*
  1896. * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
  1897. * not a no-CBs CPU.
  1898. */
  1899. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
  1900. struct rcu_data *rdp,
  1901. unsigned long flags)
  1902. {
  1903. lockdep_assert_irqs_disabled();
  1904. if (!rcu_is_nocb_cpu(smp_processor_id()))
  1905. return false; /* Not NOCBs CPU, caller must migrate CBs. */
  1906. __call_rcu_nocb_enqueue(my_rdp, rcu_segcblist_head(&rdp->cblist),
  1907. rcu_segcblist_tail(&rdp->cblist),
  1908. rcu_segcblist_n_cbs(&rdp->cblist),
  1909. rcu_segcblist_n_lazy_cbs(&rdp->cblist), flags);
  1910. rcu_segcblist_init(&rdp->cblist);
  1911. rcu_segcblist_disable(&rdp->cblist);
  1912. return true;
  1913. }
  1914. /*
  1915. * If necessary, kick off a new grace period, and either way wait
  1916. * for a subsequent grace period to complete.
  1917. */
  1918. static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  1919. {
  1920. unsigned long c;
  1921. bool d;
  1922. unsigned long flags;
  1923. bool needwake;
  1924. struct rcu_node *rnp = rdp->mynode;
  1925. local_irq_save(flags);
  1926. c = rcu_seq_snap(&rdp->rsp->gp_seq);
  1927. if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
  1928. local_irq_restore(flags);
  1929. } else {
  1930. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1931. needwake = rcu_start_this_gp(rnp, rdp, c);
  1932. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1933. if (needwake)
  1934. rcu_gp_kthread_wake(rdp->rsp);
  1935. }
  1936. /*
  1937. * Wait for the grace period. Do so interruptibly to avoid messing
  1938. * up the load average.
  1939. */
  1940. trace_rcu_this_gp(rnp, rdp, c, TPS("StartWait"));
  1941. for (;;) {
  1942. swait_event_interruptible_exclusive(
  1943. rnp->nocb_gp_wq[rcu_seq_ctr(c) & 0x1],
  1944. (d = rcu_seq_done(&rnp->gp_seq, c)));
  1945. if (likely(d))
  1946. break;
  1947. WARN_ON(signal_pending(current));
  1948. trace_rcu_this_gp(rnp, rdp, c, TPS("ResumeWait"));
  1949. }
  1950. trace_rcu_this_gp(rnp, rdp, c, TPS("EndWait"));
  1951. smp_mb(); /* Ensure that CB invocation happens after GP end. */
  1952. }
  1953. /*
  1954. * Leaders come here to wait for additional callbacks to show up.
  1955. * This function does not return until callbacks appear.
  1956. */
  1957. static void nocb_leader_wait(struct rcu_data *my_rdp)
  1958. {
  1959. bool firsttime = true;
  1960. unsigned long flags;
  1961. bool gotcbs;
  1962. struct rcu_data *rdp;
  1963. struct rcu_head **tail;
  1964. wait_again:
  1965. /* Wait for callbacks to appear. */
  1966. if (!rcu_nocb_poll) {
  1967. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, TPS("Sleep"));
  1968. swait_event_interruptible_exclusive(my_rdp->nocb_wq,
  1969. !READ_ONCE(my_rdp->nocb_leader_sleep));
  1970. raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
  1971. my_rdp->nocb_leader_sleep = true;
  1972. WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  1973. del_timer(&my_rdp->nocb_timer);
  1974. raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
  1975. } else if (firsttime) {
  1976. firsttime = false; /* Don't drown trace log with "Poll"! */
  1977. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, TPS("Poll"));
  1978. }
  1979. /*
  1980. * Each pass through the following loop checks a follower for CBs.
  1981. * We are our own first follower. Any CBs found are moved to
  1982. * nocb_gp_head, where they await a grace period.
  1983. */
  1984. gotcbs = false;
  1985. smp_mb(); /* wakeup and _sleep before ->nocb_head reads. */
  1986. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1987. rdp->nocb_gp_head = READ_ONCE(rdp->nocb_head);
  1988. if (!rdp->nocb_gp_head)
  1989. continue; /* No CBs here, try next follower. */
  1990. /* Move callbacks to wait-for-GP list, which is empty. */
  1991. WRITE_ONCE(rdp->nocb_head, NULL);
  1992. rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  1993. gotcbs = true;
  1994. }
  1995. /* No callbacks? Sleep a bit if polling, and go retry. */
  1996. if (unlikely(!gotcbs)) {
  1997. WARN_ON(signal_pending(current));
  1998. if (rcu_nocb_poll) {
  1999. schedule_timeout_interruptible(1);
  2000. } else {
  2001. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
  2002. TPS("WokeEmpty"));
  2003. }
  2004. goto wait_again;
  2005. }
  2006. /* Wait for one grace period. */
  2007. rcu_nocb_wait_gp(my_rdp);
  2008. /* Each pass through the following loop wakes a follower, if needed. */
  2009. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  2010. if (!rcu_nocb_poll &&
  2011. READ_ONCE(rdp->nocb_head) &&
  2012. READ_ONCE(my_rdp->nocb_leader_sleep)) {
  2013. raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
  2014. my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
  2015. raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
  2016. }
  2017. if (!rdp->nocb_gp_head)
  2018. continue; /* No CBs, so no need to wake follower. */
  2019. /* Append callbacks to follower's "done" list. */
  2020. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2021. tail = rdp->nocb_follower_tail;
  2022. rdp->nocb_follower_tail = rdp->nocb_gp_tail;
  2023. *tail = rdp->nocb_gp_head;
  2024. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2025. if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
  2026. /* List was empty, so wake up the follower. */
  2027. swake_up_one(&rdp->nocb_wq);
  2028. }
  2029. }
  2030. /* If we (the leader) don't have CBs, go wait some more. */
  2031. if (!my_rdp->nocb_follower_head)
  2032. goto wait_again;
  2033. }
  2034. /*
  2035. * Followers come here to wait for additional callbacks to show up.
  2036. * This function does not return until callbacks appear.
  2037. */
  2038. static void nocb_follower_wait(struct rcu_data *rdp)
  2039. {
  2040. for (;;) {
  2041. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("FollowerSleep"));
  2042. swait_event_interruptible_exclusive(rdp->nocb_wq,
  2043. READ_ONCE(rdp->nocb_follower_head));
  2044. if (smp_load_acquire(&rdp->nocb_follower_head)) {
  2045. /* ^^^ Ensure CB invocation follows _head test. */
  2046. return;
  2047. }
  2048. WARN_ON(signal_pending(current));
  2049. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WokeEmpty"));
  2050. }
  2051. }
  2052. /*
  2053. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  2054. * callbacks queued by the corresponding no-CBs CPU, however, there is
  2055. * an optional leader-follower relationship so that the grace-period
  2056. * kthreads don't have to do quite so many wakeups.
  2057. */
  2058. static int rcu_nocb_kthread(void *arg)
  2059. {
  2060. int c, cl;
  2061. unsigned long flags;
  2062. struct rcu_head *list;
  2063. struct rcu_head *next;
  2064. struct rcu_head **tail;
  2065. struct rcu_data *rdp = arg;
  2066. /* Each pass through this loop invokes one batch of callbacks */
  2067. for (;;) {
  2068. /* Wait for callbacks. */
  2069. if (rdp->nocb_leader == rdp)
  2070. nocb_leader_wait(rdp);
  2071. else
  2072. nocb_follower_wait(rdp);
  2073. /* Pull the ready-to-invoke callbacks onto local list. */
  2074. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2075. list = rdp->nocb_follower_head;
  2076. rdp->nocb_follower_head = NULL;
  2077. tail = rdp->nocb_follower_tail;
  2078. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2079. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2080. BUG_ON(!list);
  2081. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WokeNonEmpty"));
  2082. /* Each pass through the following loop invokes a callback. */
  2083. trace_rcu_batch_start(rdp->rsp->name,
  2084. atomic_long_read(&rdp->nocb_q_count_lazy),
  2085. atomic_long_read(&rdp->nocb_q_count), -1);
  2086. c = cl = 0;
  2087. while (list) {
  2088. next = list->next;
  2089. /* Wait for enqueuing to complete, if needed. */
  2090. while (next == NULL && &list->next != tail) {
  2091. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2092. TPS("WaitQueue"));
  2093. schedule_timeout_interruptible(1);
  2094. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2095. TPS("WokeQueue"));
  2096. next = list->next;
  2097. }
  2098. debug_rcu_head_unqueue(list);
  2099. local_bh_disable();
  2100. if (__rcu_reclaim(rdp->rsp->name, list))
  2101. cl++;
  2102. c++;
  2103. local_bh_enable();
  2104. cond_resched_tasks_rcu_qs();
  2105. list = next;
  2106. }
  2107. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  2108. smp_mb__before_atomic(); /* _add after CB invocation. */
  2109. atomic_long_add(-c, &rdp->nocb_q_count);
  2110. atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
  2111. }
  2112. return 0;
  2113. }
  2114. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  2115. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2116. {
  2117. return READ_ONCE(rdp->nocb_defer_wakeup);
  2118. }
  2119. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  2120. static void do_nocb_deferred_wakeup_common(struct rcu_data *rdp)
  2121. {
  2122. unsigned long flags;
  2123. int ndw;
  2124. raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
  2125. if (!rcu_nocb_need_deferred_wakeup(rdp)) {
  2126. raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
  2127. return;
  2128. }
  2129. ndw = READ_ONCE(rdp->nocb_defer_wakeup);
  2130. WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
  2131. __wake_nocb_leader(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
  2132. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
  2133. }
  2134. /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
  2135. static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
  2136. {
  2137. struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
  2138. do_nocb_deferred_wakeup_common(rdp);
  2139. }
  2140. /*
  2141. * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
  2142. * This means we do an inexact common-case check. Note that if
  2143. * we miss, ->nocb_timer will eventually clean things up.
  2144. */
  2145. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2146. {
  2147. if (rcu_nocb_need_deferred_wakeup(rdp))
  2148. do_nocb_deferred_wakeup_common(rdp);
  2149. }
  2150. void __init rcu_init_nohz(void)
  2151. {
  2152. int cpu;
  2153. bool need_rcu_nocb_mask = false;
  2154. struct rcu_state *rsp;
  2155. #if defined(CONFIG_NO_HZ_FULL)
  2156. if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
  2157. need_rcu_nocb_mask = true;
  2158. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2159. if (!cpumask_available(rcu_nocb_mask) && need_rcu_nocb_mask) {
  2160. if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
  2161. pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
  2162. return;
  2163. }
  2164. }
  2165. if (!cpumask_available(rcu_nocb_mask))
  2166. return;
  2167. #if defined(CONFIG_NO_HZ_FULL)
  2168. if (tick_nohz_full_running)
  2169. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
  2170. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2171. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  2172. pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
  2173. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  2174. rcu_nocb_mask);
  2175. }
  2176. if (cpumask_empty(rcu_nocb_mask))
  2177. pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
  2178. else
  2179. pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
  2180. cpumask_pr_args(rcu_nocb_mask));
  2181. if (rcu_nocb_poll)
  2182. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  2183. for_each_rcu_flavor(rsp) {
  2184. for_each_cpu(cpu, rcu_nocb_mask)
  2185. init_nocb_callback_list(per_cpu_ptr(rsp->rda, cpu));
  2186. rcu_organize_nocb_kthreads(rsp);
  2187. }
  2188. }
  2189. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2190. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2191. {
  2192. rdp->nocb_tail = &rdp->nocb_head;
  2193. init_swait_queue_head(&rdp->nocb_wq);
  2194. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2195. raw_spin_lock_init(&rdp->nocb_lock);
  2196. timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
  2197. }
  2198. /*
  2199. * If the specified CPU is a no-CBs CPU that does not already have its
  2200. * rcuo kthread for the specified RCU flavor, spawn it. If the CPUs are
  2201. * brought online out of order, this can require re-organizing the
  2202. * leader-follower relationships.
  2203. */
  2204. static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
  2205. {
  2206. struct rcu_data *rdp;
  2207. struct rcu_data *rdp_last;
  2208. struct rcu_data *rdp_old_leader;
  2209. struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
  2210. struct task_struct *t;
  2211. /*
  2212. * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
  2213. * then nothing to do.
  2214. */
  2215. if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
  2216. return;
  2217. /* If we didn't spawn the leader first, reorganize! */
  2218. rdp_old_leader = rdp_spawn->nocb_leader;
  2219. if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
  2220. rdp_last = NULL;
  2221. rdp = rdp_old_leader;
  2222. do {
  2223. rdp->nocb_leader = rdp_spawn;
  2224. if (rdp_last && rdp != rdp_spawn)
  2225. rdp_last->nocb_next_follower = rdp;
  2226. if (rdp == rdp_spawn) {
  2227. rdp = rdp->nocb_next_follower;
  2228. } else {
  2229. rdp_last = rdp;
  2230. rdp = rdp->nocb_next_follower;
  2231. rdp_last->nocb_next_follower = NULL;
  2232. }
  2233. } while (rdp);
  2234. rdp_spawn->nocb_next_follower = rdp_old_leader;
  2235. }
  2236. /* Spawn the kthread for this CPU and RCU flavor. */
  2237. t = kthread_run(rcu_nocb_kthread, rdp_spawn,
  2238. "rcuo%c/%d", rsp->abbr, cpu);
  2239. BUG_ON(IS_ERR(t));
  2240. WRITE_ONCE(rdp_spawn->nocb_kthread, t);
  2241. }
  2242. /*
  2243. * If the specified CPU is a no-CBs CPU that does not already have its
  2244. * rcuo kthreads, spawn them.
  2245. */
  2246. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2247. {
  2248. struct rcu_state *rsp;
  2249. if (rcu_scheduler_fully_active)
  2250. for_each_rcu_flavor(rsp)
  2251. rcu_spawn_one_nocb_kthread(rsp, cpu);
  2252. }
  2253. /*
  2254. * Once the scheduler is running, spawn rcuo kthreads for all online
  2255. * no-CBs CPUs. This assumes that the early_initcall()s happen before
  2256. * non-boot CPUs come online -- if this changes, we will need to add
  2257. * some mutual exclusion.
  2258. */
  2259. static void __init rcu_spawn_nocb_kthreads(void)
  2260. {
  2261. int cpu;
  2262. for_each_online_cpu(cpu)
  2263. rcu_spawn_all_nocb_kthreads(cpu);
  2264. }
  2265. /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
  2266. static int rcu_nocb_leader_stride = -1;
  2267. module_param(rcu_nocb_leader_stride, int, 0444);
  2268. /*
  2269. * Initialize leader-follower relationships for all no-CBs CPU.
  2270. */
  2271. static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
  2272. {
  2273. int cpu;
  2274. int ls = rcu_nocb_leader_stride;
  2275. int nl = 0; /* Next leader. */
  2276. struct rcu_data *rdp;
  2277. struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
  2278. struct rcu_data *rdp_prev = NULL;
  2279. if (!cpumask_available(rcu_nocb_mask))
  2280. return;
  2281. if (ls == -1) {
  2282. ls = int_sqrt(nr_cpu_ids);
  2283. rcu_nocb_leader_stride = ls;
  2284. }
  2285. /*
  2286. * Each pass through this loop sets up one rcu_data structure.
  2287. * Should the corresponding CPU come online in the future, then
  2288. * we will spawn the needed set of rcu_nocb_kthread() kthreads.
  2289. */
  2290. for_each_cpu(cpu, rcu_nocb_mask) {
  2291. rdp = per_cpu_ptr(rsp->rda, cpu);
  2292. if (rdp->cpu >= nl) {
  2293. /* New leader, set up for followers & next leader. */
  2294. nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
  2295. rdp->nocb_leader = rdp;
  2296. rdp_leader = rdp;
  2297. } else {
  2298. /* Another follower, link to previous leader. */
  2299. rdp->nocb_leader = rdp_leader;
  2300. rdp_prev->nocb_next_follower = rdp;
  2301. }
  2302. rdp_prev = rdp;
  2303. }
  2304. }
  2305. /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
  2306. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2307. {
  2308. if (!rcu_is_nocb_cpu(rdp->cpu))
  2309. return false;
  2310. /* If there are early-boot callbacks, move them to nocb lists. */
  2311. if (!rcu_segcblist_empty(&rdp->cblist)) {
  2312. rdp->nocb_head = rcu_segcblist_head(&rdp->cblist);
  2313. rdp->nocb_tail = rcu_segcblist_tail(&rdp->cblist);
  2314. atomic_long_set(&rdp->nocb_q_count,
  2315. rcu_segcblist_n_cbs(&rdp->cblist));
  2316. atomic_long_set(&rdp->nocb_q_count_lazy,
  2317. rcu_segcblist_n_lazy_cbs(&rdp->cblist));
  2318. rcu_segcblist_init(&rdp->cblist);
  2319. }
  2320. rcu_segcblist_disable(&rdp->cblist);
  2321. return true;
  2322. }
  2323. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  2324. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  2325. {
  2326. WARN_ON_ONCE(1); /* Should be dead code. */
  2327. return false;
  2328. }
  2329. static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  2330. {
  2331. }
  2332. static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  2333. {
  2334. return NULL;
  2335. }
  2336. static void rcu_init_one_nocb(struct rcu_node *rnp)
  2337. {
  2338. }
  2339. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  2340. bool lazy, unsigned long flags)
  2341. {
  2342. return false;
  2343. }
  2344. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
  2345. struct rcu_data *rdp,
  2346. unsigned long flags)
  2347. {
  2348. return false;
  2349. }
  2350. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2351. {
  2352. }
  2353. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2354. {
  2355. return false;
  2356. }
  2357. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2358. {
  2359. }
  2360. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2361. {
  2362. }
  2363. static void __init rcu_spawn_nocb_kthreads(void)
  2364. {
  2365. }
  2366. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2367. {
  2368. return false;
  2369. }
  2370. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  2371. /*
  2372. * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
  2373. * grace-period kthread will do force_quiescent_state() processing?
  2374. * The idea is to avoid waking up RCU core processing on such a
  2375. * CPU unless the grace period has extended for too long.
  2376. *
  2377. * This code relies on the fact that all NO_HZ_FULL CPUs are also
  2378. * CONFIG_RCU_NOCB_CPU CPUs.
  2379. */
  2380. static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
  2381. {
  2382. #ifdef CONFIG_NO_HZ_FULL
  2383. if (tick_nohz_full_cpu(smp_processor_id()) &&
  2384. (!rcu_gp_in_progress(rsp) ||
  2385. ULONG_CMP_LT(jiffies, READ_ONCE(rsp->gp_start) + HZ)))
  2386. return true;
  2387. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2388. return false;
  2389. }
  2390. /*
  2391. * Bind the RCU grace-period kthreads to the housekeeping CPU.
  2392. */
  2393. static void rcu_bind_gp_kthread(void)
  2394. {
  2395. if (!tick_nohz_full_enabled())
  2396. return;
  2397. housekeeping_affine(current, HK_FLAG_RCU);
  2398. }
  2399. /* Record the current task on dyntick-idle entry. */
  2400. static void rcu_dynticks_task_enter(void)
  2401. {
  2402. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2403. WRITE_ONCE(current->rcu_tasks_idle_cpu, smp_processor_id());
  2404. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2405. }
  2406. /* Record no current task on dyntick-idle exit. */
  2407. static void rcu_dynticks_task_exit(void)
  2408. {
  2409. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2410. WRITE_ONCE(current->rcu_tasks_idle_cpu, -1);
  2411. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2412. }