rcutree_plugin.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  3. * Internal non-public definitions that provide either classic
  4. * or preemptable 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. /*
  29. * Check the RCU kernel configuration parameters and print informative
  30. * messages about anything out of the ordinary. If you like #ifdef, you
  31. * will love this function.
  32. */
  33. static void __init rcu_bootup_announce_oddness(void)
  34. {
  35. #ifdef CONFIG_RCU_TRACE
  36. printk(KERN_INFO "\tRCU debugfs-based tracing is enabled.\n");
  37. #endif
  38. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  39. printk(KERN_INFO "\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  40. CONFIG_RCU_FANOUT);
  41. #endif
  42. #ifdef CONFIG_RCU_FANOUT_EXACT
  43. printk(KERN_INFO "\tHierarchical RCU autobalancing is disabled.\n");
  44. #endif
  45. #ifdef CONFIG_RCU_FAST_NO_HZ
  46. printk(KERN_INFO
  47. "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  48. #endif
  49. #ifdef CONFIG_PROVE_RCU
  50. printk(KERN_INFO "\tRCU lockdep checking is enabled.\n");
  51. #endif
  52. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  53. printk(KERN_INFO "\tRCU torture testing starts during boot.\n");
  54. #endif
  55. #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
  56. printk(KERN_INFO "\tVerbose stalled-CPUs detection is disabled.\n");
  57. #endif
  58. #if NUM_RCU_LVL_4 != 0
  59. printk(KERN_INFO "\tExperimental four-level hierarchy is enabled.\n");
  60. #endif
  61. }
  62. #ifdef CONFIG_TREE_PREEMPT_RCU
  63. struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
  64. DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
  65. static struct rcu_state *rcu_state = &rcu_preempt_state;
  66. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  67. /*
  68. * Tell them what RCU they are running.
  69. */
  70. static void __init rcu_bootup_announce(void)
  71. {
  72. printk(KERN_INFO "Preemptable hierarchical RCU implementation.\n");
  73. rcu_bootup_announce_oddness();
  74. }
  75. /*
  76. * Return the number of RCU-preempt batches processed thus far
  77. * for debug and statistics.
  78. */
  79. long rcu_batches_completed_preempt(void)
  80. {
  81. return rcu_preempt_state.completed;
  82. }
  83. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  84. /*
  85. * Return the number of RCU batches processed thus far for debug & stats.
  86. */
  87. long rcu_batches_completed(void)
  88. {
  89. return rcu_batches_completed_preempt();
  90. }
  91. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  92. /*
  93. * Force a quiescent state for preemptible RCU.
  94. */
  95. void rcu_force_quiescent_state(void)
  96. {
  97. force_quiescent_state(&rcu_preempt_state, 0);
  98. }
  99. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  100. /*
  101. * Record a preemptable-RCU quiescent state for the specified CPU. Note
  102. * that this just means that the task currently running on the CPU is
  103. * not in a quiescent state. There might be any number of tasks blocked
  104. * while in an RCU read-side critical section.
  105. *
  106. * Unlike the other rcu_*_qs() functions, callers to this function
  107. * must disable irqs in order to protect the assignment to
  108. * ->rcu_read_unlock_special.
  109. */
  110. static void rcu_preempt_qs(int cpu)
  111. {
  112. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  113. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  114. barrier();
  115. rdp->passed_quiesc = 1;
  116. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  117. }
  118. /*
  119. * We have entered the scheduler, and the current task might soon be
  120. * context-switched away from. If this task is in an RCU read-side
  121. * critical section, we will no longer be able to rely on the CPU to
  122. * record that fact, so we enqueue the task on the blkd_tasks list.
  123. * The task will dequeue itself when it exits the outermost enclosing
  124. * RCU read-side critical section. Therefore, the current grace period
  125. * cannot be permitted to complete until the blkd_tasks list entries
  126. * predating the current grace period drain, in other words, until
  127. * rnp->gp_tasks becomes NULL.
  128. *
  129. * Caller must disable preemption.
  130. */
  131. static void rcu_preempt_note_context_switch(int cpu)
  132. {
  133. struct task_struct *t = current;
  134. unsigned long flags;
  135. struct rcu_data *rdp;
  136. struct rcu_node *rnp;
  137. if (t->rcu_read_lock_nesting &&
  138. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  139. /* Possibly blocking in an RCU read-side critical section. */
  140. rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
  141. rnp = rdp->mynode;
  142. raw_spin_lock_irqsave(&rnp->lock, flags);
  143. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  144. t->rcu_blocked_node = rnp;
  145. /*
  146. * If this CPU has already checked in, then this task
  147. * will hold up the next grace period rather than the
  148. * current grace period. Queue the task accordingly.
  149. * If the task is queued for the current grace period
  150. * (i.e., this CPU has not yet passed through a quiescent
  151. * state for the current grace period), then as long
  152. * as that task remains queued, the current grace period
  153. * cannot end. Note that there is some uncertainty as
  154. * to exactly when the current grace period started.
  155. * We take a conservative approach, which can result
  156. * in unnecessarily waiting on tasks that started very
  157. * slightly after the current grace period began. C'est
  158. * la vie!!!
  159. *
  160. * But first, note that the current CPU must still be
  161. * on line!
  162. */
  163. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  164. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  165. if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
  166. list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
  167. rnp->gp_tasks = &t->rcu_node_entry;
  168. #ifdef CONFIG_RCU_BOOST
  169. if (rnp->boost_tasks != NULL)
  170. rnp->boost_tasks = rnp->gp_tasks;
  171. #endif /* #ifdef CONFIG_RCU_BOOST */
  172. } else {
  173. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  174. if (rnp->qsmask & rdp->grpmask)
  175. rnp->gp_tasks = &t->rcu_node_entry;
  176. }
  177. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  178. }
  179. /*
  180. * Either we were not in an RCU read-side critical section to
  181. * begin with, or we have now recorded that critical section
  182. * globally. Either way, we can now note a quiescent state
  183. * for this CPU. Again, if we were in an RCU read-side critical
  184. * section, and if that critical section was blocking the current
  185. * grace period, then the fact that the task has been enqueued
  186. * means that we continue to block the current grace period.
  187. */
  188. local_irq_save(flags);
  189. rcu_preempt_qs(cpu);
  190. local_irq_restore(flags);
  191. }
  192. /*
  193. * Tree-preemptable RCU implementation for rcu_read_lock().
  194. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  195. * if we block.
  196. */
  197. void __rcu_read_lock(void)
  198. {
  199. current->rcu_read_lock_nesting++;
  200. barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
  201. }
  202. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  203. /*
  204. * Check for preempted RCU readers blocking the current grace period
  205. * for the specified rcu_node structure. If the caller needs a reliable
  206. * answer, it must hold the rcu_node's ->lock.
  207. */
  208. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  209. {
  210. return rnp->gp_tasks != NULL;
  211. }
  212. /*
  213. * Record a quiescent state for all tasks that were previously queued
  214. * on the specified rcu_node structure and that were blocking the current
  215. * RCU grace period. The caller must hold the specified rnp->lock with
  216. * irqs disabled, and this lock is released upon return, but irqs remain
  217. * disabled.
  218. */
  219. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  220. __releases(rnp->lock)
  221. {
  222. unsigned long mask;
  223. struct rcu_node *rnp_p;
  224. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  225. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  226. return; /* Still need more quiescent states! */
  227. }
  228. rnp_p = rnp->parent;
  229. if (rnp_p == NULL) {
  230. /*
  231. * Either there is only one rcu_node in the tree,
  232. * or tasks were kicked up to root rcu_node due to
  233. * CPUs going offline.
  234. */
  235. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  236. return;
  237. }
  238. /* Report up the rest of the hierarchy. */
  239. mask = rnp->grpmask;
  240. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  241. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  242. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  243. }
  244. /*
  245. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  246. * returning NULL if at the end of the list.
  247. */
  248. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  249. struct rcu_node *rnp)
  250. {
  251. struct list_head *np;
  252. np = t->rcu_node_entry.next;
  253. if (np == &rnp->blkd_tasks)
  254. np = NULL;
  255. return np;
  256. }
  257. /*
  258. * Handle special cases during rcu_read_unlock(), such as needing to
  259. * notify RCU core processing or task having blocked during the RCU
  260. * read-side critical section.
  261. */
  262. static void rcu_read_unlock_special(struct task_struct *t)
  263. {
  264. int empty;
  265. int empty_exp;
  266. unsigned long flags;
  267. struct list_head *np;
  268. struct rcu_node *rnp;
  269. int special;
  270. /* NMI handlers cannot block and cannot safely manipulate state. */
  271. if (in_nmi())
  272. return;
  273. local_irq_save(flags);
  274. /*
  275. * If RCU core is waiting for this CPU to exit critical section,
  276. * let it know that we have done so.
  277. */
  278. special = t->rcu_read_unlock_special;
  279. if (special & RCU_READ_UNLOCK_NEED_QS) {
  280. rcu_preempt_qs(smp_processor_id());
  281. }
  282. /* Hardware IRQ handlers cannot block. */
  283. if (in_irq()) {
  284. local_irq_restore(flags);
  285. return;
  286. }
  287. /* Clean up if blocked during RCU read-side critical section. */
  288. if (special & RCU_READ_UNLOCK_BLOCKED) {
  289. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  290. /*
  291. * Remove this task from the list it blocked on. The
  292. * task can migrate while we acquire the lock, but at
  293. * most one time. So at most two passes through loop.
  294. */
  295. for (;;) {
  296. rnp = t->rcu_blocked_node;
  297. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  298. if (rnp == t->rcu_blocked_node)
  299. break;
  300. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  301. }
  302. empty = !rcu_preempt_blocked_readers_cgp(rnp);
  303. empty_exp = !rcu_preempted_readers_exp(rnp);
  304. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  305. np = rcu_next_node_entry(t, rnp);
  306. list_del_init(&t->rcu_node_entry);
  307. if (&t->rcu_node_entry == rnp->gp_tasks)
  308. rnp->gp_tasks = np;
  309. if (&t->rcu_node_entry == rnp->exp_tasks)
  310. rnp->exp_tasks = np;
  311. #ifdef CONFIG_RCU_BOOST
  312. if (&t->rcu_node_entry == rnp->boost_tasks)
  313. rnp->boost_tasks = np;
  314. #endif /* #ifdef CONFIG_RCU_BOOST */
  315. t->rcu_blocked_node = NULL;
  316. /*
  317. * If this was the last task on the current list, and if
  318. * we aren't waiting on any CPUs, report the quiescent state.
  319. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
  320. */
  321. if (empty)
  322. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  323. else
  324. rcu_report_unblock_qs_rnp(rnp, flags);
  325. #ifdef CONFIG_RCU_BOOST
  326. /* Unboost if we were boosted. */
  327. if (special & RCU_READ_UNLOCK_BOOSTED) {
  328. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
  329. rt_mutex_unlock(t->rcu_boost_mutex);
  330. t->rcu_boost_mutex = NULL;
  331. }
  332. #endif /* #ifdef CONFIG_RCU_BOOST */
  333. /*
  334. * If this was the last task on the expedited lists,
  335. * then we need to report up the rcu_node hierarchy.
  336. */
  337. if (!empty_exp && !rcu_preempted_readers_exp(rnp))
  338. rcu_report_exp_rnp(&rcu_preempt_state, rnp);
  339. } else {
  340. local_irq_restore(flags);
  341. }
  342. }
  343. /*
  344. * Tree-preemptable RCU implementation for rcu_read_unlock().
  345. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  346. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  347. * invoke rcu_read_unlock_special() to clean up after a context switch
  348. * in an RCU read-side critical section and other special cases.
  349. */
  350. void __rcu_read_unlock(void)
  351. {
  352. struct task_struct *t = current;
  353. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
  354. --t->rcu_read_lock_nesting;
  355. barrier(); /* decrement before load of ->rcu_read_unlock_special */
  356. if (t->rcu_read_lock_nesting == 0 &&
  357. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  358. rcu_read_unlock_special(t);
  359. #ifdef CONFIG_PROVE_LOCKING
  360. WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
  361. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  362. }
  363. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  364. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  365. /*
  366. * Dump detailed information for all tasks blocking the current RCU
  367. * grace period on the specified rcu_node structure.
  368. */
  369. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  370. {
  371. unsigned long flags;
  372. struct task_struct *t;
  373. if (!rcu_preempt_blocked_readers_cgp(rnp))
  374. return;
  375. raw_spin_lock_irqsave(&rnp->lock, flags);
  376. t = list_entry(rnp->gp_tasks,
  377. struct task_struct, rcu_node_entry);
  378. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  379. sched_show_task(t);
  380. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  381. }
  382. /*
  383. * Dump detailed information for all tasks blocking the current RCU
  384. * grace period.
  385. */
  386. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  387. {
  388. struct rcu_node *rnp = rcu_get_root(rsp);
  389. rcu_print_detail_task_stall_rnp(rnp);
  390. rcu_for_each_leaf_node(rsp, rnp)
  391. rcu_print_detail_task_stall_rnp(rnp);
  392. }
  393. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  394. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  395. {
  396. }
  397. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  398. /*
  399. * Scan the current list of tasks blocked within RCU read-side critical
  400. * sections, printing out the tid of each.
  401. */
  402. static void rcu_print_task_stall(struct rcu_node *rnp)
  403. {
  404. struct task_struct *t;
  405. if (!rcu_preempt_blocked_readers_cgp(rnp))
  406. return;
  407. t = list_entry(rnp->gp_tasks,
  408. struct task_struct, rcu_node_entry);
  409. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  410. printk(" P%d", t->pid);
  411. }
  412. /*
  413. * Suppress preemptible RCU's CPU stall warnings by pushing the
  414. * time of the next stall-warning message comfortably far into the
  415. * future.
  416. */
  417. static void rcu_preempt_stall_reset(void)
  418. {
  419. rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  420. }
  421. /*
  422. * Check that the list of blocked tasks for the newly completed grace
  423. * period is in fact empty. It is a serious bug to complete a grace
  424. * period that still has RCU readers blocked! This function must be
  425. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  426. * must be held by the caller.
  427. *
  428. * Also, if there are blocked tasks on the list, they automatically
  429. * block the newly created grace period, so set up ->gp_tasks accordingly.
  430. */
  431. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  432. {
  433. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  434. if (!list_empty(&rnp->blkd_tasks))
  435. rnp->gp_tasks = rnp->blkd_tasks.next;
  436. WARN_ON_ONCE(rnp->qsmask);
  437. }
  438. #ifdef CONFIG_HOTPLUG_CPU
  439. /*
  440. * Handle tasklist migration for case in which all CPUs covered by the
  441. * specified rcu_node have gone offline. Move them up to the root
  442. * rcu_node. The reason for not just moving them to the immediate
  443. * parent is to remove the need for rcu_read_unlock_special() to
  444. * make more than two attempts to acquire the target rcu_node's lock.
  445. * Returns true if there were tasks blocking the current RCU grace
  446. * period.
  447. *
  448. * Returns 1 if there was previously a task blocking the current grace
  449. * period on the specified rcu_node structure.
  450. *
  451. * The caller must hold rnp->lock with irqs disabled.
  452. */
  453. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  454. struct rcu_node *rnp,
  455. struct rcu_data *rdp)
  456. {
  457. struct list_head *lp;
  458. struct list_head *lp_root;
  459. int retval = 0;
  460. struct rcu_node *rnp_root = rcu_get_root(rsp);
  461. struct task_struct *t;
  462. if (rnp == rnp_root) {
  463. WARN_ONCE(1, "Last CPU thought to be offlined?");
  464. return 0; /* Shouldn't happen: at least one CPU online. */
  465. }
  466. /* If we are on an internal node, complain bitterly. */
  467. WARN_ON_ONCE(rnp != rdp->mynode);
  468. /*
  469. * Move tasks up to root rcu_node. Don't try to get fancy for
  470. * this corner-case operation -- just put this node's tasks
  471. * at the head of the root node's list, and update the root node's
  472. * ->gp_tasks and ->exp_tasks pointers to those of this node's,
  473. * if non-NULL. This might result in waiting for more tasks than
  474. * absolutely necessary, but this is a good performance/complexity
  475. * tradeoff.
  476. */
  477. if (rcu_preempt_blocked_readers_cgp(rnp))
  478. retval |= RCU_OFL_TASKS_NORM_GP;
  479. if (rcu_preempted_readers_exp(rnp))
  480. retval |= RCU_OFL_TASKS_EXP_GP;
  481. lp = &rnp->blkd_tasks;
  482. lp_root = &rnp_root->blkd_tasks;
  483. while (!list_empty(lp)) {
  484. t = list_entry(lp->next, typeof(*t), rcu_node_entry);
  485. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  486. list_del(&t->rcu_node_entry);
  487. t->rcu_blocked_node = rnp_root;
  488. list_add(&t->rcu_node_entry, lp_root);
  489. if (&t->rcu_node_entry == rnp->gp_tasks)
  490. rnp_root->gp_tasks = rnp->gp_tasks;
  491. if (&t->rcu_node_entry == rnp->exp_tasks)
  492. rnp_root->exp_tasks = rnp->exp_tasks;
  493. #ifdef CONFIG_RCU_BOOST
  494. if (&t->rcu_node_entry == rnp->boost_tasks)
  495. rnp_root->boost_tasks = rnp->boost_tasks;
  496. #endif /* #ifdef CONFIG_RCU_BOOST */
  497. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  498. }
  499. #ifdef CONFIG_RCU_BOOST
  500. /* In case root is being boosted and leaf is not. */
  501. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  502. if (rnp_root->boost_tasks != NULL &&
  503. rnp_root->boost_tasks != rnp_root->gp_tasks)
  504. rnp_root->boost_tasks = rnp_root->gp_tasks;
  505. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  506. #endif /* #ifdef CONFIG_RCU_BOOST */
  507. rnp->gp_tasks = NULL;
  508. rnp->exp_tasks = NULL;
  509. return retval;
  510. }
  511. /*
  512. * Do CPU-offline processing for preemptable RCU.
  513. */
  514. static void rcu_preempt_offline_cpu(int cpu)
  515. {
  516. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  517. }
  518. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  519. /*
  520. * Check for a quiescent state from the current CPU. When a task blocks,
  521. * the task is recorded in the corresponding CPU's rcu_node structure,
  522. * which is checked elsewhere.
  523. *
  524. * Caller must disable hard irqs.
  525. */
  526. static void rcu_preempt_check_callbacks(int cpu)
  527. {
  528. struct task_struct *t = current;
  529. if (t->rcu_read_lock_nesting == 0) {
  530. rcu_preempt_qs(cpu);
  531. return;
  532. }
  533. if (per_cpu(rcu_preempt_data, cpu).qs_pending)
  534. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  535. }
  536. /*
  537. * Process callbacks for preemptable RCU.
  538. */
  539. static void rcu_preempt_process_callbacks(void)
  540. {
  541. __rcu_process_callbacks(&rcu_preempt_state,
  542. &__get_cpu_var(rcu_preempt_data));
  543. }
  544. /*
  545. * Queue a preemptable-RCU callback for invocation after a grace period.
  546. */
  547. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  548. {
  549. __call_rcu(head, func, &rcu_preempt_state);
  550. }
  551. EXPORT_SYMBOL_GPL(call_rcu);
  552. /**
  553. * synchronize_rcu - wait until a grace period has elapsed.
  554. *
  555. * Control will return to the caller some time after a full grace
  556. * period has elapsed, in other words after all currently executing RCU
  557. * read-side critical sections have completed. Note, however, that
  558. * upon return from synchronize_rcu(), the caller might well be executing
  559. * concurrently with new RCU read-side critical sections that began while
  560. * synchronize_rcu() was waiting. RCU read-side critical sections are
  561. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  562. */
  563. void synchronize_rcu(void)
  564. {
  565. struct rcu_synchronize rcu;
  566. if (!rcu_scheduler_active)
  567. return;
  568. init_rcu_head_on_stack(&rcu.head);
  569. init_completion(&rcu.completion);
  570. /* Will wake me after RCU finished. */
  571. call_rcu(&rcu.head, wakeme_after_rcu);
  572. /* Wait for it. */
  573. wait_for_completion(&rcu.completion);
  574. destroy_rcu_head_on_stack(&rcu.head);
  575. }
  576. EXPORT_SYMBOL_GPL(synchronize_rcu);
  577. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  578. static long sync_rcu_preempt_exp_count;
  579. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  580. /*
  581. * Return non-zero if there are any tasks in RCU read-side critical
  582. * sections blocking the current preemptible-RCU expedited grace period.
  583. * If there is no preemptible-RCU expedited grace period currently in
  584. * progress, returns zero unconditionally.
  585. */
  586. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  587. {
  588. return rnp->exp_tasks != NULL;
  589. }
  590. /*
  591. * return non-zero if there is no RCU expedited grace period in progress
  592. * for the specified rcu_node structure, in other words, if all CPUs and
  593. * tasks covered by the specified rcu_node structure have done their bit
  594. * for the current expedited grace period. Works only for preemptible
  595. * RCU -- other RCU implementation use other means.
  596. *
  597. * Caller must hold sync_rcu_preempt_exp_mutex.
  598. */
  599. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  600. {
  601. return !rcu_preempted_readers_exp(rnp) &&
  602. ACCESS_ONCE(rnp->expmask) == 0;
  603. }
  604. /*
  605. * Report the exit from RCU read-side critical section for the last task
  606. * that queued itself during or before the current expedited preemptible-RCU
  607. * grace period. This event is reported either to the rcu_node structure on
  608. * which the task was queued or to one of that rcu_node structure's ancestors,
  609. * recursively up the tree. (Calm down, calm down, we do the recursion
  610. * iteratively!)
  611. *
  612. * Caller must hold sync_rcu_preempt_exp_mutex.
  613. */
  614. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  615. {
  616. unsigned long flags;
  617. unsigned long mask;
  618. raw_spin_lock_irqsave(&rnp->lock, flags);
  619. for (;;) {
  620. if (!sync_rcu_preempt_exp_done(rnp))
  621. break;
  622. if (rnp->parent == NULL) {
  623. wake_up(&sync_rcu_preempt_exp_wq);
  624. break;
  625. }
  626. mask = rnp->grpmask;
  627. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  628. rnp = rnp->parent;
  629. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  630. rnp->expmask &= ~mask;
  631. }
  632. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  633. }
  634. /*
  635. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  636. * grace period for the specified rcu_node structure. If there are no such
  637. * tasks, report it up the rcu_node hierarchy.
  638. *
  639. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  640. */
  641. static void
  642. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  643. {
  644. int must_wait = 0;
  645. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  646. if (!list_empty(&rnp->blkd_tasks)) {
  647. rnp->exp_tasks = rnp->blkd_tasks.next;
  648. rcu_initiate_boost(rnp);
  649. must_wait = 1;
  650. }
  651. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  652. if (!must_wait)
  653. rcu_report_exp_rnp(rsp, rnp);
  654. }
  655. /*
  656. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  657. * is to invoke synchronize_sched_expedited() to push all the tasks to
  658. * the ->blkd_tasks lists and wait for this list to drain.
  659. */
  660. void synchronize_rcu_expedited(void)
  661. {
  662. unsigned long flags;
  663. struct rcu_node *rnp;
  664. struct rcu_state *rsp = &rcu_preempt_state;
  665. long snap;
  666. int trycount = 0;
  667. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  668. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  669. smp_mb(); /* Above access cannot bleed into critical section. */
  670. /*
  671. * Acquire lock, falling back to synchronize_rcu() if too many
  672. * lock-acquisition failures. Of course, if someone does the
  673. * expedited grace period for us, just leave.
  674. */
  675. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  676. if (trycount++ < 10)
  677. udelay(trycount * num_online_cpus());
  678. else {
  679. synchronize_rcu();
  680. return;
  681. }
  682. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  683. goto mb_ret; /* Others did our work for us. */
  684. }
  685. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  686. goto unlock_mb_ret; /* Others did our work for us. */
  687. /* force all RCU readers onto ->blkd_tasks lists. */
  688. synchronize_sched_expedited();
  689. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  690. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  691. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  692. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  693. rnp->expmask = rnp->qsmaskinit;
  694. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  695. }
  696. /* Snapshot current state of ->blkd_tasks lists. */
  697. rcu_for_each_leaf_node(rsp, rnp)
  698. sync_rcu_preempt_exp_init(rsp, rnp);
  699. if (NUM_RCU_NODES > 1)
  700. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  701. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  702. /* Wait for snapshotted ->blkd_tasks lists to drain. */
  703. rnp = rcu_get_root(rsp);
  704. wait_event(sync_rcu_preempt_exp_wq,
  705. sync_rcu_preempt_exp_done(rnp));
  706. /* Clean up and exit. */
  707. smp_mb(); /* ensure expedited GP seen before counter increment. */
  708. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  709. unlock_mb_ret:
  710. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  711. mb_ret:
  712. smp_mb(); /* ensure subsequent action seen after grace period. */
  713. }
  714. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  715. /*
  716. * Check to see if there is any immediate preemptable-RCU-related work
  717. * to be done.
  718. */
  719. static int rcu_preempt_pending(int cpu)
  720. {
  721. return __rcu_pending(&rcu_preempt_state,
  722. &per_cpu(rcu_preempt_data, cpu));
  723. }
  724. /*
  725. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  726. */
  727. static int rcu_preempt_needs_cpu(int cpu)
  728. {
  729. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  730. }
  731. /**
  732. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  733. */
  734. void rcu_barrier(void)
  735. {
  736. _rcu_barrier(&rcu_preempt_state, call_rcu);
  737. }
  738. EXPORT_SYMBOL_GPL(rcu_barrier);
  739. /*
  740. * Initialize preemptable RCU's per-CPU data.
  741. */
  742. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  743. {
  744. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  745. }
  746. /*
  747. * Move preemptable RCU's callbacks from dying CPU to other online CPU.
  748. */
  749. static void rcu_preempt_send_cbs_to_online(void)
  750. {
  751. rcu_send_cbs_to_online(&rcu_preempt_state);
  752. }
  753. /*
  754. * Initialize preemptable RCU's state structures.
  755. */
  756. static void __init __rcu_init_preempt(void)
  757. {
  758. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  759. }
  760. /*
  761. * Check for a task exiting while in a preemptable-RCU read-side
  762. * critical section, clean up if so. No need to issue warnings,
  763. * as debug_check_no_locks_held() already does this if lockdep
  764. * is enabled.
  765. */
  766. void exit_rcu(void)
  767. {
  768. struct task_struct *t = current;
  769. if (t->rcu_read_lock_nesting == 0)
  770. return;
  771. t->rcu_read_lock_nesting = 1;
  772. rcu_read_unlock();
  773. }
  774. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  775. static struct rcu_state *rcu_state = &rcu_sched_state;
  776. /*
  777. * Tell them what RCU they are running.
  778. */
  779. static void __init rcu_bootup_announce(void)
  780. {
  781. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  782. rcu_bootup_announce_oddness();
  783. }
  784. /*
  785. * Return the number of RCU batches processed thus far for debug & stats.
  786. */
  787. long rcu_batches_completed(void)
  788. {
  789. return rcu_batches_completed_sched();
  790. }
  791. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  792. /*
  793. * Force a quiescent state for RCU, which, because there is no preemptible
  794. * RCU, becomes the same as rcu-sched.
  795. */
  796. void rcu_force_quiescent_state(void)
  797. {
  798. rcu_sched_force_quiescent_state();
  799. }
  800. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  801. /*
  802. * Because preemptable RCU does not exist, we never have to check for
  803. * CPUs being in quiescent states.
  804. */
  805. static void rcu_preempt_note_context_switch(int cpu)
  806. {
  807. }
  808. /*
  809. * Because preemptable RCU does not exist, there are never any preempted
  810. * RCU readers.
  811. */
  812. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  813. {
  814. return 0;
  815. }
  816. #ifdef CONFIG_HOTPLUG_CPU
  817. /* Because preemptible RCU does not exist, no quieting of tasks. */
  818. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  819. {
  820. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  821. }
  822. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  823. /*
  824. * Because preemptable RCU does not exist, we never have to check for
  825. * tasks blocked within RCU read-side critical sections.
  826. */
  827. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  828. {
  829. }
  830. /*
  831. * Because preemptable RCU does not exist, we never have to check for
  832. * tasks blocked within RCU read-side critical sections.
  833. */
  834. static void rcu_print_task_stall(struct rcu_node *rnp)
  835. {
  836. }
  837. /*
  838. * Because preemptible RCU does not exist, there is no need to suppress
  839. * its CPU stall warnings.
  840. */
  841. static void rcu_preempt_stall_reset(void)
  842. {
  843. }
  844. /*
  845. * Because there is no preemptable RCU, there can be no readers blocked,
  846. * so there is no need to check for blocked tasks. So check only for
  847. * bogus qsmask values.
  848. */
  849. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  850. {
  851. WARN_ON_ONCE(rnp->qsmask);
  852. }
  853. #ifdef CONFIG_HOTPLUG_CPU
  854. /*
  855. * Because preemptable RCU does not exist, it never needs to migrate
  856. * tasks that were blocked within RCU read-side critical sections, and
  857. * such non-existent tasks cannot possibly have been blocking the current
  858. * grace period.
  859. */
  860. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  861. struct rcu_node *rnp,
  862. struct rcu_data *rdp)
  863. {
  864. return 0;
  865. }
  866. /*
  867. * Because preemptable RCU does not exist, it never needs CPU-offline
  868. * processing.
  869. */
  870. static void rcu_preempt_offline_cpu(int cpu)
  871. {
  872. }
  873. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  874. /*
  875. * Because preemptable RCU does not exist, it never has any callbacks
  876. * to check.
  877. */
  878. static void rcu_preempt_check_callbacks(int cpu)
  879. {
  880. }
  881. /*
  882. * Because preemptable RCU does not exist, it never has any callbacks
  883. * to process.
  884. */
  885. static void rcu_preempt_process_callbacks(void)
  886. {
  887. }
  888. /*
  889. * Wait for an rcu-preempt grace period, but make it happen quickly.
  890. * But because preemptable RCU does not exist, map to rcu-sched.
  891. */
  892. void synchronize_rcu_expedited(void)
  893. {
  894. synchronize_sched_expedited();
  895. }
  896. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  897. #ifdef CONFIG_HOTPLUG_CPU
  898. /*
  899. * Because preemptable RCU does not exist, there is never any need to
  900. * report on tasks preempted in RCU read-side critical sections during
  901. * expedited RCU grace periods.
  902. */
  903. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  904. {
  905. return;
  906. }
  907. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  908. /*
  909. * Because preemptable RCU does not exist, it never has any work to do.
  910. */
  911. static int rcu_preempt_pending(int cpu)
  912. {
  913. return 0;
  914. }
  915. /*
  916. * Because preemptable RCU does not exist, it never needs any CPU.
  917. */
  918. static int rcu_preempt_needs_cpu(int cpu)
  919. {
  920. return 0;
  921. }
  922. /*
  923. * Because preemptable RCU does not exist, rcu_barrier() is just
  924. * another name for rcu_barrier_sched().
  925. */
  926. void rcu_barrier(void)
  927. {
  928. rcu_barrier_sched();
  929. }
  930. EXPORT_SYMBOL_GPL(rcu_barrier);
  931. /*
  932. * Because preemptable RCU does not exist, there is no per-CPU
  933. * data to initialize.
  934. */
  935. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  936. {
  937. }
  938. /*
  939. * Because there is no preemptable RCU, there are no callbacks to move.
  940. */
  941. static void rcu_preempt_send_cbs_to_online(void)
  942. {
  943. }
  944. /*
  945. * Because preemptable RCU does not exist, it need not be initialized.
  946. */
  947. static void __init __rcu_init_preempt(void)
  948. {
  949. }
  950. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  951. #ifdef CONFIG_RCU_BOOST
  952. #include "rtmutex_common.h"
  953. #ifdef CONFIG_RCU_TRACE
  954. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  955. {
  956. if (list_empty(&rnp->blkd_tasks))
  957. rnp->n_balk_blkd_tasks++;
  958. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  959. rnp->n_balk_exp_gp_tasks++;
  960. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  961. rnp->n_balk_boost_tasks++;
  962. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  963. rnp->n_balk_notblocked++;
  964. else if (rnp->gp_tasks != NULL &&
  965. ULONG_CMP_GE(jiffies, rnp->boost_time))
  966. rnp->n_balk_notyet++;
  967. else
  968. rnp->n_balk_nos++;
  969. }
  970. #else /* #ifdef CONFIG_RCU_TRACE */
  971. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  972. {
  973. }
  974. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  975. /*
  976. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  977. * or ->boost_tasks, advancing the pointer to the next task in the
  978. * ->blkd_tasks list.
  979. *
  980. * Note that irqs must be enabled: boosting the task can block.
  981. * Returns 1 if there are more tasks needing to be boosted.
  982. */
  983. static int rcu_boost(struct rcu_node *rnp)
  984. {
  985. unsigned long flags;
  986. struct rt_mutex mtx;
  987. struct task_struct *t;
  988. struct list_head *tb;
  989. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
  990. return 0; /* Nothing left to boost. */
  991. raw_spin_lock_irqsave(&rnp->lock, flags);
  992. /*
  993. * Recheck under the lock: all tasks in need of boosting
  994. * might exit their RCU read-side critical sections on their own.
  995. */
  996. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  997. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  998. return 0;
  999. }
  1000. /*
  1001. * Preferentially boost tasks blocking expedited grace periods.
  1002. * This cannot starve the normal grace periods because a second
  1003. * expedited grace period must boost all blocked tasks, including
  1004. * those blocking the pre-existing normal grace period.
  1005. */
  1006. if (rnp->exp_tasks != NULL) {
  1007. tb = rnp->exp_tasks;
  1008. rnp->n_exp_boosts++;
  1009. } else {
  1010. tb = rnp->boost_tasks;
  1011. rnp->n_normal_boosts++;
  1012. }
  1013. rnp->n_tasks_boosted++;
  1014. /*
  1015. * We boost task t by manufacturing an rt_mutex that appears to
  1016. * be held by task t. We leave a pointer to that rt_mutex where
  1017. * task t can find it, and task t will release the mutex when it
  1018. * exits its outermost RCU read-side critical section. Then
  1019. * simply acquiring this artificial rt_mutex will boost task
  1020. * t's priority. (Thanks to tglx for suggesting this approach!)
  1021. *
  1022. * Note that task t must acquire rnp->lock to remove itself from
  1023. * the ->blkd_tasks list, which it will do from exit() if from
  1024. * nowhere else. We therefore are guaranteed that task t will
  1025. * stay around at least until we drop rnp->lock. Note that
  1026. * rnp->lock also resolves races between our priority boosting
  1027. * and task t's exiting its outermost RCU read-side critical
  1028. * section.
  1029. */
  1030. t = container_of(tb, struct task_struct, rcu_node_entry);
  1031. rt_mutex_init_proxy_locked(&mtx, t);
  1032. t->rcu_boost_mutex = &mtx;
  1033. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED;
  1034. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1035. rt_mutex_lock(&mtx); /* Side effect: boosts task t's priority. */
  1036. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  1037. return rnp->exp_tasks != NULL || rnp->boost_tasks != NULL;
  1038. }
  1039. /*
  1040. * Timer handler to initiate waking up of boost kthreads that
  1041. * have yielded the CPU due to excessive numbers of tasks to
  1042. * boost. We wake up the per-rcu_node kthread, which in turn
  1043. * will wake up the booster kthread.
  1044. */
  1045. static void rcu_boost_kthread_timer(unsigned long arg)
  1046. {
  1047. unsigned long flags;
  1048. struct rcu_node *rnp = (struct rcu_node *)arg;
  1049. raw_spin_lock_irqsave(&rnp->lock, flags);
  1050. invoke_rcu_node_kthread(rnp);
  1051. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1052. }
  1053. /*
  1054. * Priority-boosting kthread. One per leaf rcu_node and one for the
  1055. * root rcu_node.
  1056. */
  1057. static int rcu_boost_kthread(void *arg)
  1058. {
  1059. struct rcu_node *rnp = (struct rcu_node *)arg;
  1060. int spincnt = 0;
  1061. int more2boost;
  1062. for (;;) {
  1063. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1064. wait_event_interruptible(rnp->boost_wq, rnp->boost_tasks ||
  1065. rnp->exp_tasks ||
  1066. kthread_should_stop());
  1067. if (kthread_should_stop())
  1068. break;
  1069. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1070. more2boost = rcu_boost(rnp);
  1071. if (more2boost)
  1072. spincnt++;
  1073. else
  1074. spincnt = 0;
  1075. if (spincnt > 10) {
  1076. rcu_yield(rcu_boost_kthread_timer, (unsigned long)rnp);
  1077. spincnt = 0;
  1078. }
  1079. }
  1080. rnp->boost_kthread_status = RCU_KTHREAD_STOPPED;
  1081. return 0;
  1082. }
  1083. /*
  1084. * Check to see if it is time to start boosting RCU readers that are
  1085. * blocking the current grace period, and, if so, tell the per-rcu_node
  1086. * kthread to start boosting them. If there is an expedited grace
  1087. * period in progress, it is always time to boost.
  1088. *
  1089. * The caller must hold rnp->lock.
  1090. */
  1091. static void rcu_initiate_boost(struct rcu_node *rnp)
  1092. {
  1093. struct task_struct *t;
  1094. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1095. rnp->n_balk_exp_gp_tasks++;
  1096. return;
  1097. }
  1098. if (rnp->exp_tasks != NULL ||
  1099. (rnp->gp_tasks != NULL &&
  1100. rnp->boost_tasks == NULL &&
  1101. rnp->qsmask == 0 &&
  1102. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1103. if (rnp->exp_tasks == NULL)
  1104. rnp->boost_tasks = rnp->gp_tasks;
  1105. t = rnp->boost_kthread_task;
  1106. if (t != NULL)
  1107. wake_up_process(t);
  1108. } else
  1109. rcu_initiate_boost_trace(rnp);
  1110. }
  1111. /*
  1112. * Set the affinity of the boost kthread. The CPU-hotplug locks are
  1113. * held, so no one should be messing with the existence of the boost
  1114. * kthread.
  1115. */
  1116. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
  1117. cpumask_var_t cm)
  1118. {
  1119. struct task_struct *t;
  1120. t = rnp->boost_kthread_task;
  1121. if (t != NULL)
  1122. set_cpus_allowed_ptr(rnp->boost_kthread_task, cm);
  1123. }
  1124. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1125. /*
  1126. * Do priority-boost accounting for the start of a new grace period.
  1127. */
  1128. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1129. {
  1130. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1131. }
  1132. /*
  1133. * Initialize the RCU-boost waitqueue.
  1134. */
  1135. static void __init rcu_init_boost_waitqueue(struct rcu_node *rnp)
  1136. {
  1137. init_waitqueue_head(&rnp->boost_wq);
  1138. }
  1139. /*
  1140. * Create an RCU-boost kthread for the specified node if one does not
  1141. * already exist. We only create this kthread for preemptible RCU.
  1142. * Returns zero if all is well, a negated errno otherwise.
  1143. */
  1144. static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1145. struct rcu_node *rnp,
  1146. int rnp_index)
  1147. {
  1148. unsigned long flags;
  1149. struct sched_param sp;
  1150. struct task_struct *t;
  1151. if (&rcu_preempt_state != rsp)
  1152. return 0;
  1153. if (rnp->boost_kthread_task != NULL)
  1154. return 0;
  1155. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1156. "rcub%d", rnp_index);
  1157. if (IS_ERR(t))
  1158. return PTR_ERR(t);
  1159. raw_spin_lock_irqsave(&rnp->lock, flags);
  1160. rnp->boost_kthread_task = t;
  1161. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1162. wake_up_process(t);
  1163. sp.sched_priority = RCU_KTHREAD_PRIO;
  1164. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1165. return 0;
  1166. }
  1167. #ifdef CONFIG_HOTPLUG_CPU
  1168. static void rcu_stop_boost_kthread(struct rcu_node *rnp)
  1169. {
  1170. unsigned long flags;
  1171. struct task_struct *t;
  1172. raw_spin_lock_irqsave(&rnp->lock, flags);
  1173. t = rnp->boost_kthread_task;
  1174. rnp->boost_kthread_task = NULL;
  1175. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1176. if (t != NULL)
  1177. kthread_stop(t);
  1178. }
  1179. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  1180. #else /* #ifdef CONFIG_RCU_BOOST */
  1181. static void rcu_initiate_boost(struct rcu_node *rnp)
  1182. {
  1183. }
  1184. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
  1185. cpumask_var_t cm)
  1186. {
  1187. }
  1188. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1189. {
  1190. }
  1191. static void __init rcu_init_boost_waitqueue(struct rcu_node *rnp)
  1192. {
  1193. }
  1194. static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1195. struct rcu_node *rnp,
  1196. int rnp_index)
  1197. {
  1198. return 0;
  1199. }
  1200. #ifdef CONFIG_HOTPLUG_CPU
  1201. static void rcu_stop_boost_kthread(struct rcu_node *rnp)
  1202. {
  1203. }
  1204. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  1205. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1206. #ifndef CONFIG_SMP
  1207. void synchronize_sched_expedited(void)
  1208. {
  1209. cond_resched();
  1210. }
  1211. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  1212. #else /* #ifndef CONFIG_SMP */
  1213. static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
  1214. static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
  1215. static int synchronize_sched_expedited_cpu_stop(void *data)
  1216. {
  1217. /*
  1218. * There must be a full memory barrier on each affected CPU
  1219. * between the time that try_stop_cpus() is called and the
  1220. * time that it returns.
  1221. *
  1222. * In the current initial implementation of cpu_stop, the
  1223. * above condition is already met when the control reaches
  1224. * this point and the following smp_mb() is not strictly
  1225. * necessary. Do smp_mb() anyway for documentation and
  1226. * robustness against future implementation changes.
  1227. */
  1228. smp_mb(); /* See above comment block. */
  1229. return 0;
  1230. }
  1231. /*
  1232. * Wait for an rcu-sched grace period to elapse, but use "big hammer"
  1233. * approach to force grace period to end quickly. This consumes
  1234. * significant time on all CPUs, and is thus not recommended for
  1235. * any sort of common-case code.
  1236. *
  1237. * Note that it is illegal to call this function while holding any
  1238. * lock that is acquired by a CPU-hotplug notifier. Failing to
  1239. * observe this restriction will result in deadlock.
  1240. *
  1241. * This implementation can be thought of as an application of ticket
  1242. * locking to RCU, with sync_sched_expedited_started and
  1243. * sync_sched_expedited_done taking on the roles of the halves
  1244. * of the ticket-lock word. Each task atomically increments
  1245. * sync_sched_expedited_started upon entry, snapshotting the old value,
  1246. * then attempts to stop all the CPUs. If this succeeds, then each
  1247. * CPU will have executed a context switch, resulting in an RCU-sched
  1248. * grace period. We are then done, so we use atomic_cmpxchg() to
  1249. * update sync_sched_expedited_done to match our snapshot -- but
  1250. * only if someone else has not already advanced past our snapshot.
  1251. *
  1252. * On the other hand, if try_stop_cpus() fails, we check the value
  1253. * of sync_sched_expedited_done. If it has advanced past our
  1254. * initial snapshot, then someone else must have forced a grace period
  1255. * some time after we took our snapshot. In this case, our work is
  1256. * done for us, and we can simply return. Otherwise, we try again,
  1257. * but keep our initial snapshot for purposes of checking for someone
  1258. * doing our work for us.
  1259. *
  1260. * If we fail too many times in a row, we fall back to synchronize_sched().
  1261. */
  1262. void synchronize_sched_expedited(void)
  1263. {
  1264. int firstsnap, s, snap, trycount = 0;
  1265. /* Note that atomic_inc_return() implies full memory barrier. */
  1266. firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
  1267. get_online_cpus();
  1268. /*
  1269. * Each pass through the following loop attempts to force a
  1270. * context switch on each CPU.
  1271. */
  1272. while (try_stop_cpus(cpu_online_mask,
  1273. synchronize_sched_expedited_cpu_stop,
  1274. NULL) == -EAGAIN) {
  1275. put_online_cpus();
  1276. /* No joy, try again later. Or just synchronize_sched(). */
  1277. if (trycount++ < 10)
  1278. udelay(trycount * num_online_cpus());
  1279. else {
  1280. synchronize_sched();
  1281. return;
  1282. }
  1283. /* Check to see if someone else did our work for us. */
  1284. s = atomic_read(&sync_sched_expedited_done);
  1285. if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
  1286. smp_mb(); /* ensure test happens before caller kfree */
  1287. return;
  1288. }
  1289. /*
  1290. * Refetching sync_sched_expedited_started allows later
  1291. * callers to piggyback on our grace period. We subtract
  1292. * 1 to get the same token that the last incrementer got.
  1293. * We retry after they started, so our grace period works
  1294. * for them, and they started after our first try, so their
  1295. * grace period works for us.
  1296. */
  1297. get_online_cpus();
  1298. snap = atomic_read(&sync_sched_expedited_started) - 1;
  1299. smp_mb(); /* ensure read is before try_stop_cpus(). */
  1300. }
  1301. /*
  1302. * Everyone up to our most recent fetch is covered by our grace
  1303. * period. Update the counter, but only if our work is still
  1304. * relevant -- which it won't be if someone who started later
  1305. * than we did beat us to the punch.
  1306. */
  1307. do {
  1308. s = atomic_read(&sync_sched_expedited_done);
  1309. if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
  1310. smp_mb(); /* ensure test happens before caller kfree */
  1311. break;
  1312. }
  1313. } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
  1314. put_online_cpus();
  1315. }
  1316. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  1317. #endif /* #else #ifndef CONFIG_SMP */
  1318. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1319. /*
  1320. * Check to see if any future RCU-related work will need to be done
  1321. * by the current CPU, even if none need be done immediately, returning
  1322. * 1 if so. This function is part of the RCU implementation; it is -not-
  1323. * an exported member of the RCU API.
  1324. *
  1325. * Because we have preemptible RCU, just check whether this CPU needs
  1326. * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
  1327. * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
  1328. */
  1329. int rcu_needs_cpu(int cpu)
  1330. {
  1331. return rcu_needs_cpu_quick_check(cpu);
  1332. }
  1333. /*
  1334. * Check to see if we need to continue a callback-flush operations to
  1335. * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
  1336. * entry is not configured, so we never do need to.
  1337. */
  1338. static void rcu_needs_cpu_flush(void)
  1339. {
  1340. }
  1341. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1342. #define RCU_NEEDS_CPU_FLUSHES 5
  1343. static DEFINE_PER_CPU(int, rcu_dyntick_drain);
  1344. static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
  1345. /*
  1346. * Check to see if any future RCU-related work will need to be done
  1347. * by the current CPU, even if none need be done immediately, returning
  1348. * 1 if so. This function is part of the RCU implementation; it is -not-
  1349. * an exported member of the RCU API.
  1350. *
  1351. * Because we are not supporting preemptible RCU, attempt to accelerate
  1352. * any current grace periods so that RCU no longer needs this CPU, but
  1353. * only if all other CPUs are already in dynticks-idle mode. This will
  1354. * allow the CPU cores to be powered down immediately, as opposed to after
  1355. * waiting many milliseconds for grace periods to elapse.
  1356. *
  1357. * Because it is not legal to invoke rcu_process_callbacks() with irqs
  1358. * disabled, we do one pass of force_quiescent_state(), then do a
  1359. * invoke_rcu_cpu_kthread() to cause rcu_process_callbacks() to be invoked
  1360. * later. The per-cpu rcu_dyntick_drain variable controls the sequencing.
  1361. */
  1362. int rcu_needs_cpu(int cpu)
  1363. {
  1364. int c = 0;
  1365. int snap;
  1366. int thatcpu;
  1367. /* Check for being in the holdoff period. */
  1368. if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
  1369. return rcu_needs_cpu_quick_check(cpu);
  1370. /* Don't bother unless we are the last non-dyntick-idle CPU. */
  1371. for_each_online_cpu(thatcpu) {
  1372. if (thatcpu == cpu)
  1373. continue;
  1374. snap = atomic_add_return(0, &per_cpu(rcu_dynticks,
  1375. thatcpu).dynticks);
  1376. smp_mb(); /* Order sampling of snap with end of grace period. */
  1377. if ((snap & 0x1) != 0) {
  1378. per_cpu(rcu_dyntick_drain, cpu) = 0;
  1379. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
  1380. return rcu_needs_cpu_quick_check(cpu);
  1381. }
  1382. }
  1383. /* Check and update the rcu_dyntick_drain sequencing. */
  1384. if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  1385. /* First time through, initialize the counter. */
  1386. per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
  1387. } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  1388. /* We have hit the limit, so time to give up. */
  1389. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
  1390. return rcu_needs_cpu_quick_check(cpu);
  1391. }
  1392. /* Do one step pushing remaining RCU callbacks through. */
  1393. if (per_cpu(rcu_sched_data, cpu).nxtlist) {
  1394. rcu_sched_qs(cpu);
  1395. force_quiescent_state(&rcu_sched_state, 0);
  1396. c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
  1397. }
  1398. if (per_cpu(rcu_bh_data, cpu).nxtlist) {
  1399. rcu_bh_qs(cpu);
  1400. force_quiescent_state(&rcu_bh_state, 0);
  1401. c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
  1402. }
  1403. /* If RCU callbacks are still pending, RCU still needs this CPU. */
  1404. if (c)
  1405. invoke_rcu_cpu_kthread();
  1406. return c;
  1407. }
  1408. /*
  1409. * Check to see if we need to continue a callback-flush operations to
  1410. * allow the last CPU to enter dyntick-idle mode.
  1411. */
  1412. static void rcu_needs_cpu_flush(void)
  1413. {
  1414. int cpu = smp_processor_id();
  1415. unsigned long flags;
  1416. if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
  1417. return;
  1418. local_irq_save(flags);
  1419. (void)rcu_needs_cpu(cpu);
  1420. local_irq_restore(flags);
  1421. }
  1422. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */