rcuclassic.c 22 KB

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