rcuclassic.c 22 KB

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