rcuclassic.c 16 KB

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