rcupdate.c 16 KB

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