rcutree_plugin.h 64 KB

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