rcuclassic.c 21 KB

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