rcutree_plugin.h 25 KB

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