rcupreempt.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion, realtime implementation
  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, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * With thanks to Esben Nielsen, Bill Huey, and Ingo Molnar
  22. * for pushing me away from locks and towards counters, and
  23. * to Suparna Bhattacharya for pushing me completely away
  24. * from atomic instructions on the read side.
  25. *
  26. * - Added handling of Dynamic Ticks
  27. * Copyright 2007 - Paul E. Mckenney <paulmck@us.ibm.com>
  28. * - Steven Rostedt <srostedt@redhat.com>
  29. *
  30. * Papers: http://www.rdrop.com/users/paulmck/RCU
  31. *
  32. * Design Document: http://lwn.net/Articles/253651/
  33. *
  34. * For detailed explanation of Read-Copy Update mechanism see -
  35. * Documentation/RCU/ *.txt
  36. *
  37. */
  38. #include <linux/types.h>
  39. #include <linux/kernel.h>
  40. #include <linux/init.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/smp.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/sched.h>
  46. #include <asm/atomic.h>
  47. #include <linux/bitops.h>
  48. #include <linux/module.h>
  49. #include <linux/kthread.h>
  50. #include <linux/completion.h>
  51. #include <linux/moduleparam.h>
  52. #include <linux/percpu.h>
  53. #include <linux/notifier.h>
  54. #include <linux/cpu.h>
  55. #include <linux/random.h>
  56. #include <linux/delay.h>
  57. #include <linux/cpumask.h>
  58. #include <linux/rcupreempt_trace.h>
  59. #include <asm/byteorder.h>
  60. /*
  61. * PREEMPT_RCU data structures.
  62. */
  63. /*
  64. * GP_STAGES specifies the number of times the state machine has
  65. * to go through the all the rcu_try_flip_states (see below)
  66. * in a single Grace Period.
  67. *
  68. * GP in GP_STAGES stands for Grace Period ;)
  69. */
  70. #define GP_STAGES 2
  71. struct rcu_data {
  72. spinlock_t lock; /* Protect rcu_data fields. */
  73. long completed; /* Number of last completed batch. */
  74. int waitlistcount;
  75. struct rcu_head *nextlist;
  76. struct rcu_head **nexttail;
  77. struct rcu_head *waitlist[GP_STAGES];
  78. struct rcu_head **waittail[GP_STAGES];
  79. struct rcu_head *donelist; /* from waitlist & waitschedlist */
  80. struct rcu_head **donetail;
  81. long rcu_flipctr[2];
  82. struct rcu_head *nextschedlist;
  83. struct rcu_head **nextschedtail;
  84. struct rcu_head *waitschedlist;
  85. struct rcu_head **waitschedtail;
  86. int rcu_sched_sleeping;
  87. #ifdef CONFIG_RCU_TRACE
  88. struct rcupreempt_trace trace;
  89. #endif /* #ifdef CONFIG_RCU_TRACE */
  90. };
  91. /*
  92. * States for rcu_try_flip() and friends.
  93. */
  94. enum rcu_try_flip_states {
  95. /*
  96. * Stay here if nothing is happening. Flip the counter if somthing
  97. * starts happening. Denoted by "I"
  98. */
  99. rcu_try_flip_idle_state,
  100. /*
  101. * Wait here for all CPUs to notice that the counter has flipped. This
  102. * prevents the old set of counters from ever being incremented once
  103. * we leave this state, which in turn is necessary because we cannot
  104. * test any individual counter for zero -- we can only check the sum.
  105. * Denoted by "A".
  106. */
  107. rcu_try_flip_waitack_state,
  108. /*
  109. * Wait here for the sum of the old per-CPU counters to reach zero.
  110. * Denoted by "Z".
  111. */
  112. rcu_try_flip_waitzero_state,
  113. /*
  114. * Wait here for each of the other CPUs to execute a memory barrier.
  115. * This is necessary to ensure that these other CPUs really have
  116. * completed executing their RCU read-side critical sections, despite
  117. * their CPUs wildly reordering memory. Denoted by "M".
  118. */
  119. rcu_try_flip_waitmb_state,
  120. };
  121. /*
  122. * States for rcu_ctrlblk.rcu_sched_sleep.
  123. */
  124. enum rcu_sched_sleep_states {
  125. rcu_sched_not_sleeping, /* Not sleeping, callbacks need GP. */
  126. rcu_sched_sleep_prep, /* Thinking of sleeping, rechecking. */
  127. rcu_sched_sleeping, /* Sleeping, awaken if GP needed. */
  128. };
  129. struct rcu_ctrlblk {
  130. spinlock_t fliplock; /* Protect state-machine transitions. */
  131. long completed; /* Number of last completed batch. */
  132. enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
  133. the rcu state machine */
  134. spinlock_t schedlock; /* Protect rcu_sched sleep state. */
  135. enum rcu_sched_sleep_states sched_sleep; /* rcu_sched state. */
  136. wait_queue_head_t sched_wq; /* Place for rcu_sched to sleep. */
  137. };
  138. static DEFINE_PER_CPU(struct rcu_data, rcu_data);
  139. static struct rcu_ctrlblk rcu_ctrlblk = {
  140. .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
  141. .completed = 0,
  142. .rcu_try_flip_state = rcu_try_flip_idle_state,
  143. .schedlock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.schedlock),
  144. .sched_sleep = rcu_sched_not_sleeping,
  145. .sched_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rcu_ctrlblk.sched_wq),
  146. };
  147. static struct task_struct *rcu_sched_grace_period_task;
  148. #ifdef CONFIG_RCU_TRACE
  149. static char *rcu_try_flip_state_names[] =
  150. { "idle", "waitack", "waitzero", "waitmb" };
  151. #endif /* #ifdef CONFIG_RCU_TRACE */
  152. static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
  153. /*
  154. * Enum and per-CPU flag to determine when each CPU has seen
  155. * the most recent counter flip.
  156. */
  157. enum rcu_flip_flag_values {
  158. rcu_flip_seen, /* Steady/initial state, last flip seen. */
  159. /* Only GP detector can update. */
  160. rcu_flipped /* Flip just completed, need confirmation. */
  161. /* Only corresponding CPU can update. */
  162. };
  163. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
  164. = rcu_flip_seen;
  165. /*
  166. * Enum and per-CPU flag to determine when each CPU has executed the
  167. * needed memory barrier to fence in memory references from its last RCU
  168. * read-side critical section in the just-completed grace period.
  169. */
  170. enum rcu_mb_flag_values {
  171. rcu_mb_done, /* Steady/initial state, no mb()s required. */
  172. /* Only GP detector can update. */
  173. rcu_mb_needed /* Flip just completed, need an mb(). */
  174. /* Only corresponding CPU can update. */
  175. };
  176. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
  177. = rcu_mb_done;
  178. /*
  179. * RCU_DATA_ME: find the current CPU's rcu_data structure.
  180. * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
  181. */
  182. #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
  183. #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
  184. /*
  185. * Helper macro for tracing when the appropriate rcu_data is not
  186. * cached in a local variable, but where the CPU number is so cached.
  187. */
  188. #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
  189. /*
  190. * Helper macro for tracing when the appropriate rcu_data is not
  191. * cached in a local variable.
  192. */
  193. #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
  194. /*
  195. * Helper macro for tracing when the appropriate rcu_data is pointed
  196. * to by a local variable.
  197. */
  198. #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
  199. #define RCU_SCHED_BATCH_TIME (HZ / 50)
  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. void __rcu_read_lock(void)
  210. {
  211. int idx;
  212. struct task_struct *t = current;
  213. int nesting;
  214. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  215. if (nesting != 0) {
  216. /* An earlier rcu_read_lock() covers us, just count it. */
  217. t->rcu_read_lock_nesting = nesting + 1;
  218. } else {
  219. unsigned long flags;
  220. /*
  221. * We disable interrupts for the following reasons:
  222. * - If we get scheduling clock interrupt here, and we
  223. * end up acking the counter flip, it's like a promise
  224. * that we will never increment the old counter again.
  225. * Thus we will break that promise if that
  226. * scheduling clock interrupt happens between the time
  227. * we pick the .completed field and the time that we
  228. * increment our counter.
  229. *
  230. * - We don't want to be preempted out here.
  231. *
  232. * NMIs can still occur, of course, and might themselves
  233. * contain rcu_read_lock().
  234. */
  235. local_irq_save(flags);
  236. /*
  237. * Outermost nesting of rcu_read_lock(), so increment
  238. * the current counter for the current CPU. Use volatile
  239. * casts to prevent the compiler from reordering.
  240. */
  241. idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
  242. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
  243. /*
  244. * Now that the per-CPU counter has been incremented, we
  245. * are protected from races with rcu_read_lock() invoked
  246. * from NMI handlers on this CPU. We can therefore safely
  247. * increment the nesting counter, relieving further NMIs
  248. * of the need to increment the per-CPU counter.
  249. */
  250. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
  251. /*
  252. * Now that we have preventing any NMIs from storing
  253. * to the ->rcu_flipctr_idx, we can safely use it to
  254. * remember which counter to decrement in the matching
  255. * rcu_read_unlock().
  256. */
  257. ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
  258. local_irq_restore(flags);
  259. }
  260. }
  261. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  262. void __rcu_read_unlock(void)
  263. {
  264. int idx;
  265. struct task_struct *t = current;
  266. int nesting;
  267. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  268. if (nesting > 1) {
  269. /*
  270. * We are still protected by the enclosing rcu_read_lock(),
  271. * so simply decrement the counter.
  272. */
  273. t->rcu_read_lock_nesting = nesting - 1;
  274. } else {
  275. unsigned long flags;
  276. /*
  277. * Disable local interrupts to prevent the grace-period
  278. * detection state machine from seeing us half-done.
  279. * NMIs can still occur, of course, and might themselves
  280. * contain rcu_read_lock() and rcu_read_unlock().
  281. */
  282. local_irq_save(flags);
  283. /*
  284. * Outermost nesting of rcu_read_unlock(), so we must
  285. * decrement the current counter for the current CPU.
  286. * This must be done carefully, because NMIs can
  287. * occur at any point in this code, and any rcu_read_lock()
  288. * and rcu_read_unlock() pairs in the NMI handlers
  289. * must interact non-destructively with this code.
  290. * Lots of volatile casts, and -very- careful ordering.
  291. *
  292. * Changes to this code, including this one, must be
  293. * inspected, validated, and tested extremely carefully!!!
  294. */
  295. /*
  296. * First, pick up the index.
  297. */
  298. idx = ACCESS_ONCE(t->rcu_flipctr_idx);
  299. /*
  300. * Now that we have fetched the counter index, it is
  301. * safe to decrement the per-task RCU nesting counter.
  302. * After this, any interrupts or NMIs will increment and
  303. * decrement the per-CPU counters.
  304. */
  305. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
  306. /*
  307. * It is now safe to decrement this task's nesting count.
  308. * NMIs that occur after this statement will route their
  309. * rcu_read_lock() calls through this "else" clause, and
  310. * will thus start incrementing the per-CPU counter on
  311. * their own. They will also clobber ->rcu_flipctr_idx,
  312. * but that is OK, since we have already fetched it.
  313. */
  314. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
  315. local_irq_restore(flags);
  316. }
  317. }
  318. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  319. /*
  320. * If a global counter flip has occurred since the last time that we
  321. * advanced callbacks, advance them. Hardware interrupts must be
  322. * disabled when calling this function.
  323. */
  324. static void __rcu_advance_callbacks(struct rcu_data *rdp)
  325. {
  326. int cpu;
  327. int i;
  328. int wlc = 0;
  329. if (rdp->completed != rcu_ctrlblk.completed) {
  330. if (rdp->waitlist[GP_STAGES - 1] != NULL) {
  331. *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
  332. rdp->donetail = rdp->waittail[GP_STAGES - 1];
  333. RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
  334. }
  335. for (i = GP_STAGES - 2; i >= 0; i--) {
  336. if (rdp->waitlist[i] != NULL) {
  337. rdp->waitlist[i + 1] = rdp->waitlist[i];
  338. rdp->waittail[i + 1] = rdp->waittail[i];
  339. wlc++;
  340. } else {
  341. rdp->waitlist[i + 1] = NULL;
  342. rdp->waittail[i + 1] =
  343. &rdp->waitlist[i + 1];
  344. }
  345. }
  346. if (rdp->nextlist != NULL) {
  347. rdp->waitlist[0] = rdp->nextlist;
  348. rdp->waittail[0] = rdp->nexttail;
  349. wlc++;
  350. rdp->nextlist = NULL;
  351. rdp->nexttail = &rdp->nextlist;
  352. RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
  353. } else {
  354. rdp->waitlist[0] = NULL;
  355. rdp->waittail[0] = &rdp->waitlist[0];
  356. }
  357. rdp->waitlistcount = wlc;
  358. rdp->completed = rcu_ctrlblk.completed;
  359. }
  360. /*
  361. * Check to see if this CPU needs to report that it has seen
  362. * the most recent counter flip, thereby declaring that all
  363. * subsequent rcu_read_lock() invocations will respect this flip.
  364. */
  365. cpu = raw_smp_processor_id();
  366. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  367. smp_mb(); /* Subsequent counter accesses must see new value */
  368. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  369. smp_mb(); /* Subsequent RCU read-side critical sections */
  370. /* seen -after- acknowledgement. */
  371. }
  372. }
  373. DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_dyntick_sched, rcu_dyntick_sched) = {
  374. .dynticks = 1,
  375. };
  376. #ifdef CONFIG_NO_HZ
  377. static DEFINE_PER_CPU(int, rcu_update_flag);
  378. /**
  379. * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
  380. *
  381. * If the CPU was idle with dynamic ticks active, this updates the
  382. * rcu_dyntick_sched.dynticks to let the RCU handling know that the
  383. * CPU is active.
  384. */
  385. void rcu_irq_enter(void)
  386. {
  387. int cpu = smp_processor_id();
  388. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  389. if (per_cpu(rcu_update_flag, cpu))
  390. per_cpu(rcu_update_flag, cpu)++;
  391. /*
  392. * Only update if we are coming from a stopped ticks mode
  393. * (rcu_dyntick_sched.dynticks is even).
  394. */
  395. if (!in_interrupt() &&
  396. (rdssp->dynticks & 0x1) == 0) {
  397. /*
  398. * The following might seem like we could have a race
  399. * with NMI/SMIs. But this really isn't a problem.
  400. * Here we do a read/modify/write, and the race happens
  401. * when an NMI/SMI comes in after the read and before
  402. * the write. But NMI/SMIs will increment this counter
  403. * twice before returning, so the zero bit will not
  404. * be corrupted by the NMI/SMI which is the most important
  405. * part.
  406. *
  407. * The only thing is that we would bring back the counter
  408. * to a postion that it was in during the NMI/SMI.
  409. * But the zero bit would be set, so the rest of the
  410. * counter would again be ignored.
  411. *
  412. * On return from the IRQ, the counter may have the zero
  413. * bit be 0 and the counter the same as the return from
  414. * the NMI/SMI. If the state machine was so unlucky to
  415. * see that, it still doesn't matter, since all
  416. * RCU read-side critical sections on this CPU would
  417. * have already completed.
  418. */
  419. rdssp->dynticks++;
  420. /*
  421. * The following memory barrier ensures that any
  422. * rcu_read_lock() primitives in the irq handler
  423. * are seen by other CPUs to follow the above
  424. * increment to rcu_dyntick_sched.dynticks. This is
  425. * required in order for other CPUs to correctly
  426. * determine when it is safe to advance the RCU
  427. * grace-period state machine.
  428. */
  429. smp_mb(); /* see above block comment. */
  430. /*
  431. * Since we can't determine the dynamic tick mode from
  432. * the rcu_dyntick_sched.dynticks after this routine,
  433. * we use a second flag to acknowledge that we came
  434. * from an idle state with ticks stopped.
  435. */
  436. per_cpu(rcu_update_flag, cpu)++;
  437. /*
  438. * If we take an NMI/SMI now, they will also increment
  439. * the rcu_update_flag, and will not update the
  440. * rcu_dyntick_sched.dynticks on exit. That is for
  441. * this IRQ to do.
  442. */
  443. }
  444. }
  445. /**
  446. * rcu_irq_exit - Called from exiting Hard irq context.
  447. *
  448. * If the CPU was idle with dynamic ticks active, update the
  449. * rcu_dyntick_sched.dynticks to put let the RCU handling be
  450. * aware that the CPU is going back to idle with no ticks.
  451. */
  452. void rcu_irq_exit(void)
  453. {
  454. int cpu = smp_processor_id();
  455. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  456. /*
  457. * rcu_update_flag is set if we interrupted the CPU
  458. * when it was idle with ticks stopped.
  459. * Once this occurs, we keep track of interrupt nesting
  460. * because a NMI/SMI could also come in, and we still
  461. * only want the IRQ that started the increment of the
  462. * rcu_dyntick_sched.dynticks to be the one that modifies
  463. * it on exit.
  464. */
  465. if (per_cpu(rcu_update_flag, cpu)) {
  466. if (--per_cpu(rcu_update_flag, cpu))
  467. return;
  468. /* This must match the interrupt nesting */
  469. WARN_ON(in_interrupt());
  470. /*
  471. * If an NMI/SMI happens now we are still
  472. * protected by the rcu_dyntick_sched.dynticks being odd.
  473. */
  474. /*
  475. * The following memory barrier ensures that any
  476. * rcu_read_unlock() primitives in the irq handler
  477. * are seen by other CPUs to preceed the following
  478. * increment to rcu_dyntick_sched.dynticks. This
  479. * is required in order for other CPUs to determine
  480. * when it is safe to advance the RCU grace-period
  481. * state machine.
  482. */
  483. smp_mb(); /* see above block comment. */
  484. rdssp->dynticks++;
  485. WARN_ON(rdssp->dynticks & 0x1);
  486. }
  487. }
  488. static void dyntick_save_progress_counter(int cpu)
  489. {
  490. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  491. rdssp->dynticks_snap = rdssp->dynticks;
  492. }
  493. static inline int
  494. rcu_try_flip_waitack_needed(int cpu)
  495. {
  496. long curr;
  497. long snap;
  498. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  499. curr = rdssp->dynticks;
  500. snap = rdssp->dynticks_snap;
  501. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  502. /*
  503. * If the CPU remained in dynticks mode for the entire time
  504. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  505. * then it cannot be in the middle of an rcu_read_lock(), so
  506. * the next rcu_read_lock() it executes must use the new value
  507. * of the counter. So we can safely pretend that this CPU
  508. * already acknowledged the counter.
  509. */
  510. if ((curr == snap) && ((curr & 0x1) == 0))
  511. return 0;
  512. /*
  513. * If the CPU passed through or entered a dynticks idle phase with
  514. * no active irq handlers, then, as above, we can safely pretend
  515. * that this CPU already acknowledged the counter.
  516. */
  517. if ((curr - snap) > 2 || (curr & 0x1) == 0)
  518. return 0;
  519. /* We need this CPU to explicitly acknowledge the counter flip. */
  520. return 1;
  521. }
  522. static inline int
  523. rcu_try_flip_waitmb_needed(int cpu)
  524. {
  525. long curr;
  526. long snap;
  527. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  528. curr = rdssp->dynticks;
  529. snap = rdssp->dynticks_snap;
  530. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  531. /*
  532. * If the CPU remained in dynticks mode for the entire time
  533. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  534. * then it cannot have executed an RCU read-side critical section
  535. * during that time, so there is no need for it to execute a
  536. * memory barrier.
  537. */
  538. if ((curr == snap) && ((curr & 0x1) == 0))
  539. return 0;
  540. /*
  541. * If the CPU either entered or exited an outermost interrupt,
  542. * SMI, NMI, or whatever handler, then we know that it executed
  543. * a memory barrier when doing so. So we don't need another one.
  544. */
  545. if (curr != snap)
  546. return 0;
  547. /* We need the CPU to execute a memory barrier. */
  548. return 1;
  549. }
  550. static void dyntick_save_progress_counter_sched(int cpu)
  551. {
  552. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  553. rdssp->sched_dynticks_snap = rdssp->dynticks;
  554. }
  555. static int rcu_qsctr_inc_needed_dyntick(int cpu)
  556. {
  557. long curr;
  558. long snap;
  559. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  560. curr = rdssp->dynticks;
  561. snap = rdssp->sched_dynticks_snap;
  562. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  563. /*
  564. * If the CPU remained in dynticks mode for the entire time
  565. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  566. * then it cannot be in the middle of an rcu_read_lock(), so
  567. * the next rcu_read_lock() it executes must use the new value
  568. * of the counter. Therefore, this CPU has been in a quiescent
  569. * state the entire time, and we don't need to wait for it.
  570. */
  571. if ((curr == snap) && ((curr & 0x1) == 0))
  572. return 0;
  573. /*
  574. * If the CPU passed through or entered a dynticks idle phase with
  575. * no active irq handlers, then, as above, this CPU has already
  576. * passed through a quiescent state.
  577. */
  578. if ((curr - snap) > 2 || (snap & 0x1) == 0)
  579. return 0;
  580. /* We need this CPU to go through a quiescent state. */
  581. return 1;
  582. }
  583. #else /* !CONFIG_NO_HZ */
  584. # define dyntick_save_progress_counter(cpu) do { } while (0)
  585. # define rcu_try_flip_waitack_needed(cpu) (1)
  586. # define rcu_try_flip_waitmb_needed(cpu) (1)
  587. # define dyntick_save_progress_counter_sched(cpu) do { } while (0)
  588. # define rcu_qsctr_inc_needed_dyntick(cpu) (1)
  589. #endif /* CONFIG_NO_HZ */
  590. static void save_qsctr_sched(int cpu)
  591. {
  592. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  593. rdssp->sched_qs_snap = rdssp->sched_qs;
  594. }
  595. static inline int rcu_qsctr_inc_needed(int cpu)
  596. {
  597. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  598. /*
  599. * If there has been a quiescent state, no more need to wait
  600. * on this CPU.
  601. */
  602. if (rdssp->sched_qs != rdssp->sched_qs_snap) {
  603. smp_mb(); /* force ordering with cpu entering schedule(). */
  604. return 0;
  605. }
  606. /* We need this CPU to go through a quiescent state. */
  607. return 1;
  608. }
  609. /*
  610. * Get here when RCU is idle. Decide whether we need to
  611. * move out of idle state, and return non-zero if so.
  612. * "Straightforward" approach for the moment, might later
  613. * use callback-list lengths, grace-period duration, or
  614. * some such to determine when to exit idle state.
  615. * Might also need a pre-idle test that does not acquire
  616. * the lock, but let's get the simple case working first...
  617. */
  618. static int
  619. rcu_try_flip_idle(void)
  620. {
  621. int cpu;
  622. RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
  623. if (!rcu_pending(smp_processor_id())) {
  624. RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
  625. return 0;
  626. }
  627. /*
  628. * Do the flip.
  629. */
  630. RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
  631. rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
  632. /*
  633. * Need a memory barrier so that other CPUs see the new
  634. * counter value before they see the subsequent change of all
  635. * the rcu_flip_flag instances to rcu_flipped.
  636. */
  637. smp_mb(); /* see above block comment. */
  638. /* Now ask each CPU for acknowledgement of the flip. */
  639. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  640. per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
  641. dyntick_save_progress_counter(cpu);
  642. }
  643. return 1;
  644. }
  645. /*
  646. * Wait for CPUs to acknowledge the flip.
  647. */
  648. static int
  649. rcu_try_flip_waitack(void)
  650. {
  651. int cpu;
  652. RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
  653. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  654. if (rcu_try_flip_waitack_needed(cpu) &&
  655. per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
  656. RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
  657. return 0;
  658. }
  659. /*
  660. * Make sure our checks above don't bleed into subsequent
  661. * waiting for the sum of the counters to reach zero.
  662. */
  663. smp_mb(); /* see above block comment. */
  664. RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
  665. return 1;
  666. }
  667. /*
  668. * Wait for collective ``last'' counter to reach zero,
  669. * then tell all CPUs to do an end-of-grace-period memory barrier.
  670. */
  671. static int
  672. rcu_try_flip_waitzero(void)
  673. {
  674. int cpu;
  675. int lastidx = !(rcu_ctrlblk.completed & 0x1);
  676. int sum = 0;
  677. /* Check to see if the sum of the "last" counters is zero. */
  678. RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
  679. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  680. sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
  681. if (sum != 0) {
  682. RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
  683. return 0;
  684. }
  685. /*
  686. * This ensures that the other CPUs see the call for
  687. * memory barriers -after- the sum to zero has been
  688. * detected here
  689. */
  690. smp_mb(); /* ^^^^^^^^^^^^ */
  691. /* Call for a memory barrier from each CPU. */
  692. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  693. per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
  694. dyntick_save_progress_counter(cpu);
  695. }
  696. RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
  697. return 1;
  698. }
  699. /*
  700. * Wait for all CPUs to do their end-of-grace-period memory barrier.
  701. * Return 0 once all CPUs have done so.
  702. */
  703. static int
  704. rcu_try_flip_waitmb(void)
  705. {
  706. int cpu;
  707. RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
  708. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  709. if (rcu_try_flip_waitmb_needed(cpu) &&
  710. per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
  711. RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
  712. return 0;
  713. }
  714. smp_mb(); /* Ensure that the above checks precede any following flip. */
  715. RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
  716. return 1;
  717. }
  718. /*
  719. * Attempt a single flip of the counters. Remember, a single flip does
  720. * -not- constitute a grace period. Instead, the interval between
  721. * at least GP_STAGES consecutive flips is a grace period.
  722. *
  723. * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
  724. * on a large SMP, they might want to use a hierarchical organization of
  725. * the per-CPU-counter pairs.
  726. */
  727. static void rcu_try_flip(void)
  728. {
  729. unsigned long flags;
  730. RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
  731. if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
  732. RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
  733. return;
  734. }
  735. /*
  736. * Take the next transition(s) through the RCU grace-period
  737. * flip-counter state machine.
  738. */
  739. switch (rcu_ctrlblk.rcu_try_flip_state) {
  740. case rcu_try_flip_idle_state:
  741. if (rcu_try_flip_idle())
  742. rcu_ctrlblk.rcu_try_flip_state =
  743. rcu_try_flip_waitack_state;
  744. break;
  745. case rcu_try_flip_waitack_state:
  746. if (rcu_try_flip_waitack())
  747. rcu_ctrlblk.rcu_try_flip_state =
  748. rcu_try_flip_waitzero_state;
  749. break;
  750. case rcu_try_flip_waitzero_state:
  751. if (rcu_try_flip_waitzero())
  752. rcu_ctrlblk.rcu_try_flip_state =
  753. rcu_try_flip_waitmb_state;
  754. break;
  755. case rcu_try_flip_waitmb_state:
  756. if (rcu_try_flip_waitmb())
  757. rcu_ctrlblk.rcu_try_flip_state =
  758. rcu_try_flip_idle_state;
  759. }
  760. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  761. }
  762. /*
  763. * Check to see if this CPU needs to do a memory barrier in order to
  764. * ensure that any prior RCU read-side critical sections have committed
  765. * their counter manipulations and critical-section memory references
  766. * before declaring the grace period to be completed.
  767. */
  768. static void rcu_check_mb(int cpu)
  769. {
  770. if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
  771. smp_mb(); /* Ensure RCU read-side accesses are visible. */
  772. per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
  773. }
  774. }
  775. void rcu_check_callbacks(int cpu, int user)
  776. {
  777. unsigned long flags;
  778. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  779. /*
  780. * If this CPU took its interrupt from user mode or from the
  781. * idle loop, and this is not a nested interrupt, then
  782. * this CPU has to have exited all prior preept-disable
  783. * sections of code. So increment the counter to note this.
  784. *
  785. * The memory barrier is needed to handle the case where
  786. * writes from a preempt-disable section of code get reordered
  787. * into schedule() by this CPU's write buffer. So the memory
  788. * barrier makes sure that the rcu_qsctr_inc() is seen by other
  789. * CPUs to happen after any such write.
  790. */
  791. if (user ||
  792. (idle_cpu(cpu) && !in_softirq() &&
  793. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  794. smp_mb(); /* Guard against aggressive schedule(). */
  795. rcu_qsctr_inc(cpu);
  796. }
  797. rcu_check_mb(cpu);
  798. if (rcu_ctrlblk.completed == rdp->completed)
  799. rcu_try_flip();
  800. spin_lock_irqsave(&rdp->lock, flags);
  801. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  802. __rcu_advance_callbacks(rdp);
  803. if (rdp->donelist == NULL) {
  804. spin_unlock_irqrestore(&rdp->lock, flags);
  805. } else {
  806. spin_unlock_irqrestore(&rdp->lock, flags);
  807. raise_softirq(RCU_SOFTIRQ);
  808. }
  809. }
  810. /*
  811. * Needed by dynticks, to make sure all RCU processing has finished
  812. * when we go idle:
  813. */
  814. void rcu_advance_callbacks(int cpu, int user)
  815. {
  816. unsigned long flags;
  817. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  818. if (rcu_ctrlblk.completed == rdp->completed) {
  819. rcu_try_flip();
  820. if (rcu_ctrlblk.completed == rdp->completed)
  821. return;
  822. }
  823. spin_lock_irqsave(&rdp->lock, flags);
  824. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  825. __rcu_advance_callbacks(rdp);
  826. spin_unlock_irqrestore(&rdp->lock, flags);
  827. }
  828. #ifdef CONFIG_HOTPLUG_CPU
  829. #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
  830. *dsttail = srclist; \
  831. if (srclist != NULL) { \
  832. dsttail = srctail; \
  833. srclist = NULL; \
  834. srctail = &srclist;\
  835. } \
  836. } while (0)
  837. void rcu_offline_cpu(int cpu)
  838. {
  839. int i;
  840. struct rcu_head *list = NULL;
  841. unsigned long flags;
  842. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  843. struct rcu_head *schedlist = NULL;
  844. struct rcu_head **schedtail = &schedlist;
  845. struct rcu_head **tail = &list;
  846. /*
  847. * Remove all callbacks from the newly dead CPU, retaining order.
  848. * Otherwise rcu_barrier() will fail
  849. */
  850. spin_lock_irqsave(&rdp->lock, flags);
  851. rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
  852. for (i = GP_STAGES - 1; i >= 0; i--)
  853. rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
  854. list, tail);
  855. rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
  856. rcu_offline_cpu_enqueue(rdp->waitschedlist, rdp->waitschedtail,
  857. schedlist, schedtail);
  858. rcu_offline_cpu_enqueue(rdp->nextschedlist, rdp->nextschedtail,
  859. schedlist, schedtail);
  860. rdp->rcu_sched_sleeping = 0;
  861. spin_unlock_irqrestore(&rdp->lock, flags);
  862. rdp->waitlistcount = 0;
  863. /* Disengage the newly dead CPU from the grace-period computation. */
  864. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  865. rcu_check_mb(cpu);
  866. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  867. smp_mb(); /* Subsequent counter accesses must see new value */
  868. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  869. smp_mb(); /* Subsequent RCU read-side critical sections */
  870. /* seen -after- acknowledgement. */
  871. }
  872. RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  873. RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
  874. RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
  875. RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
  876. cpu_clear(cpu, rcu_cpu_online_map);
  877. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  878. /*
  879. * Place the removed callbacks on the current CPU's queue.
  880. * Make them all start a new grace period: simple approach,
  881. * in theory could starve a given set of callbacks, but
  882. * you would need to be doing some serious CPU hotplugging
  883. * to make this happen. If this becomes a problem, adding
  884. * a synchronize_rcu() to the hotplug path would be a simple
  885. * fix.
  886. */
  887. local_irq_save(flags); /* disable preempt till we know what lock. */
  888. rdp = RCU_DATA_ME();
  889. spin_lock(&rdp->lock);
  890. *rdp->nexttail = list;
  891. if (list)
  892. rdp->nexttail = tail;
  893. *rdp->nextschedtail = schedlist;
  894. if (schedlist)
  895. rdp->nextschedtail = schedtail;
  896. spin_unlock_irqrestore(&rdp->lock, flags);
  897. }
  898. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  899. void rcu_offline_cpu(int cpu)
  900. {
  901. }
  902. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  903. void __cpuinit rcu_online_cpu(int cpu)
  904. {
  905. unsigned long flags;
  906. struct rcu_data *rdp;
  907. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  908. cpu_set(cpu, rcu_cpu_online_map);
  909. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  910. /*
  911. * The rcu_sched grace-period processing might have bypassed
  912. * this CPU, given that it was not in the rcu_cpu_online_map
  913. * when the grace-period scan started. This means that the
  914. * grace-period task might sleep. So make sure that if this
  915. * should happen, the first callback posted to this CPU will
  916. * wake up the grace-period task if need be.
  917. */
  918. rdp = RCU_DATA_CPU(cpu);
  919. spin_lock_irqsave(&rdp->lock, flags);
  920. rdp->rcu_sched_sleeping = 1;
  921. spin_unlock_irqrestore(&rdp->lock, flags);
  922. }
  923. static void rcu_process_callbacks(struct softirq_action *unused)
  924. {
  925. unsigned long flags;
  926. struct rcu_head *next, *list;
  927. struct rcu_data *rdp;
  928. local_irq_save(flags);
  929. rdp = RCU_DATA_ME();
  930. spin_lock(&rdp->lock);
  931. list = rdp->donelist;
  932. if (list == NULL) {
  933. spin_unlock_irqrestore(&rdp->lock, flags);
  934. return;
  935. }
  936. rdp->donelist = NULL;
  937. rdp->donetail = &rdp->donelist;
  938. RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
  939. spin_unlock_irqrestore(&rdp->lock, flags);
  940. while (list) {
  941. next = list->next;
  942. list->func(list);
  943. list = next;
  944. RCU_TRACE_ME(rcupreempt_trace_invoke);
  945. }
  946. }
  947. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  948. {
  949. unsigned long flags;
  950. struct rcu_data *rdp;
  951. head->func = func;
  952. head->next = NULL;
  953. local_irq_save(flags);
  954. rdp = RCU_DATA_ME();
  955. spin_lock(&rdp->lock);
  956. __rcu_advance_callbacks(rdp);
  957. *rdp->nexttail = head;
  958. rdp->nexttail = &head->next;
  959. RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
  960. spin_unlock_irqrestore(&rdp->lock, flags);
  961. }
  962. EXPORT_SYMBOL_GPL(call_rcu);
  963. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  964. {
  965. unsigned long flags;
  966. struct rcu_data *rdp;
  967. int wake_gp = 0;
  968. head->func = func;
  969. head->next = NULL;
  970. local_irq_save(flags);
  971. rdp = RCU_DATA_ME();
  972. spin_lock(&rdp->lock);
  973. *rdp->nextschedtail = head;
  974. rdp->nextschedtail = &head->next;
  975. if (rdp->rcu_sched_sleeping) {
  976. /* Grace-period processing might be sleeping... */
  977. rdp->rcu_sched_sleeping = 0;
  978. wake_gp = 1;
  979. }
  980. spin_unlock_irqrestore(&rdp->lock, flags);
  981. if (wake_gp) {
  982. /* Wake up grace-period processing, unless someone beat us. */
  983. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  984. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleeping)
  985. wake_gp = 0;
  986. rcu_ctrlblk.sched_sleep = rcu_sched_not_sleeping;
  987. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  988. if (wake_gp)
  989. wake_up_interruptible(&rcu_ctrlblk.sched_wq);
  990. }
  991. }
  992. EXPORT_SYMBOL_GPL(call_rcu_sched);
  993. /*
  994. * Wait until all currently running preempt_disable() code segments
  995. * (including hardware-irq-disable segments) complete. Note that
  996. * in -rt this does -not- necessarily result in all currently executing
  997. * interrupt -handlers- having completed.
  998. */
  999. synchronize_rcu_xxx(__synchronize_sched, call_rcu_sched)
  1000. EXPORT_SYMBOL_GPL(__synchronize_sched);
  1001. /*
  1002. * kthread function that manages call_rcu_sched grace periods.
  1003. */
  1004. static int rcu_sched_grace_period(void *arg)
  1005. {
  1006. int couldsleep; /* might sleep after current pass. */
  1007. int couldsleepnext = 0; /* might sleep after next pass. */
  1008. int cpu;
  1009. unsigned long flags;
  1010. struct rcu_data *rdp;
  1011. int ret;
  1012. /*
  1013. * Each pass through the following loop handles one
  1014. * rcu_sched grace period cycle.
  1015. */
  1016. do {
  1017. /* Save each CPU's current state. */
  1018. for_each_online_cpu(cpu) {
  1019. dyntick_save_progress_counter_sched(cpu);
  1020. save_qsctr_sched(cpu);
  1021. }
  1022. /*
  1023. * Sleep for about an RCU grace-period's worth to
  1024. * allow better batching and to consume less CPU.
  1025. */
  1026. schedule_timeout_interruptible(RCU_SCHED_BATCH_TIME);
  1027. /*
  1028. * If there was nothing to do last time, prepare to
  1029. * sleep at the end of the current grace period cycle.
  1030. */
  1031. couldsleep = couldsleepnext;
  1032. couldsleepnext = 1;
  1033. if (couldsleep) {
  1034. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1035. rcu_ctrlblk.sched_sleep = rcu_sched_sleep_prep;
  1036. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1037. }
  1038. /*
  1039. * Wait on each CPU in turn to have either visited
  1040. * a quiescent state or been in dynticks-idle mode.
  1041. */
  1042. for_each_online_cpu(cpu) {
  1043. while (rcu_qsctr_inc_needed(cpu) &&
  1044. rcu_qsctr_inc_needed_dyntick(cpu)) {
  1045. /* resched_cpu(cpu); @@@ */
  1046. schedule_timeout_interruptible(1);
  1047. }
  1048. }
  1049. /* Advance callbacks for each CPU. */
  1050. for_each_online_cpu(cpu) {
  1051. rdp = RCU_DATA_CPU(cpu);
  1052. spin_lock_irqsave(&rdp->lock, flags);
  1053. /*
  1054. * We are running on this CPU irq-disabled, so no
  1055. * CPU can go offline until we re-enable irqs.
  1056. * The current CPU might have already gone
  1057. * offline (between the for_each_offline_cpu and
  1058. * the spin_lock_irqsave), but in that case all its
  1059. * callback lists will be empty, so no harm done.
  1060. *
  1061. * Advance the callbacks! We share normal RCU's
  1062. * donelist, since callbacks are invoked the
  1063. * same way in either case.
  1064. */
  1065. if (rdp->waitschedlist != NULL) {
  1066. *rdp->donetail = rdp->waitschedlist;
  1067. rdp->donetail = rdp->waitschedtail;
  1068. /*
  1069. * Next rcu_check_callbacks() will
  1070. * do the required raise_softirq().
  1071. */
  1072. }
  1073. if (rdp->nextschedlist != NULL) {
  1074. rdp->waitschedlist = rdp->nextschedlist;
  1075. rdp->waitschedtail = rdp->nextschedtail;
  1076. couldsleep = 0;
  1077. couldsleepnext = 0;
  1078. } else {
  1079. rdp->waitschedlist = NULL;
  1080. rdp->waitschedtail = &rdp->waitschedlist;
  1081. }
  1082. rdp->nextschedlist = NULL;
  1083. rdp->nextschedtail = &rdp->nextschedlist;
  1084. /* Mark sleep intention. */
  1085. rdp->rcu_sched_sleeping = couldsleep;
  1086. spin_unlock_irqrestore(&rdp->lock, flags);
  1087. }
  1088. /* If we saw callbacks on the last scan, go deal with them. */
  1089. if (!couldsleep)
  1090. continue;
  1091. /* Attempt to block... */
  1092. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1093. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleep_prep) {
  1094. /*
  1095. * Someone posted a callback after we scanned.
  1096. * Go take care of it.
  1097. */
  1098. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1099. couldsleepnext = 0;
  1100. continue;
  1101. }
  1102. /* Block until the next person posts a callback. */
  1103. rcu_ctrlblk.sched_sleep = rcu_sched_sleeping;
  1104. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1105. ret = 0;
  1106. __wait_event_interruptible(rcu_ctrlblk.sched_wq,
  1107. rcu_ctrlblk.sched_sleep != rcu_sched_sleeping,
  1108. ret);
  1109. /*
  1110. * Signals would prevent us from sleeping, and we cannot
  1111. * do much with them in any case. So flush them.
  1112. */
  1113. if (ret)
  1114. flush_signals(current);
  1115. couldsleepnext = 0;
  1116. } while (!kthread_should_stop());
  1117. return (0);
  1118. }
  1119. /*
  1120. * Check to see if any future RCU-related work will need to be done
  1121. * by the current CPU, even if none need be done immediately, returning
  1122. * 1 if so. Assumes that notifiers would take care of handling any
  1123. * outstanding requests from the RCU core.
  1124. *
  1125. * This function is part of the RCU implementation; it is -not-
  1126. * an exported member of the RCU API.
  1127. */
  1128. int rcu_needs_cpu(int cpu)
  1129. {
  1130. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1131. return (rdp->donelist != NULL ||
  1132. !!rdp->waitlistcount ||
  1133. rdp->nextlist != NULL ||
  1134. rdp->nextschedlist != NULL ||
  1135. rdp->waitschedlist != NULL);
  1136. }
  1137. int rcu_pending(int cpu)
  1138. {
  1139. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1140. /* The CPU has at least one callback queued somewhere. */
  1141. if (rdp->donelist != NULL ||
  1142. !!rdp->waitlistcount ||
  1143. rdp->nextlist != NULL ||
  1144. rdp->nextschedlist != NULL ||
  1145. rdp->waitschedlist != NULL)
  1146. return 1;
  1147. /* The RCU core needs an acknowledgement from this CPU. */
  1148. if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
  1149. (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
  1150. return 1;
  1151. /* This CPU has fallen behind the global grace-period number. */
  1152. if (rdp->completed != rcu_ctrlblk.completed)
  1153. return 1;
  1154. /* Nothing needed from this CPU. */
  1155. return 0;
  1156. }
  1157. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1158. unsigned long action, void *hcpu)
  1159. {
  1160. long cpu = (long)hcpu;
  1161. switch (action) {
  1162. case CPU_UP_PREPARE:
  1163. case CPU_UP_PREPARE_FROZEN:
  1164. rcu_online_cpu(cpu);
  1165. break;
  1166. case CPU_UP_CANCELED:
  1167. case CPU_UP_CANCELED_FROZEN:
  1168. case CPU_DEAD:
  1169. case CPU_DEAD_FROZEN:
  1170. rcu_offline_cpu(cpu);
  1171. break;
  1172. default:
  1173. break;
  1174. }
  1175. return NOTIFY_OK;
  1176. }
  1177. static struct notifier_block __cpuinitdata rcu_nb = {
  1178. .notifier_call = rcu_cpu_notify,
  1179. };
  1180. void __init __rcu_init(void)
  1181. {
  1182. int cpu;
  1183. int i;
  1184. struct rcu_data *rdp;
  1185. printk(KERN_NOTICE "Preemptible RCU implementation.\n");
  1186. for_each_possible_cpu(cpu) {
  1187. rdp = RCU_DATA_CPU(cpu);
  1188. spin_lock_init(&rdp->lock);
  1189. rdp->completed = 0;
  1190. rdp->waitlistcount = 0;
  1191. rdp->nextlist = NULL;
  1192. rdp->nexttail = &rdp->nextlist;
  1193. for (i = 0; i < GP_STAGES; i++) {
  1194. rdp->waitlist[i] = NULL;
  1195. rdp->waittail[i] = &rdp->waitlist[i];
  1196. }
  1197. rdp->donelist = NULL;
  1198. rdp->donetail = &rdp->donelist;
  1199. rdp->rcu_flipctr[0] = 0;
  1200. rdp->rcu_flipctr[1] = 0;
  1201. rdp->nextschedlist = NULL;
  1202. rdp->nextschedtail = &rdp->nextschedlist;
  1203. rdp->waitschedlist = NULL;
  1204. rdp->waitschedtail = &rdp->waitschedlist;
  1205. rdp->rcu_sched_sleeping = 0;
  1206. }
  1207. register_cpu_notifier(&rcu_nb);
  1208. /*
  1209. * We don't need protection against CPU-Hotplug here
  1210. * since
  1211. * a) If a CPU comes online while we are iterating over the
  1212. * cpu_online_map below, we would only end up making a
  1213. * duplicate call to rcu_online_cpu() which sets the corresponding
  1214. * CPU's mask in the rcu_cpu_online_map.
  1215. *
  1216. * b) A CPU cannot go offline at this point in time since the user
  1217. * does not have access to the sysfs interface, nor do we
  1218. * suspend the system.
  1219. */
  1220. for_each_online_cpu(cpu)
  1221. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
  1222. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1223. }
  1224. /*
  1225. * Late-boot-time RCU initialization that must wait until after scheduler
  1226. * has been initialized.
  1227. */
  1228. void __init rcu_init_sched(void)
  1229. {
  1230. rcu_sched_grace_period_task = kthread_run(rcu_sched_grace_period,
  1231. NULL,
  1232. "rcu_sched_grace_period");
  1233. WARN_ON(IS_ERR(rcu_sched_grace_period_task));
  1234. }
  1235. #ifdef CONFIG_RCU_TRACE
  1236. long *rcupreempt_flipctr(int cpu)
  1237. {
  1238. return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  1239. }
  1240. EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
  1241. int rcupreempt_flip_flag(int cpu)
  1242. {
  1243. return per_cpu(rcu_flip_flag, cpu);
  1244. }
  1245. EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
  1246. int rcupreempt_mb_flag(int cpu)
  1247. {
  1248. return per_cpu(rcu_mb_flag, cpu);
  1249. }
  1250. EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
  1251. char *rcupreempt_try_flip_state_name(void)
  1252. {
  1253. return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
  1254. }
  1255. EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
  1256. struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
  1257. {
  1258. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1259. return &rdp->trace;
  1260. }
  1261. EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
  1262. #endif /* #ifdef RCU_TRACE */