rcutree_plugin.h 68 KB

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