tree_plugin.h 80 KB

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