rcuclassic.c 22 KB

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