rcutree_plugin.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 = 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. 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. Note, however, that
  492. * upon return from synchronize_rcu(), the caller might well be executing
  493. * concurrently with new RCU read-side critical sections that began while
  494. * synchronize_rcu() was waiting. RCU read-side critical sections are
  495. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  496. */
  497. void synchronize_rcu(void)
  498. {
  499. struct rcu_synchronize rcu;
  500. if (!rcu_scheduler_active)
  501. return;
  502. init_rcu_head_on_stack(&rcu.head);
  503. init_completion(&rcu.completion);
  504. /* Will wake me after RCU finished. */
  505. call_rcu(&rcu.head, wakeme_after_rcu);
  506. /* Wait for it. */
  507. wait_for_completion(&rcu.completion);
  508. destroy_rcu_head_on_stack(&rcu.head);
  509. }
  510. EXPORT_SYMBOL_GPL(synchronize_rcu);
  511. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  512. static long sync_rcu_preempt_exp_count;
  513. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  514. /*
  515. * Return non-zero if there are any tasks in RCU read-side critical
  516. * sections blocking the current preemptible-RCU expedited grace period.
  517. * If there is no preemptible-RCU expedited grace period currently in
  518. * progress, returns zero unconditionally.
  519. */
  520. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  521. {
  522. return !list_empty(&rnp->blocked_tasks[2]) ||
  523. !list_empty(&rnp->blocked_tasks[3]);
  524. }
  525. /*
  526. * return non-zero if there is no RCU expedited grace period in progress
  527. * for the specified rcu_node structure, in other words, if all CPUs and
  528. * tasks covered by the specified rcu_node structure have done their bit
  529. * for the current expedited grace period. Works only for preemptible
  530. * RCU -- other RCU implementation use other means.
  531. *
  532. * Caller must hold sync_rcu_preempt_exp_mutex.
  533. */
  534. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  535. {
  536. return !rcu_preempted_readers_exp(rnp) &&
  537. ACCESS_ONCE(rnp->expmask) == 0;
  538. }
  539. /*
  540. * Report the exit from RCU read-side critical section for the last task
  541. * that queued itself during or before the current expedited preemptible-RCU
  542. * grace period. This event is reported either to the rcu_node structure on
  543. * which the task was queued or to one of that rcu_node structure's ancestors,
  544. * recursively up the tree. (Calm down, calm down, we do the recursion
  545. * iteratively!)
  546. *
  547. * Caller must hold sync_rcu_preempt_exp_mutex.
  548. */
  549. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  550. {
  551. unsigned long flags;
  552. unsigned long mask;
  553. raw_spin_lock_irqsave(&rnp->lock, flags);
  554. for (;;) {
  555. if (!sync_rcu_preempt_exp_done(rnp))
  556. break;
  557. if (rnp->parent == NULL) {
  558. wake_up(&sync_rcu_preempt_exp_wq);
  559. break;
  560. }
  561. mask = rnp->grpmask;
  562. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  563. rnp = rnp->parent;
  564. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  565. rnp->expmask &= ~mask;
  566. }
  567. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  568. }
  569. /*
  570. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  571. * grace period for the specified rcu_node structure. If there are no such
  572. * tasks, report it up the rcu_node hierarchy.
  573. *
  574. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  575. */
  576. static void
  577. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  578. {
  579. int must_wait;
  580. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  581. list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
  582. list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
  583. must_wait = rcu_preempted_readers_exp(rnp);
  584. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  585. if (!must_wait)
  586. rcu_report_exp_rnp(rsp, rnp);
  587. }
  588. /*
  589. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  590. * is to invoke synchronize_sched_expedited() to push all the tasks to
  591. * the ->blocked_tasks[] lists, move all entries from the first set of
  592. * ->blocked_tasks[] lists to the second set, and finally wait for this
  593. * second set to drain.
  594. */
  595. void synchronize_rcu_expedited(void)
  596. {
  597. unsigned long flags;
  598. struct rcu_node *rnp;
  599. struct rcu_state *rsp = &rcu_preempt_state;
  600. long snap;
  601. int trycount = 0;
  602. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  603. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  604. smp_mb(); /* Above access cannot bleed into critical section. */
  605. /*
  606. * Acquire lock, falling back to synchronize_rcu() if too many
  607. * lock-acquisition failures. Of course, if someone does the
  608. * expedited grace period for us, just leave.
  609. */
  610. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  611. if (trycount++ < 10)
  612. udelay(trycount * num_online_cpus());
  613. else {
  614. synchronize_rcu();
  615. return;
  616. }
  617. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  618. goto mb_ret; /* Others did our work for us. */
  619. }
  620. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  621. goto unlock_mb_ret; /* Others did our work for us. */
  622. /* force all RCU readers onto blocked_tasks[]. */
  623. synchronize_sched_expedited();
  624. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  625. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  626. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  627. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  628. rnp->expmask = rnp->qsmaskinit;
  629. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  630. }
  631. /* Snapshot current state of ->blocked_tasks[] lists. */
  632. rcu_for_each_leaf_node(rsp, rnp)
  633. sync_rcu_preempt_exp_init(rsp, rnp);
  634. if (NUM_RCU_NODES > 1)
  635. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  636. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  637. /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
  638. rnp = rcu_get_root(rsp);
  639. wait_event(sync_rcu_preempt_exp_wq,
  640. sync_rcu_preempt_exp_done(rnp));
  641. /* Clean up and exit. */
  642. smp_mb(); /* ensure expedited GP seen before counter increment. */
  643. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  644. unlock_mb_ret:
  645. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  646. mb_ret:
  647. smp_mb(); /* ensure subsequent action seen after grace period. */
  648. }
  649. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  650. /*
  651. * Check to see if there is any immediate preemptable-RCU-related work
  652. * to be done.
  653. */
  654. static int rcu_preempt_pending(int cpu)
  655. {
  656. return __rcu_pending(&rcu_preempt_state,
  657. &per_cpu(rcu_preempt_data, cpu));
  658. }
  659. /*
  660. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  661. */
  662. static int rcu_preempt_needs_cpu(int cpu)
  663. {
  664. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  665. }
  666. /**
  667. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  668. */
  669. void rcu_barrier(void)
  670. {
  671. _rcu_barrier(&rcu_preempt_state, call_rcu);
  672. }
  673. EXPORT_SYMBOL_GPL(rcu_barrier);
  674. /*
  675. * Initialize preemptable RCU's per-CPU data.
  676. */
  677. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  678. {
  679. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  680. }
  681. /*
  682. * Move preemptable RCU's callbacks to ->orphan_cbs_list.
  683. */
  684. static void rcu_preempt_send_cbs_to_orphanage(void)
  685. {
  686. rcu_send_cbs_to_orphanage(&rcu_preempt_state);
  687. }
  688. /*
  689. * Initialize preemptable RCU's state structures.
  690. */
  691. static void __init __rcu_init_preempt(void)
  692. {
  693. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  694. }
  695. /*
  696. * Check for a task exiting while in a preemptable-RCU read-side
  697. * critical section, clean up if so. No need to issue warnings,
  698. * as debug_check_no_locks_held() already does this if lockdep
  699. * is enabled.
  700. */
  701. void exit_rcu(void)
  702. {
  703. struct task_struct *t = current;
  704. if (t->rcu_read_lock_nesting == 0)
  705. return;
  706. t->rcu_read_lock_nesting = 1;
  707. rcu_read_unlock();
  708. }
  709. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  710. /*
  711. * Tell them what RCU they are running.
  712. */
  713. static void __init rcu_bootup_announce(void)
  714. {
  715. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  716. rcu_bootup_announce_oddness();
  717. }
  718. /*
  719. * Return the number of RCU batches processed thus far for debug & stats.
  720. */
  721. long rcu_batches_completed(void)
  722. {
  723. return rcu_batches_completed_sched();
  724. }
  725. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  726. /*
  727. * Force a quiescent state for RCU, which, because there is no preemptible
  728. * RCU, becomes the same as rcu-sched.
  729. */
  730. void rcu_force_quiescent_state(void)
  731. {
  732. rcu_sched_force_quiescent_state();
  733. }
  734. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  735. /*
  736. * Because preemptable RCU does not exist, we never have to check for
  737. * CPUs being in quiescent states.
  738. */
  739. static void rcu_preempt_note_context_switch(int cpu)
  740. {
  741. }
  742. /*
  743. * Because preemptable RCU does not exist, there are never any preempted
  744. * RCU readers.
  745. */
  746. static int rcu_preempted_readers(struct rcu_node *rnp)
  747. {
  748. return 0;
  749. }
  750. #ifdef CONFIG_HOTPLUG_CPU
  751. /* Because preemptible RCU does not exist, no quieting of tasks. */
  752. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  753. {
  754. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  755. }
  756. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  757. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  758. /*
  759. * Because preemptable RCU does not exist, we never have to check for
  760. * tasks blocked within RCU read-side critical sections.
  761. */
  762. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  763. {
  764. }
  765. /*
  766. * Because preemptable RCU does not exist, we never have to check for
  767. * tasks blocked within RCU read-side critical sections.
  768. */
  769. static void rcu_print_task_stall(struct rcu_node *rnp)
  770. {
  771. }
  772. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  773. /*
  774. * Because there is no preemptable RCU, there can be no readers blocked,
  775. * so there is no need to check for blocked tasks. So check only for
  776. * bogus qsmask values.
  777. */
  778. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  779. {
  780. WARN_ON_ONCE(rnp->qsmask);
  781. }
  782. #ifdef CONFIG_HOTPLUG_CPU
  783. /*
  784. * Because preemptable RCU does not exist, it never needs to migrate
  785. * tasks that were blocked within RCU read-side critical sections, and
  786. * such non-existent tasks cannot possibly have been blocking the current
  787. * grace period.
  788. */
  789. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  790. struct rcu_node *rnp,
  791. struct rcu_data *rdp)
  792. {
  793. return 0;
  794. }
  795. /*
  796. * Because preemptable RCU does not exist, it never needs CPU-offline
  797. * processing.
  798. */
  799. static void rcu_preempt_offline_cpu(int cpu)
  800. {
  801. }
  802. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  803. /*
  804. * Because preemptable RCU does not exist, it never has any callbacks
  805. * to check.
  806. */
  807. static void rcu_preempt_check_callbacks(int cpu)
  808. {
  809. }
  810. /*
  811. * Because preemptable RCU does not exist, it never has any callbacks
  812. * to process.
  813. */
  814. static void rcu_preempt_process_callbacks(void)
  815. {
  816. }
  817. /*
  818. * In classic RCU, call_rcu() is just call_rcu_sched().
  819. */
  820. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  821. {
  822. call_rcu_sched(head, func);
  823. }
  824. EXPORT_SYMBOL_GPL(call_rcu);
  825. /*
  826. * Wait for an rcu-preempt grace period, but make it happen quickly.
  827. * But because preemptable RCU does not exist, map to rcu-sched.
  828. */
  829. void synchronize_rcu_expedited(void)
  830. {
  831. synchronize_sched_expedited();
  832. }
  833. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  834. #ifdef CONFIG_HOTPLUG_CPU
  835. /*
  836. * Because preemptable RCU does not exist, there is never any need to
  837. * report on tasks preempted in RCU read-side critical sections during
  838. * expedited RCU grace periods.
  839. */
  840. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  841. {
  842. return;
  843. }
  844. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  845. /*
  846. * Because preemptable RCU does not exist, it never has any work to do.
  847. */
  848. static int rcu_preempt_pending(int cpu)
  849. {
  850. return 0;
  851. }
  852. /*
  853. * Because preemptable RCU does not exist, it never needs any CPU.
  854. */
  855. static int rcu_preempt_needs_cpu(int cpu)
  856. {
  857. return 0;
  858. }
  859. /*
  860. * Because preemptable RCU does not exist, rcu_barrier() is just
  861. * another name for rcu_barrier_sched().
  862. */
  863. void rcu_barrier(void)
  864. {
  865. rcu_barrier_sched();
  866. }
  867. EXPORT_SYMBOL_GPL(rcu_barrier);
  868. /*
  869. * Because preemptable RCU does not exist, there is no per-CPU
  870. * data to initialize.
  871. */
  872. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  873. {
  874. }
  875. /*
  876. * Because there is no preemptable RCU, there are no callbacks to move.
  877. */
  878. static void rcu_preempt_send_cbs_to_orphanage(void)
  879. {
  880. }
  881. /*
  882. * Because preemptable RCU does not exist, it need not be initialized.
  883. */
  884. static void __init __rcu_init_preempt(void)
  885. {
  886. }
  887. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  888. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  889. /*
  890. * Check to see if any future RCU-related work will need to be done
  891. * by the current CPU, even if none need be done immediately, returning
  892. * 1 if so. This function is part of the RCU implementation; it is -not-
  893. * an exported member of the RCU API.
  894. *
  895. * Because we have preemptible RCU, just check whether this CPU needs
  896. * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
  897. * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
  898. */
  899. int rcu_needs_cpu(int cpu)
  900. {
  901. return rcu_needs_cpu_quick_check(cpu);
  902. }
  903. /*
  904. * Check to see if we need to continue a callback-flush operations to
  905. * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
  906. * entry is not configured, so we never do need to.
  907. */
  908. static void rcu_needs_cpu_flush(void)
  909. {
  910. }
  911. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  912. #define RCU_NEEDS_CPU_FLUSHES 5
  913. static DEFINE_PER_CPU(int, rcu_dyntick_drain);
  914. static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
  915. /*
  916. * Check to see if any future RCU-related work will need to be done
  917. * by the current CPU, even if none need be done immediately, returning
  918. * 1 if so. This function is part of the RCU implementation; it is -not-
  919. * an exported member of the RCU API.
  920. *
  921. * Because we are not supporting preemptible RCU, attempt to accelerate
  922. * any current grace periods so that RCU no longer needs this CPU, but
  923. * only if all other CPUs are already in dynticks-idle mode. This will
  924. * allow the CPU cores to be powered down immediately, as opposed to after
  925. * waiting many milliseconds for grace periods to elapse.
  926. *
  927. * Because it is not legal to invoke rcu_process_callbacks() with irqs
  928. * disabled, we do one pass of force_quiescent_state(), then do a
  929. * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
  930. * The per-cpu rcu_dyntick_drain variable controls the sequencing.
  931. */
  932. int rcu_needs_cpu(int cpu)
  933. {
  934. int c = 0;
  935. int snap;
  936. int snap_nmi;
  937. int thatcpu;
  938. /* Check for being in the holdoff period. */
  939. if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
  940. return rcu_needs_cpu_quick_check(cpu);
  941. /* Don't bother unless we are the last non-dyntick-idle CPU. */
  942. for_each_online_cpu(thatcpu) {
  943. if (thatcpu == cpu)
  944. continue;
  945. snap = per_cpu(rcu_dynticks, thatcpu).dynticks;
  946. snap_nmi = per_cpu(rcu_dynticks, thatcpu).dynticks_nmi;
  947. smp_mb(); /* Order sampling of snap with end of grace period. */
  948. if (((snap & 0x1) != 0) || ((snap_nmi & 0x1) != 0)) {
  949. per_cpu(rcu_dyntick_drain, cpu) = 0;
  950. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
  951. return rcu_needs_cpu_quick_check(cpu);
  952. }
  953. }
  954. /* Check and update the rcu_dyntick_drain sequencing. */
  955. if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  956. /* First time through, initialize the counter. */
  957. per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
  958. } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  959. /* We have hit the limit, so time to give up. */
  960. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
  961. return rcu_needs_cpu_quick_check(cpu);
  962. }
  963. /* Do one step pushing remaining RCU callbacks through. */
  964. if (per_cpu(rcu_sched_data, cpu).nxtlist) {
  965. rcu_sched_qs(cpu);
  966. force_quiescent_state(&rcu_sched_state, 0);
  967. c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
  968. }
  969. if (per_cpu(rcu_bh_data, cpu).nxtlist) {
  970. rcu_bh_qs(cpu);
  971. force_quiescent_state(&rcu_bh_state, 0);
  972. c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
  973. }
  974. /* If RCU callbacks are still pending, RCU still needs this CPU. */
  975. if (c)
  976. raise_softirq(RCU_SOFTIRQ);
  977. return c;
  978. }
  979. /*
  980. * Check to see if we need to continue a callback-flush operations to
  981. * allow the last CPU to enter dyntick-idle mode.
  982. */
  983. static void rcu_needs_cpu_flush(void)
  984. {
  985. int cpu = smp_processor_id();
  986. unsigned long flags;
  987. if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
  988. return;
  989. local_irq_save(flags);
  990. (void)rcu_needs_cpu(cpu);
  991. local_irq_restore(flags);
  992. }
  993. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */