rcupdate.c 16 KB

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