rcutree_plugin.h 30 KB

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