rcutree_plugin.h 75 KB

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