rcutree_plugin.h 69 KB

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