tree_plugin.h 80 KB

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