rcuclassic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. #include <linux/time.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. .pending = -300,
  62. .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
  63. .cpumask = CPU_MASK_NONE,
  64. };
  65. static struct rcu_ctrlblk rcu_bh_ctrlblk = {
  66. .cur = -300,
  67. .completed = -300,
  68. .pending = -300,
  69. .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
  70. .cpumask = CPU_MASK_NONE,
  71. };
  72. DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
  73. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
  74. static int blimit = 10;
  75. static int qhimark = 10000;
  76. static int qlowmark = 100;
  77. #ifdef CONFIG_SMP
  78. static void force_quiescent_state(struct rcu_data *rdp,
  79. struct rcu_ctrlblk *rcp)
  80. {
  81. int cpu;
  82. cpumask_t cpumask;
  83. unsigned long flags;
  84. set_need_resched();
  85. spin_lock_irqsave(&rcp->lock, flags);
  86. if (unlikely(!rcp->signaled)) {
  87. rcp->signaled = 1;
  88. /*
  89. * Don't send IPI to itself. With irqs disabled,
  90. * rdp->cpu is the current cpu.
  91. *
  92. * cpu_online_map is updated by the _cpu_down()
  93. * using __stop_machine(). Since we're in irqs disabled
  94. * section, __stop_machine() is not exectuting, hence
  95. * the cpu_online_map is stable.
  96. *
  97. * However, a cpu might have been offlined _just_ before
  98. * we disabled irqs while entering here.
  99. * And rcu subsystem might not yet have handled the CPU_DEAD
  100. * notification, leading to the offlined cpu's bit
  101. * being set in the rcp->cpumask.
  102. *
  103. * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent
  104. * sending smp_reschedule() to an offlined CPU.
  105. */
  106. cpus_and(cpumask, rcp->cpumask, cpu_online_map);
  107. cpu_clear(rdp->cpu, cpumask);
  108. for_each_cpu_mask_nr(cpu, cpumask)
  109. smp_send_reschedule(cpu);
  110. }
  111. spin_unlock_irqrestore(&rcp->lock, flags);
  112. }
  113. #else
  114. static inline void force_quiescent_state(struct rcu_data *rdp,
  115. struct rcu_ctrlblk *rcp)
  116. {
  117. set_need_resched();
  118. }
  119. #endif
  120. static void __call_rcu(struct rcu_head *head, struct rcu_ctrlblk *rcp,
  121. struct rcu_data *rdp)
  122. {
  123. long batch;
  124. head->next = NULL;
  125. smp_mb(); /* Read of rcu->cur must happen after any change by caller. */
  126. /*
  127. * Determine the batch number of this callback.
  128. *
  129. * Using ACCESS_ONCE to avoid the following error when gcc eliminates
  130. * local variable "batch" and emits codes like this:
  131. * 1) rdp->batch = rcp->cur + 1 # gets old value
  132. * ......
  133. * 2)rcu_batch_after(rcp->cur + 1, rdp->batch) # gets new value
  134. * then [*nxttail[0], *nxttail[1]) may contain callbacks
  135. * that batch# = rdp->batch, see the comment of struct rcu_data.
  136. */
  137. batch = ACCESS_ONCE(rcp->cur) + 1;
  138. if (rdp->nxtlist && rcu_batch_after(batch, rdp->batch)) {
  139. /* process callbacks */
  140. rdp->nxttail[0] = rdp->nxttail[1];
  141. rdp->nxttail[1] = rdp->nxttail[2];
  142. if (rcu_batch_after(batch - 1, rdp->batch))
  143. rdp->nxttail[0] = rdp->nxttail[2];
  144. }
  145. rdp->batch = batch;
  146. *rdp->nxttail[2] = head;
  147. rdp->nxttail[2] = &head->next;
  148. if (unlikely(++rdp->qlen > qhimark)) {
  149. rdp->blimit = INT_MAX;
  150. force_quiescent_state(rdp, &rcu_ctrlblk);
  151. }
  152. }
  153. /**
  154. * call_rcu - Queue an RCU callback for invocation after a grace period.
  155. * @head: structure to be used for queueing the RCU updates.
  156. * @func: actual update function to be invoked after the grace period
  157. *
  158. * The update function will be invoked some time after a full grace
  159. * period elapses, in other words after all currently executing RCU
  160. * read-side critical sections have completed. RCU read-side critical
  161. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  162. * and may be nested.
  163. */
  164. void call_rcu(struct rcu_head *head,
  165. void (*func)(struct rcu_head *rcu))
  166. {
  167. unsigned long flags;
  168. head->func = func;
  169. local_irq_save(flags);
  170. __call_rcu(head, &rcu_ctrlblk, &__get_cpu_var(rcu_data));
  171. local_irq_restore(flags);
  172. }
  173. EXPORT_SYMBOL_GPL(call_rcu);
  174. /**
  175. * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
  176. * @head: structure to be used for queueing the RCU updates.
  177. * @func: actual update function to be invoked after the grace period
  178. *
  179. * The update function will be invoked some time after a full grace
  180. * period elapses, in other words after all currently executing RCU
  181. * read-side critical sections have completed. call_rcu_bh() assumes
  182. * that the read-side critical sections end on completion of a softirq
  183. * handler. This means that read-side critical sections in process
  184. * context must not be interrupted by softirqs. This interface is to be
  185. * used when most of the read-side critical sections are in softirq context.
  186. * RCU read-side critical sections are delimited by rcu_read_lock() and
  187. * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
  188. * and rcu_read_unlock_bh(), if in process context. These may be nested.
  189. */
  190. void call_rcu_bh(struct rcu_head *head,
  191. void (*func)(struct rcu_head *rcu))
  192. {
  193. unsigned long flags;
  194. head->func = func;
  195. local_irq_save(flags);
  196. __call_rcu(head, &rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
  197. local_irq_restore(flags);
  198. }
  199. EXPORT_SYMBOL_GPL(call_rcu_bh);
  200. /*
  201. * Return the number of RCU batches processed thus far. Useful
  202. * for debug and statistics.
  203. */
  204. long rcu_batches_completed(void)
  205. {
  206. return rcu_ctrlblk.completed;
  207. }
  208. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  209. /*
  210. * Return the number of RCU batches processed thus far. Useful
  211. * for debug and statistics.
  212. */
  213. long rcu_batches_completed_bh(void)
  214. {
  215. return rcu_bh_ctrlblk.completed;
  216. }
  217. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  218. /* Raises the softirq for processing rcu_callbacks. */
  219. static inline void raise_rcu_softirq(void)
  220. {
  221. raise_softirq(RCU_SOFTIRQ);
  222. }
  223. /*
  224. * Invoke the completed RCU callbacks. They are expected to be in
  225. * a per-cpu list.
  226. */
  227. static void rcu_do_batch(struct rcu_data *rdp)
  228. {
  229. unsigned long flags;
  230. struct rcu_head *next, *list;
  231. int count = 0;
  232. list = rdp->donelist;
  233. while (list) {
  234. next = list->next;
  235. prefetch(next);
  236. list->func(list);
  237. list = next;
  238. if (++count >= rdp->blimit)
  239. break;
  240. }
  241. rdp->donelist = list;
  242. local_irq_save(flags);
  243. rdp->qlen -= count;
  244. local_irq_restore(flags);
  245. if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
  246. rdp->blimit = blimit;
  247. if (!rdp->donelist)
  248. rdp->donetail = &rdp->donelist;
  249. else
  250. raise_rcu_softirq();
  251. }
  252. /*
  253. * Grace period handling:
  254. * The grace period handling consists out of two steps:
  255. * - A new grace period is started.
  256. * This is done by rcu_start_batch. The start is not broadcasted to
  257. * all cpus, they must pick this up by comparing rcp->cur with
  258. * rdp->quiescbatch. All cpus are recorded in the
  259. * rcu_ctrlblk.cpumask bitmap.
  260. * - All cpus must go through a quiescent state.
  261. * Since the start of the grace period is not broadcasted, at least two
  262. * calls to rcu_check_quiescent_state are required:
  263. * The first call just notices that a new grace period is running. The
  264. * following calls check if there was a quiescent state since the beginning
  265. * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
  266. * the bitmap is empty, then the grace period is completed.
  267. * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
  268. * period (if necessary).
  269. */
  270. #ifdef CONFIG_DEBUG_RCU_STALL
  271. static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
  272. {
  273. rcp->gp_check = get_seconds() + 3;
  274. }
  275. static void print_other_cpu_stall(struct rcu_ctrlblk *rcp)
  276. {
  277. int cpu;
  278. long delta;
  279. unsigned long flags;
  280. /* Only let one CPU complain about others per time interval. */
  281. spin_lock_irqsave(&rcp->lock, flags);
  282. delta = get_seconds() - rcp->gp_check;
  283. if (delta < 2L || cpus_empty(rcp->cpumask)) {
  284. spin_unlock(&rcp->lock);
  285. return;
  286. }
  287. rcp->gp_check = get_seconds() + 30;
  288. spin_unlock_irqrestore(&rcp->lock, flags);
  289. /* OK, time to rat on our buddy... */
  290. printk(KERN_ERR "RCU detected CPU stalls:");
  291. for_each_cpu_mask(cpu, rcp->cpumask)
  292. printk(" %d", cpu);
  293. printk(" (detected by %d, t=%lu/%lu)\n",
  294. smp_processor_id(), get_seconds(), rcp->gp_check);
  295. }
  296. static void print_cpu_stall(struct rcu_ctrlblk *rcp)
  297. {
  298. unsigned long flags;
  299. printk(KERN_ERR "RCU detected CPU %d stall (t=%lu/%lu)\n",
  300. smp_processor_id(), get_seconds(), rcp->gp_check);
  301. dump_stack();
  302. spin_lock_irqsave(&rcp->lock, flags);
  303. if ((long)(get_seconds() - rcp->gp_check) >= 0L)
  304. rcp->gp_check = get_seconds() + 30;
  305. spin_unlock_irqrestore(&rcp->lock, flags);
  306. }
  307. static void check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
  308. {
  309. long delta;
  310. delta = get_seconds() - rcp->gp_check;
  311. if (cpu_isset(smp_processor_id(), rcp->cpumask) && delta >= 0L) {
  312. /* We haven't checked in, so go dump stack. */
  313. print_cpu_stall(rcp);
  314. } else {
  315. if (!cpus_empty(rcp->cpumask) && delta >= 2L) {
  316. /* They had two seconds to dump stack, so complain. */
  317. print_other_cpu_stall(rcp);
  318. }
  319. }
  320. }
  321. #else /* #ifdef CONFIG_DEBUG_RCU_STALL */
  322. static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
  323. {
  324. }
  325. static inline void
  326. check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
  327. {
  328. }
  329. #endif /* #else #ifdef CONFIG_DEBUG_RCU_STALL */
  330. /*
  331. * Register a new batch of callbacks, and start it up if there is currently no
  332. * active batch and the batch to be registered has not already occurred.
  333. * Caller must hold rcu_ctrlblk.lock.
  334. */
  335. static void rcu_start_batch(struct rcu_ctrlblk *rcp)
  336. {
  337. if (rcp->cur != rcp->pending &&
  338. rcp->completed == rcp->cur) {
  339. rcp->cur++;
  340. record_gp_check_time(rcp);
  341. /*
  342. * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
  343. * Barrier Otherwise it can cause tickless idle CPUs to be
  344. * included in rcp->cpumask, which will extend graceperiods
  345. * unnecessarily.
  346. */
  347. smp_mb();
  348. cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
  349. rcp->signaled = 0;
  350. }
  351. }
  352. /*
  353. * cpu went through a quiescent state since the beginning of the grace period.
  354. * Clear it from the cpu mask and complete the grace period if it was the last
  355. * cpu. Start another grace period if someone has further entries pending
  356. */
  357. static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
  358. {
  359. cpu_clear(cpu, rcp->cpumask);
  360. if (cpus_empty(rcp->cpumask)) {
  361. /* batch completed ! */
  362. rcp->completed = rcp->cur;
  363. rcu_start_batch(rcp);
  364. }
  365. }
  366. /*
  367. * Check if the cpu has gone through a quiescent state (say context
  368. * switch). If so and if it already hasn't done so in this RCU
  369. * quiescent cycle, then indicate that it has done so.
  370. */
  371. static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
  372. struct rcu_data *rdp)
  373. {
  374. unsigned long flags;
  375. if (rdp->quiescbatch != rcp->cur) {
  376. /* start new grace period: */
  377. rdp->qs_pending = 1;
  378. rdp->passed_quiesc = 0;
  379. rdp->quiescbatch = rcp->cur;
  380. return;
  381. }
  382. /* Grace period already completed for this cpu?
  383. * qs_pending is checked instead of the actual bitmap to avoid
  384. * cacheline trashing.
  385. */
  386. if (!rdp->qs_pending)
  387. return;
  388. /*
  389. * Was there a quiescent state since the beginning of the grace
  390. * period? If no, then exit and wait for the next call.
  391. */
  392. if (!rdp->passed_quiesc)
  393. return;
  394. rdp->qs_pending = 0;
  395. spin_lock_irqsave(&rcp->lock, flags);
  396. /*
  397. * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
  398. * during cpu startup. Ignore the quiescent state.
  399. */
  400. if (likely(rdp->quiescbatch == rcp->cur))
  401. cpu_quiet(rdp->cpu, rcp);
  402. spin_unlock_irqrestore(&rcp->lock, flags);
  403. }
  404. #ifdef CONFIG_HOTPLUG_CPU
  405. /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
  406. * locking requirements, the list it's pulling from has to belong to a cpu
  407. * which is dead and hence not processing interrupts.
  408. */
  409. static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
  410. struct rcu_head **tail, long batch)
  411. {
  412. unsigned long flags;
  413. if (list) {
  414. local_irq_save(flags);
  415. this_rdp->batch = batch;
  416. *this_rdp->nxttail[2] = list;
  417. this_rdp->nxttail[2] = tail;
  418. local_irq_restore(flags);
  419. }
  420. }
  421. static void __rcu_offline_cpu(struct rcu_data *this_rdp,
  422. struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
  423. {
  424. unsigned long flags;
  425. /*
  426. * if the cpu going offline owns the grace period
  427. * we can block indefinitely waiting for it, so flush
  428. * it here
  429. */
  430. spin_lock_irqsave(&rcp->lock, flags);
  431. if (rcp->cur != rcp->completed)
  432. cpu_quiet(rdp->cpu, rcp);
  433. rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail, rcp->cur + 1);
  434. rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail[2], rcp->cur + 1);
  435. spin_unlock(&rcp->lock);
  436. this_rdp->qlen += rdp->qlen;
  437. local_irq_restore(flags);
  438. }
  439. static void rcu_offline_cpu(int cpu)
  440. {
  441. struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
  442. struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
  443. __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
  444. &per_cpu(rcu_data, cpu));
  445. __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
  446. &per_cpu(rcu_bh_data, cpu));
  447. put_cpu_var(rcu_data);
  448. put_cpu_var(rcu_bh_data);
  449. }
  450. #else
  451. static void rcu_offline_cpu(int cpu)
  452. {
  453. }
  454. #endif
  455. /*
  456. * This does the RCU processing work from softirq context.
  457. */
  458. static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
  459. struct rcu_data *rdp)
  460. {
  461. unsigned long flags;
  462. long completed_snap;
  463. if (rdp->nxtlist) {
  464. local_irq_save(flags);
  465. completed_snap = ACCESS_ONCE(rcp->completed);
  466. /*
  467. * move the other grace-period-completed entries to
  468. * [rdp->nxtlist, *rdp->nxttail[0]) temporarily
  469. */
  470. if (!rcu_batch_before(completed_snap, rdp->batch))
  471. rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2];
  472. else if (!rcu_batch_before(completed_snap, rdp->batch - 1))
  473. rdp->nxttail[0] = rdp->nxttail[1];
  474. /*
  475. * the grace period for entries in
  476. * [rdp->nxtlist, *rdp->nxttail[0]) has completed and
  477. * move these entries to donelist
  478. */
  479. if (rdp->nxttail[0] != &rdp->nxtlist) {
  480. *rdp->donetail = rdp->nxtlist;
  481. rdp->donetail = rdp->nxttail[0];
  482. rdp->nxtlist = *rdp->nxttail[0];
  483. *rdp->donetail = NULL;
  484. if (rdp->nxttail[1] == rdp->nxttail[0])
  485. rdp->nxttail[1] = &rdp->nxtlist;
  486. if (rdp->nxttail[2] == rdp->nxttail[0])
  487. rdp->nxttail[2] = &rdp->nxtlist;
  488. rdp->nxttail[0] = &rdp->nxtlist;
  489. }
  490. local_irq_restore(flags);
  491. if (rcu_batch_after(rdp->batch, rcp->pending)) {
  492. unsigned long flags;
  493. /* and start it/schedule start if it's a new batch */
  494. spin_lock_irqsave(&rcp->lock, flags);
  495. if (rcu_batch_after(rdp->batch, rcp->pending)) {
  496. rcp->pending = rdp->batch;
  497. rcu_start_batch(rcp);
  498. }
  499. spin_unlock_irqrestore(&rcp->lock, flags);
  500. }
  501. }
  502. rcu_check_quiescent_state(rcp, rdp);
  503. if (rdp->donelist)
  504. rcu_do_batch(rdp);
  505. }
  506. static void rcu_process_callbacks(struct softirq_action *unused)
  507. {
  508. /*
  509. * Memory references from any prior RCU read-side critical sections
  510. * executed by the interrupted code must be see before any RCU
  511. * grace-period manupulations below.
  512. */
  513. smp_mb(); /* See above block comment. */
  514. __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
  515. __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
  516. /*
  517. * Memory references from any later RCU read-side critical sections
  518. * executed by the interrupted code must be see after any RCU
  519. * grace-period manupulations above.
  520. */
  521. smp_mb(); /* See above block comment. */
  522. }
  523. static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
  524. {
  525. /* Check for CPU stalls, if enabled. */
  526. check_cpu_stall(rcp, rdp);
  527. if (rdp->nxtlist) {
  528. long completed_snap = ACCESS_ONCE(rcp->completed);
  529. /*
  530. * This cpu has pending rcu entries and the grace period
  531. * for them has completed.
  532. */
  533. if (!rcu_batch_before(completed_snap, rdp->batch))
  534. return 1;
  535. if (!rcu_batch_before(completed_snap, rdp->batch - 1) &&
  536. rdp->nxttail[0] != rdp->nxttail[1])
  537. return 1;
  538. if (rdp->nxttail[0] != &rdp->nxtlist)
  539. return 1;
  540. /*
  541. * This cpu has pending rcu entries and the new batch
  542. * for then hasn't been started nor scheduled start
  543. */
  544. if (rcu_batch_after(rdp->batch, rcp->pending))
  545. return 1;
  546. }
  547. /* This cpu has finished callbacks to invoke */
  548. if (rdp->donelist)
  549. return 1;
  550. /* The rcu core waits for a quiescent state from the cpu */
  551. if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
  552. return 1;
  553. /* nothing to do */
  554. return 0;
  555. }
  556. /*
  557. * Check to see if there is any immediate RCU-related work to be done
  558. * by the current CPU, returning 1 if so. This function is part of the
  559. * RCU implementation; it is -not- an exported member of the RCU API.
  560. */
  561. int rcu_pending(int cpu)
  562. {
  563. return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
  564. __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
  565. }
  566. /*
  567. * Check to see if any future RCU-related work will need to be done
  568. * by the current CPU, even if none need be done immediately, returning
  569. * 1 if so. This function is part of the RCU implementation; it is -not-
  570. * an exported member of the RCU API.
  571. */
  572. int rcu_needs_cpu(int cpu)
  573. {
  574. struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
  575. struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
  576. return !!rdp->nxtlist || !!rdp_bh->nxtlist || rcu_pending(cpu);
  577. }
  578. /*
  579. * Top-level function driving RCU grace-period detection, normally
  580. * invoked from the scheduler-clock interrupt. This function simply
  581. * increments counters that are read only from softirq by this same
  582. * CPU, so there are no memory barriers required.
  583. */
  584. void rcu_check_callbacks(int cpu, int user)
  585. {
  586. if (user ||
  587. (idle_cpu(cpu) && !in_softirq() &&
  588. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  589. /*
  590. * Get here if this CPU took its interrupt from user
  591. * mode or from the idle loop, and if this is not a
  592. * nested interrupt. In this case, the CPU is in
  593. * a quiescent state, so count it.
  594. *
  595. * Also do a memory barrier. This is needed to handle
  596. * the case where writes from a preempt-disable section
  597. * of code get reordered into schedule() by this CPU's
  598. * write buffer. The memory barrier makes sure that
  599. * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see
  600. * by other CPUs to happen after any such write.
  601. */
  602. smp_mb(); /* See above block comment. */
  603. rcu_qsctr_inc(cpu);
  604. rcu_bh_qsctr_inc(cpu);
  605. } else if (!in_softirq()) {
  606. /*
  607. * Get here if this CPU did not take its interrupt from
  608. * softirq, in other words, if it is not interrupting
  609. * a rcu_bh read-side critical section. This is an _bh
  610. * critical section, so count it. The memory barrier
  611. * is needed for the same reason as is the above one.
  612. */
  613. smp_mb(); /* See above block comment. */
  614. rcu_bh_qsctr_inc(cpu);
  615. }
  616. raise_rcu_softirq();
  617. }
  618. static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
  619. struct rcu_data *rdp)
  620. {
  621. long flags;
  622. spin_lock_irqsave(&rcp->lock, flags);
  623. memset(rdp, 0, sizeof(*rdp));
  624. rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2] = &rdp->nxtlist;
  625. rdp->donetail = &rdp->donelist;
  626. rdp->quiescbatch = rcp->completed;
  627. rdp->qs_pending = 0;
  628. rdp->cpu = cpu;
  629. rdp->blimit = blimit;
  630. spin_unlock_irqrestore(&rcp->lock, flags);
  631. }
  632. static void __cpuinit rcu_online_cpu(int cpu)
  633. {
  634. struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
  635. struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
  636. rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
  637. rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
  638. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  639. }
  640. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  641. unsigned long action, void *hcpu)
  642. {
  643. long cpu = (long)hcpu;
  644. switch (action) {
  645. case CPU_UP_PREPARE:
  646. case CPU_UP_PREPARE_FROZEN:
  647. rcu_online_cpu(cpu);
  648. break;
  649. case CPU_DEAD:
  650. case CPU_DEAD_FROZEN:
  651. rcu_offline_cpu(cpu);
  652. break;
  653. default:
  654. break;
  655. }
  656. return NOTIFY_OK;
  657. }
  658. static struct notifier_block __cpuinitdata rcu_nb = {
  659. .notifier_call = rcu_cpu_notify,
  660. };
  661. /*
  662. * Initializes rcu mechanism. Assumed to be called early.
  663. * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
  664. * Note that rcu_qsctr and friends are implicitly
  665. * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
  666. */
  667. void __init __rcu_init(void)
  668. {
  669. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
  670. (void *)(long)smp_processor_id());
  671. /* Register notifier for non-boot CPUs */
  672. register_cpu_notifier(&rcu_nb);
  673. }
  674. module_param(blimit, int, 0);
  675. module_param(qhimark, int, 0);
  676. module_param(qlowmark, int, 0);