rcutree_plugin.h 14 KB

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