rcupdate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2001
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. *
  23. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  24. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  25. * Papers:
  26. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  27. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  28. *
  29. * For detailed explanation of Read-Copy Update mechanism see -
  30. * http://lse.sourceforge.net/locking/rcupdate.html
  31. *
  32. */
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/smp.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <asm/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/module.h>
  43. #include <linux/completion.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/percpu.h>
  46. #include <linux/notifier.h>
  47. #include <linux/rcupdate.h>
  48. #include <linux/cpu.h>
  49. /* Definition for rcupdate control block. */
  50. struct rcu_ctrlblk rcu_ctrlblk =
  51. { .cur = -300, .completed = -300 };
  52. struct rcu_ctrlblk rcu_bh_ctrlblk =
  53. { .cur = -300, .completed = -300 };
  54. /* Bookkeeping of the progress of the grace period */
  55. struct rcu_state {
  56. spinlock_t lock; /* Guard this struct and writes to rcu_ctrlblk */
  57. cpumask_t cpumask; /* CPUs that need to switch in order */
  58. /* for current batch to proceed. */
  59. };
  60. static struct rcu_state rcu_state ____cacheline_maxaligned_in_smp =
  61. {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
  62. static struct rcu_state rcu_bh_state ____cacheline_maxaligned_in_smp =
  63. {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
  64. DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
  65. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
  66. /* Fake initialization required by compiler */
  67. static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
  68. static int maxbatch = 10;
  69. /**
  70. * call_rcu - Queue an RCU callback for invocation after a grace period.
  71. * @head: structure to be used for queueing the RCU updates.
  72. * @func: actual update function to be invoked after the grace period
  73. *
  74. * The update function will be invoked some time after a full grace
  75. * period elapses, in other words after all currently executing RCU
  76. * read-side critical sections have completed. RCU read-side critical
  77. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  78. * and may be nested.
  79. */
  80. void fastcall call_rcu(struct rcu_head *head,
  81. void (*func)(struct rcu_head *rcu))
  82. {
  83. unsigned long flags;
  84. struct rcu_data *rdp;
  85. head->func = func;
  86. head->next = NULL;
  87. local_irq_save(flags);
  88. rdp = &__get_cpu_var(rcu_data);
  89. *rdp->nxttail = head;
  90. rdp->nxttail = &head->next;
  91. local_irq_restore(flags);
  92. }
  93. /**
  94. * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
  95. * @head: structure to be used for queueing the RCU updates.
  96. * @func: actual update function to be invoked after the grace period
  97. *
  98. * The update function will be invoked some time after a full grace
  99. * period elapses, in other words after all currently executing RCU
  100. * read-side critical sections have completed. call_rcu_bh() assumes
  101. * that the read-side critical sections end on completion of a softirq
  102. * handler. This means that read-side critical sections in process
  103. * context must not be interrupted by softirqs. This interface is to be
  104. * used when most of the read-side critical sections are in softirq context.
  105. * RCU read-side critical sections are delimited by rcu_read_lock() and
  106. * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
  107. * and rcu_read_unlock_bh(), if in process context. These may be nested.
  108. */
  109. void fastcall call_rcu_bh(struct rcu_head *head,
  110. void (*func)(struct rcu_head *rcu))
  111. {
  112. unsigned long flags;
  113. struct rcu_data *rdp;
  114. head->func = func;
  115. head->next = NULL;
  116. local_irq_save(flags);
  117. rdp = &__get_cpu_var(rcu_bh_data);
  118. *rdp->nxttail = head;
  119. rdp->nxttail = &head->next;
  120. local_irq_restore(flags);
  121. }
  122. /*
  123. * Invoke the completed RCU callbacks. They are expected to be in
  124. * a per-cpu list.
  125. */
  126. static void rcu_do_batch(struct rcu_data *rdp)
  127. {
  128. struct rcu_head *next, *list;
  129. int count = 0;
  130. list = rdp->donelist;
  131. while (list) {
  132. next = rdp->donelist = list->next;
  133. list->func(list);
  134. list = next;
  135. if (++count >= maxbatch)
  136. break;
  137. }
  138. if (!rdp->donelist)
  139. rdp->donetail = &rdp->donelist;
  140. else
  141. tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
  142. }
  143. /*
  144. * Grace period handling:
  145. * The grace period handling consists out of two steps:
  146. * - A new grace period is started.
  147. * This is done by rcu_start_batch. The start is not broadcasted to
  148. * all cpus, they must pick this up by comparing rcp->cur with
  149. * rdp->quiescbatch. All cpus are recorded in the
  150. * rcu_state.cpumask bitmap.
  151. * - All cpus must go through a quiescent state.
  152. * Since the start of the grace period is not broadcasted, at least two
  153. * calls to rcu_check_quiescent_state are required:
  154. * The first call just notices that a new grace period is running. The
  155. * following calls check if there was a quiescent state since the beginning
  156. * of the grace period. If so, it updates rcu_state.cpumask. If
  157. * the bitmap is empty, then the grace period is completed.
  158. * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
  159. * period (if necessary).
  160. */
  161. /*
  162. * Register a new batch of callbacks, and start it up if there is currently no
  163. * active batch and the batch to be registered has not already occurred.
  164. * Caller must hold rcu_state.lock.
  165. */
  166. static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp,
  167. int next_pending)
  168. {
  169. if (next_pending)
  170. rcp->next_pending = 1;
  171. if (rcp->next_pending &&
  172. rcp->completed == rcp->cur) {
  173. /* Can't change, since spin lock held. */
  174. cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
  175. rcp->next_pending = 0;
  176. /* next_pending == 0 must be visible in __rcu_process_callbacks()
  177. * before it can see new value of cur.
  178. */
  179. smp_wmb();
  180. rcp->cur++;
  181. }
  182. }
  183. /*
  184. * cpu went through a quiescent state since the beginning of the grace period.
  185. * Clear it from the cpu mask and complete the grace period if it was the last
  186. * cpu. Start another grace period if someone has further entries pending
  187. */
  188. static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
  189. {
  190. cpu_clear(cpu, rsp->cpumask);
  191. if (cpus_empty(rsp->cpumask)) {
  192. /* batch completed ! */
  193. rcp->completed = rcp->cur;
  194. rcu_start_batch(rcp, rsp, 0);
  195. }
  196. }
  197. /*
  198. * Check if the cpu has gone through a quiescent state (say context
  199. * switch). If so and if it already hasn't done so in this RCU
  200. * quiescent cycle, then indicate that it has done so.
  201. */
  202. static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
  203. struct rcu_state *rsp, struct rcu_data *rdp)
  204. {
  205. if (rdp->quiescbatch != rcp->cur) {
  206. /* start new grace period: */
  207. rdp->qs_pending = 1;
  208. rdp->passed_quiesc = 0;
  209. rdp->quiescbatch = rcp->cur;
  210. return;
  211. }
  212. /* Grace period already completed for this cpu?
  213. * qs_pending is checked instead of the actual bitmap to avoid
  214. * cacheline trashing.
  215. */
  216. if (!rdp->qs_pending)
  217. return;
  218. /*
  219. * Was there a quiescent state since the beginning of the grace
  220. * period? If no, then exit and wait for the next call.
  221. */
  222. if (!rdp->passed_quiesc)
  223. return;
  224. rdp->qs_pending = 0;
  225. spin_lock(&rsp->lock);
  226. /*
  227. * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
  228. * during cpu startup. Ignore the quiescent state.
  229. */
  230. if (likely(rdp->quiescbatch == rcp->cur))
  231. cpu_quiet(rdp->cpu, rcp, rsp);
  232. spin_unlock(&rsp->lock);
  233. }
  234. #ifdef CONFIG_HOTPLUG_CPU
  235. /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
  236. * locking requirements, the list it's pulling from has to belong to a cpu
  237. * which is dead and hence not processing interrupts.
  238. */
  239. static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
  240. struct rcu_head **tail)
  241. {
  242. local_irq_disable();
  243. *this_rdp->nxttail = list;
  244. if (list)
  245. this_rdp->nxttail = tail;
  246. local_irq_enable();
  247. }
  248. static void __rcu_offline_cpu(struct rcu_data *this_rdp,
  249. struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
  250. {
  251. /* if the cpu going offline owns the grace period
  252. * we can block indefinitely waiting for it, so flush
  253. * it here
  254. */
  255. spin_lock_bh(&rsp->lock);
  256. if (rcp->cur != rcp->completed)
  257. cpu_quiet(rdp->cpu, rcp, rsp);
  258. spin_unlock_bh(&rsp->lock);
  259. rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
  260. rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
  261. }
  262. static void rcu_offline_cpu(int cpu)
  263. {
  264. struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
  265. struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
  266. __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
  267. &per_cpu(rcu_data, cpu));
  268. __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
  269. &per_cpu(rcu_bh_data, cpu));
  270. put_cpu_var(rcu_data);
  271. put_cpu_var(rcu_bh_data);
  272. tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
  273. }
  274. #else
  275. static void rcu_offline_cpu(int cpu)
  276. {
  277. }
  278. #endif
  279. /*
  280. * This does the RCU processing work from tasklet context.
  281. */
  282. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
  283. struct rcu_state *rsp, struct rcu_data *rdp)
  284. {
  285. if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
  286. *rdp->donetail = rdp->curlist;
  287. rdp->donetail = rdp->curtail;
  288. rdp->curlist = NULL;
  289. rdp->curtail = &rdp->curlist;
  290. }
  291. local_irq_disable();
  292. if (rdp->nxtlist && !rdp->curlist) {
  293. rdp->curlist = rdp->nxtlist;
  294. rdp->curtail = rdp->nxttail;
  295. rdp->nxtlist = NULL;
  296. rdp->nxttail = &rdp->nxtlist;
  297. local_irq_enable();
  298. /*
  299. * start the next batch of callbacks
  300. */
  301. /* determine batch number */
  302. rdp->batch = rcp->cur + 1;
  303. /* see the comment and corresponding wmb() in
  304. * the rcu_start_batch()
  305. */
  306. smp_rmb();
  307. if (!rcp->next_pending) {
  308. /* and start it/schedule start if it's a new batch */
  309. spin_lock(&rsp->lock);
  310. rcu_start_batch(rcp, rsp, 1);
  311. spin_unlock(&rsp->lock);
  312. }
  313. } else {
  314. local_irq_enable();
  315. }
  316. rcu_check_quiescent_state(rcp, rsp, rdp);
  317. if (rdp->donelist)
  318. rcu_do_batch(rdp);
  319. }
  320. static void rcu_process_callbacks(unsigned long unused)
  321. {
  322. __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
  323. &__get_cpu_var(rcu_data));
  324. __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
  325. &__get_cpu_var(rcu_bh_data));
  326. }
  327. void rcu_check_callbacks(int cpu, int user)
  328. {
  329. if (user ||
  330. (idle_cpu(cpu) && !in_softirq() &&
  331. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  332. rcu_qsctr_inc(cpu);
  333. rcu_bh_qsctr_inc(cpu);
  334. } else if (!in_softirq())
  335. rcu_bh_qsctr_inc(cpu);
  336. tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
  337. }
  338. static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
  339. struct rcu_data *rdp)
  340. {
  341. memset(rdp, 0, sizeof(*rdp));
  342. rdp->curtail = &rdp->curlist;
  343. rdp->nxttail = &rdp->nxtlist;
  344. rdp->donetail = &rdp->donelist;
  345. rdp->quiescbatch = rcp->completed;
  346. rdp->qs_pending = 0;
  347. rdp->cpu = cpu;
  348. }
  349. static void __devinit rcu_online_cpu(int cpu)
  350. {
  351. struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
  352. struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
  353. rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
  354. rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
  355. tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
  356. }
  357. static int __devinit rcu_cpu_notify(struct notifier_block *self,
  358. unsigned long action, void *hcpu)
  359. {
  360. long cpu = (long)hcpu;
  361. switch (action) {
  362. case CPU_UP_PREPARE:
  363. rcu_online_cpu(cpu);
  364. break;
  365. case CPU_DEAD:
  366. rcu_offline_cpu(cpu);
  367. break;
  368. default:
  369. break;
  370. }
  371. return NOTIFY_OK;
  372. }
  373. static struct notifier_block __devinitdata rcu_nb = {
  374. .notifier_call = rcu_cpu_notify,
  375. };
  376. /*
  377. * Initializes rcu mechanism. Assumed to be called early.
  378. * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
  379. * Note that rcu_qsctr and friends are implicitly
  380. * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
  381. */
  382. void __init rcu_init(void)
  383. {
  384. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
  385. (void *)(long)smp_processor_id());
  386. /* Register notifier for non-boot CPUs */
  387. register_cpu_notifier(&rcu_nb);
  388. }
  389. struct rcu_synchronize {
  390. struct rcu_head head;
  391. struct completion completion;
  392. };
  393. /* Because of FASTCALL declaration of complete, we use this wrapper */
  394. static void wakeme_after_rcu(struct rcu_head *head)
  395. {
  396. struct rcu_synchronize *rcu;
  397. rcu = container_of(head, struct rcu_synchronize, head);
  398. complete(&rcu->completion);
  399. }
  400. /**
  401. * synchronize_rcu - wait until a grace period has elapsed.
  402. *
  403. * Control will return to the caller some time after a full grace
  404. * period has elapsed, in other words after all currently executing RCU
  405. * read-side critical sections have completed. RCU read-side critical
  406. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  407. * and may be nested.
  408. *
  409. * If your read-side code is not protected by rcu_read_lock(), do -not-
  410. * use synchronize_rcu().
  411. */
  412. void synchronize_rcu(void)
  413. {
  414. struct rcu_synchronize rcu;
  415. init_completion(&rcu.completion);
  416. /* Will wake me after RCU finished */
  417. call_rcu(&rcu.head, wakeme_after_rcu);
  418. /* Wait for it */
  419. wait_for_completion(&rcu.completion);
  420. }
  421. /*
  422. * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
  423. */
  424. void synchronize_kernel(void)
  425. {
  426. synchronize_rcu();
  427. }
  428. module_param(maxbatch, int, 0);
  429. EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
  430. EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
  431. EXPORT_SYMBOL_GPL(synchronize_rcu);
  432. EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */