rcutree_plugin.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #ifdef CONFIG_TREE_PREEMPT_RCU
  27. struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
  28. DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
  29. /*
  30. * Tell them what RCU they are running.
  31. */
  32. static inline void rcu_bootup_announce(void)
  33. {
  34. printk(KERN_INFO
  35. "Experimental preemptable hierarchical RCU implementation.\n");
  36. }
  37. /*
  38. * Return the number of RCU-preempt batches processed thus far
  39. * for debug and statistics.
  40. */
  41. long rcu_batches_completed_preempt(void)
  42. {
  43. return rcu_preempt_state.completed;
  44. }
  45. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  46. /*
  47. * Return the number of RCU batches processed thus far for debug & stats.
  48. */
  49. long rcu_batches_completed(void)
  50. {
  51. return rcu_batches_completed_preempt();
  52. }
  53. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  54. /*
  55. * Record a preemptable-RCU quiescent state for the specified CPU. Note
  56. * that this just means that the task currently running on the CPU is
  57. * not in a quiescent state. There might be any number of tasks blocked
  58. * while in an RCU read-side critical section.
  59. */
  60. static void rcu_preempt_qs(int cpu)
  61. {
  62. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  63. rdp->passed_quiesc_completed = rdp->completed;
  64. barrier();
  65. rdp->passed_quiesc = 1;
  66. }
  67. /*
  68. * We have entered the scheduler, and the current task might soon be
  69. * context-switched away from. If this task is in an RCU read-side
  70. * critical section, we will no longer be able to rely on the CPU to
  71. * record that fact, so we enqueue the task on the appropriate entry
  72. * of the blocked_tasks[] array. The task will dequeue itself when
  73. * it exits the outermost enclosing RCU read-side critical section.
  74. * Therefore, the current grace period cannot be permitted to complete
  75. * until the blocked_tasks[] entry indexed by the low-order bit of
  76. * rnp->gpnum empties.
  77. *
  78. * Caller must disable preemption.
  79. */
  80. static void rcu_preempt_note_context_switch(int cpu)
  81. {
  82. struct task_struct *t = current;
  83. unsigned long flags;
  84. int phase;
  85. struct rcu_data *rdp;
  86. struct rcu_node *rnp;
  87. if (t->rcu_read_lock_nesting &&
  88. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  89. /* Possibly blocking in an RCU read-side critical section. */
  90. rdp = rcu_preempt_state.rda[cpu];
  91. rnp = rdp->mynode;
  92. spin_lock_irqsave(&rnp->lock, flags);
  93. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  94. t->rcu_blocked_node = rnp;
  95. /*
  96. * If this CPU has already checked in, then this task
  97. * will hold up the next grace period rather than the
  98. * current grace period. Queue the task accordingly.
  99. * If the task is queued for the current grace period
  100. * (i.e., this CPU has not yet passed through a quiescent
  101. * state for the current grace period), then as long
  102. * as that task remains queued, the current grace period
  103. * cannot end.
  104. *
  105. * But first, note that the current CPU must still be
  106. * on line!
  107. */
  108. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  109. phase = !(rnp->qsmask & rdp->grpmask) ^ (rnp->gpnum & 0x1);
  110. list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
  111. smp_mb(); /* Ensure later ctxt swtch seen after above. */
  112. spin_unlock_irqrestore(&rnp->lock, flags);
  113. }
  114. /*
  115. * Either we were not in an RCU read-side critical section to
  116. * begin with, or we have now recorded that critical section
  117. * globally. Either way, we can now note a quiescent state
  118. * for this CPU. Again, if we were in an RCU read-side critical
  119. * section, and if that critical section was blocking the current
  120. * grace period, then the fact that the task has been enqueued
  121. * means that we continue to block the current grace period.
  122. */
  123. rcu_preempt_qs(cpu);
  124. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  125. }
  126. /*
  127. * Tree-preemptable RCU implementation for rcu_read_lock().
  128. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  129. * if we block.
  130. */
  131. void __rcu_read_lock(void)
  132. {
  133. ACCESS_ONCE(current->rcu_read_lock_nesting)++;
  134. barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
  135. }
  136. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  137. static void rcu_read_unlock_special(struct task_struct *t)
  138. {
  139. int empty;
  140. unsigned long flags;
  141. unsigned long mask;
  142. struct rcu_node *rnp;
  143. int special;
  144. /* NMI handlers cannot block and cannot safely manipulate state. */
  145. if (in_nmi())
  146. return;
  147. local_irq_save(flags);
  148. /*
  149. * If RCU core is waiting for this CPU to exit critical section,
  150. * let it know that we have done so.
  151. */
  152. special = t->rcu_read_unlock_special;
  153. if (special & RCU_READ_UNLOCK_NEED_QS) {
  154. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  155. rcu_preempt_qs(smp_processor_id());
  156. }
  157. /* Hardware IRQ handlers cannot block. */
  158. if (in_irq()) {
  159. local_irq_restore(flags);
  160. return;
  161. }
  162. /* Clean up if blocked during RCU read-side critical section. */
  163. if (special & RCU_READ_UNLOCK_BLOCKED) {
  164. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  165. /*
  166. * Remove this task from the list it blocked on. The
  167. * task can migrate while we acquire the lock, but at
  168. * most one time. So at most two passes through loop.
  169. */
  170. for (;;) {
  171. rnp = t->rcu_blocked_node;
  172. spin_lock(&rnp->lock);
  173. if (rnp == t->rcu_blocked_node)
  174. break;
  175. spin_unlock(&rnp->lock);
  176. }
  177. empty = list_empty(&rnp->blocked_tasks[rnp->gpnum & 0x1]);
  178. list_del_init(&t->rcu_node_entry);
  179. t->rcu_blocked_node = NULL;
  180. /*
  181. * If this was the last task on the current list, and if
  182. * we aren't waiting on any CPUs, report the quiescent state.
  183. * Note that both cpu_quiet_msk_finish() and cpu_quiet_msk()
  184. * drop rnp->lock and restore irq.
  185. */
  186. if (!empty && rnp->qsmask == 0 &&
  187. list_empty(&rnp->blocked_tasks[rnp->gpnum & 0x1])) {
  188. struct rcu_node *rnp_p;
  189. if (rnp->parent == NULL) {
  190. /* Only one rcu_node in the tree. */
  191. cpu_quiet_msk_finish(&rcu_preempt_state, flags);
  192. return;
  193. }
  194. /* Report up the rest of the hierarchy. */
  195. mask = rnp->grpmask;
  196. spin_unlock_irqrestore(&rnp->lock, flags);
  197. rnp_p = rnp->parent;
  198. spin_lock_irqsave(&rnp_p->lock, flags);
  199. WARN_ON_ONCE(rnp->qsmask);
  200. cpu_quiet_msk(mask, &rcu_preempt_state, rnp_p, flags);
  201. return;
  202. }
  203. spin_unlock(&rnp->lock);
  204. }
  205. local_irq_restore(flags);
  206. }
  207. /*
  208. * Tree-preemptable RCU implementation for rcu_read_unlock().
  209. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  210. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  211. * invoke rcu_read_unlock_special() to clean up after a context switch
  212. * in an RCU read-side critical section and other special cases.
  213. */
  214. void __rcu_read_unlock(void)
  215. {
  216. struct task_struct *t = current;
  217. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
  218. if (--ACCESS_ONCE(t->rcu_read_lock_nesting) == 0 &&
  219. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  220. rcu_read_unlock_special(t);
  221. }
  222. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  223. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  224. /*
  225. * Scan the current list of tasks blocked within RCU read-side critical
  226. * sections, printing out the tid of each.
  227. */
  228. static void rcu_print_task_stall(struct rcu_node *rnp)
  229. {
  230. unsigned long flags;
  231. struct list_head *lp;
  232. int phase = rnp->gpnum & 0x1;
  233. struct task_struct *t;
  234. if (!list_empty(&rnp->blocked_tasks[phase])) {
  235. spin_lock_irqsave(&rnp->lock, flags);
  236. phase = rnp->gpnum & 0x1; /* re-read under lock. */
  237. lp = &rnp->blocked_tasks[phase];
  238. list_for_each_entry(t, lp, rcu_node_entry)
  239. printk(" P%d", t->pid);
  240. spin_unlock_irqrestore(&rnp->lock, flags);
  241. }
  242. }
  243. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  244. /*
  245. * Check that the list of blocked tasks for the newly completed grace
  246. * period is in fact empty. It is a serious bug to complete a grace
  247. * period that still has RCU readers blocked! This function must be
  248. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  249. * must be held by the caller.
  250. */
  251. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  252. {
  253. WARN_ON_ONCE(!list_empty(&rnp->blocked_tasks[rnp->gpnum & 0x1]));
  254. WARN_ON_ONCE(rnp->qsmask);
  255. }
  256. /*
  257. * Check for preempted RCU readers for the specified rcu_node structure.
  258. * If the caller needs a reliable answer, it must hold the rcu_node's
  259. * >lock.
  260. */
  261. static int rcu_preempted_readers(struct rcu_node *rnp)
  262. {
  263. return !list_empty(&rnp->blocked_tasks[rnp->gpnum & 0x1]);
  264. }
  265. #ifdef CONFIG_HOTPLUG_CPU
  266. /*
  267. * Handle tasklist migration for case in which all CPUs covered by the
  268. * specified rcu_node have gone offline. Move them up to the root
  269. * rcu_node. The reason for not just moving them to the immediate
  270. * parent is to remove the need for rcu_read_unlock_special() to
  271. * make more than two attempts to acquire the target rcu_node's lock.
  272. *
  273. * The caller must hold rnp->lock with irqs disabled.
  274. */
  275. static void rcu_preempt_offline_tasks(struct rcu_state *rsp,
  276. struct rcu_node *rnp,
  277. struct rcu_data *rdp)
  278. {
  279. int i;
  280. struct list_head *lp;
  281. struct list_head *lp_root;
  282. struct rcu_node *rnp_root = rcu_get_root(rsp);
  283. struct task_struct *tp;
  284. if (rnp == rnp_root) {
  285. WARN_ONCE(1, "Last CPU thought to be offlined?");
  286. return; /* Shouldn't happen: at least one CPU online. */
  287. }
  288. WARN_ON_ONCE(rnp != rdp->mynode &&
  289. (!list_empty(&rnp->blocked_tasks[0]) ||
  290. !list_empty(&rnp->blocked_tasks[1])));
  291. /*
  292. * Move tasks up to root rcu_node. Rely on the fact that the
  293. * root rcu_node can be at most one ahead of the rest of the
  294. * rcu_nodes in terms of gp_num value. This fact allows us to
  295. * move the blocked_tasks[] array directly, element by element.
  296. */
  297. for (i = 0; i < 2; i++) {
  298. lp = &rnp->blocked_tasks[i];
  299. lp_root = &rnp_root->blocked_tasks[i];
  300. while (!list_empty(lp)) {
  301. tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
  302. spin_lock(&rnp_root->lock); /* irqs already disabled */
  303. list_del(&tp->rcu_node_entry);
  304. tp->rcu_blocked_node = rnp_root;
  305. list_add(&tp->rcu_node_entry, lp_root);
  306. spin_unlock(&rnp_root->lock); /* irqs remain disabled */
  307. }
  308. }
  309. }
  310. /*
  311. * Do CPU-offline processing for preemptable RCU.
  312. */
  313. static void rcu_preempt_offline_cpu(int cpu)
  314. {
  315. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  316. }
  317. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  318. /*
  319. * Check for a quiescent state from the current CPU. When a task blocks,
  320. * the task is recorded in the corresponding CPU's rcu_node structure,
  321. * which is checked elsewhere.
  322. *
  323. * Caller must disable hard irqs.
  324. */
  325. static void rcu_preempt_check_callbacks(int cpu)
  326. {
  327. struct task_struct *t = current;
  328. if (t->rcu_read_lock_nesting == 0) {
  329. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  330. rcu_preempt_qs(cpu);
  331. return;
  332. }
  333. if (per_cpu(rcu_preempt_data, cpu).qs_pending) {
  334. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  335. }
  336. }
  337. /*
  338. * Process callbacks for preemptable RCU.
  339. */
  340. static void rcu_preempt_process_callbacks(void)
  341. {
  342. __rcu_process_callbacks(&rcu_preempt_state,
  343. &__get_cpu_var(rcu_preempt_data));
  344. }
  345. /*
  346. * Queue a preemptable-RCU callback for invocation after a grace period.
  347. */
  348. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  349. {
  350. __call_rcu(head, func, &rcu_preempt_state);
  351. }
  352. EXPORT_SYMBOL_GPL(call_rcu);
  353. /*
  354. * Check to see if there is any immediate preemptable-RCU-related work
  355. * to be done.
  356. */
  357. static int rcu_preempt_pending(int cpu)
  358. {
  359. return __rcu_pending(&rcu_preempt_state,
  360. &per_cpu(rcu_preempt_data, cpu));
  361. }
  362. /*
  363. * Does preemptable RCU need the CPU to stay out of dynticks mode?
  364. */
  365. static int rcu_preempt_needs_cpu(int cpu)
  366. {
  367. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  368. }
  369. /*
  370. * Initialize preemptable RCU's per-CPU data.
  371. */
  372. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  373. {
  374. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  375. }
  376. /*
  377. * Check for a task exiting while in a preemptable-RCU read-side
  378. * critical section, clean up if so. No need to issue warnings,
  379. * as debug_check_no_locks_held() already does this if lockdep
  380. * is enabled.
  381. */
  382. void exit_rcu(void)
  383. {
  384. struct task_struct *t = current;
  385. if (t->rcu_read_lock_nesting == 0)
  386. return;
  387. t->rcu_read_lock_nesting = 1;
  388. rcu_read_unlock();
  389. }
  390. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  391. /*
  392. * Tell them what RCU they are running.
  393. */
  394. static inline void rcu_bootup_announce(void)
  395. {
  396. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  397. }
  398. /*
  399. * Return the number of RCU batches processed thus far for debug & stats.
  400. */
  401. long rcu_batches_completed(void)
  402. {
  403. return rcu_batches_completed_sched();
  404. }
  405. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  406. /*
  407. * Because preemptable RCU does not exist, we never have to check for
  408. * CPUs being in quiescent states.
  409. */
  410. static void rcu_preempt_note_context_switch(int cpu)
  411. {
  412. }
  413. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  414. /*
  415. * Because preemptable RCU does not exist, we never have to check for
  416. * tasks blocked within RCU read-side critical sections.
  417. */
  418. static void rcu_print_task_stall(struct rcu_node *rnp)
  419. {
  420. }
  421. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  422. /*
  423. * Because there is no preemptable RCU, there can be no readers blocked,
  424. * so there is no need to check for blocked tasks.
  425. */
  426. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  427. {
  428. }
  429. /*
  430. * Because preemptable RCU does not exist, there are never any preempted
  431. * RCU readers.
  432. */
  433. static int rcu_preempted_readers(struct rcu_node *rnp)
  434. {
  435. return 0;
  436. }
  437. #ifdef CONFIG_HOTPLUG_CPU
  438. /*
  439. * Because preemptable RCU does not exist, it never needs to migrate
  440. * tasks that were blocked within RCU read-side critical sections.
  441. */
  442. static void rcu_preempt_offline_tasks(struct rcu_state *rsp,
  443. struct rcu_node *rnp,
  444. struct rcu_data *rdp)
  445. {
  446. }
  447. /*
  448. * Because preemptable RCU does not exist, it never needs CPU-offline
  449. * processing.
  450. */
  451. static void rcu_preempt_offline_cpu(int cpu)
  452. {
  453. }
  454. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  455. /*
  456. * Because preemptable RCU does not exist, it never has any callbacks
  457. * to check.
  458. */
  459. void rcu_preempt_check_callbacks(int cpu)
  460. {
  461. }
  462. /*
  463. * Because preemptable RCU does not exist, it never has any callbacks
  464. * to process.
  465. */
  466. void rcu_preempt_process_callbacks(void)
  467. {
  468. }
  469. /*
  470. * In classic RCU, call_rcu() is just call_rcu_sched().
  471. */
  472. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  473. {
  474. call_rcu_sched(head, func);
  475. }
  476. EXPORT_SYMBOL_GPL(call_rcu);
  477. /*
  478. * Because preemptable RCU does not exist, it never has any work to do.
  479. */
  480. static int rcu_preempt_pending(int cpu)
  481. {
  482. return 0;
  483. }
  484. /*
  485. * Because preemptable RCU does not exist, it never needs any CPU.
  486. */
  487. static int rcu_preempt_needs_cpu(int cpu)
  488. {
  489. return 0;
  490. }
  491. /*
  492. * Because preemptable RCU does not exist, there is no per-CPU
  493. * data to initialize.
  494. */
  495. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  496. {
  497. }
  498. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */