rcupdate.c 16 KB

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