rcuclassic.c 16 KB

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