rcutree_plugin.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(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 = per_cpu_ptr(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. 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. --t->rcu_read_lock_nesting;
  314. barrier(); /* decrement before load of ->rcu_read_unlock_special */
  315. if (t->rcu_read_lock_nesting == 0 &&
  316. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  317. rcu_read_unlock_special(t);
  318. #ifdef CONFIG_PROVE_LOCKING
  319. WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
  320. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  321. }
  322. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  323. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  324. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  325. /*
  326. * Dump detailed information for all tasks blocking the current RCU
  327. * grace period on the specified rcu_node structure.
  328. */
  329. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  330. {
  331. unsigned long flags;
  332. struct list_head *lp;
  333. int phase;
  334. struct task_struct *t;
  335. if (rcu_preempted_readers(rnp)) {
  336. raw_spin_lock_irqsave(&rnp->lock, flags);
  337. phase = rnp->gpnum & 0x1;
  338. lp = &rnp->blocked_tasks[phase];
  339. list_for_each_entry(t, lp, rcu_node_entry)
  340. sched_show_task(t);
  341. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  342. }
  343. }
  344. /*
  345. * Dump detailed information for all tasks blocking the current RCU
  346. * grace period.
  347. */
  348. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  349. {
  350. struct rcu_node *rnp = rcu_get_root(rsp);
  351. rcu_print_detail_task_stall_rnp(rnp);
  352. rcu_for_each_leaf_node(rsp, rnp)
  353. rcu_print_detail_task_stall_rnp(rnp);
  354. }
  355. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  356. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  357. {
  358. }
  359. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  360. /*
  361. * Scan the current list of tasks blocked within RCU read-side critical
  362. * sections, printing out the tid of each.
  363. */
  364. static void rcu_print_task_stall(struct rcu_node *rnp)
  365. {
  366. struct list_head *lp;
  367. int phase;
  368. struct task_struct *t;
  369. if (rcu_preempted_readers(rnp)) {
  370. phase = rnp->gpnum & 0x1;
  371. lp = &rnp->blocked_tasks[phase];
  372. list_for_each_entry(t, lp, rcu_node_entry)
  373. printk(" P%d", t->pid);
  374. }
  375. }
  376. /*
  377. * Suppress preemptible RCU's CPU stall warnings by pushing the
  378. * time of the next stall-warning message comfortably far into the
  379. * future.
  380. */
  381. static void rcu_preempt_stall_reset(void)
  382. {
  383. rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  384. }
  385. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  386. /*
  387. * Check that the list of blocked tasks for the newly completed grace
  388. * period is in fact empty. It is a serious bug to complete a grace
  389. * period that still has RCU readers blocked! This function must be
  390. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  391. * must be held by the caller.
  392. */
  393. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  394. {
  395. WARN_ON_ONCE(rcu_preempted_readers(rnp));
  396. WARN_ON_ONCE(rnp->qsmask);
  397. }
  398. #ifdef CONFIG_HOTPLUG_CPU
  399. /*
  400. * Handle tasklist migration for case in which all CPUs covered by the
  401. * specified rcu_node have gone offline. Move them up to the root
  402. * rcu_node. The reason for not just moving them to the immediate
  403. * parent is to remove the need for rcu_read_unlock_special() to
  404. * make more than two attempts to acquire the target rcu_node's lock.
  405. * Returns true if there were tasks blocking the current RCU grace
  406. * period.
  407. *
  408. * Returns 1 if there was previously a task blocking the current grace
  409. * period on the specified rcu_node structure.
  410. *
  411. * The caller must hold rnp->lock with irqs disabled.
  412. */
  413. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  414. struct rcu_node *rnp,
  415. struct rcu_data *rdp)
  416. {
  417. int i;
  418. struct list_head *lp;
  419. struct list_head *lp_root;
  420. int retval = 0;
  421. struct rcu_node *rnp_root = rcu_get_root(rsp);
  422. struct task_struct *tp;
  423. if (rnp == rnp_root) {
  424. WARN_ONCE(1, "Last CPU thought to be offlined?");
  425. return 0; /* Shouldn't happen: at least one CPU online. */
  426. }
  427. WARN_ON_ONCE(rnp != rdp->mynode &&
  428. (!list_empty(&rnp->blocked_tasks[0]) ||
  429. !list_empty(&rnp->blocked_tasks[1]) ||
  430. !list_empty(&rnp->blocked_tasks[2]) ||
  431. !list_empty(&rnp->blocked_tasks[3])));
  432. /*
  433. * Move tasks up to root rcu_node. Rely on the fact that the
  434. * root rcu_node can be at most one ahead of the rest of the
  435. * rcu_nodes in terms of gp_num value. This fact allows us to
  436. * move the blocked_tasks[] array directly, element by element.
  437. */
  438. if (rcu_preempted_readers(rnp))
  439. retval |= RCU_OFL_TASKS_NORM_GP;
  440. if (rcu_preempted_readers_exp(rnp))
  441. retval |= RCU_OFL_TASKS_EXP_GP;
  442. for (i = 0; i < 4; i++) {
  443. lp = &rnp->blocked_tasks[i];
  444. lp_root = &rnp_root->blocked_tasks[i];
  445. while (!list_empty(lp)) {
  446. tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
  447. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  448. list_del(&tp->rcu_node_entry);
  449. tp->rcu_blocked_node = rnp_root;
  450. list_add(&tp->rcu_node_entry, lp_root);
  451. raw_spin_unlock(&rnp_root->lock); /* irqs remain disabled */
  452. }
  453. }
  454. return retval;
  455. }
  456. /*
  457. * Do CPU-offline processing for preemptable RCU.
  458. */
  459. static void rcu_preempt_offline_cpu(int cpu)
  460. {
  461. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  462. }
  463. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  464. /*
  465. * Check for a quiescent state from the current CPU. When a task blocks,
  466. * the task is recorded in the corresponding CPU's rcu_node structure,
  467. * which is checked elsewhere.
  468. *
  469. * Caller must disable hard irqs.
  470. */
  471. static void rcu_preempt_check_callbacks(int cpu)
  472. {
  473. struct task_struct *t = current;
  474. if (t->rcu_read_lock_nesting == 0) {
  475. rcu_preempt_qs(cpu);
  476. return;
  477. }
  478. if (per_cpu(rcu_preempt_data, cpu).qs_pending)
  479. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  480. }
  481. /*
  482. * Process callbacks for preemptable RCU.
  483. */
  484. static void rcu_preempt_process_callbacks(void)
  485. {
  486. __rcu_process_callbacks(&rcu_preempt_state,
  487. &__get_cpu_var(rcu_preempt_data));
  488. }
  489. /*
  490. * Queue a preemptable-RCU callback for invocation after a grace period.
  491. */
  492. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  493. {
  494. __call_rcu(head, func, &rcu_preempt_state);
  495. }
  496. EXPORT_SYMBOL_GPL(call_rcu);
  497. /**
  498. * synchronize_rcu - wait until a grace period has elapsed.
  499. *
  500. * Control will return to the caller some time after a full grace
  501. * period has elapsed, in other words after all currently executing RCU
  502. * read-side critical sections have completed. Note, however, that
  503. * upon return from synchronize_rcu(), the caller might well be executing
  504. * concurrently with new RCU read-side critical sections that began while
  505. * synchronize_rcu() was waiting. RCU read-side critical sections are
  506. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  507. */
  508. void synchronize_rcu(void)
  509. {
  510. struct rcu_synchronize rcu;
  511. if (!rcu_scheduler_active)
  512. return;
  513. init_rcu_head_on_stack(&rcu.head);
  514. init_completion(&rcu.completion);
  515. /* Will wake me after RCU finished. */
  516. call_rcu(&rcu.head, wakeme_after_rcu);
  517. /* Wait for it. */
  518. wait_for_completion(&rcu.completion);
  519. destroy_rcu_head_on_stack(&rcu.head);
  520. }
  521. EXPORT_SYMBOL_GPL(synchronize_rcu);
  522. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  523. static long sync_rcu_preempt_exp_count;
  524. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  525. /*
  526. * Return non-zero if there are any tasks in RCU read-side critical
  527. * sections blocking the current preemptible-RCU expedited grace period.
  528. * If there is no preemptible-RCU expedited grace period currently in
  529. * progress, returns zero unconditionally.
  530. */
  531. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  532. {
  533. return !list_empty(&rnp->blocked_tasks[2]) ||
  534. !list_empty(&rnp->blocked_tasks[3]);
  535. }
  536. /*
  537. * return non-zero if there is no RCU expedited grace period in progress
  538. * for the specified rcu_node structure, in other words, if all CPUs and
  539. * tasks covered by the specified rcu_node structure have done their bit
  540. * for the current expedited grace period. Works only for preemptible
  541. * RCU -- other RCU implementation use other means.
  542. *
  543. * Caller must hold sync_rcu_preempt_exp_mutex.
  544. */
  545. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  546. {
  547. return !rcu_preempted_readers_exp(rnp) &&
  548. ACCESS_ONCE(rnp->expmask) == 0;
  549. }
  550. /*
  551. * Report the exit from RCU read-side critical section for the last task
  552. * that queued itself during or before the current expedited preemptible-RCU
  553. * grace period. This event is reported either to the rcu_node structure on
  554. * which the task was queued or to one of that rcu_node structure's ancestors,
  555. * recursively up the tree. (Calm down, calm down, we do the recursion
  556. * iteratively!)
  557. *
  558. * Caller must hold sync_rcu_preempt_exp_mutex.
  559. */
  560. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  561. {
  562. unsigned long flags;
  563. unsigned long mask;
  564. raw_spin_lock_irqsave(&rnp->lock, flags);
  565. for (;;) {
  566. if (!sync_rcu_preempt_exp_done(rnp))
  567. break;
  568. if (rnp->parent == NULL) {
  569. wake_up(&sync_rcu_preempt_exp_wq);
  570. break;
  571. }
  572. mask = rnp->grpmask;
  573. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  574. rnp = rnp->parent;
  575. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  576. rnp->expmask &= ~mask;
  577. }
  578. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  579. }
  580. /*
  581. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  582. * grace period for the specified rcu_node structure. If there are no such
  583. * tasks, report it up the rcu_node hierarchy.
  584. *
  585. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  586. */
  587. static void
  588. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  589. {
  590. int must_wait;
  591. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  592. list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
  593. list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
  594. must_wait = rcu_preempted_readers_exp(rnp);
  595. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  596. if (!must_wait)
  597. rcu_report_exp_rnp(rsp, rnp);
  598. }
  599. /*
  600. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  601. * is to invoke synchronize_sched_expedited() to push all the tasks to
  602. * the ->blocked_tasks[] lists, move all entries from the first set of
  603. * ->blocked_tasks[] lists to the second set, and finally wait for this
  604. * second set to drain.
  605. */
  606. void synchronize_rcu_expedited(void)
  607. {
  608. unsigned long flags;
  609. struct rcu_node *rnp;
  610. struct rcu_state *rsp = &rcu_preempt_state;
  611. long snap;
  612. int trycount = 0;
  613. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  614. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  615. smp_mb(); /* Above access cannot bleed into critical section. */
  616. /*
  617. * Acquire lock, falling back to synchronize_rcu() if too many
  618. * lock-acquisition failures. Of course, if someone does the
  619. * expedited grace period for us, just leave.
  620. */
  621. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  622. if (trycount++ < 10)
  623. udelay(trycount * num_online_cpus());
  624. else {
  625. synchronize_rcu();
  626. return;
  627. }
  628. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  629. goto mb_ret; /* Others did our work for us. */
  630. }
  631. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  632. goto unlock_mb_ret; /* Others did our work for us. */
  633. /* force all RCU readers onto blocked_tasks[]. */
  634. synchronize_sched_expedited();
  635. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  636. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  637. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  638. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  639. rnp->expmask = rnp->qsmaskinit;
  640. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  641. }
  642. /* Snapshot current state of ->blocked_tasks[] lists. */
  643. rcu_for_each_leaf_node(rsp, rnp)
  644. sync_rcu_preempt_exp_init(rsp, rnp);
  645. if (NUM_RCU_NODES > 1)
  646. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  647. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  648. /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
  649. rnp = rcu_get_root(rsp);
  650. wait_event(sync_rcu_preempt_exp_wq,
  651. sync_rcu_preempt_exp_done(rnp));
  652. /* Clean up and exit. */
  653. smp_mb(); /* ensure expedited GP seen before counter increment. */
  654. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  655. unlock_mb_ret:
  656. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  657. mb_ret:
  658. smp_mb(); /* ensure subsequent action seen after grace period. */
  659. }
  660. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  661. /*
  662. * Check to see if there is any immediate preemptable-RCU-related work
  663. * to be done.
  664. */
  665. static int rcu_preempt_pending(int cpu)
  666. {
  667. return __rcu_pending(&rcu_preempt_state,
  668. &per_cpu(rcu_preempt_data, cpu));
  669. }
  670. /*
  671. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  672. */
  673. static int rcu_preempt_needs_cpu(int cpu)
  674. {
  675. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  676. }
  677. /**
  678. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  679. */
  680. void rcu_barrier(void)
  681. {
  682. _rcu_barrier(&rcu_preempt_state, call_rcu);
  683. }
  684. EXPORT_SYMBOL_GPL(rcu_barrier);
  685. /*
  686. * Initialize preemptable RCU's per-CPU data.
  687. */
  688. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  689. {
  690. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  691. }
  692. /*
  693. * Move preemptable RCU's callbacks to ->orphan_cbs_list.
  694. */
  695. static void rcu_preempt_send_cbs_to_orphanage(void)
  696. {
  697. rcu_send_cbs_to_orphanage(&rcu_preempt_state);
  698. }
  699. /*
  700. * Initialize preemptable RCU's state structures.
  701. */
  702. static void __init __rcu_init_preempt(void)
  703. {
  704. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  705. }
  706. /*
  707. * Check for a task exiting while in a preemptable-RCU read-side
  708. * critical section, clean up if so. No need to issue warnings,
  709. * as debug_check_no_locks_held() already does this if lockdep
  710. * is enabled.
  711. */
  712. void exit_rcu(void)
  713. {
  714. struct task_struct *t = current;
  715. if (t->rcu_read_lock_nesting == 0)
  716. return;
  717. t->rcu_read_lock_nesting = 1;
  718. rcu_read_unlock();
  719. }
  720. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  721. /*
  722. * Tell them what RCU they are running.
  723. */
  724. static void __init rcu_bootup_announce(void)
  725. {
  726. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  727. rcu_bootup_announce_oddness();
  728. }
  729. /*
  730. * Return the number of RCU batches processed thus far for debug & stats.
  731. */
  732. long rcu_batches_completed(void)
  733. {
  734. return rcu_batches_completed_sched();
  735. }
  736. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  737. /*
  738. * Force a quiescent state for RCU, which, because there is no preemptible
  739. * RCU, becomes the same as rcu-sched.
  740. */
  741. void rcu_force_quiescent_state(void)
  742. {
  743. rcu_sched_force_quiescent_state();
  744. }
  745. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  746. /*
  747. * Because preemptable RCU does not exist, we never have to check for
  748. * CPUs being in quiescent states.
  749. */
  750. static void rcu_preempt_note_context_switch(int cpu)
  751. {
  752. }
  753. /*
  754. * Because preemptable RCU does not exist, there are never any preempted
  755. * RCU readers.
  756. */
  757. static int rcu_preempted_readers(struct rcu_node *rnp)
  758. {
  759. return 0;
  760. }
  761. #ifdef CONFIG_HOTPLUG_CPU
  762. /* Because preemptible RCU does not exist, no quieting of tasks. */
  763. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  764. {
  765. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  766. }
  767. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  768. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  769. /*
  770. * Because preemptable RCU does not exist, we never have to check for
  771. * tasks blocked within RCU read-side critical sections.
  772. */
  773. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  774. {
  775. }
  776. /*
  777. * Because preemptable RCU does not exist, we never have to check for
  778. * tasks blocked within RCU read-side critical sections.
  779. */
  780. static void rcu_print_task_stall(struct rcu_node *rnp)
  781. {
  782. }
  783. /*
  784. * Because preemptible RCU does not exist, there is no need to suppress
  785. * its CPU stall warnings.
  786. */
  787. static void rcu_preempt_stall_reset(void)
  788. {
  789. }
  790. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  791. /*
  792. * Because there is no preemptable RCU, there can be no readers blocked,
  793. * so there is no need to check for blocked tasks. So check only for
  794. * bogus qsmask values.
  795. */
  796. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  797. {
  798. WARN_ON_ONCE(rnp->qsmask);
  799. }
  800. #ifdef CONFIG_HOTPLUG_CPU
  801. /*
  802. * Because preemptable RCU does not exist, it never needs to migrate
  803. * tasks that were blocked within RCU read-side critical sections, and
  804. * such non-existent tasks cannot possibly have been blocking the current
  805. * grace period.
  806. */
  807. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  808. struct rcu_node *rnp,
  809. struct rcu_data *rdp)
  810. {
  811. return 0;
  812. }
  813. /*
  814. * Because preemptable RCU does not exist, it never needs CPU-offline
  815. * processing.
  816. */
  817. static void rcu_preempt_offline_cpu(int cpu)
  818. {
  819. }
  820. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  821. /*
  822. * Because preemptable RCU does not exist, it never has any callbacks
  823. * to check.
  824. */
  825. static void rcu_preempt_check_callbacks(int cpu)
  826. {
  827. }
  828. /*
  829. * Because preemptable RCU does not exist, it never has any callbacks
  830. * to process.
  831. */
  832. static void rcu_preempt_process_callbacks(void)
  833. {
  834. }
  835. /*
  836. * Wait for an rcu-preempt grace period, but make it happen quickly.
  837. * But because preemptable RCU does not exist, map to rcu-sched.
  838. */
  839. void synchronize_rcu_expedited(void)
  840. {
  841. synchronize_sched_expedited();
  842. }
  843. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  844. #ifdef CONFIG_HOTPLUG_CPU
  845. /*
  846. * Because preemptable RCU does not exist, there is never any need to
  847. * report on tasks preempted in RCU read-side critical sections during
  848. * expedited RCU grace periods.
  849. */
  850. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  851. {
  852. return;
  853. }
  854. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  855. /*
  856. * Because preemptable RCU does not exist, it never has any work to do.
  857. */
  858. static int rcu_preempt_pending(int cpu)
  859. {
  860. return 0;
  861. }
  862. /*
  863. * Because preemptable RCU does not exist, it never needs any CPU.
  864. */
  865. static int rcu_preempt_needs_cpu(int cpu)
  866. {
  867. return 0;
  868. }
  869. /*
  870. * Because preemptable RCU does not exist, rcu_barrier() is just
  871. * another name for rcu_barrier_sched().
  872. */
  873. void rcu_barrier(void)
  874. {
  875. rcu_barrier_sched();
  876. }
  877. EXPORT_SYMBOL_GPL(rcu_barrier);
  878. /*
  879. * Because preemptable RCU does not exist, there is no per-CPU
  880. * data to initialize.
  881. */
  882. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  883. {
  884. }
  885. /*
  886. * Because there is no preemptable RCU, there are no callbacks to move.
  887. */
  888. static void rcu_preempt_send_cbs_to_orphanage(void)
  889. {
  890. }
  891. /*
  892. * Because preemptable RCU does not exist, it need not be initialized.
  893. */
  894. static void __init __rcu_init_preempt(void)
  895. {
  896. }
  897. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  898. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  899. /*
  900. * Check to see if any future RCU-related work will need to be done
  901. * by the current CPU, even if none need be done immediately, returning
  902. * 1 if so. This function is part of the RCU implementation; it is -not-
  903. * an exported member of the RCU API.
  904. *
  905. * Because we have preemptible RCU, just check whether this CPU needs
  906. * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
  907. * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
  908. */
  909. int rcu_needs_cpu(int cpu)
  910. {
  911. return rcu_needs_cpu_quick_check(cpu);
  912. }
  913. /*
  914. * Check to see if we need to continue a callback-flush operations to
  915. * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
  916. * entry is not configured, so we never do need to.
  917. */
  918. static void rcu_needs_cpu_flush(void)
  919. {
  920. }
  921. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  922. #define RCU_NEEDS_CPU_FLUSHES 5
  923. static DEFINE_PER_CPU(int, rcu_dyntick_drain);
  924. static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
  925. /*
  926. * Check to see if any future RCU-related work will need to be done
  927. * by the current CPU, even if none need be done immediately, returning
  928. * 1 if so. This function is part of the RCU implementation; it is -not-
  929. * an exported member of the RCU API.
  930. *
  931. * Because we are not supporting preemptible RCU, attempt to accelerate
  932. * any current grace periods so that RCU no longer needs this CPU, but
  933. * only if all other CPUs are already in dynticks-idle mode. This will
  934. * allow the CPU cores to be powered down immediately, as opposed to after
  935. * waiting many milliseconds for grace periods to elapse.
  936. *
  937. * Because it is not legal to invoke rcu_process_callbacks() with irqs
  938. * disabled, we do one pass of force_quiescent_state(), then do a
  939. * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
  940. * The per-cpu rcu_dyntick_drain variable controls the sequencing.
  941. */
  942. int rcu_needs_cpu(int cpu)
  943. {
  944. int c = 0;
  945. int snap;
  946. int snap_nmi;
  947. int thatcpu;
  948. /* Check for being in the holdoff period. */
  949. if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
  950. return rcu_needs_cpu_quick_check(cpu);
  951. /* Don't bother unless we are the last non-dyntick-idle CPU. */
  952. for_each_online_cpu(thatcpu) {
  953. if (thatcpu == cpu)
  954. continue;
  955. snap = per_cpu(rcu_dynticks, thatcpu).dynticks;
  956. snap_nmi = per_cpu(rcu_dynticks, thatcpu).dynticks_nmi;
  957. smp_mb(); /* Order sampling of snap with end of grace period. */
  958. if (((snap & 0x1) != 0) || ((snap_nmi & 0x1) != 0)) {
  959. per_cpu(rcu_dyntick_drain, cpu) = 0;
  960. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
  961. return rcu_needs_cpu_quick_check(cpu);
  962. }
  963. }
  964. /* Check and update the rcu_dyntick_drain sequencing. */
  965. if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  966. /* First time through, initialize the counter. */
  967. per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
  968. } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  969. /* We have hit the limit, so time to give up. */
  970. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
  971. return rcu_needs_cpu_quick_check(cpu);
  972. }
  973. /* Do one step pushing remaining RCU callbacks through. */
  974. if (per_cpu(rcu_sched_data, cpu).nxtlist) {
  975. rcu_sched_qs(cpu);
  976. force_quiescent_state(&rcu_sched_state, 0);
  977. c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
  978. }
  979. if (per_cpu(rcu_bh_data, cpu).nxtlist) {
  980. rcu_bh_qs(cpu);
  981. force_quiescent_state(&rcu_bh_state, 0);
  982. c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
  983. }
  984. /* If RCU callbacks are still pending, RCU still needs this CPU. */
  985. if (c)
  986. raise_softirq(RCU_SOFTIRQ);
  987. return c;
  988. }
  989. /*
  990. * Check to see if we need to continue a callback-flush operations to
  991. * allow the last CPU to enter dyntick-idle mode.
  992. */
  993. static void rcu_needs_cpu_flush(void)
  994. {
  995. int cpu = smp_processor_id();
  996. unsigned long flags;
  997. if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
  998. return;
  999. local_irq_save(flags);
  1000. (void)rcu_needs_cpu(cpu);
  1001. local_irq_restore(flags);
  1002. }
  1003. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */