rcutree_plugin.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. /*
  28. * Check the RCU kernel configuration parameters and print informative
  29. * messages about anything out of the ordinary. If you like #ifdef, you
  30. * will love this function.
  31. */
  32. static void __init rcu_bootup_announce_oddness(void)
  33. {
  34. #ifdef CONFIG_RCU_TRACE
  35. printk(KERN_INFO "\tRCU debugfs-based tracing is enabled.\n");
  36. #endif
  37. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  38. printk(KERN_INFO "\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  39. CONFIG_RCU_FANOUT);
  40. #endif
  41. #ifdef CONFIG_RCU_FANOUT_EXACT
  42. printk(KERN_INFO "\tHierarchical RCU autobalancing is disabled.\n");
  43. #endif
  44. #ifdef CONFIG_RCU_FAST_NO_HZ
  45. printk(KERN_INFO
  46. "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  47. #endif
  48. #ifdef CONFIG_PROVE_RCU
  49. printk(KERN_INFO "\tRCU lockdep checking is enabled.\n");
  50. #endif
  51. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  52. printk(KERN_INFO "\tRCU torture testing starts during boot.\n");
  53. #endif
  54. #ifndef CONFIG_RCU_CPU_STALL_DETECTOR
  55. printk(KERN_INFO
  56. "\tRCU-based detection of stalled CPUs is disabled.\n");
  57. #endif
  58. #ifndef CONFIG_RCU_CPU_STALL_VERBOSE
  59. printk(KERN_INFO "\tVerbose stalled-CPUs detection is disabled.\n");
  60. #endif
  61. #if NUM_RCU_LVL_4 != 0
  62. printk(KERN_INFO "\tExperimental four-level hierarchy is enabled.\n");
  63. #endif
  64. }
  65. #ifdef CONFIG_TREE_PREEMPT_RCU
  66. struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
  67. DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
  68. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  69. /*
  70. * Tell them what RCU they are running.
  71. */
  72. static void __init rcu_bootup_announce(void)
  73. {
  74. printk(KERN_INFO "Preemptable hierarchical RCU implementation.\n");
  75. rcu_bootup_announce_oddness();
  76. }
  77. /*
  78. * Return the number of RCU-preempt batches processed thus far
  79. * for debug and statistics.
  80. */
  81. long rcu_batches_completed_preempt(void)
  82. {
  83. return rcu_preempt_state.completed;
  84. }
  85. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  86. /*
  87. * Return the number of RCU batches processed thus far for debug & stats.
  88. */
  89. long rcu_batches_completed(void)
  90. {
  91. return rcu_batches_completed_preempt();
  92. }
  93. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  94. /*
  95. * Force a quiescent state for preemptible RCU.
  96. */
  97. void rcu_force_quiescent_state(void)
  98. {
  99. force_quiescent_state(&rcu_preempt_state, 0);
  100. }
  101. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  102. /*
  103. * Record a preemptable-RCU quiescent state for the specified CPU. Note
  104. * that this just means that the task currently running on the CPU is
  105. * not in a quiescent state. There might be any number of tasks blocked
  106. * while in an RCU read-side critical section.
  107. *
  108. * Unlike the other rcu_*_qs() functions, callers to this function
  109. * must disable irqs in order to protect the assignment to
  110. * ->rcu_read_unlock_special.
  111. */
  112. static void rcu_preempt_qs(int cpu)
  113. {
  114. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  115. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  116. barrier();
  117. rdp->passed_quiesc = 1;
  118. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  119. }
  120. /*
  121. * We have entered the scheduler, and the current task might soon be
  122. * context-switched away from. If this task is in an RCU read-side
  123. * critical section, we will no longer be able to rely on the CPU to
  124. * record that fact, so we enqueue the task on the appropriate entry
  125. * of the blocked_tasks[] array. The task will dequeue itself when
  126. * it exits the outermost enclosing RCU read-side critical section.
  127. * Therefore, the current grace period cannot be permitted to complete
  128. * until the blocked_tasks[] entry indexed by the low-order bit of
  129. * rnp->gpnum empties.
  130. *
  131. * Caller must disable preemption.
  132. */
  133. static void rcu_preempt_note_context_switch(int cpu)
  134. {
  135. struct task_struct *t = current;
  136. unsigned long flags;
  137. int phase;
  138. struct rcu_data *rdp;
  139. struct rcu_node *rnp;
  140. if (t->rcu_read_lock_nesting &&
  141. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  142. /* Possibly blocking in an RCU read-side critical section. */
  143. rdp = rcu_preempt_state.rda[cpu];
  144. rnp = rdp->mynode;
  145. raw_spin_lock_irqsave(&rnp->lock, flags);
  146. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  147. t->rcu_blocked_node = rnp;
  148. /*
  149. * If this CPU has already checked in, then this task
  150. * will hold up the next grace period rather than the
  151. * current grace period. Queue the task accordingly.
  152. * If the task is queued for the current grace period
  153. * (i.e., this CPU has not yet passed through a quiescent
  154. * state for the current grace period), then as long
  155. * as that task remains queued, the current grace period
  156. * cannot end.
  157. *
  158. * But first, note that the current CPU must still be
  159. * on line!
  160. */
  161. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  162. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  163. phase = (rnp->gpnum + !(rnp->qsmask & rdp->grpmask)) & 0x1;
  164. list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
  165. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  166. }
  167. /*
  168. * Either we were not in an RCU read-side critical section to
  169. * begin with, or we have now recorded that critical section
  170. * globally. Either way, we can now note a quiescent state
  171. * for this CPU. Again, if we were in an RCU read-side critical
  172. * section, and if that critical section was blocking the current
  173. * grace period, then the fact that the task has been enqueued
  174. * means that we continue to block the current grace period.
  175. */
  176. local_irq_save(flags);
  177. rcu_preempt_qs(cpu);
  178. local_irq_restore(flags);
  179. }
  180. /*
  181. * Tree-preemptable RCU implementation for rcu_read_lock().
  182. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  183. * if we block.
  184. */
  185. void __rcu_read_lock(void)
  186. {
  187. ACCESS_ONCE(current->rcu_read_lock_nesting)++;
  188. barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
  189. }
  190. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  191. /*
  192. * Check for preempted RCU readers blocking the current grace period
  193. * for the specified rcu_node structure. If the caller needs a reliable
  194. * answer, it must hold the rcu_node's ->lock.
  195. */
  196. static int rcu_preempted_readers(struct rcu_node *rnp)
  197. {
  198. int phase = rnp->gpnum & 0x1;
  199. return !list_empty(&rnp->blocked_tasks[phase]) ||
  200. !list_empty(&rnp->blocked_tasks[phase + 2]);
  201. }
  202. /*
  203. * Record a quiescent state for all tasks that were previously queued
  204. * on the specified rcu_node structure and that were blocking the current
  205. * RCU grace period. The caller must hold the specified rnp->lock with
  206. * irqs disabled, and this lock is released upon return, but irqs remain
  207. * disabled.
  208. */
  209. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  210. __releases(rnp->lock)
  211. {
  212. unsigned long mask;
  213. struct rcu_node *rnp_p;
  214. if (rnp->qsmask != 0 || rcu_preempted_readers(rnp)) {
  215. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  216. return; /* Still need more quiescent states! */
  217. }
  218. rnp_p = rnp->parent;
  219. if (rnp_p == NULL) {
  220. /*
  221. * Either there is only one rcu_node in the tree,
  222. * or tasks were kicked up to root rcu_node due to
  223. * CPUs going offline.
  224. */
  225. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  226. return;
  227. }
  228. /* Report up the rest of the hierarchy. */
  229. mask = rnp->grpmask;
  230. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  231. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  232. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  233. }
  234. /*
  235. * Handle special cases during rcu_read_unlock(), such as needing to
  236. * notify RCU core processing or task having blocked during the RCU
  237. * read-side critical section.
  238. */
  239. static void rcu_read_unlock_special(struct task_struct *t)
  240. {
  241. int empty;
  242. int empty_exp;
  243. unsigned long flags;
  244. struct rcu_node *rnp;
  245. int special;
  246. /* NMI handlers cannot block and cannot safely manipulate state. */
  247. if (in_nmi())
  248. return;
  249. local_irq_save(flags);
  250. /*
  251. * If RCU core is waiting for this CPU to exit critical section,
  252. * let it know that we have done so.
  253. */
  254. special = t->rcu_read_unlock_special;
  255. if (special & RCU_READ_UNLOCK_NEED_QS) {
  256. rcu_preempt_qs(smp_processor_id());
  257. }
  258. /* Hardware IRQ handlers cannot block. */
  259. if (in_irq()) {
  260. local_irq_restore(flags);
  261. return;
  262. }
  263. /* Clean up if blocked during RCU read-side critical section. */
  264. if (special & RCU_READ_UNLOCK_BLOCKED) {
  265. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  266. /*
  267. * Remove this task from the list it blocked on. The
  268. * task can migrate while we acquire the lock, but at
  269. * most one time. So at most two passes through loop.
  270. */
  271. for (;;) {
  272. rnp = t->rcu_blocked_node;
  273. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  274. if (rnp == t->rcu_blocked_node)
  275. break;
  276. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  277. }
  278. empty = !rcu_preempted_readers(rnp);
  279. empty_exp = !rcu_preempted_readers_exp(rnp);
  280. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  281. list_del_init(&t->rcu_node_entry);
  282. t->rcu_blocked_node = NULL;
  283. /*
  284. * If this was the last task on the current list, and if
  285. * we aren't waiting on any CPUs, report the quiescent state.
  286. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
  287. */
  288. if (empty)
  289. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  290. else
  291. rcu_report_unblock_qs_rnp(rnp, flags);
  292. /*
  293. * If this was the last task on the expedited lists,
  294. * then we need to report up the rcu_node hierarchy.
  295. */
  296. if (!empty_exp && !rcu_preempted_readers_exp(rnp))
  297. rcu_report_exp_rnp(&rcu_preempt_state, rnp);
  298. } else {
  299. local_irq_restore(flags);
  300. }
  301. }
  302. /*
  303. * Tree-preemptable RCU implementation for rcu_read_unlock().
  304. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  305. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  306. * invoke rcu_read_unlock_special() to clean up after a context switch
  307. * in an RCU read-side critical section and other special cases.
  308. */
  309. void __rcu_read_unlock(void)
  310. {
  311. struct task_struct *t = current;
  312. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
  313. if (--ACCESS_ONCE(t->rcu_read_lock_nesting) == 0 &&
  314. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  315. rcu_read_unlock_special(t);
  316. #ifdef CONFIG_PROVE_LOCKING
  317. WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
  318. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  319. }
  320. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  321. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  322. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  323. /*
  324. * Dump detailed information for all tasks blocking the current RCU
  325. * grace period on the specified rcu_node structure.
  326. */
  327. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  328. {
  329. unsigned long flags;
  330. struct list_head *lp;
  331. int phase;
  332. struct task_struct *t;
  333. if (rcu_preempted_readers(rnp)) {
  334. raw_spin_lock_irqsave(&rnp->lock, flags);
  335. phase = rnp->gpnum & 0x1;
  336. lp = &rnp->blocked_tasks[phase];
  337. list_for_each_entry(t, lp, rcu_node_entry)
  338. sched_show_task(t);
  339. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  340. }
  341. }
  342. /*
  343. * Dump detailed information for all tasks blocking the current RCU
  344. * grace period.
  345. */
  346. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  347. {
  348. struct rcu_node *rnp = rcu_get_root(rsp);
  349. rcu_print_detail_task_stall_rnp(rnp);
  350. rcu_for_each_leaf_node(rsp, rnp)
  351. rcu_print_detail_task_stall_rnp(rnp);
  352. }
  353. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  354. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  355. {
  356. }
  357. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  358. /*
  359. * Scan the current list of tasks blocked within RCU read-side critical
  360. * sections, printing out the tid of each.
  361. */
  362. static void rcu_print_task_stall(struct rcu_node *rnp)
  363. {
  364. struct list_head *lp;
  365. int phase;
  366. struct task_struct *t;
  367. if (rcu_preempted_readers(rnp)) {
  368. phase = rnp->gpnum & 0x1;
  369. lp = &rnp->blocked_tasks[phase];
  370. list_for_each_entry(t, lp, rcu_node_entry)
  371. printk(" P%d", t->pid);
  372. }
  373. }
  374. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  375. /*
  376. * Check that the list of blocked tasks for the newly completed grace
  377. * period is in fact empty. It is a serious bug to complete a grace
  378. * period that still has RCU readers blocked! This function must be
  379. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  380. * must be held by the caller.
  381. */
  382. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  383. {
  384. WARN_ON_ONCE(rcu_preempted_readers(rnp));
  385. WARN_ON_ONCE(rnp->qsmask);
  386. }
  387. #ifdef CONFIG_HOTPLUG_CPU
  388. /*
  389. * Handle tasklist migration for case in which all CPUs covered by the
  390. * specified rcu_node have gone offline. Move them up to the root
  391. * rcu_node. The reason for not just moving them to the immediate
  392. * parent is to remove the need for rcu_read_unlock_special() to
  393. * make more than two attempts to acquire the target rcu_node's lock.
  394. * Returns true if there were tasks blocking the current RCU grace
  395. * period.
  396. *
  397. * Returns 1 if there was previously a task blocking the current grace
  398. * period on the specified rcu_node structure.
  399. *
  400. * The caller must hold rnp->lock with irqs disabled.
  401. */
  402. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  403. struct rcu_node *rnp,
  404. struct rcu_data *rdp)
  405. {
  406. int i;
  407. struct list_head *lp;
  408. struct list_head *lp_root;
  409. int retval = 0;
  410. struct rcu_node *rnp_root = rcu_get_root(rsp);
  411. struct task_struct *tp;
  412. if (rnp == rnp_root) {
  413. WARN_ONCE(1, "Last CPU thought to be offlined?");
  414. return 0; /* Shouldn't happen: at least one CPU online. */
  415. }
  416. WARN_ON_ONCE(rnp != rdp->mynode &&
  417. (!list_empty(&rnp->blocked_tasks[0]) ||
  418. !list_empty(&rnp->blocked_tasks[1]) ||
  419. !list_empty(&rnp->blocked_tasks[2]) ||
  420. !list_empty(&rnp->blocked_tasks[3])));
  421. /*
  422. * Move tasks up to root rcu_node. Rely on the fact that the
  423. * root rcu_node can be at most one ahead of the rest of the
  424. * rcu_nodes in terms of gp_num value. This fact allows us to
  425. * move the blocked_tasks[] array directly, element by element.
  426. */
  427. if (rcu_preempted_readers(rnp))
  428. retval |= RCU_OFL_TASKS_NORM_GP;
  429. if (rcu_preempted_readers_exp(rnp))
  430. retval |= RCU_OFL_TASKS_EXP_GP;
  431. for (i = 0; i < 4; i++) {
  432. lp = &rnp->blocked_tasks[i];
  433. lp_root = &rnp_root->blocked_tasks[i];
  434. while (!list_empty(lp)) {
  435. tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
  436. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  437. list_del(&tp->rcu_node_entry);
  438. tp->rcu_blocked_node = rnp_root;
  439. list_add(&tp->rcu_node_entry, lp_root);
  440. raw_spin_unlock(&rnp_root->lock); /* irqs remain disabled */
  441. }
  442. }
  443. return retval;
  444. }
  445. /*
  446. * Do CPU-offline processing for preemptable RCU.
  447. */
  448. static void rcu_preempt_offline_cpu(int cpu)
  449. {
  450. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  451. }
  452. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  453. /*
  454. * Check for a quiescent state from the current CPU. When a task blocks,
  455. * the task is recorded in the corresponding CPU's rcu_node structure,
  456. * which is checked elsewhere.
  457. *
  458. * Caller must disable hard irqs.
  459. */
  460. static void rcu_preempt_check_callbacks(int cpu)
  461. {
  462. struct task_struct *t = current;
  463. if (t->rcu_read_lock_nesting == 0) {
  464. rcu_preempt_qs(cpu);
  465. return;
  466. }
  467. if (per_cpu(rcu_preempt_data, cpu).qs_pending)
  468. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  469. }
  470. /*
  471. * Process callbacks for preemptable RCU.
  472. */
  473. static void rcu_preempt_process_callbacks(void)
  474. {
  475. __rcu_process_callbacks(&rcu_preempt_state,
  476. &__get_cpu_var(rcu_preempt_data));
  477. }
  478. /*
  479. * Queue a preemptable-RCU callback for invocation after a grace period.
  480. */
  481. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  482. {
  483. __call_rcu(head, func, &rcu_preempt_state);
  484. }
  485. EXPORT_SYMBOL_GPL(call_rcu);
  486. /**
  487. * synchronize_rcu - wait until a grace period has elapsed.
  488. *
  489. * Control will return to the caller some time after a full grace
  490. * period has elapsed, in other words after all currently executing RCU
  491. * read-side critical sections have completed. RCU read-side critical
  492. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  493. * and may be nested.
  494. */
  495. void synchronize_rcu(void)
  496. {
  497. struct rcu_synchronize rcu;
  498. if (!rcu_scheduler_active)
  499. return;
  500. init_rcu_head_on_stack(&rcu.head);
  501. init_completion(&rcu.completion);
  502. /* Will wake me after RCU finished. */
  503. call_rcu(&rcu.head, wakeme_after_rcu);
  504. /* Wait for it. */
  505. wait_for_completion(&rcu.completion);
  506. destroy_rcu_head_on_stack(&rcu.head);
  507. }
  508. EXPORT_SYMBOL_GPL(synchronize_rcu);
  509. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  510. static long sync_rcu_preempt_exp_count;
  511. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  512. /*
  513. * Return non-zero if there are any tasks in RCU read-side critical
  514. * sections blocking the current preemptible-RCU expedited grace period.
  515. * If there is no preemptible-RCU expedited grace period currently in
  516. * progress, returns zero unconditionally.
  517. */
  518. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  519. {
  520. return !list_empty(&rnp->blocked_tasks[2]) ||
  521. !list_empty(&rnp->blocked_tasks[3]);
  522. }
  523. /*
  524. * return non-zero if there is no RCU expedited grace period in progress
  525. * for the specified rcu_node structure, in other words, if all CPUs and
  526. * tasks covered by the specified rcu_node structure have done their bit
  527. * for the current expedited grace period. Works only for preemptible
  528. * RCU -- other RCU implementation use other means.
  529. *
  530. * Caller must hold sync_rcu_preempt_exp_mutex.
  531. */
  532. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  533. {
  534. return !rcu_preempted_readers_exp(rnp) &&
  535. ACCESS_ONCE(rnp->expmask) == 0;
  536. }
  537. /*
  538. * Report the exit from RCU read-side critical section for the last task
  539. * that queued itself during or before the current expedited preemptible-RCU
  540. * grace period. This event is reported either to the rcu_node structure on
  541. * which the task was queued or to one of that rcu_node structure's ancestors,
  542. * recursively up the tree. (Calm down, calm down, we do the recursion
  543. * iteratively!)
  544. *
  545. * Caller must hold sync_rcu_preempt_exp_mutex.
  546. */
  547. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  548. {
  549. unsigned long flags;
  550. unsigned long mask;
  551. raw_spin_lock_irqsave(&rnp->lock, flags);
  552. for (;;) {
  553. if (!sync_rcu_preempt_exp_done(rnp))
  554. break;
  555. if (rnp->parent == NULL) {
  556. wake_up(&sync_rcu_preempt_exp_wq);
  557. break;
  558. }
  559. mask = rnp->grpmask;
  560. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  561. rnp = rnp->parent;
  562. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  563. rnp->expmask &= ~mask;
  564. }
  565. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  566. }
  567. /*
  568. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  569. * grace period for the specified rcu_node structure. If there are no such
  570. * tasks, report it up the rcu_node hierarchy.
  571. *
  572. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  573. */
  574. static void
  575. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  576. {
  577. int must_wait;
  578. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  579. list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
  580. list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
  581. must_wait = rcu_preempted_readers_exp(rnp);
  582. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  583. if (!must_wait)
  584. rcu_report_exp_rnp(rsp, rnp);
  585. }
  586. /*
  587. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  588. * is to invoke synchronize_sched_expedited() to push all the tasks to
  589. * the ->blocked_tasks[] lists, move all entries from the first set of
  590. * ->blocked_tasks[] lists to the second set, and finally wait for this
  591. * second set to drain.
  592. */
  593. void synchronize_rcu_expedited(void)
  594. {
  595. unsigned long flags;
  596. struct rcu_node *rnp;
  597. struct rcu_state *rsp = &rcu_preempt_state;
  598. long snap;
  599. int trycount = 0;
  600. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  601. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  602. smp_mb(); /* Above access cannot bleed into critical section. */
  603. /*
  604. * Acquire lock, falling back to synchronize_rcu() if too many
  605. * lock-acquisition failures. Of course, if someone does the
  606. * expedited grace period for us, just leave.
  607. */
  608. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  609. if (trycount++ < 10)
  610. udelay(trycount * num_online_cpus());
  611. else {
  612. synchronize_rcu();
  613. return;
  614. }
  615. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  616. goto mb_ret; /* Others did our work for us. */
  617. }
  618. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  619. goto unlock_mb_ret; /* Others did our work for us. */
  620. /* force all RCU readers onto blocked_tasks[]. */
  621. synchronize_sched_expedited();
  622. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  623. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  624. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  625. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  626. rnp->expmask = rnp->qsmaskinit;
  627. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  628. }
  629. /* Snapshot current state of ->blocked_tasks[] lists. */
  630. rcu_for_each_leaf_node(rsp, rnp)
  631. sync_rcu_preempt_exp_init(rsp, rnp);
  632. if (NUM_RCU_NODES > 1)
  633. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  634. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  635. /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
  636. rnp = rcu_get_root(rsp);
  637. wait_event(sync_rcu_preempt_exp_wq,
  638. sync_rcu_preempt_exp_done(rnp));
  639. /* Clean up and exit. */
  640. smp_mb(); /* ensure expedited GP seen before counter increment. */
  641. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  642. unlock_mb_ret:
  643. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  644. mb_ret:
  645. smp_mb(); /* ensure subsequent action seen after grace period. */
  646. }
  647. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  648. /*
  649. * Check to see if there is any immediate preemptable-RCU-related work
  650. * to be done.
  651. */
  652. static int rcu_preempt_pending(int cpu)
  653. {
  654. return __rcu_pending(&rcu_preempt_state,
  655. &per_cpu(rcu_preempt_data, cpu));
  656. }
  657. /*
  658. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  659. */
  660. static int rcu_preempt_needs_cpu(int cpu)
  661. {
  662. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  663. }
  664. /**
  665. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  666. */
  667. void rcu_barrier(void)
  668. {
  669. _rcu_barrier(&rcu_preempt_state, call_rcu);
  670. }
  671. EXPORT_SYMBOL_GPL(rcu_barrier);
  672. /*
  673. * Initialize preemptable RCU's per-CPU data.
  674. */
  675. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  676. {
  677. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  678. }
  679. /*
  680. * Move preemptable RCU's callbacks to ->orphan_cbs_list.
  681. */
  682. static void rcu_preempt_send_cbs_to_orphanage(void)
  683. {
  684. rcu_send_cbs_to_orphanage(&rcu_preempt_state);
  685. }
  686. /*
  687. * Initialize preemptable RCU's state structures.
  688. */
  689. static void __init __rcu_init_preempt(void)
  690. {
  691. RCU_INIT_FLAVOR(&rcu_preempt_state, rcu_preempt_data);
  692. }
  693. /*
  694. * Check for a task exiting while in a preemptable-RCU read-side
  695. * critical section, clean up if so. No need to issue warnings,
  696. * as debug_check_no_locks_held() already does this if lockdep
  697. * is enabled.
  698. */
  699. void exit_rcu(void)
  700. {
  701. struct task_struct *t = current;
  702. if (t->rcu_read_lock_nesting == 0)
  703. return;
  704. t->rcu_read_lock_nesting = 1;
  705. rcu_read_unlock();
  706. }
  707. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  708. /*
  709. * Tell them what RCU they are running.
  710. */
  711. static void __init rcu_bootup_announce(void)
  712. {
  713. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  714. rcu_bootup_announce_oddness();
  715. }
  716. /*
  717. * Return the number of RCU batches processed thus far for debug & stats.
  718. */
  719. long rcu_batches_completed(void)
  720. {
  721. return rcu_batches_completed_sched();
  722. }
  723. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  724. /*
  725. * Force a quiescent state for RCU, which, because there is no preemptible
  726. * RCU, becomes the same as rcu-sched.
  727. */
  728. void rcu_force_quiescent_state(void)
  729. {
  730. rcu_sched_force_quiescent_state();
  731. }
  732. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  733. /*
  734. * Because preemptable RCU does not exist, we never have to check for
  735. * CPUs being in quiescent states.
  736. */
  737. static void rcu_preempt_note_context_switch(int cpu)
  738. {
  739. }
  740. /*
  741. * Because preemptable RCU does not exist, there are never any preempted
  742. * RCU readers.
  743. */
  744. static int rcu_preempted_readers(struct rcu_node *rnp)
  745. {
  746. return 0;
  747. }
  748. #ifdef CONFIG_HOTPLUG_CPU
  749. /* Because preemptible RCU does not exist, no quieting of tasks. */
  750. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  751. {
  752. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  753. }
  754. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  755. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  756. /*
  757. * Because preemptable RCU does not exist, we never have to check for
  758. * tasks blocked within RCU read-side critical sections.
  759. */
  760. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  761. {
  762. }
  763. /*
  764. * Because preemptable RCU does not exist, we never have to check for
  765. * tasks blocked within RCU read-side critical sections.
  766. */
  767. static void rcu_print_task_stall(struct rcu_node *rnp)
  768. {
  769. }
  770. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  771. /*
  772. * Because there is no preemptable RCU, there can be no readers blocked,
  773. * so there is no need to check for blocked tasks. So check only for
  774. * bogus qsmask values.
  775. */
  776. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  777. {
  778. WARN_ON_ONCE(rnp->qsmask);
  779. }
  780. #ifdef CONFIG_HOTPLUG_CPU
  781. /*
  782. * Because preemptable RCU does not exist, it never needs to migrate
  783. * tasks that were blocked within RCU read-side critical sections, and
  784. * such non-existent tasks cannot possibly have been blocking the current
  785. * grace period.
  786. */
  787. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  788. struct rcu_node *rnp,
  789. struct rcu_data *rdp)
  790. {
  791. return 0;
  792. }
  793. /*
  794. * Because preemptable RCU does not exist, it never needs CPU-offline
  795. * processing.
  796. */
  797. static void rcu_preempt_offline_cpu(int cpu)
  798. {
  799. }
  800. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  801. /*
  802. * Because preemptable RCU does not exist, it never has any callbacks
  803. * to check.
  804. */
  805. static void rcu_preempt_check_callbacks(int cpu)
  806. {
  807. }
  808. /*
  809. * Because preemptable RCU does not exist, it never has any callbacks
  810. * to process.
  811. */
  812. static void rcu_preempt_process_callbacks(void)
  813. {
  814. }
  815. /*
  816. * In classic RCU, call_rcu() is just call_rcu_sched().
  817. */
  818. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  819. {
  820. call_rcu_sched(head, func);
  821. }
  822. EXPORT_SYMBOL_GPL(call_rcu);
  823. /*
  824. * Wait for an rcu-preempt grace period, but make it happen quickly.
  825. * But because preemptable RCU does not exist, map to rcu-sched.
  826. */
  827. void synchronize_rcu_expedited(void)
  828. {
  829. synchronize_sched_expedited();
  830. }
  831. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  832. #ifdef CONFIG_HOTPLUG_CPU
  833. /*
  834. * Because preemptable RCU does not exist, there is never any need to
  835. * report on tasks preempted in RCU read-side critical sections during
  836. * expedited RCU grace periods.
  837. */
  838. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  839. {
  840. return;
  841. }
  842. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  843. /*
  844. * Because preemptable RCU does not exist, it never has any work to do.
  845. */
  846. static int rcu_preempt_pending(int cpu)
  847. {
  848. return 0;
  849. }
  850. /*
  851. * Because preemptable RCU does not exist, it never needs any CPU.
  852. */
  853. static int rcu_preempt_needs_cpu(int cpu)
  854. {
  855. return 0;
  856. }
  857. /*
  858. * Because preemptable RCU does not exist, rcu_barrier() is just
  859. * another name for rcu_barrier_sched().
  860. */
  861. void rcu_barrier(void)
  862. {
  863. rcu_barrier_sched();
  864. }
  865. EXPORT_SYMBOL_GPL(rcu_barrier);
  866. /*
  867. * Because preemptable RCU does not exist, there is no per-CPU
  868. * data to initialize.
  869. */
  870. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  871. {
  872. }
  873. /*
  874. * Because there is no preemptable RCU, there are no callbacks to move.
  875. */
  876. static void rcu_preempt_send_cbs_to_orphanage(void)
  877. {
  878. }
  879. /*
  880. * Because preemptable RCU does not exist, it need not be initialized.
  881. */
  882. static void __init __rcu_init_preempt(void)
  883. {
  884. }
  885. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  886. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  887. /*
  888. * Check to see if any future RCU-related work will need to be done
  889. * by the current CPU, even if none need be done immediately, returning
  890. * 1 if so. This function is part of the RCU implementation; it is -not-
  891. * an exported member of the RCU API.
  892. *
  893. * Because we have preemptible RCU, just check whether this CPU needs
  894. * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
  895. * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
  896. */
  897. int rcu_needs_cpu(int cpu)
  898. {
  899. return rcu_needs_cpu_quick_check(cpu);
  900. }
  901. /*
  902. * Check to see if we need to continue a callback-flush operations to
  903. * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
  904. * entry is not configured, so we never do need to.
  905. */
  906. static void rcu_needs_cpu_flush(void)
  907. {
  908. }
  909. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  910. #define RCU_NEEDS_CPU_FLUSHES 5
  911. static DEFINE_PER_CPU(int, rcu_dyntick_drain);
  912. static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
  913. /*
  914. * Check to see if any future RCU-related work will need to be done
  915. * by the current CPU, even if none need be done immediately, returning
  916. * 1 if so. This function is part of the RCU implementation; it is -not-
  917. * an exported member of the RCU API.
  918. *
  919. * Because we are not supporting preemptible RCU, attempt to accelerate
  920. * any current grace periods so that RCU no longer needs this CPU, but
  921. * only if all other CPUs are already in dynticks-idle mode. This will
  922. * allow the CPU cores to be powered down immediately, as opposed to after
  923. * waiting many milliseconds for grace periods to elapse.
  924. *
  925. * Because it is not legal to invoke rcu_process_callbacks() with irqs
  926. * disabled, we do one pass of force_quiescent_state(), then do a
  927. * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
  928. * The per-cpu rcu_dyntick_drain variable controls the sequencing.
  929. */
  930. int rcu_needs_cpu(int cpu)
  931. {
  932. int c = 0;
  933. int snap;
  934. int snap_nmi;
  935. int thatcpu;
  936. /* Check for being in the holdoff period. */
  937. if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
  938. return rcu_needs_cpu_quick_check(cpu);
  939. /* Don't bother unless we are the last non-dyntick-idle CPU. */
  940. for_each_online_cpu(thatcpu) {
  941. if (thatcpu == cpu)
  942. continue;
  943. snap = per_cpu(rcu_dynticks, thatcpu).dynticks;
  944. snap_nmi = per_cpu(rcu_dynticks, thatcpu).dynticks_nmi;
  945. smp_mb(); /* Order sampling of snap with end of grace period. */
  946. if (((snap & 0x1) != 0) || ((snap_nmi & 0x1) != 0)) {
  947. per_cpu(rcu_dyntick_drain, cpu) = 0;
  948. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
  949. return rcu_needs_cpu_quick_check(cpu);
  950. }
  951. }
  952. /* Check and update the rcu_dyntick_drain sequencing. */
  953. if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  954. /* First time through, initialize the counter. */
  955. per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
  956. } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  957. /* We have hit the limit, so time to give up. */
  958. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
  959. return rcu_needs_cpu_quick_check(cpu);
  960. }
  961. /* Do one step pushing remaining RCU callbacks through. */
  962. if (per_cpu(rcu_sched_data, cpu).nxtlist) {
  963. rcu_sched_qs(cpu);
  964. force_quiescent_state(&rcu_sched_state, 0);
  965. c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
  966. }
  967. if (per_cpu(rcu_bh_data, cpu).nxtlist) {
  968. rcu_bh_qs(cpu);
  969. force_quiescent_state(&rcu_bh_state, 0);
  970. c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
  971. }
  972. /* If RCU callbacks are still pending, RCU still needs this CPU. */
  973. if (c)
  974. raise_softirq(RCU_SOFTIRQ);
  975. return c;
  976. }
  977. /*
  978. * Check to see if we need to continue a callback-flush operations to
  979. * allow the last CPU to enter dyntick-idle mode.
  980. */
  981. static void rcu_needs_cpu_flush(void)
  982. {
  983. int cpu = smp_processor_id();
  984. unsigned long flags;
  985. if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
  986. return;
  987. local_irq_save(flags);
  988. (void)rcu_needs_cpu(cpu);
  989. local_irq_restore(flags);
  990. }
  991. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */