rcutree_plugin.h 62 KB

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