rcutree_plugin.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. 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. 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. 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. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  189. 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. spin_lock(&rnp->lock); /* irqs already disabled. */
  233. if (rnp == t->rcu_blocked_node)
  234. break;
  235. 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. 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. /*
  282. * Scan the current list of tasks blocked within RCU read-side critical
  283. * sections, printing out the tid of each.
  284. */
  285. static void rcu_print_task_stall(struct rcu_node *rnp)
  286. {
  287. unsigned long flags;
  288. struct list_head *lp;
  289. int phase;
  290. struct task_struct *t;
  291. if (rcu_preempted_readers(rnp)) {
  292. spin_lock_irqsave(&rnp->lock, flags);
  293. phase = rnp->gpnum & 0x1;
  294. lp = &rnp->blocked_tasks[phase];
  295. list_for_each_entry(t, lp, rcu_node_entry)
  296. printk(" P%d", t->pid);
  297. spin_unlock_irqrestore(&rnp->lock, flags);
  298. }
  299. }
  300. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  301. /*
  302. * Check that the list of blocked tasks for the newly completed grace
  303. * period is in fact empty. It is a serious bug to complete a grace
  304. * period that still has RCU readers blocked! This function must be
  305. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  306. * must be held by the caller.
  307. */
  308. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  309. {
  310. WARN_ON_ONCE(rcu_preempted_readers(rnp));
  311. WARN_ON_ONCE(rnp->qsmask);
  312. }
  313. #ifdef CONFIG_HOTPLUG_CPU
  314. /*
  315. * Handle tasklist migration for case in which all CPUs covered by the
  316. * specified rcu_node have gone offline. Move them up to the root
  317. * rcu_node. The reason for not just moving them to the immediate
  318. * parent is to remove the need for rcu_read_unlock_special() to
  319. * make more than two attempts to acquire the target rcu_node's lock.
  320. * Returns true if there were tasks blocking the current RCU grace
  321. * period.
  322. *
  323. * Returns 1 if there was previously a task blocking the current grace
  324. * period on the specified rcu_node structure.
  325. *
  326. * The caller must hold rnp->lock with irqs disabled.
  327. */
  328. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  329. struct rcu_node *rnp,
  330. struct rcu_data *rdp)
  331. {
  332. int i;
  333. struct list_head *lp;
  334. struct list_head *lp_root;
  335. int retval = 0;
  336. struct rcu_node *rnp_root = rcu_get_root(rsp);
  337. struct task_struct *tp;
  338. if (rnp == rnp_root) {
  339. WARN_ONCE(1, "Last CPU thought to be offlined?");
  340. return 0; /* Shouldn't happen: at least one CPU online. */
  341. }
  342. WARN_ON_ONCE(rnp != rdp->mynode &&
  343. (!list_empty(&rnp->blocked_tasks[0]) ||
  344. !list_empty(&rnp->blocked_tasks[1]) ||
  345. !list_empty(&rnp->blocked_tasks[2]) ||
  346. !list_empty(&rnp->blocked_tasks[3])));
  347. /*
  348. * Move tasks up to root rcu_node. Rely on the fact that the
  349. * root rcu_node can be at most one ahead of the rest of the
  350. * rcu_nodes in terms of gp_num value. This fact allows us to
  351. * move the blocked_tasks[] array directly, element by element.
  352. */
  353. if (rcu_preempted_readers(rnp))
  354. retval |= RCU_OFL_TASKS_NORM_GP;
  355. if (rcu_preempted_readers_exp(rnp))
  356. retval |= RCU_OFL_TASKS_EXP_GP;
  357. for (i = 0; i < 4; i++) {
  358. lp = &rnp->blocked_tasks[i];
  359. lp_root = &rnp_root->blocked_tasks[i];
  360. while (!list_empty(lp)) {
  361. tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
  362. spin_lock(&rnp_root->lock); /* irqs already disabled */
  363. list_del(&tp->rcu_node_entry);
  364. tp->rcu_blocked_node = rnp_root;
  365. list_add(&tp->rcu_node_entry, lp_root);
  366. spin_unlock(&rnp_root->lock); /* irqs remain disabled */
  367. }
  368. }
  369. return retval;
  370. }
  371. /*
  372. * Do CPU-offline processing for preemptable RCU.
  373. */
  374. static void rcu_preempt_offline_cpu(int cpu)
  375. {
  376. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  377. }
  378. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  379. /*
  380. * Check for a quiescent state from the current CPU. When a task blocks,
  381. * the task is recorded in the corresponding CPU's rcu_node structure,
  382. * which is checked elsewhere.
  383. *
  384. * Caller must disable hard irqs.
  385. */
  386. static void rcu_preempt_check_callbacks(int cpu)
  387. {
  388. struct task_struct *t = current;
  389. if (t->rcu_read_lock_nesting == 0) {
  390. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  391. rcu_preempt_qs(cpu);
  392. return;
  393. }
  394. if (per_cpu(rcu_preempt_data, cpu).qs_pending)
  395. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  396. }
  397. /*
  398. * Process callbacks for preemptable RCU.
  399. */
  400. static void rcu_preempt_process_callbacks(void)
  401. {
  402. __rcu_process_callbacks(&rcu_preempt_state,
  403. &__get_cpu_var(rcu_preempt_data));
  404. }
  405. /*
  406. * Queue a preemptable-RCU callback for invocation after a grace period.
  407. */
  408. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  409. {
  410. __call_rcu(head, func, &rcu_preempt_state);
  411. }
  412. EXPORT_SYMBOL_GPL(call_rcu);
  413. /**
  414. * synchronize_rcu - wait until a grace period has elapsed.
  415. *
  416. * Control will return to the caller some time after a full grace
  417. * period has elapsed, in other words after all currently executing RCU
  418. * read-side critical sections have completed. RCU read-side critical
  419. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  420. * and may be nested.
  421. */
  422. void synchronize_rcu(void)
  423. {
  424. struct rcu_synchronize rcu;
  425. if (!rcu_scheduler_active)
  426. return;
  427. init_completion(&rcu.completion);
  428. /* Will wake me after RCU finished. */
  429. call_rcu(&rcu.head, wakeme_after_rcu);
  430. /* Wait for it. */
  431. wait_for_completion(&rcu.completion);
  432. }
  433. EXPORT_SYMBOL_GPL(synchronize_rcu);
  434. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  435. static long sync_rcu_preempt_exp_count;
  436. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  437. /*
  438. * Return non-zero if there are any tasks in RCU read-side critical
  439. * sections blocking the current preemptible-RCU expedited grace period.
  440. * If there is no preemptible-RCU expedited grace period currently in
  441. * progress, returns zero unconditionally.
  442. */
  443. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  444. {
  445. return !list_empty(&rnp->blocked_tasks[2]) ||
  446. !list_empty(&rnp->blocked_tasks[3]);
  447. }
  448. /*
  449. * return non-zero if there is no RCU expedited grace period in progress
  450. * for the specified rcu_node structure, in other words, if all CPUs and
  451. * tasks covered by the specified rcu_node structure have done their bit
  452. * for the current expedited grace period. Works only for preemptible
  453. * RCU -- other RCU implementation use other means.
  454. *
  455. * Caller must hold sync_rcu_preempt_exp_mutex.
  456. */
  457. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  458. {
  459. return !rcu_preempted_readers_exp(rnp) &&
  460. ACCESS_ONCE(rnp->expmask) == 0;
  461. }
  462. /*
  463. * Report the exit from RCU read-side critical section for the last task
  464. * that queued itself during or before the current expedited preemptible-RCU
  465. * grace period. This event is reported either to the rcu_node structure on
  466. * which the task was queued or to one of that rcu_node structure's ancestors,
  467. * recursively up the tree. (Calm down, calm down, we do the recursion
  468. * iteratively!)
  469. *
  470. * Caller must hold sync_rcu_preempt_exp_mutex.
  471. */
  472. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  473. {
  474. unsigned long flags;
  475. unsigned long mask;
  476. spin_lock_irqsave(&rnp->lock, flags);
  477. for (;;) {
  478. if (!sync_rcu_preempt_exp_done(rnp))
  479. break;
  480. if (rnp->parent == NULL) {
  481. wake_up(&sync_rcu_preempt_exp_wq);
  482. break;
  483. }
  484. mask = rnp->grpmask;
  485. spin_unlock(&rnp->lock); /* irqs remain disabled */
  486. rnp = rnp->parent;
  487. spin_lock(&rnp->lock); /* irqs already disabled */
  488. rnp->expmask &= ~mask;
  489. }
  490. spin_unlock_irqrestore(&rnp->lock, flags);
  491. }
  492. /*
  493. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  494. * grace period for the specified rcu_node structure. If there are no such
  495. * tasks, report it up the rcu_node hierarchy.
  496. *
  497. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  498. */
  499. static void
  500. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  501. {
  502. int must_wait;
  503. spin_lock(&rnp->lock); /* irqs already disabled */
  504. list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
  505. list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
  506. must_wait = rcu_preempted_readers_exp(rnp);
  507. spin_unlock(&rnp->lock); /* irqs remain disabled */
  508. if (!must_wait)
  509. rcu_report_exp_rnp(rsp, rnp);
  510. }
  511. /*
  512. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  513. * is to invoke synchronize_sched_expedited() to push all the tasks to
  514. * the ->blocked_tasks[] lists, move all entries from the first set of
  515. * ->blocked_tasks[] lists to the second set, and finally wait for this
  516. * second set to drain.
  517. */
  518. void synchronize_rcu_expedited(void)
  519. {
  520. unsigned long flags;
  521. struct rcu_node *rnp;
  522. struct rcu_state *rsp = &rcu_preempt_state;
  523. long snap;
  524. int trycount = 0;
  525. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  526. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  527. smp_mb(); /* Above access cannot bleed into critical section. */
  528. /*
  529. * Acquire lock, falling back to synchronize_rcu() if too many
  530. * lock-acquisition failures. Of course, if someone does the
  531. * expedited grace period for us, just leave.
  532. */
  533. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  534. if (trycount++ < 10)
  535. udelay(trycount * num_online_cpus());
  536. else {
  537. synchronize_rcu();
  538. return;
  539. }
  540. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  541. goto mb_ret; /* Others did our work for us. */
  542. }
  543. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  544. goto unlock_mb_ret; /* Others did our work for us. */
  545. /* force all RCU readers onto blocked_tasks[]. */
  546. synchronize_sched_expedited();
  547. spin_lock_irqsave(&rsp->onofflock, flags);
  548. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  549. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  550. spin_lock(&rnp->lock); /* irqs already disabled. */
  551. rnp->expmask = rnp->qsmaskinit;
  552. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  553. }
  554. /* Snapshot current state of ->blocked_tasks[] lists. */
  555. rcu_for_each_leaf_node(rsp, rnp)
  556. sync_rcu_preempt_exp_init(rsp, rnp);
  557. if (NUM_RCU_NODES > 1)
  558. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  559. spin_unlock_irqrestore(&rsp->onofflock, flags);
  560. /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
  561. rnp = rcu_get_root(rsp);
  562. wait_event(sync_rcu_preempt_exp_wq,
  563. sync_rcu_preempt_exp_done(rnp));
  564. /* Clean up and exit. */
  565. smp_mb(); /* ensure expedited GP seen before counter increment. */
  566. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  567. unlock_mb_ret:
  568. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  569. mb_ret:
  570. smp_mb(); /* ensure subsequent action seen after grace period. */
  571. }
  572. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  573. /*
  574. * Check to see if there is any immediate preemptable-RCU-related work
  575. * to be done.
  576. */
  577. static int rcu_preempt_pending(int cpu)
  578. {
  579. return __rcu_pending(&rcu_preempt_state,
  580. &per_cpu(rcu_preempt_data, cpu));
  581. }
  582. /*
  583. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  584. */
  585. static int rcu_preempt_needs_cpu(int cpu)
  586. {
  587. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  588. }
  589. /**
  590. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  591. */
  592. void rcu_barrier(void)
  593. {
  594. _rcu_barrier(&rcu_preempt_state, call_rcu);
  595. }
  596. EXPORT_SYMBOL_GPL(rcu_barrier);
  597. /*
  598. * Initialize preemptable RCU's per-CPU data.
  599. */
  600. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  601. {
  602. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  603. }
  604. /*
  605. * Move preemptable RCU's callbacks to ->orphan_cbs_list.
  606. */
  607. static void rcu_preempt_send_cbs_to_orphanage(void)
  608. {
  609. rcu_send_cbs_to_orphanage(&rcu_preempt_state);
  610. }
  611. /*
  612. * Initialize preemptable RCU's state structures.
  613. */
  614. static void __init __rcu_init_preempt(void)
  615. {
  616. RCU_INIT_FLAVOR(&rcu_preempt_state, rcu_preempt_data);
  617. }
  618. /*
  619. * Check for a task exiting while in a preemptable-RCU read-side
  620. * critical section, clean up if so. No need to issue warnings,
  621. * as debug_check_no_locks_held() already does this if lockdep
  622. * is enabled.
  623. */
  624. void exit_rcu(void)
  625. {
  626. struct task_struct *t = current;
  627. if (t->rcu_read_lock_nesting == 0)
  628. return;
  629. t->rcu_read_lock_nesting = 1;
  630. rcu_read_unlock();
  631. }
  632. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  633. /*
  634. * Tell them what RCU they are running.
  635. */
  636. static void __init rcu_bootup_announce(void)
  637. {
  638. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  639. }
  640. /*
  641. * Return the number of RCU batches processed thus far for debug & stats.
  642. */
  643. long rcu_batches_completed(void)
  644. {
  645. return rcu_batches_completed_sched();
  646. }
  647. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  648. /*
  649. * Force a quiescent state for RCU, which, because there is no preemptible
  650. * RCU, becomes the same as rcu-sched.
  651. */
  652. void rcu_force_quiescent_state(void)
  653. {
  654. rcu_sched_force_quiescent_state();
  655. }
  656. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  657. /*
  658. * Because preemptable RCU does not exist, we never have to check for
  659. * CPUs being in quiescent states.
  660. */
  661. static void rcu_preempt_note_context_switch(int cpu)
  662. {
  663. }
  664. /*
  665. * Because preemptable RCU does not exist, there are never any preempted
  666. * RCU readers.
  667. */
  668. static int rcu_preempted_readers(struct rcu_node *rnp)
  669. {
  670. return 0;
  671. }
  672. #ifdef CONFIG_HOTPLUG_CPU
  673. /* Because preemptible RCU does not exist, no quieting of tasks. */
  674. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  675. {
  676. spin_unlock_irqrestore(&rnp->lock, flags);
  677. }
  678. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  679. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  680. /*
  681. * Because preemptable RCU does not exist, we never have to check for
  682. * tasks blocked within RCU read-side critical sections.
  683. */
  684. static void rcu_print_task_stall(struct rcu_node *rnp)
  685. {
  686. }
  687. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  688. /*
  689. * Because there is no preemptable RCU, there can be no readers blocked,
  690. * so there is no need to check for blocked tasks. So check only for
  691. * bogus qsmask values.
  692. */
  693. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  694. {
  695. WARN_ON_ONCE(rnp->qsmask);
  696. }
  697. #ifdef CONFIG_HOTPLUG_CPU
  698. /*
  699. * Because preemptable RCU does not exist, it never needs to migrate
  700. * tasks that were blocked within RCU read-side critical sections, and
  701. * such non-existent tasks cannot possibly have been blocking the current
  702. * grace period.
  703. */
  704. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  705. struct rcu_node *rnp,
  706. struct rcu_data *rdp)
  707. {
  708. return 0;
  709. }
  710. /*
  711. * Because preemptable RCU does not exist, it never needs CPU-offline
  712. * processing.
  713. */
  714. static void rcu_preempt_offline_cpu(int cpu)
  715. {
  716. }
  717. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  718. /*
  719. * Because preemptable RCU does not exist, it never has any callbacks
  720. * to check.
  721. */
  722. static void rcu_preempt_check_callbacks(int cpu)
  723. {
  724. }
  725. /*
  726. * Because preemptable RCU does not exist, it never has any callbacks
  727. * to process.
  728. */
  729. static void rcu_preempt_process_callbacks(void)
  730. {
  731. }
  732. /*
  733. * In classic RCU, call_rcu() is just call_rcu_sched().
  734. */
  735. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  736. {
  737. call_rcu_sched(head, func);
  738. }
  739. EXPORT_SYMBOL_GPL(call_rcu);
  740. /*
  741. * Wait for an rcu-preempt grace period, but make it happen quickly.
  742. * But because preemptable RCU does not exist, map to rcu-sched.
  743. */
  744. void synchronize_rcu_expedited(void)
  745. {
  746. synchronize_sched_expedited();
  747. }
  748. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  749. #ifdef CONFIG_HOTPLUG_CPU
  750. /*
  751. * Because preemptable RCU does not exist, there is never any need to
  752. * report on tasks preempted in RCU read-side critical sections during
  753. * expedited RCU grace periods.
  754. */
  755. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  756. {
  757. return;
  758. }
  759. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  760. /*
  761. * Because preemptable RCU does not exist, it never has any work to do.
  762. */
  763. static int rcu_preempt_pending(int cpu)
  764. {
  765. return 0;
  766. }
  767. /*
  768. * Because preemptable RCU does not exist, it never needs any CPU.
  769. */
  770. static int rcu_preempt_needs_cpu(int cpu)
  771. {
  772. return 0;
  773. }
  774. /*
  775. * Because preemptable RCU does not exist, rcu_barrier() is just
  776. * another name for rcu_barrier_sched().
  777. */
  778. void rcu_barrier(void)
  779. {
  780. rcu_barrier_sched();
  781. }
  782. EXPORT_SYMBOL_GPL(rcu_barrier);
  783. /*
  784. * Because preemptable RCU does not exist, there is no per-CPU
  785. * data to initialize.
  786. */
  787. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  788. {
  789. }
  790. /*
  791. * Because there is no preemptable RCU, there are no callbacks to move.
  792. */
  793. static void rcu_preempt_send_cbs_to_orphanage(void)
  794. {
  795. }
  796. /*
  797. * Because preemptable RCU does not exist, it need not be initialized.
  798. */
  799. static void __init __rcu_init_preempt(void)
  800. {
  801. }
  802. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */