rcupdate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/rcuref.h>
  49. #include <linux/cpu.h>
  50. /* Definition for rcupdate control block. */
  51. struct rcu_ctrlblk rcu_ctrlblk =
  52. { .cur = -300, .completed = -300 };
  53. struct rcu_ctrlblk rcu_bh_ctrlblk =
  54. { .cur = -300, .completed = -300 };
  55. /* Bookkeeping of the progress of the grace period */
  56. struct rcu_state {
  57. spinlock_t lock; /* Guard this struct and writes to rcu_ctrlblk */
  58. cpumask_t cpumask; /* CPUs that need to switch in order */
  59. /* for current batch to proceed. */
  60. };
  61. static struct rcu_state rcu_state ____cacheline_maxaligned_in_smp =
  62. {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
  63. static struct rcu_state rcu_bh_state ____cacheline_maxaligned_in_smp =
  64. {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
  65. DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
  66. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
  67. /* Fake initialization required by compiler */
  68. static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
  69. static int maxbatch = 10000;
  70. #ifndef __HAVE_ARCH_CMPXCHG
  71. /*
  72. * We use an array of spinlocks for the rcurefs -- similar to ones in sparc
  73. * 32 bit atomic_t implementations, and a hash function similar to that
  74. * for our refcounting needs.
  75. * Can't help multiprocessors which donot have cmpxchg :(
  76. */
  77. spinlock_t __rcuref_hash[RCUREF_HASH_SIZE] = {
  78. [0 ... (RCUREF_HASH_SIZE-1)] = SPIN_LOCK_UNLOCKED
  79. };
  80. #endif
  81. /**
  82. * call_rcu - Queue an RCU callback for invocation after a grace period.
  83. * @head: structure to be used for queueing the RCU updates.
  84. * @func: actual update function to be invoked after the grace period
  85. *
  86. * The update function will be invoked some time after a full grace
  87. * period elapses, in other words after all currently executing RCU
  88. * read-side critical sections have completed. RCU read-side critical
  89. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  90. * and may be nested.
  91. */
  92. void fastcall call_rcu(struct rcu_head *head,
  93. void (*func)(struct rcu_head *rcu))
  94. {
  95. unsigned long flags;
  96. struct rcu_data *rdp;
  97. head->func = func;
  98. head->next = NULL;
  99. local_irq_save(flags);
  100. rdp = &__get_cpu_var(rcu_data);
  101. *rdp->nxttail = head;
  102. rdp->nxttail = &head->next;
  103. if (unlikely(++rdp->count > 10000))
  104. set_need_resched();
  105. local_irq_restore(flags);
  106. }
  107. /**
  108. * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
  109. * @head: structure to be used for queueing the RCU updates.
  110. * @func: actual update function to be invoked after the grace period
  111. *
  112. * The update function will be invoked some time after a full grace
  113. * period elapses, in other words after all currently executing RCU
  114. * read-side critical sections have completed. call_rcu_bh() assumes
  115. * that the read-side critical sections end on completion of a softirq
  116. * handler. This means that read-side critical sections in process
  117. * context must not be interrupted by softirqs. This interface is to be
  118. * used when most of the read-side critical sections are in softirq context.
  119. * RCU read-side critical sections are delimited by rcu_read_lock() and
  120. * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
  121. * and rcu_read_unlock_bh(), if in process context. These may be nested.
  122. */
  123. void fastcall call_rcu_bh(struct rcu_head *head,
  124. void (*func)(struct rcu_head *rcu))
  125. {
  126. unsigned long flags;
  127. struct rcu_data *rdp;
  128. head->func = func;
  129. head->next = NULL;
  130. local_irq_save(flags);
  131. rdp = &__get_cpu_var(rcu_bh_data);
  132. *rdp->nxttail = head;
  133. rdp->nxttail = &head->next;
  134. rdp->count++;
  135. /*
  136. * Should we directly call rcu_do_batch() here ?
  137. * if (unlikely(rdp->count > 10000))
  138. * rcu_do_batch(rdp);
  139. */
  140. local_irq_restore(flags);
  141. }
  142. /*
  143. * Return the number of RCU batches processed thus far. Useful
  144. * for debug and statistics.
  145. */
  146. long rcu_batches_completed(void)
  147. {
  148. return rcu_ctrlblk.completed;
  149. }
  150. /*
  151. * Invoke the completed RCU callbacks. They are expected to be in
  152. * a per-cpu list.
  153. */
  154. static void rcu_do_batch(struct rcu_data *rdp)
  155. {
  156. struct rcu_head *next, *list;
  157. int count = 0;
  158. list = rdp->donelist;
  159. while (list) {
  160. next = rdp->donelist = list->next;
  161. list->func(list);
  162. list = next;
  163. rdp->count--;
  164. if (++count >= maxbatch)
  165. break;
  166. }
  167. if (!rdp->donelist)
  168. rdp->donetail = &rdp->donelist;
  169. else
  170. tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
  171. }
  172. /*
  173. * Grace period handling:
  174. * The grace period handling consists out of two steps:
  175. * - A new grace period is started.
  176. * This is done by rcu_start_batch. The start is not broadcasted to
  177. * all cpus, they must pick this up by comparing rcp->cur with
  178. * rdp->quiescbatch. All cpus are recorded in the
  179. * rcu_state.cpumask bitmap.
  180. * - All cpus must go through a quiescent state.
  181. * Since the start of the grace period is not broadcasted, at least two
  182. * calls to rcu_check_quiescent_state are required:
  183. * The first call just notices that a new grace period is running. The
  184. * following calls check if there was a quiescent state since the beginning
  185. * of the grace period. If so, it updates rcu_state.cpumask. If
  186. * the bitmap is empty, then the grace period is completed.
  187. * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
  188. * period (if necessary).
  189. */
  190. /*
  191. * Register a new batch of callbacks, and start it up if there is currently no
  192. * active batch and the batch to be registered has not already occurred.
  193. * Caller must hold rcu_state.lock.
  194. */
  195. static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp,
  196. int next_pending)
  197. {
  198. if (next_pending)
  199. rcp->next_pending = 1;
  200. if (rcp->next_pending &&
  201. rcp->completed == rcp->cur) {
  202. /* Can't change, since spin lock held. */
  203. cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
  204. rcp->next_pending = 0;
  205. /* next_pending == 0 must be visible in __rcu_process_callbacks()
  206. * before it can see new value of cur.
  207. */
  208. smp_wmb();
  209. rcp->cur++;
  210. }
  211. }
  212. /*
  213. * cpu went through a quiescent state since the beginning of the grace period.
  214. * Clear it from the cpu mask and complete the grace period if it was the last
  215. * cpu. Start another grace period if someone has further entries pending
  216. */
  217. static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
  218. {
  219. cpu_clear(cpu, rsp->cpumask);
  220. if (cpus_empty(rsp->cpumask)) {
  221. /* batch completed ! */
  222. rcp->completed = rcp->cur;
  223. rcu_start_batch(rcp, rsp, 0);
  224. }
  225. }
  226. /*
  227. * Check if the cpu has gone through a quiescent state (say context
  228. * switch). If so and if it already hasn't done so in this RCU
  229. * quiescent cycle, then indicate that it has done so.
  230. */
  231. static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
  232. struct rcu_state *rsp, struct rcu_data *rdp)
  233. {
  234. if (rdp->quiescbatch != rcp->cur) {
  235. /* start new grace period: */
  236. rdp->qs_pending = 1;
  237. rdp->passed_quiesc = 0;
  238. rdp->quiescbatch = rcp->cur;
  239. return;
  240. }
  241. /* Grace period already completed for this cpu?
  242. * qs_pending is checked instead of the actual bitmap to avoid
  243. * cacheline trashing.
  244. */
  245. if (!rdp->qs_pending)
  246. return;
  247. /*
  248. * Was there a quiescent state since the beginning of the grace
  249. * period? If no, then exit and wait for the next call.
  250. */
  251. if (!rdp->passed_quiesc)
  252. return;
  253. rdp->qs_pending = 0;
  254. spin_lock(&rsp->lock);
  255. /*
  256. * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
  257. * during cpu startup. Ignore the quiescent state.
  258. */
  259. if (likely(rdp->quiescbatch == rcp->cur))
  260. cpu_quiet(rdp->cpu, rcp, rsp);
  261. spin_unlock(&rsp->lock);
  262. }
  263. #ifdef CONFIG_HOTPLUG_CPU
  264. /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
  265. * locking requirements, the list it's pulling from has to belong to a cpu
  266. * which is dead and hence not processing interrupts.
  267. */
  268. static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
  269. struct rcu_head **tail)
  270. {
  271. local_irq_disable();
  272. *this_rdp->nxttail = list;
  273. if (list)
  274. this_rdp->nxttail = tail;
  275. local_irq_enable();
  276. }
  277. static void __rcu_offline_cpu(struct rcu_data *this_rdp,
  278. struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
  279. {
  280. /* if the cpu going offline owns the grace period
  281. * we can block indefinitely waiting for it, so flush
  282. * it here
  283. */
  284. spin_lock_bh(&rsp->lock);
  285. if (rcp->cur != rcp->completed)
  286. cpu_quiet(rdp->cpu, rcp, rsp);
  287. spin_unlock_bh(&rsp->lock);
  288. rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
  289. rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
  290. }
  291. static void rcu_offline_cpu(int cpu)
  292. {
  293. struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
  294. struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
  295. __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
  296. &per_cpu(rcu_data, cpu));
  297. __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
  298. &per_cpu(rcu_bh_data, cpu));
  299. put_cpu_var(rcu_data);
  300. put_cpu_var(rcu_bh_data);
  301. tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
  302. }
  303. #else
  304. static void rcu_offline_cpu(int cpu)
  305. {
  306. }
  307. #endif
  308. /*
  309. * This does the RCU processing work from tasklet context.
  310. */
  311. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
  312. struct rcu_state *rsp, struct rcu_data *rdp)
  313. {
  314. if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
  315. *rdp->donetail = rdp->curlist;
  316. rdp->donetail = rdp->curtail;
  317. rdp->curlist = NULL;
  318. rdp->curtail = &rdp->curlist;
  319. }
  320. local_irq_disable();
  321. if (rdp->nxtlist && !rdp->curlist) {
  322. rdp->curlist = rdp->nxtlist;
  323. rdp->curtail = rdp->nxttail;
  324. rdp->nxtlist = NULL;
  325. rdp->nxttail = &rdp->nxtlist;
  326. local_irq_enable();
  327. /*
  328. * start the next batch of callbacks
  329. */
  330. /* determine batch number */
  331. rdp->batch = rcp->cur + 1;
  332. /* see the comment and corresponding wmb() in
  333. * the rcu_start_batch()
  334. */
  335. smp_rmb();
  336. if (!rcp->next_pending) {
  337. /* and start it/schedule start if it's a new batch */
  338. spin_lock(&rsp->lock);
  339. rcu_start_batch(rcp, rsp, 1);
  340. spin_unlock(&rsp->lock);
  341. }
  342. } else {
  343. local_irq_enable();
  344. }
  345. rcu_check_quiescent_state(rcp, rsp, rdp);
  346. if (rdp->donelist)
  347. rcu_do_batch(rdp);
  348. }
  349. static void rcu_process_callbacks(unsigned long unused)
  350. {
  351. __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
  352. &__get_cpu_var(rcu_data));
  353. __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
  354. &__get_cpu_var(rcu_bh_data));
  355. }
  356. void rcu_check_callbacks(int cpu, int user)
  357. {
  358. if (user ||
  359. (idle_cpu(cpu) && !in_softirq() &&
  360. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  361. rcu_qsctr_inc(cpu);
  362. rcu_bh_qsctr_inc(cpu);
  363. } else if (!in_softirq())
  364. rcu_bh_qsctr_inc(cpu);
  365. tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
  366. }
  367. static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
  368. struct rcu_data *rdp)
  369. {
  370. memset(rdp, 0, sizeof(*rdp));
  371. rdp->curtail = &rdp->curlist;
  372. rdp->nxttail = &rdp->nxtlist;
  373. rdp->donetail = &rdp->donelist;
  374. rdp->quiescbatch = rcp->completed;
  375. rdp->qs_pending = 0;
  376. rdp->cpu = cpu;
  377. }
  378. static void __devinit rcu_online_cpu(int cpu)
  379. {
  380. struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
  381. struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
  382. rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
  383. rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
  384. tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
  385. }
  386. static int __devinit rcu_cpu_notify(struct notifier_block *self,
  387. unsigned long action, void *hcpu)
  388. {
  389. long cpu = (long)hcpu;
  390. switch (action) {
  391. case CPU_UP_PREPARE:
  392. rcu_online_cpu(cpu);
  393. break;
  394. case CPU_DEAD:
  395. rcu_offline_cpu(cpu);
  396. break;
  397. default:
  398. break;
  399. }
  400. return NOTIFY_OK;
  401. }
  402. static struct notifier_block __devinitdata rcu_nb = {
  403. .notifier_call = rcu_cpu_notify,
  404. };
  405. /*
  406. * Initializes rcu mechanism. Assumed to be called early.
  407. * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
  408. * Note that rcu_qsctr and friends are implicitly
  409. * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
  410. */
  411. void __init rcu_init(void)
  412. {
  413. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
  414. (void *)(long)smp_processor_id());
  415. /* Register notifier for non-boot CPUs */
  416. register_cpu_notifier(&rcu_nb);
  417. }
  418. struct rcu_synchronize {
  419. struct rcu_head head;
  420. struct completion completion;
  421. };
  422. /* Because of FASTCALL declaration of complete, we use this wrapper */
  423. static void wakeme_after_rcu(struct rcu_head *head)
  424. {
  425. struct rcu_synchronize *rcu;
  426. rcu = container_of(head, struct rcu_synchronize, head);
  427. complete(&rcu->completion);
  428. }
  429. /**
  430. * synchronize_rcu - wait until a grace period has elapsed.
  431. *
  432. * Control will return to the caller some time after a full grace
  433. * period has elapsed, in other words after all currently executing RCU
  434. * read-side critical sections have completed. RCU read-side critical
  435. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  436. * and may be nested.
  437. *
  438. * If your read-side code is not protected by rcu_read_lock(), do -not-
  439. * use synchronize_rcu().
  440. */
  441. void synchronize_rcu(void)
  442. {
  443. struct rcu_synchronize rcu;
  444. init_completion(&rcu.completion);
  445. /* Will wake me after RCU finished */
  446. call_rcu(&rcu.head, wakeme_after_rcu);
  447. /* Wait for it */
  448. wait_for_completion(&rcu.completion);
  449. }
  450. /*
  451. * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
  452. */
  453. void synchronize_kernel(void)
  454. {
  455. synchronize_rcu();
  456. }
  457. module_param(maxbatch, int, 0);
  458. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  459. EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
  460. EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
  461. EXPORT_SYMBOL_GPL(synchronize_rcu);
  462. EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */