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