rcupreempt.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. void rcu_nmi_enter(void)
  489. {
  490. rcu_irq_enter();
  491. }
  492. void rcu_nmi_exit(void)
  493. {
  494. rcu_irq_exit();
  495. }
  496. static void dyntick_save_progress_counter(int cpu)
  497. {
  498. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  499. rdssp->dynticks_snap = rdssp->dynticks;
  500. }
  501. static inline int
  502. rcu_try_flip_waitack_needed(int cpu)
  503. {
  504. long curr;
  505. long snap;
  506. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  507. curr = rdssp->dynticks;
  508. snap = rdssp->dynticks_snap;
  509. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  510. /*
  511. * If the CPU remained in dynticks mode for the entire time
  512. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  513. * then it cannot be in the middle of an rcu_read_lock(), so
  514. * the next rcu_read_lock() it executes must use the new value
  515. * of the counter. So we can safely pretend that this CPU
  516. * already acknowledged the counter.
  517. */
  518. if ((curr == snap) && ((curr & 0x1) == 0))
  519. return 0;
  520. /*
  521. * If the CPU passed through or entered a dynticks idle phase with
  522. * no active irq handlers, then, as above, we can safely pretend
  523. * that this CPU already acknowledged the counter.
  524. */
  525. if ((curr - snap) > 2 || (curr & 0x1) == 0)
  526. return 0;
  527. /* We need this CPU to explicitly acknowledge the counter flip. */
  528. return 1;
  529. }
  530. static inline int
  531. rcu_try_flip_waitmb_needed(int cpu)
  532. {
  533. long curr;
  534. long snap;
  535. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  536. curr = rdssp->dynticks;
  537. snap = rdssp->dynticks_snap;
  538. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  539. /*
  540. * If the CPU remained in dynticks mode for the entire time
  541. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  542. * then it cannot have executed an RCU read-side critical section
  543. * during that time, so there is no need for it to execute a
  544. * memory barrier.
  545. */
  546. if ((curr == snap) && ((curr & 0x1) == 0))
  547. return 0;
  548. /*
  549. * If the CPU either entered or exited an outermost interrupt,
  550. * SMI, NMI, or whatever handler, then we know that it executed
  551. * a memory barrier when doing so. So we don't need another one.
  552. */
  553. if (curr != snap)
  554. return 0;
  555. /* We need the CPU to execute a memory barrier. */
  556. return 1;
  557. }
  558. static void dyntick_save_progress_counter_sched(int cpu)
  559. {
  560. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  561. rdssp->sched_dynticks_snap = rdssp->dynticks;
  562. }
  563. static int rcu_qsctr_inc_needed_dyntick(int cpu)
  564. {
  565. long curr;
  566. long snap;
  567. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  568. curr = rdssp->dynticks;
  569. snap = rdssp->sched_dynticks_snap;
  570. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  571. /*
  572. * If the CPU remained in dynticks mode for the entire time
  573. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  574. * then it cannot be in the middle of an rcu_read_lock(), so
  575. * the next rcu_read_lock() it executes must use the new value
  576. * of the counter. Therefore, this CPU has been in a quiescent
  577. * state the entire time, and we don't need to wait for it.
  578. */
  579. if ((curr == snap) && ((curr & 0x1) == 0))
  580. return 0;
  581. /*
  582. * If the CPU passed through or entered a dynticks idle phase with
  583. * no active irq handlers, then, as above, this CPU has already
  584. * passed through a quiescent state.
  585. */
  586. if ((curr - snap) > 2 || (snap & 0x1) == 0)
  587. return 0;
  588. /* We need this CPU to go through a quiescent state. */
  589. return 1;
  590. }
  591. #else /* !CONFIG_NO_HZ */
  592. # define dyntick_save_progress_counter(cpu) do { } while (0)
  593. # define rcu_try_flip_waitack_needed(cpu) (1)
  594. # define rcu_try_flip_waitmb_needed(cpu) (1)
  595. # define dyntick_save_progress_counter_sched(cpu) do { } while (0)
  596. # define rcu_qsctr_inc_needed_dyntick(cpu) (1)
  597. #endif /* CONFIG_NO_HZ */
  598. static void save_qsctr_sched(int cpu)
  599. {
  600. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  601. rdssp->sched_qs_snap = rdssp->sched_qs;
  602. }
  603. static inline int rcu_qsctr_inc_needed(int cpu)
  604. {
  605. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  606. /*
  607. * If there has been a quiescent state, no more need to wait
  608. * on this CPU.
  609. */
  610. if (rdssp->sched_qs != rdssp->sched_qs_snap) {
  611. smp_mb(); /* force ordering with cpu entering schedule(). */
  612. return 0;
  613. }
  614. /* We need this CPU to go through a quiescent state. */
  615. return 1;
  616. }
  617. /*
  618. * Get here when RCU is idle. Decide whether we need to
  619. * move out of idle state, and return non-zero if so.
  620. * "Straightforward" approach for the moment, might later
  621. * use callback-list lengths, grace-period duration, or
  622. * some such to determine when to exit idle state.
  623. * Might also need a pre-idle test that does not acquire
  624. * the lock, but let's get the simple case working first...
  625. */
  626. static int
  627. rcu_try_flip_idle(void)
  628. {
  629. int cpu;
  630. RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
  631. if (!rcu_pending(smp_processor_id())) {
  632. RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
  633. return 0;
  634. }
  635. /*
  636. * Do the flip.
  637. */
  638. RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
  639. rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
  640. /*
  641. * Need a memory barrier so that other CPUs see the new
  642. * counter value before they see the subsequent change of all
  643. * the rcu_flip_flag instances to rcu_flipped.
  644. */
  645. smp_mb(); /* see above block comment. */
  646. /* Now ask each CPU for acknowledgement of the flip. */
  647. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  648. per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
  649. dyntick_save_progress_counter(cpu);
  650. }
  651. return 1;
  652. }
  653. /*
  654. * Wait for CPUs to acknowledge the flip.
  655. */
  656. static int
  657. rcu_try_flip_waitack(void)
  658. {
  659. int cpu;
  660. RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
  661. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  662. if (rcu_try_flip_waitack_needed(cpu) &&
  663. per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
  664. RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
  665. return 0;
  666. }
  667. /*
  668. * Make sure our checks above don't bleed into subsequent
  669. * waiting for the sum of the counters to reach zero.
  670. */
  671. smp_mb(); /* see above block comment. */
  672. RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
  673. return 1;
  674. }
  675. /*
  676. * Wait for collective ``last'' counter to reach zero,
  677. * then tell all CPUs to do an end-of-grace-period memory barrier.
  678. */
  679. static int
  680. rcu_try_flip_waitzero(void)
  681. {
  682. int cpu;
  683. int lastidx = !(rcu_ctrlblk.completed & 0x1);
  684. int sum = 0;
  685. /* Check to see if the sum of the "last" counters is zero. */
  686. RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
  687. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  688. sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
  689. if (sum != 0) {
  690. RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
  691. return 0;
  692. }
  693. /*
  694. * This ensures that the other CPUs see the call for
  695. * memory barriers -after- the sum to zero has been
  696. * detected here
  697. */
  698. smp_mb(); /* ^^^^^^^^^^^^ */
  699. /* Call for a memory barrier from each CPU. */
  700. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  701. per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
  702. dyntick_save_progress_counter(cpu);
  703. }
  704. RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
  705. return 1;
  706. }
  707. /*
  708. * Wait for all CPUs to do their end-of-grace-period memory barrier.
  709. * Return 0 once all CPUs have done so.
  710. */
  711. static int
  712. rcu_try_flip_waitmb(void)
  713. {
  714. int cpu;
  715. RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
  716. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  717. if (rcu_try_flip_waitmb_needed(cpu) &&
  718. per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
  719. RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
  720. return 0;
  721. }
  722. smp_mb(); /* Ensure that the above checks precede any following flip. */
  723. RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
  724. return 1;
  725. }
  726. /*
  727. * Attempt a single flip of the counters. Remember, a single flip does
  728. * -not- constitute a grace period. Instead, the interval between
  729. * at least GP_STAGES consecutive flips is a grace period.
  730. *
  731. * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
  732. * on a large SMP, they might want to use a hierarchical organization of
  733. * the per-CPU-counter pairs.
  734. */
  735. static void rcu_try_flip(void)
  736. {
  737. unsigned long flags;
  738. RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
  739. if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
  740. RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
  741. return;
  742. }
  743. /*
  744. * Take the next transition(s) through the RCU grace-period
  745. * flip-counter state machine.
  746. */
  747. switch (rcu_ctrlblk.rcu_try_flip_state) {
  748. case rcu_try_flip_idle_state:
  749. if (rcu_try_flip_idle())
  750. rcu_ctrlblk.rcu_try_flip_state =
  751. rcu_try_flip_waitack_state;
  752. break;
  753. case rcu_try_flip_waitack_state:
  754. if (rcu_try_flip_waitack())
  755. rcu_ctrlblk.rcu_try_flip_state =
  756. rcu_try_flip_waitzero_state;
  757. break;
  758. case rcu_try_flip_waitzero_state:
  759. if (rcu_try_flip_waitzero())
  760. rcu_ctrlblk.rcu_try_flip_state =
  761. rcu_try_flip_waitmb_state;
  762. break;
  763. case rcu_try_flip_waitmb_state:
  764. if (rcu_try_flip_waitmb())
  765. rcu_ctrlblk.rcu_try_flip_state =
  766. rcu_try_flip_idle_state;
  767. }
  768. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  769. }
  770. /*
  771. * Check to see if this CPU needs to do a memory barrier in order to
  772. * ensure that any prior RCU read-side critical sections have committed
  773. * their counter manipulations and critical-section memory references
  774. * before declaring the grace period to be completed.
  775. */
  776. static void rcu_check_mb(int cpu)
  777. {
  778. if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
  779. smp_mb(); /* Ensure RCU read-side accesses are visible. */
  780. per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
  781. }
  782. }
  783. void rcu_check_callbacks(int cpu, int user)
  784. {
  785. unsigned long flags;
  786. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  787. /*
  788. * If this CPU took its interrupt from user mode or from the
  789. * idle loop, and this is not a nested interrupt, then
  790. * this CPU has to have exited all prior preept-disable
  791. * sections of code. So increment the counter to note this.
  792. *
  793. * The memory barrier is needed to handle the case where
  794. * writes from a preempt-disable section of code get reordered
  795. * into schedule() by this CPU's write buffer. So the memory
  796. * barrier makes sure that the rcu_qsctr_inc() is seen by other
  797. * CPUs to happen after any such write.
  798. */
  799. if (user ||
  800. (idle_cpu(cpu) && !in_softirq() &&
  801. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  802. smp_mb(); /* Guard against aggressive schedule(). */
  803. rcu_qsctr_inc(cpu);
  804. }
  805. rcu_check_mb(cpu);
  806. if (rcu_ctrlblk.completed == rdp->completed)
  807. rcu_try_flip();
  808. spin_lock_irqsave(&rdp->lock, flags);
  809. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  810. __rcu_advance_callbacks(rdp);
  811. if (rdp->donelist == NULL) {
  812. spin_unlock_irqrestore(&rdp->lock, flags);
  813. } else {
  814. spin_unlock_irqrestore(&rdp->lock, flags);
  815. raise_softirq(RCU_SOFTIRQ);
  816. }
  817. }
  818. /*
  819. * Needed by dynticks, to make sure all RCU processing has finished
  820. * when we go idle:
  821. */
  822. void rcu_advance_callbacks(int cpu, int user)
  823. {
  824. unsigned long flags;
  825. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  826. if (rcu_ctrlblk.completed == rdp->completed) {
  827. rcu_try_flip();
  828. if (rcu_ctrlblk.completed == rdp->completed)
  829. return;
  830. }
  831. spin_lock_irqsave(&rdp->lock, flags);
  832. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  833. __rcu_advance_callbacks(rdp);
  834. spin_unlock_irqrestore(&rdp->lock, flags);
  835. }
  836. #ifdef CONFIG_HOTPLUG_CPU
  837. #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
  838. *dsttail = srclist; \
  839. if (srclist != NULL) { \
  840. dsttail = srctail; \
  841. srclist = NULL; \
  842. srctail = &srclist;\
  843. } \
  844. } while (0)
  845. void rcu_offline_cpu(int cpu)
  846. {
  847. int i;
  848. struct rcu_head *list = NULL;
  849. unsigned long flags;
  850. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  851. struct rcu_head *schedlist = NULL;
  852. struct rcu_head **schedtail = &schedlist;
  853. struct rcu_head **tail = &list;
  854. /*
  855. * Remove all callbacks from the newly dead CPU, retaining order.
  856. * Otherwise rcu_barrier() will fail
  857. */
  858. spin_lock_irqsave(&rdp->lock, flags);
  859. rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
  860. for (i = GP_STAGES - 1; i >= 0; i--)
  861. rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
  862. list, tail);
  863. rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
  864. rcu_offline_cpu_enqueue(rdp->waitschedlist, rdp->waitschedtail,
  865. schedlist, schedtail);
  866. rcu_offline_cpu_enqueue(rdp->nextschedlist, rdp->nextschedtail,
  867. schedlist, schedtail);
  868. rdp->rcu_sched_sleeping = 0;
  869. spin_unlock_irqrestore(&rdp->lock, flags);
  870. rdp->waitlistcount = 0;
  871. /* Disengage the newly dead CPU from the grace-period computation. */
  872. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  873. rcu_check_mb(cpu);
  874. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  875. smp_mb(); /* Subsequent counter accesses must see new value */
  876. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  877. smp_mb(); /* Subsequent RCU read-side critical sections */
  878. /* seen -after- acknowledgement. */
  879. }
  880. RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  881. RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
  882. RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
  883. RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
  884. cpu_clear(cpu, rcu_cpu_online_map);
  885. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  886. /*
  887. * Place the removed callbacks on the current CPU's queue.
  888. * Make them all start a new grace period: simple approach,
  889. * in theory could starve a given set of callbacks, but
  890. * you would need to be doing some serious CPU hotplugging
  891. * to make this happen. If this becomes a problem, adding
  892. * a synchronize_rcu() to the hotplug path would be a simple
  893. * fix.
  894. */
  895. local_irq_save(flags); /* disable preempt till we know what lock. */
  896. rdp = RCU_DATA_ME();
  897. spin_lock(&rdp->lock);
  898. *rdp->nexttail = list;
  899. if (list)
  900. rdp->nexttail = tail;
  901. *rdp->nextschedtail = schedlist;
  902. if (schedlist)
  903. rdp->nextschedtail = schedtail;
  904. spin_unlock_irqrestore(&rdp->lock, flags);
  905. }
  906. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  907. void rcu_offline_cpu(int cpu)
  908. {
  909. }
  910. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  911. void __cpuinit rcu_online_cpu(int cpu)
  912. {
  913. unsigned long flags;
  914. struct rcu_data *rdp;
  915. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  916. cpu_set(cpu, rcu_cpu_online_map);
  917. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  918. /*
  919. * The rcu_sched grace-period processing might have bypassed
  920. * this CPU, given that it was not in the rcu_cpu_online_map
  921. * when the grace-period scan started. This means that the
  922. * grace-period task might sleep. So make sure that if this
  923. * should happen, the first callback posted to this CPU will
  924. * wake up the grace-period task if need be.
  925. */
  926. rdp = RCU_DATA_CPU(cpu);
  927. spin_lock_irqsave(&rdp->lock, flags);
  928. rdp->rcu_sched_sleeping = 1;
  929. spin_unlock_irqrestore(&rdp->lock, flags);
  930. }
  931. static void rcu_process_callbacks(struct softirq_action *unused)
  932. {
  933. unsigned long flags;
  934. struct rcu_head *next, *list;
  935. struct rcu_data *rdp;
  936. local_irq_save(flags);
  937. rdp = RCU_DATA_ME();
  938. spin_lock(&rdp->lock);
  939. list = rdp->donelist;
  940. if (list == NULL) {
  941. spin_unlock_irqrestore(&rdp->lock, flags);
  942. return;
  943. }
  944. rdp->donelist = NULL;
  945. rdp->donetail = &rdp->donelist;
  946. RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
  947. spin_unlock_irqrestore(&rdp->lock, flags);
  948. while (list) {
  949. next = list->next;
  950. list->func(list);
  951. list = next;
  952. RCU_TRACE_ME(rcupreempt_trace_invoke);
  953. }
  954. }
  955. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  956. {
  957. unsigned long flags;
  958. struct rcu_data *rdp;
  959. head->func = func;
  960. head->next = NULL;
  961. local_irq_save(flags);
  962. rdp = RCU_DATA_ME();
  963. spin_lock(&rdp->lock);
  964. __rcu_advance_callbacks(rdp);
  965. *rdp->nexttail = head;
  966. rdp->nexttail = &head->next;
  967. RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
  968. spin_unlock_irqrestore(&rdp->lock, flags);
  969. }
  970. EXPORT_SYMBOL_GPL(call_rcu);
  971. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  972. {
  973. unsigned long flags;
  974. struct rcu_data *rdp;
  975. int wake_gp = 0;
  976. head->func = func;
  977. head->next = NULL;
  978. local_irq_save(flags);
  979. rdp = RCU_DATA_ME();
  980. spin_lock(&rdp->lock);
  981. *rdp->nextschedtail = head;
  982. rdp->nextschedtail = &head->next;
  983. if (rdp->rcu_sched_sleeping) {
  984. /* Grace-period processing might be sleeping... */
  985. rdp->rcu_sched_sleeping = 0;
  986. wake_gp = 1;
  987. }
  988. spin_unlock_irqrestore(&rdp->lock, flags);
  989. if (wake_gp) {
  990. /* Wake up grace-period processing, unless someone beat us. */
  991. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  992. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleeping)
  993. wake_gp = 0;
  994. rcu_ctrlblk.sched_sleep = rcu_sched_not_sleeping;
  995. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  996. if (wake_gp)
  997. wake_up_interruptible(&rcu_ctrlblk.sched_wq);
  998. }
  999. }
  1000. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1001. /*
  1002. * Wait until all currently running preempt_disable() code segments
  1003. * (including hardware-irq-disable segments) complete. Note that
  1004. * in -rt this does -not- necessarily result in all currently executing
  1005. * interrupt -handlers- having completed.
  1006. */
  1007. synchronize_rcu_xxx(__synchronize_sched, call_rcu_sched)
  1008. EXPORT_SYMBOL_GPL(__synchronize_sched);
  1009. /*
  1010. * kthread function that manages call_rcu_sched grace periods.
  1011. */
  1012. static int rcu_sched_grace_period(void *arg)
  1013. {
  1014. int couldsleep; /* might sleep after current pass. */
  1015. int couldsleepnext = 0; /* might sleep after next pass. */
  1016. int cpu;
  1017. unsigned long flags;
  1018. struct rcu_data *rdp;
  1019. int ret;
  1020. /*
  1021. * Each pass through the following loop handles one
  1022. * rcu_sched grace period cycle.
  1023. */
  1024. do {
  1025. /* Save each CPU's current state. */
  1026. for_each_online_cpu(cpu) {
  1027. dyntick_save_progress_counter_sched(cpu);
  1028. save_qsctr_sched(cpu);
  1029. }
  1030. /*
  1031. * Sleep for about an RCU grace-period's worth to
  1032. * allow better batching and to consume less CPU.
  1033. */
  1034. schedule_timeout_interruptible(RCU_SCHED_BATCH_TIME);
  1035. /*
  1036. * If there was nothing to do last time, prepare to
  1037. * sleep at the end of the current grace period cycle.
  1038. */
  1039. couldsleep = couldsleepnext;
  1040. couldsleepnext = 1;
  1041. if (couldsleep) {
  1042. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1043. rcu_ctrlblk.sched_sleep = rcu_sched_sleep_prep;
  1044. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1045. }
  1046. /*
  1047. * Wait on each CPU in turn to have either visited
  1048. * a quiescent state or been in dynticks-idle mode.
  1049. */
  1050. for_each_online_cpu(cpu) {
  1051. while (rcu_qsctr_inc_needed(cpu) &&
  1052. rcu_qsctr_inc_needed_dyntick(cpu)) {
  1053. /* resched_cpu(cpu); @@@ */
  1054. schedule_timeout_interruptible(1);
  1055. }
  1056. }
  1057. /* Advance callbacks for each CPU. */
  1058. for_each_online_cpu(cpu) {
  1059. rdp = RCU_DATA_CPU(cpu);
  1060. spin_lock_irqsave(&rdp->lock, flags);
  1061. /*
  1062. * We are running on this CPU irq-disabled, so no
  1063. * CPU can go offline until we re-enable irqs.
  1064. * The current CPU might have already gone
  1065. * offline (between the for_each_offline_cpu and
  1066. * the spin_lock_irqsave), but in that case all its
  1067. * callback lists will be empty, so no harm done.
  1068. *
  1069. * Advance the callbacks! We share normal RCU's
  1070. * donelist, since callbacks are invoked the
  1071. * same way in either case.
  1072. */
  1073. if (rdp->waitschedlist != NULL) {
  1074. *rdp->donetail = rdp->waitschedlist;
  1075. rdp->donetail = rdp->waitschedtail;
  1076. /*
  1077. * Next rcu_check_callbacks() will
  1078. * do the required raise_softirq().
  1079. */
  1080. }
  1081. if (rdp->nextschedlist != NULL) {
  1082. rdp->waitschedlist = rdp->nextschedlist;
  1083. rdp->waitschedtail = rdp->nextschedtail;
  1084. couldsleep = 0;
  1085. couldsleepnext = 0;
  1086. } else {
  1087. rdp->waitschedlist = NULL;
  1088. rdp->waitschedtail = &rdp->waitschedlist;
  1089. }
  1090. rdp->nextschedlist = NULL;
  1091. rdp->nextschedtail = &rdp->nextschedlist;
  1092. /* Mark sleep intention. */
  1093. rdp->rcu_sched_sleeping = couldsleep;
  1094. spin_unlock_irqrestore(&rdp->lock, flags);
  1095. }
  1096. /* If we saw callbacks on the last scan, go deal with them. */
  1097. if (!couldsleep)
  1098. continue;
  1099. /* Attempt to block... */
  1100. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1101. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleep_prep) {
  1102. /*
  1103. * Someone posted a callback after we scanned.
  1104. * Go take care of it.
  1105. */
  1106. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1107. couldsleepnext = 0;
  1108. continue;
  1109. }
  1110. /* Block until the next person posts a callback. */
  1111. rcu_ctrlblk.sched_sleep = rcu_sched_sleeping;
  1112. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1113. ret = 0;
  1114. __wait_event_interruptible(rcu_ctrlblk.sched_wq,
  1115. rcu_ctrlblk.sched_sleep != rcu_sched_sleeping,
  1116. ret);
  1117. /*
  1118. * Signals would prevent us from sleeping, and we cannot
  1119. * do much with them in any case. So flush them.
  1120. */
  1121. if (ret)
  1122. flush_signals(current);
  1123. couldsleepnext = 0;
  1124. } while (!kthread_should_stop());
  1125. return (0);
  1126. }
  1127. /*
  1128. * Check to see if any future RCU-related work will need to be done
  1129. * by the current CPU, even if none need be done immediately, returning
  1130. * 1 if so. Assumes that notifiers would take care of handling any
  1131. * outstanding requests from the RCU core.
  1132. *
  1133. * This function is part of the RCU implementation; it is -not-
  1134. * an exported member of the RCU API.
  1135. */
  1136. int rcu_needs_cpu(int cpu)
  1137. {
  1138. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1139. return (rdp->donelist != NULL ||
  1140. !!rdp->waitlistcount ||
  1141. rdp->nextlist != NULL ||
  1142. rdp->nextschedlist != NULL ||
  1143. rdp->waitschedlist != NULL);
  1144. }
  1145. int rcu_pending(int cpu)
  1146. {
  1147. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1148. /* The CPU has at least one callback queued somewhere. */
  1149. if (rdp->donelist != NULL ||
  1150. !!rdp->waitlistcount ||
  1151. rdp->nextlist != NULL ||
  1152. rdp->nextschedlist != NULL ||
  1153. rdp->waitschedlist != NULL)
  1154. return 1;
  1155. /* The RCU core needs an acknowledgement from this CPU. */
  1156. if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
  1157. (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
  1158. return 1;
  1159. /* This CPU has fallen behind the global grace-period number. */
  1160. if (rdp->completed != rcu_ctrlblk.completed)
  1161. return 1;
  1162. /* Nothing needed from this CPU. */
  1163. return 0;
  1164. }
  1165. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1166. unsigned long action, void *hcpu)
  1167. {
  1168. long cpu = (long)hcpu;
  1169. switch (action) {
  1170. case CPU_UP_PREPARE:
  1171. case CPU_UP_PREPARE_FROZEN:
  1172. rcu_online_cpu(cpu);
  1173. break;
  1174. case CPU_UP_CANCELED:
  1175. case CPU_UP_CANCELED_FROZEN:
  1176. case CPU_DEAD:
  1177. case CPU_DEAD_FROZEN:
  1178. rcu_offline_cpu(cpu);
  1179. break;
  1180. default:
  1181. break;
  1182. }
  1183. return NOTIFY_OK;
  1184. }
  1185. static struct notifier_block __cpuinitdata rcu_nb = {
  1186. .notifier_call = rcu_cpu_notify,
  1187. };
  1188. void __init __rcu_init(void)
  1189. {
  1190. int cpu;
  1191. int i;
  1192. struct rcu_data *rdp;
  1193. printk(KERN_NOTICE "Preemptible RCU implementation.\n");
  1194. for_each_possible_cpu(cpu) {
  1195. rdp = RCU_DATA_CPU(cpu);
  1196. spin_lock_init(&rdp->lock);
  1197. rdp->completed = 0;
  1198. rdp->waitlistcount = 0;
  1199. rdp->nextlist = NULL;
  1200. rdp->nexttail = &rdp->nextlist;
  1201. for (i = 0; i < GP_STAGES; i++) {
  1202. rdp->waitlist[i] = NULL;
  1203. rdp->waittail[i] = &rdp->waitlist[i];
  1204. }
  1205. rdp->donelist = NULL;
  1206. rdp->donetail = &rdp->donelist;
  1207. rdp->rcu_flipctr[0] = 0;
  1208. rdp->rcu_flipctr[1] = 0;
  1209. rdp->nextschedlist = NULL;
  1210. rdp->nextschedtail = &rdp->nextschedlist;
  1211. rdp->waitschedlist = NULL;
  1212. rdp->waitschedtail = &rdp->waitschedlist;
  1213. rdp->rcu_sched_sleeping = 0;
  1214. }
  1215. register_cpu_notifier(&rcu_nb);
  1216. /*
  1217. * We don't need protection against CPU-Hotplug here
  1218. * since
  1219. * a) If a CPU comes online while we are iterating over the
  1220. * cpu_online_map below, we would only end up making a
  1221. * duplicate call to rcu_online_cpu() which sets the corresponding
  1222. * CPU's mask in the rcu_cpu_online_map.
  1223. *
  1224. * b) A CPU cannot go offline at this point in time since the user
  1225. * does not have access to the sysfs interface, nor do we
  1226. * suspend the system.
  1227. */
  1228. for_each_online_cpu(cpu)
  1229. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
  1230. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1231. }
  1232. /*
  1233. * Late-boot-time RCU initialization that must wait until after scheduler
  1234. * has been initialized.
  1235. */
  1236. void __init rcu_init_sched(void)
  1237. {
  1238. rcu_sched_grace_period_task = kthread_run(rcu_sched_grace_period,
  1239. NULL,
  1240. "rcu_sched_grace_period");
  1241. WARN_ON(IS_ERR(rcu_sched_grace_period_task));
  1242. }
  1243. #ifdef CONFIG_RCU_TRACE
  1244. long *rcupreempt_flipctr(int cpu)
  1245. {
  1246. return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  1247. }
  1248. EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
  1249. int rcupreempt_flip_flag(int cpu)
  1250. {
  1251. return per_cpu(rcu_flip_flag, cpu);
  1252. }
  1253. EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
  1254. int rcupreempt_mb_flag(int cpu)
  1255. {
  1256. return per_cpu(rcu_mb_flag, cpu);
  1257. }
  1258. EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
  1259. char *rcupreempt_try_flip_state_name(void)
  1260. {
  1261. return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
  1262. }
  1263. EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
  1264. struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
  1265. {
  1266. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1267. return &rdp->trace;
  1268. }
  1269. EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
  1270. #endif /* #ifdef RCU_TRACE */