rcutree.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
  23. *
  24. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  25. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  26. *
  27. * For detailed explanation of Read-Copy Update mechanism see -
  28. * Documentation/RCU
  29. */
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/smp.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <asm/atomic.h>
  39. #include <linux/bitops.h>
  40. #include <linux/module.h>
  41. #include <linux/completion.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/percpu.h>
  44. #include <linux/notifier.h>
  45. #include <linux/cpu.h>
  46. #include <linux/mutex.h>
  47. #include <linux/time.h>
  48. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  49. static struct lock_class_key rcu_lock_key;
  50. struct lockdep_map rcu_lock_map =
  51. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
  52. EXPORT_SYMBOL_GPL(rcu_lock_map);
  53. #endif
  54. /* Data structures. */
  55. #define RCU_STATE_INITIALIZER(name) { \
  56. .level = { &name.node[0] }, \
  57. .levelcnt = { \
  58. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  59. NUM_RCU_LVL_1, \
  60. NUM_RCU_LVL_2, \
  61. NUM_RCU_LVL_3, /* == MAX_RCU_LVLS */ \
  62. }, \
  63. .signaled = RCU_SIGNAL_INIT, \
  64. .gpnum = -300, \
  65. .completed = -300, \
  66. .onofflock = __SPIN_LOCK_UNLOCKED(&name.onofflock), \
  67. .fqslock = __SPIN_LOCK_UNLOCKED(&name.fqslock), \
  68. .n_force_qs = 0, \
  69. .n_force_qs_ngp = 0, \
  70. }
  71. static struct rcu_state rcu_state = RCU_STATE_INITIALIZER(rcu_state);
  72. static DEFINE_PER_CPU(struct rcu_data, rcu_data);
  73. static struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh_state);
  74. static DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
  75. /*
  76. * Increment the quiescent state counter.
  77. * The counter is a bit degenerated: We do not need to know
  78. * how many quiescent states passed, just if there was at least
  79. * one since the start of the grace period. Thus just a flag.
  80. */
  81. void rcu_qsctr_inc(int cpu)
  82. {
  83. struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
  84. rdp->passed_quiesc = 1;
  85. rdp->passed_quiesc_completed = rdp->completed;
  86. }
  87. void rcu_bh_qsctr_inc(int cpu)
  88. {
  89. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  90. rdp->passed_quiesc = 1;
  91. rdp->passed_quiesc_completed = rdp->completed;
  92. }
  93. #ifdef CONFIG_NO_HZ
  94. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  95. .dynticks_nesting = 1,
  96. .dynticks = 1,
  97. };
  98. #endif /* #ifdef CONFIG_NO_HZ */
  99. static int blimit = 10; /* Maximum callbacks per softirq. */
  100. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  101. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  102. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  103. /*
  104. * Return the number of RCU batches processed thus far for debug & stats.
  105. */
  106. long rcu_batches_completed(void)
  107. {
  108. return rcu_state.completed;
  109. }
  110. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  111. /*
  112. * Return the number of RCU BH batches processed thus far for debug & stats.
  113. */
  114. long rcu_batches_completed_bh(void)
  115. {
  116. return rcu_bh_state.completed;
  117. }
  118. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  119. /*
  120. * Does the CPU have callbacks ready to be invoked?
  121. */
  122. static int
  123. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  124. {
  125. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  126. }
  127. /*
  128. * Does the current CPU require a yet-as-unscheduled grace period?
  129. */
  130. static int
  131. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  132. {
  133. /* ACCESS_ONCE() because we are accessing outside of lock. */
  134. return *rdp->nxttail[RCU_DONE_TAIL] &&
  135. ACCESS_ONCE(rsp->completed) == ACCESS_ONCE(rsp->gpnum);
  136. }
  137. /*
  138. * Return the root node of the specified rcu_state structure.
  139. */
  140. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  141. {
  142. return &rsp->node[0];
  143. }
  144. #ifdef CONFIG_SMP
  145. /*
  146. * If the specified CPU is offline, tell the caller that it is in
  147. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  148. * Grace periods can end up waiting on an offline CPU when that
  149. * CPU is in the process of coming online -- it will be added to the
  150. * rcu_node bitmasks before it actually makes it online. The same thing
  151. * can happen while a CPU is in the process of coming online. Because this
  152. * race is quite rare, we check for it after detecting that the grace
  153. * period has been delayed rather than checking each and every CPU
  154. * each and every time we start a new grace period.
  155. */
  156. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  157. {
  158. /*
  159. * If the CPU is offline, it is in a quiescent state. We can
  160. * trust its state not to change because interrupts are disabled.
  161. */
  162. if (cpu_is_offline(rdp->cpu)) {
  163. rdp->offline_fqs++;
  164. return 1;
  165. }
  166. /* The CPU is online, so send it a reschedule IPI. */
  167. if (rdp->cpu != smp_processor_id())
  168. smp_send_reschedule(rdp->cpu);
  169. else
  170. set_need_resched();
  171. rdp->resched_ipi++;
  172. return 0;
  173. }
  174. #endif /* #ifdef CONFIG_SMP */
  175. #ifdef CONFIG_NO_HZ
  176. static DEFINE_RATELIMIT_STATE(rcu_rs, 10 * HZ, 5);
  177. /**
  178. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  179. *
  180. * Enter nohz mode, in other words, -leave- the mode in which RCU
  181. * read-side critical sections can occur. (Though RCU read-side
  182. * critical sections can occur in irq handlers in nohz mode, a possibility
  183. * handled by rcu_irq_enter() and rcu_irq_exit()).
  184. */
  185. void rcu_enter_nohz(void)
  186. {
  187. unsigned long flags;
  188. struct rcu_dynticks *rdtp;
  189. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  190. local_irq_save(flags);
  191. rdtp = &__get_cpu_var(rcu_dynticks);
  192. rdtp->dynticks++;
  193. rdtp->dynticks_nesting--;
  194. WARN_ON_RATELIMIT(rdtp->dynticks & 0x1, &rcu_rs);
  195. local_irq_restore(flags);
  196. }
  197. /*
  198. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  199. *
  200. * Exit nohz mode, in other words, -enter- the mode in which RCU
  201. * read-side critical sections normally occur.
  202. */
  203. void rcu_exit_nohz(void)
  204. {
  205. unsigned long flags;
  206. struct rcu_dynticks *rdtp;
  207. local_irq_save(flags);
  208. rdtp = &__get_cpu_var(rcu_dynticks);
  209. rdtp->dynticks++;
  210. rdtp->dynticks_nesting++;
  211. WARN_ON_RATELIMIT(!(rdtp->dynticks & 0x1), &rcu_rs);
  212. local_irq_restore(flags);
  213. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  214. }
  215. /**
  216. * rcu_nmi_enter - inform RCU of entry to NMI context
  217. *
  218. * If the CPU was idle with dynamic ticks active, and there is no
  219. * irq handler running, this updates rdtp->dynticks_nmi to let the
  220. * RCU grace-period handling know that the CPU is active.
  221. */
  222. void rcu_nmi_enter(void)
  223. {
  224. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  225. if (rdtp->dynticks & 0x1)
  226. return;
  227. rdtp->dynticks_nmi++;
  228. WARN_ON_RATELIMIT(!(rdtp->dynticks_nmi & 0x1), &rcu_rs);
  229. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  230. }
  231. /**
  232. * rcu_nmi_exit - inform RCU of exit from NMI context
  233. *
  234. * If the CPU was idle with dynamic ticks active, and there is no
  235. * irq handler running, this updates rdtp->dynticks_nmi to let the
  236. * RCU grace-period handling know that the CPU is no longer active.
  237. */
  238. void rcu_nmi_exit(void)
  239. {
  240. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  241. if (rdtp->dynticks & 0x1)
  242. return;
  243. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  244. rdtp->dynticks_nmi++;
  245. WARN_ON_RATELIMIT(rdtp->dynticks_nmi & 0x1, &rcu_rs);
  246. }
  247. /**
  248. * rcu_irq_enter - inform RCU of entry to hard irq context
  249. *
  250. * If the CPU was idle with dynamic ticks active, this updates the
  251. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  252. */
  253. void rcu_irq_enter(void)
  254. {
  255. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  256. if (rdtp->dynticks_nesting++)
  257. return;
  258. rdtp->dynticks++;
  259. WARN_ON_RATELIMIT(!(rdtp->dynticks & 0x1), &rcu_rs);
  260. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  261. }
  262. /**
  263. * rcu_irq_exit - inform RCU of exit from hard irq context
  264. *
  265. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  266. * to put let the RCU handling be aware that the CPU is going back to idle
  267. * with no ticks.
  268. */
  269. void rcu_irq_exit(void)
  270. {
  271. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  272. if (--rdtp->dynticks_nesting)
  273. return;
  274. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  275. rdtp->dynticks++;
  276. WARN_ON_RATELIMIT(rdtp->dynticks & 0x1, &rcu_rs);
  277. /* If the interrupt queued a callback, get out of dyntick mode. */
  278. if (__get_cpu_var(rcu_data).nxtlist ||
  279. __get_cpu_var(rcu_bh_data).nxtlist)
  280. set_need_resched();
  281. }
  282. /*
  283. * Record the specified "completed" value, which is later used to validate
  284. * dynticks counter manipulations. Specify "rsp->completed - 1" to
  285. * unconditionally invalidate any future dynticks manipulations (which is
  286. * useful at the beginning of a grace period).
  287. */
  288. static void dyntick_record_completed(struct rcu_state *rsp, long comp)
  289. {
  290. rsp->dynticks_completed = comp;
  291. }
  292. #ifdef CONFIG_SMP
  293. /*
  294. * Recall the previously recorded value of the completion for dynticks.
  295. */
  296. static long dyntick_recall_completed(struct rcu_state *rsp)
  297. {
  298. return rsp->dynticks_completed;
  299. }
  300. /*
  301. * Snapshot the specified CPU's dynticks counter so that we can later
  302. * credit them with an implicit quiescent state. Return 1 if this CPU
  303. * is already in a quiescent state courtesy of dynticks idle mode.
  304. */
  305. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  306. {
  307. int ret;
  308. int snap;
  309. int snap_nmi;
  310. snap = rdp->dynticks->dynticks;
  311. snap_nmi = rdp->dynticks->dynticks_nmi;
  312. smp_mb(); /* Order sampling of snap with end of grace period. */
  313. rdp->dynticks_snap = snap;
  314. rdp->dynticks_nmi_snap = snap_nmi;
  315. ret = ((snap & 0x1) == 0) && ((snap_nmi & 0x1) == 0);
  316. if (ret)
  317. rdp->dynticks_fqs++;
  318. return ret;
  319. }
  320. /*
  321. * Return true if the specified CPU has passed through a quiescent
  322. * state by virtue of being in or having passed through an dynticks
  323. * idle state since the last call to dyntick_save_progress_counter()
  324. * for this same CPU.
  325. */
  326. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  327. {
  328. long curr;
  329. long curr_nmi;
  330. long snap;
  331. long snap_nmi;
  332. curr = rdp->dynticks->dynticks;
  333. snap = rdp->dynticks_snap;
  334. curr_nmi = rdp->dynticks->dynticks_nmi;
  335. snap_nmi = rdp->dynticks_nmi_snap;
  336. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  337. /*
  338. * If the CPU passed through or entered a dynticks idle phase with
  339. * no active irq/NMI handlers, then we can safely pretend that the CPU
  340. * already acknowledged the request to pass through a quiescent
  341. * state. Either way, that CPU cannot possibly be in an RCU
  342. * read-side critical section that started before the beginning
  343. * of the current RCU grace period.
  344. */
  345. if ((curr != snap || (curr & 0x1) == 0) &&
  346. (curr_nmi != snap_nmi || (curr_nmi & 0x1) == 0)) {
  347. rdp->dynticks_fqs++;
  348. return 1;
  349. }
  350. /* Go check for the CPU being offline. */
  351. return rcu_implicit_offline_qs(rdp);
  352. }
  353. #endif /* #ifdef CONFIG_SMP */
  354. #else /* #ifdef CONFIG_NO_HZ */
  355. static void dyntick_record_completed(struct rcu_state *rsp, long comp)
  356. {
  357. }
  358. #ifdef CONFIG_SMP
  359. /*
  360. * If there are no dynticks, then the only way that a CPU can passively
  361. * be in a quiescent state is to be offline. Unlike dynticks idle, which
  362. * is a point in time during the prior (already finished) grace period,
  363. * an offline CPU is always in a quiescent state, and thus can be
  364. * unconditionally applied. So just return the current value of completed.
  365. */
  366. static long dyntick_recall_completed(struct rcu_state *rsp)
  367. {
  368. return rsp->completed;
  369. }
  370. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  371. {
  372. return 0;
  373. }
  374. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  375. {
  376. return rcu_implicit_offline_qs(rdp);
  377. }
  378. #endif /* #ifdef CONFIG_SMP */
  379. #endif /* #else #ifdef CONFIG_NO_HZ */
  380. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  381. static void record_gp_stall_check_time(struct rcu_state *rsp)
  382. {
  383. rsp->gp_start = jiffies;
  384. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  385. }
  386. static void print_other_cpu_stall(struct rcu_state *rsp)
  387. {
  388. int cpu;
  389. long delta;
  390. unsigned long flags;
  391. struct rcu_node *rnp = rcu_get_root(rsp);
  392. struct rcu_node *rnp_cur = rsp->level[NUM_RCU_LVLS - 1];
  393. struct rcu_node *rnp_end = &rsp->node[NUM_RCU_NODES];
  394. /* Only let one CPU complain about others per time interval. */
  395. spin_lock_irqsave(&rnp->lock, flags);
  396. delta = jiffies - rsp->jiffies_stall;
  397. if (delta < RCU_STALL_RAT_DELAY || rsp->gpnum == rsp->completed) {
  398. spin_unlock_irqrestore(&rnp->lock, flags);
  399. return;
  400. }
  401. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  402. spin_unlock_irqrestore(&rnp->lock, flags);
  403. /* OK, time to rat on our buddy... */
  404. printk(KERN_ERR "INFO: RCU detected CPU stalls:");
  405. for (; rnp_cur < rnp_end; rnp_cur++) {
  406. if (rnp_cur->qsmask == 0)
  407. continue;
  408. for (cpu = 0; cpu <= rnp_cur->grphi - rnp_cur->grplo; cpu++)
  409. if (rnp_cur->qsmask & (1UL << cpu))
  410. printk(" %d", rnp_cur->grplo + cpu);
  411. }
  412. printk(" (detected by %d, t=%ld jiffies)\n",
  413. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  414. force_quiescent_state(rsp, 0); /* Kick them all. */
  415. }
  416. static void print_cpu_stall(struct rcu_state *rsp)
  417. {
  418. unsigned long flags;
  419. struct rcu_node *rnp = rcu_get_root(rsp);
  420. printk(KERN_ERR "INFO: RCU detected CPU %d stall (t=%lu jiffies)\n",
  421. smp_processor_id(), jiffies - rsp->gp_start);
  422. dump_stack();
  423. spin_lock_irqsave(&rnp->lock, flags);
  424. if ((long)(jiffies - rsp->jiffies_stall) >= 0)
  425. rsp->jiffies_stall =
  426. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  427. spin_unlock_irqrestore(&rnp->lock, flags);
  428. set_need_resched(); /* kick ourselves to get things going. */
  429. }
  430. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  431. {
  432. long delta;
  433. struct rcu_node *rnp;
  434. delta = jiffies - rsp->jiffies_stall;
  435. rnp = rdp->mynode;
  436. if ((rnp->qsmask & rdp->grpmask) && delta >= 0) {
  437. /* We haven't checked in, so go dump stack. */
  438. print_cpu_stall(rsp);
  439. } else if (rsp->gpnum != rsp->completed &&
  440. delta >= RCU_STALL_RAT_DELAY) {
  441. /* They had two time units to dump stack, so complain. */
  442. print_other_cpu_stall(rsp);
  443. }
  444. }
  445. #else /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  446. static void record_gp_stall_check_time(struct rcu_state *rsp)
  447. {
  448. }
  449. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  450. {
  451. }
  452. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  453. /*
  454. * Update CPU-local rcu_data state to record the newly noticed grace period.
  455. * This is used both when we started the grace period and when we notice
  456. * that someone else started the grace period.
  457. */
  458. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  459. {
  460. rdp->qs_pending = 1;
  461. rdp->passed_quiesc = 0;
  462. rdp->gpnum = rsp->gpnum;
  463. rdp->n_rcu_pending_force_qs = rdp->n_rcu_pending +
  464. RCU_JIFFIES_TILL_FORCE_QS;
  465. }
  466. /*
  467. * Did someone else start a new RCU grace period start since we last
  468. * checked? Update local state appropriately if so. Must be called
  469. * on the CPU corresponding to rdp.
  470. */
  471. static int
  472. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  473. {
  474. unsigned long flags;
  475. int ret = 0;
  476. local_irq_save(flags);
  477. if (rdp->gpnum != rsp->gpnum) {
  478. note_new_gpnum(rsp, rdp);
  479. ret = 1;
  480. }
  481. local_irq_restore(flags);
  482. return ret;
  483. }
  484. /*
  485. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  486. * in preparation for detecting the next grace period. The caller must hold
  487. * the root node's ->lock, which is released before return. Hard irqs must
  488. * be disabled.
  489. */
  490. static void
  491. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  492. __releases(rcu_get_root(rsp)->lock)
  493. {
  494. struct rcu_data *rdp = rsp->rda[smp_processor_id()];
  495. struct rcu_node *rnp = rcu_get_root(rsp);
  496. struct rcu_node *rnp_cur;
  497. struct rcu_node *rnp_end;
  498. if (!cpu_needs_another_gp(rsp, rdp)) {
  499. spin_unlock_irqrestore(&rnp->lock, flags);
  500. return;
  501. }
  502. /* Advance to a new grace period and initialize state. */
  503. rsp->gpnum++;
  504. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  505. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  506. rdp->n_rcu_pending_force_qs = rdp->n_rcu_pending +
  507. RCU_JIFFIES_TILL_FORCE_QS;
  508. record_gp_stall_check_time(rsp);
  509. dyntick_record_completed(rsp, rsp->completed - 1);
  510. note_new_gpnum(rsp, rdp);
  511. /*
  512. * Because we are first, we know that all our callbacks will
  513. * be covered by this upcoming grace period, even the ones
  514. * that were registered arbitrarily recently.
  515. */
  516. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  517. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  518. /* Special-case the common single-level case. */
  519. if (NUM_RCU_NODES == 1) {
  520. rnp->qsmask = rnp->qsmaskinit;
  521. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  522. spin_unlock_irqrestore(&rnp->lock, flags);
  523. return;
  524. }
  525. spin_unlock(&rnp->lock); /* leave irqs disabled. */
  526. /* Exclude any concurrent CPU-hotplug operations. */
  527. spin_lock(&rsp->onofflock); /* irqs already disabled. */
  528. /*
  529. * Set the quiescent-state-needed bits in all the non-leaf RCU
  530. * nodes for all currently online CPUs. This operation relies
  531. * on the layout of the hierarchy within the rsp->node[] array.
  532. * Note that other CPUs will access only the leaves of the
  533. * hierarchy, which still indicate that no grace period is in
  534. * progress. In addition, we have excluded CPU-hotplug operations.
  535. *
  536. * We therefore do not need to hold any locks. Any required
  537. * memory barriers will be supplied by the locks guarding the
  538. * leaf rcu_nodes in the hierarchy.
  539. */
  540. rnp_end = rsp->level[NUM_RCU_LVLS - 1];
  541. for (rnp_cur = &rsp->node[0]; rnp_cur < rnp_end; rnp_cur++)
  542. rnp_cur->qsmask = rnp_cur->qsmaskinit;
  543. /*
  544. * Now set up the leaf nodes. Here we must be careful. First,
  545. * we need to hold the lock in order to exclude other CPUs, which
  546. * might be contending for the leaf nodes' locks. Second, as
  547. * soon as we initialize a given leaf node, its CPUs might run
  548. * up the rest of the hierarchy. We must therefore acquire locks
  549. * for each node that we touch during this stage. (But we still
  550. * are excluding CPU-hotplug operations.)
  551. *
  552. * Note that the grace period cannot complete until we finish
  553. * the initialization process, as there will be at least one
  554. * qsmask bit set in the root node until that time, namely the
  555. * one corresponding to this CPU.
  556. */
  557. rnp_end = &rsp->node[NUM_RCU_NODES];
  558. rnp_cur = rsp->level[NUM_RCU_LVLS - 1];
  559. for (; rnp_cur < rnp_end; rnp_cur++) {
  560. spin_lock(&rnp_cur->lock); /* irqs already disabled. */
  561. rnp_cur->qsmask = rnp_cur->qsmaskinit;
  562. spin_unlock(&rnp_cur->lock); /* irqs already disabled. */
  563. }
  564. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  565. spin_unlock_irqrestore(&rsp->onofflock, flags);
  566. }
  567. /*
  568. * Advance this CPU's callbacks, but only if the current grace period
  569. * has ended. This may be called only from the CPU to whom the rdp
  570. * belongs.
  571. */
  572. static void
  573. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  574. {
  575. long completed_snap;
  576. unsigned long flags;
  577. local_irq_save(flags);
  578. completed_snap = ACCESS_ONCE(rsp->completed); /* outside of lock. */
  579. /* Did another grace period end? */
  580. if (rdp->completed != completed_snap) {
  581. /* Advance callbacks. No harm if list empty. */
  582. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  583. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  584. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  585. /* Remember that we saw this grace-period completion. */
  586. rdp->completed = completed_snap;
  587. }
  588. local_irq_restore(flags);
  589. }
  590. /*
  591. * Similar to cpu_quiet(), for which it is a helper function. Allows
  592. * a group of CPUs to be quieted at one go, though all the CPUs in the
  593. * group must be represented by the same leaf rcu_node structure.
  594. * That structure's lock must be held upon entry, and it is released
  595. * before return.
  596. */
  597. static void
  598. cpu_quiet_msk(unsigned long mask, struct rcu_state *rsp, struct rcu_node *rnp,
  599. unsigned long flags)
  600. __releases(rnp->lock)
  601. {
  602. /* Walk up the rcu_node hierarchy. */
  603. for (;;) {
  604. if (!(rnp->qsmask & mask)) {
  605. /* Our bit has already been cleared, so done. */
  606. spin_unlock_irqrestore(&rnp->lock, flags);
  607. return;
  608. }
  609. rnp->qsmask &= ~mask;
  610. if (rnp->qsmask != 0) {
  611. /* Other bits still set at this level, so done. */
  612. spin_unlock_irqrestore(&rnp->lock, flags);
  613. return;
  614. }
  615. mask = rnp->grpmask;
  616. if (rnp->parent == NULL) {
  617. /* No more levels. Exit loop holding root lock. */
  618. break;
  619. }
  620. spin_unlock_irqrestore(&rnp->lock, flags);
  621. rnp = rnp->parent;
  622. spin_lock_irqsave(&rnp->lock, flags);
  623. }
  624. /*
  625. * Get here if we are the last CPU to pass through a quiescent
  626. * state for this grace period. Clean up and let rcu_start_gp()
  627. * start up the next grace period if one is needed. Note that
  628. * we still hold rnp->lock, as required by rcu_start_gp(), which
  629. * will release it.
  630. */
  631. rsp->completed = rsp->gpnum;
  632. rcu_process_gp_end(rsp, rsp->rda[smp_processor_id()]);
  633. rcu_start_gp(rsp, flags); /* releases rnp->lock. */
  634. }
  635. /*
  636. * Record a quiescent state for the specified CPU, which must either be
  637. * the current CPU or an offline CPU. The lastcomp argument is used to
  638. * make sure we are still in the grace period of interest. We don't want
  639. * to end the current grace period based on quiescent states detected in
  640. * an earlier grace period!
  641. */
  642. static void
  643. cpu_quiet(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  644. {
  645. unsigned long flags;
  646. unsigned long mask;
  647. struct rcu_node *rnp;
  648. rnp = rdp->mynode;
  649. spin_lock_irqsave(&rnp->lock, flags);
  650. if (lastcomp != ACCESS_ONCE(rsp->completed)) {
  651. /*
  652. * Someone beat us to it for this grace period, so leave.
  653. * The race with GP start is resolved by the fact that we
  654. * hold the leaf rcu_node lock, so that the per-CPU bits
  655. * cannot yet be initialized -- so we would simply find our
  656. * CPU's bit already cleared in cpu_quiet_msk() if this race
  657. * occurred.
  658. */
  659. rdp->passed_quiesc = 0; /* try again later! */
  660. spin_unlock_irqrestore(&rnp->lock, flags);
  661. return;
  662. }
  663. mask = rdp->grpmask;
  664. if ((rnp->qsmask & mask) == 0) {
  665. spin_unlock_irqrestore(&rnp->lock, flags);
  666. } else {
  667. rdp->qs_pending = 0;
  668. /*
  669. * This GP can't end until cpu checks in, so all of our
  670. * callbacks can be processed during the next GP.
  671. */
  672. rdp = rsp->rda[smp_processor_id()];
  673. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  674. cpu_quiet_msk(mask, rsp, rnp, flags); /* releases rnp->lock */
  675. }
  676. }
  677. /*
  678. * Check to see if there is a new grace period of which this CPU
  679. * is not yet aware, and if so, set up local rcu_data state for it.
  680. * Otherwise, see if this CPU has just passed through its first
  681. * quiescent state for this grace period, and record that fact if so.
  682. */
  683. static void
  684. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  685. {
  686. /* If there is now a new grace period, record and return. */
  687. if (check_for_new_grace_period(rsp, rdp))
  688. return;
  689. /*
  690. * Does this CPU still need to do its part for current grace period?
  691. * If no, return and let the other CPUs do their part as well.
  692. */
  693. if (!rdp->qs_pending)
  694. return;
  695. /*
  696. * Was there a quiescent state since the beginning of the grace
  697. * period? If no, then exit and wait for the next call.
  698. */
  699. if (!rdp->passed_quiesc)
  700. return;
  701. /* Tell RCU we are done (but cpu_quiet() will be the judge of that). */
  702. cpu_quiet(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  703. }
  704. #ifdef CONFIG_HOTPLUG_CPU
  705. /*
  706. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  707. * and move all callbacks from the outgoing CPU to the current one.
  708. */
  709. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  710. {
  711. int i;
  712. unsigned long flags;
  713. long lastcomp;
  714. unsigned long mask;
  715. struct rcu_data *rdp = rsp->rda[cpu];
  716. struct rcu_data *rdp_me;
  717. struct rcu_node *rnp;
  718. /* Exclude any attempts to start a new grace period. */
  719. spin_lock_irqsave(&rsp->onofflock, flags);
  720. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  721. rnp = rdp->mynode;
  722. mask = rdp->grpmask; /* rnp->grplo is constant. */
  723. do {
  724. spin_lock(&rnp->lock); /* irqs already disabled. */
  725. rnp->qsmaskinit &= ~mask;
  726. if (rnp->qsmaskinit != 0) {
  727. spin_unlock(&rnp->lock); /* irqs already disabled. */
  728. break;
  729. }
  730. mask = rnp->grpmask;
  731. spin_unlock(&rnp->lock); /* irqs already disabled. */
  732. rnp = rnp->parent;
  733. } while (rnp != NULL);
  734. lastcomp = rsp->completed;
  735. spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  736. /* Being offline is a quiescent state, so go record it. */
  737. cpu_quiet(cpu, rsp, rdp, lastcomp);
  738. /*
  739. * Move callbacks from the outgoing CPU to the running CPU.
  740. * Note that the outgoing CPU is now quiscent, so it is now
  741. * (uncharacteristically) safe to access it rcu_data structure.
  742. * Note also that we must carefully retain the order of the
  743. * outgoing CPU's callbacks in order for rcu_barrier() to work
  744. * correctly. Finally, note that we start all the callbacks
  745. * afresh, even those that have passed through a grace period
  746. * and are therefore ready to invoke. The theory is that hotplug
  747. * events are rare, and that if they are frequent enough to
  748. * indefinitely delay callbacks, you have far worse things to
  749. * be worrying about.
  750. */
  751. rdp_me = rsp->rda[smp_processor_id()];
  752. if (rdp->nxtlist != NULL) {
  753. *rdp_me->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
  754. rdp_me->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  755. rdp->nxtlist = NULL;
  756. for (i = 0; i < RCU_NEXT_SIZE; i++)
  757. rdp->nxttail[i] = &rdp->nxtlist;
  758. rdp_me->qlen += rdp->qlen;
  759. rdp->qlen = 0;
  760. }
  761. local_irq_restore(flags);
  762. }
  763. /*
  764. * Remove the specified CPU from the RCU hierarchy and move any pending
  765. * callbacks that it might have to the current CPU. This code assumes
  766. * that at least one CPU in the system will remain running at all times.
  767. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  768. */
  769. static void rcu_offline_cpu(int cpu)
  770. {
  771. __rcu_offline_cpu(cpu, &rcu_state);
  772. __rcu_offline_cpu(cpu, &rcu_bh_state);
  773. }
  774. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  775. static void rcu_offline_cpu(int cpu)
  776. {
  777. }
  778. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  779. /*
  780. * Invoke any RCU callbacks that have made it to the end of their grace
  781. * period. Thottle as specified by rdp->blimit.
  782. */
  783. static void rcu_do_batch(struct rcu_data *rdp)
  784. {
  785. unsigned long flags;
  786. struct rcu_head *next, *list, **tail;
  787. int count;
  788. /* If no callbacks are ready, just return.*/
  789. if (!cpu_has_callbacks_ready_to_invoke(rdp))
  790. return;
  791. /*
  792. * Extract the list of ready callbacks, disabling to prevent
  793. * races with call_rcu() from interrupt handlers.
  794. */
  795. local_irq_save(flags);
  796. list = rdp->nxtlist;
  797. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  798. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  799. tail = rdp->nxttail[RCU_DONE_TAIL];
  800. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  801. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  802. rdp->nxttail[count] = &rdp->nxtlist;
  803. local_irq_restore(flags);
  804. /* Invoke callbacks. */
  805. count = 0;
  806. while (list) {
  807. next = list->next;
  808. prefetch(next);
  809. list->func(list);
  810. list = next;
  811. if (++count >= rdp->blimit)
  812. break;
  813. }
  814. local_irq_save(flags);
  815. /* Update count, and requeue any remaining callbacks. */
  816. rdp->qlen -= count;
  817. if (list != NULL) {
  818. *tail = rdp->nxtlist;
  819. rdp->nxtlist = list;
  820. for (count = 0; count < RCU_NEXT_SIZE; count++)
  821. if (&rdp->nxtlist == rdp->nxttail[count])
  822. rdp->nxttail[count] = tail;
  823. else
  824. break;
  825. }
  826. /* Reinstate batch limit if we have worked down the excess. */
  827. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  828. rdp->blimit = blimit;
  829. local_irq_restore(flags);
  830. /* Re-raise the RCU softirq if there are callbacks remaining. */
  831. if (cpu_has_callbacks_ready_to_invoke(rdp))
  832. raise_softirq(RCU_SOFTIRQ);
  833. }
  834. /*
  835. * Check to see if this CPU is in a non-context-switch quiescent state
  836. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  837. * Also schedule the RCU softirq handler.
  838. *
  839. * This function must be called with hardirqs disabled. It is normally
  840. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  841. * false, there is no point in invoking rcu_check_callbacks().
  842. */
  843. void rcu_check_callbacks(int cpu, int user)
  844. {
  845. if (user ||
  846. (idle_cpu(cpu) && rcu_scheduler_active &&
  847. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  848. /*
  849. * Get here if this CPU took its interrupt from user
  850. * mode or from the idle loop, and if this is not a
  851. * nested interrupt. In this case, the CPU is in
  852. * a quiescent state, so count it.
  853. *
  854. * No memory barrier is required here because both
  855. * rcu_qsctr_inc() and rcu_bh_qsctr_inc() reference
  856. * only CPU-local variables that other CPUs neither
  857. * access nor modify, at least not while the corresponding
  858. * CPU is online.
  859. */
  860. rcu_qsctr_inc(cpu);
  861. rcu_bh_qsctr_inc(cpu);
  862. } else if (!in_softirq()) {
  863. /*
  864. * Get here if this CPU did not take its interrupt from
  865. * softirq, in other words, if it is not interrupting
  866. * a rcu_bh read-side critical section. This is an _bh
  867. * critical section, so count it.
  868. */
  869. rcu_bh_qsctr_inc(cpu);
  870. }
  871. raise_softirq(RCU_SOFTIRQ);
  872. }
  873. #ifdef CONFIG_SMP
  874. /*
  875. * Scan the leaf rcu_node structures, processing dyntick state for any that
  876. * have not yet encountered a quiescent state, using the function specified.
  877. * Returns 1 if the current grace period ends while scanning (possibly
  878. * because we made it end).
  879. */
  880. static int rcu_process_dyntick(struct rcu_state *rsp, long lastcomp,
  881. int (*f)(struct rcu_data *))
  882. {
  883. unsigned long bit;
  884. int cpu;
  885. unsigned long flags;
  886. unsigned long mask;
  887. struct rcu_node *rnp_cur = rsp->level[NUM_RCU_LVLS - 1];
  888. struct rcu_node *rnp_end = &rsp->node[NUM_RCU_NODES];
  889. for (; rnp_cur < rnp_end; rnp_cur++) {
  890. mask = 0;
  891. spin_lock_irqsave(&rnp_cur->lock, flags);
  892. if (rsp->completed != lastcomp) {
  893. spin_unlock_irqrestore(&rnp_cur->lock, flags);
  894. return 1;
  895. }
  896. if (rnp_cur->qsmask == 0) {
  897. spin_unlock_irqrestore(&rnp_cur->lock, flags);
  898. continue;
  899. }
  900. cpu = rnp_cur->grplo;
  901. bit = 1;
  902. for (; cpu <= rnp_cur->grphi; cpu++, bit <<= 1) {
  903. if ((rnp_cur->qsmask & bit) != 0 && f(rsp->rda[cpu]))
  904. mask |= bit;
  905. }
  906. if (mask != 0 && rsp->completed == lastcomp) {
  907. /* cpu_quiet_msk() releases rnp_cur->lock. */
  908. cpu_quiet_msk(mask, rsp, rnp_cur, flags);
  909. continue;
  910. }
  911. spin_unlock_irqrestore(&rnp_cur->lock, flags);
  912. }
  913. return 0;
  914. }
  915. /*
  916. * Force quiescent states on reluctant CPUs, and also detect which
  917. * CPUs are in dyntick-idle mode.
  918. */
  919. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  920. {
  921. unsigned long flags;
  922. long lastcomp;
  923. struct rcu_data *rdp = rsp->rda[smp_processor_id()];
  924. struct rcu_node *rnp = rcu_get_root(rsp);
  925. u8 signaled;
  926. if (ACCESS_ONCE(rsp->completed) == ACCESS_ONCE(rsp->gpnum))
  927. return; /* No grace period in progress, nothing to force. */
  928. if (!spin_trylock_irqsave(&rsp->fqslock, flags)) {
  929. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  930. return; /* Someone else is already on the job. */
  931. }
  932. if (relaxed &&
  933. (long)(rsp->jiffies_force_qs - jiffies) >= 0 &&
  934. (rdp->n_rcu_pending_force_qs - rdp->n_rcu_pending) >= 0)
  935. goto unlock_ret; /* no emergency and done recently. */
  936. rsp->n_force_qs++;
  937. spin_lock(&rnp->lock);
  938. lastcomp = rsp->completed;
  939. signaled = rsp->signaled;
  940. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  941. rdp->n_rcu_pending_force_qs = rdp->n_rcu_pending +
  942. RCU_JIFFIES_TILL_FORCE_QS;
  943. if (lastcomp == rsp->gpnum) {
  944. rsp->n_force_qs_ngp++;
  945. spin_unlock(&rnp->lock);
  946. goto unlock_ret; /* no GP in progress, time updated. */
  947. }
  948. spin_unlock(&rnp->lock);
  949. switch (signaled) {
  950. case RCU_GP_INIT:
  951. break; /* grace period still initializing, ignore. */
  952. case RCU_SAVE_DYNTICK:
  953. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  954. break; /* So gcc recognizes the dead code. */
  955. /* Record dyntick-idle state. */
  956. if (rcu_process_dyntick(rsp, lastcomp,
  957. dyntick_save_progress_counter))
  958. goto unlock_ret;
  959. /* Update state, record completion counter. */
  960. spin_lock(&rnp->lock);
  961. if (lastcomp == rsp->completed) {
  962. rsp->signaled = RCU_FORCE_QS;
  963. dyntick_record_completed(rsp, lastcomp);
  964. }
  965. spin_unlock(&rnp->lock);
  966. break;
  967. case RCU_FORCE_QS:
  968. /* Check dyntick-idle state, send IPI to laggarts. */
  969. if (rcu_process_dyntick(rsp, dyntick_recall_completed(rsp),
  970. rcu_implicit_dynticks_qs))
  971. goto unlock_ret;
  972. /* Leave state in case more forcing is required. */
  973. break;
  974. }
  975. unlock_ret:
  976. spin_unlock_irqrestore(&rsp->fqslock, flags);
  977. }
  978. #else /* #ifdef CONFIG_SMP */
  979. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  980. {
  981. set_need_resched();
  982. }
  983. #endif /* #else #ifdef CONFIG_SMP */
  984. /*
  985. * This does the RCU processing work from softirq context for the
  986. * specified rcu_state and rcu_data structures. This may be called
  987. * only from the CPU to whom the rdp belongs.
  988. */
  989. static void
  990. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  991. {
  992. unsigned long flags;
  993. /*
  994. * If an RCU GP has gone long enough, go check for dyntick
  995. * idle CPUs and, if needed, send resched IPIs.
  996. */
  997. if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0 ||
  998. (rdp->n_rcu_pending_force_qs - rdp->n_rcu_pending) < 0)
  999. force_quiescent_state(rsp, 1);
  1000. /*
  1001. * Advance callbacks in response to end of earlier grace
  1002. * period that some other CPU ended.
  1003. */
  1004. rcu_process_gp_end(rsp, rdp);
  1005. /* Update RCU state based on any recent quiescent states. */
  1006. rcu_check_quiescent_state(rsp, rdp);
  1007. /* Does this CPU require a not-yet-started grace period? */
  1008. if (cpu_needs_another_gp(rsp, rdp)) {
  1009. spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1010. rcu_start_gp(rsp, flags); /* releases above lock */
  1011. }
  1012. /* If there are callbacks ready, invoke them. */
  1013. rcu_do_batch(rdp);
  1014. }
  1015. /*
  1016. * Do softirq processing for the current CPU.
  1017. */
  1018. static void rcu_process_callbacks(struct softirq_action *unused)
  1019. {
  1020. /*
  1021. * Memory references from any prior RCU read-side critical sections
  1022. * executed by the interrupted code must be seen before any RCU
  1023. * grace-period manipulations below.
  1024. */
  1025. smp_mb(); /* See above block comment. */
  1026. __rcu_process_callbacks(&rcu_state, &__get_cpu_var(rcu_data));
  1027. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1028. /*
  1029. * Memory references from any later RCU read-side critical sections
  1030. * executed by the interrupted code must be seen after any RCU
  1031. * grace-period manipulations above.
  1032. */
  1033. smp_mb(); /* See above block comment. */
  1034. }
  1035. static void
  1036. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1037. struct rcu_state *rsp)
  1038. {
  1039. unsigned long flags;
  1040. struct rcu_data *rdp;
  1041. head->func = func;
  1042. head->next = NULL;
  1043. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1044. /*
  1045. * Opportunistically note grace-period endings and beginnings.
  1046. * Note that we might see a beginning right after we see an
  1047. * end, but never vice versa, since this CPU has to pass through
  1048. * a quiescent state betweentimes.
  1049. */
  1050. local_irq_save(flags);
  1051. rdp = rsp->rda[smp_processor_id()];
  1052. rcu_process_gp_end(rsp, rdp);
  1053. check_for_new_grace_period(rsp, rdp);
  1054. /* Add the callback to our list. */
  1055. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1056. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1057. /* Start a new grace period if one not already started. */
  1058. if (ACCESS_ONCE(rsp->completed) == ACCESS_ONCE(rsp->gpnum)) {
  1059. unsigned long nestflag;
  1060. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1061. spin_lock_irqsave(&rnp_root->lock, nestflag);
  1062. rcu_start_gp(rsp, nestflag); /* releases rnp_root->lock. */
  1063. }
  1064. /* Force the grace period if too many callbacks or too long waiting. */
  1065. if (unlikely(++rdp->qlen > qhimark)) {
  1066. rdp->blimit = LONG_MAX;
  1067. force_quiescent_state(rsp, 0);
  1068. } else if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0 ||
  1069. (rdp->n_rcu_pending_force_qs - rdp->n_rcu_pending) < 0)
  1070. force_quiescent_state(rsp, 1);
  1071. local_irq_restore(flags);
  1072. }
  1073. /*
  1074. * Queue an RCU callback for invocation after a grace period.
  1075. */
  1076. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1077. {
  1078. __call_rcu(head, func, &rcu_state);
  1079. }
  1080. EXPORT_SYMBOL_GPL(call_rcu);
  1081. /*
  1082. * Queue an RCU for invocation after a quicker grace period.
  1083. */
  1084. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1085. {
  1086. __call_rcu(head, func, &rcu_bh_state);
  1087. }
  1088. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1089. /*
  1090. * Check to see if there is any immediate RCU-related work to be done
  1091. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1092. * The checks are in order of increasing expense: checks that can be
  1093. * carried out against CPU-local state are performed first. However,
  1094. * we must check for CPU stalls first, else we might not get a chance.
  1095. */
  1096. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1097. {
  1098. rdp->n_rcu_pending++;
  1099. /* Check for CPU stalls, if enabled. */
  1100. check_cpu_stall(rsp, rdp);
  1101. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1102. if (rdp->qs_pending)
  1103. return 1;
  1104. /* Does this CPU have callbacks ready to invoke? */
  1105. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1106. return 1;
  1107. /* Has RCU gone idle with this CPU needing another grace period? */
  1108. if (cpu_needs_another_gp(rsp, rdp))
  1109. return 1;
  1110. /* Has another RCU grace period completed? */
  1111. if (ACCESS_ONCE(rsp->completed) != rdp->completed) /* outside of lock */
  1112. return 1;
  1113. /* Has a new RCU grace period started? */
  1114. if (ACCESS_ONCE(rsp->gpnum) != rdp->gpnum) /* outside of lock */
  1115. return 1;
  1116. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1117. if (ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum) &&
  1118. ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0 ||
  1119. (rdp->n_rcu_pending_force_qs - rdp->n_rcu_pending) < 0))
  1120. return 1;
  1121. /* nothing to do */
  1122. return 0;
  1123. }
  1124. /*
  1125. * Check to see if there is any immediate RCU-related work to be done
  1126. * by the current CPU, returning 1 if so. This function is part of the
  1127. * RCU implementation; it is -not- an exported member of the RCU API.
  1128. */
  1129. int rcu_pending(int cpu)
  1130. {
  1131. return __rcu_pending(&rcu_state, &per_cpu(rcu_data, cpu)) ||
  1132. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu));
  1133. }
  1134. /*
  1135. * Check to see if any future RCU-related work will need to be done
  1136. * by the current CPU, even if none need be done immediately, returning
  1137. * 1 if so. This function is part of the RCU implementation; it is -not-
  1138. * an exported member of the RCU API.
  1139. */
  1140. int rcu_needs_cpu(int cpu)
  1141. {
  1142. /* RCU callbacks either ready or pending? */
  1143. return per_cpu(rcu_data, cpu).nxtlist ||
  1144. per_cpu(rcu_bh_data, cpu).nxtlist;
  1145. }
  1146. /*
  1147. * Initialize a CPU's per-CPU RCU data. We take this "scorched earth"
  1148. * approach so that we don't have to worry about how long the CPU has
  1149. * been gone, or whether it ever was online previously. We do trust the
  1150. * ->mynode field, as it is constant for a given struct rcu_data and
  1151. * initialized during early boot.
  1152. *
  1153. * Note that only one online or offline event can be happening at a given
  1154. * time. Note also that we can accept some slop in the rsp->completed
  1155. * access due to the fact that this CPU cannot possibly have any RCU
  1156. * callbacks in flight yet.
  1157. */
  1158. static void __cpuinit
  1159. rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
  1160. {
  1161. unsigned long flags;
  1162. int i;
  1163. long lastcomp;
  1164. unsigned long mask;
  1165. struct rcu_data *rdp = rsp->rda[cpu];
  1166. struct rcu_node *rnp = rcu_get_root(rsp);
  1167. /* Set up local state, ensuring consistent view of global state. */
  1168. spin_lock_irqsave(&rnp->lock, flags);
  1169. lastcomp = rsp->completed;
  1170. rdp->completed = lastcomp;
  1171. rdp->gpnum = lastcomp;
  1172. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1173. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1174. rdp->beenonline = 1; /* We have now been online. */
  1175. rdp->passed_quiesc_completed = lastcomp - 1;
  1176. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1177. rdp->nxtlist = NULL;
  1178. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1179. rdp->nxttail[i] = &rdp->nxtlist;
  1180. rdp->qlen = 0;
  1181. rdp->blimit = blimit;
  1182. #ifdef CONFIG_NO_HZ
  1183. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1184. #endif /* #ifdef CONFIG_NO_HZ */
  1185. rdp->cpu = cpu;
  1186. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1187. /*
  1188. * A new grace period might start here. If so, we won't be part
  1189. * of it, but that is OK, as we are currently in a quiescent state.
  1190. */
  1191. /* Exclude any attempts to start a new GP on large systems. */
  1192. spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1193. /* Add CPU to rcu_node bitmasks. */
  1194. rnp = rdp->mynode;
  1195. mask = rdp->grpmask;
  1196. do {
  1197. /* Exclude any attempts to start a new GP on small systems. */
  1198. spin_lock(&rnp->lock); /* irqs already disabled. */
  1199. rnp->qsmaskinit |= mask;
  1200. mask = rnp->grpmask;
  1201. spin_unlock(&rnp->lock); /* irqs already disabled. */
  1202. rnp = rnp->parent;
  1203. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1204. spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  1205. /*
  1206. * A new grace period might start here. If so, we will be part of
  1207. * it, and its gpnum will be greater than ours, so we will
  1208. * participate. It is also possible for the gpnum to have been
  1209. * incremented before this function was called, and the bitmasks
  1210. * to not be filled out until now, in which case we will also
  1211. * participate due to our gpnum being behind.
  1212. */
  1213. /* Since it is coming online, the CPU is in a quiescent state. */
  1214. cpu_quiet(cpu, rsp, rdp, lastcomp);
  1215. local_irq_restore(flags);
  1216. }
  1217. static void __cpuinit rcu_online_cpu(int cpu)
  1218. {
  1219. rcu_init_percpu_data(cpu, &rcu_state);
  1220. rcu_init_percpu_data(cpu, &rcu_bh_state);
  1221. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1222. }
  1223. /*
  1224. * Handle CPU online/offline notifcation events.
  1225. */
  1226. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1227. unsigned long action, void *hcpu)
  1228. {
  1229. long cpu = (long)hcpu;
  1230. switch (action) {
  1231. case CPU_UP_PREPARE:
  1232. case CPU_UP_PREPARE_FROZEN:
  1233. rcu_online_cpu(cpu);
  1234. break;
  1235. case CPU_DEAD:
  1236. case CPU_DEAD_FROZEN:
  1237. case CPU_UP_CANCELED:
  1238. case CPU_UP_CANCELED_FROZEN:
  1239. rcu_offline_cpu(cpu);
  1240. break;
  1241. default:
  1242. break;
  1243. }
  1244. return NOTIFY_OK;
  1245. }
  1246. /*
  1247. * Compute the per-level fanout, either using the exact fanout specified
  1248. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  1249. */
  1250. #ifdef CONFIG_RCU_FANOUT_EXACT
  1251. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1252. {
  1253. int i;
  1254. for (i = NUM_RCU_LVLS - 1; i >= 0; i--)
  1255. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  1256. }
  1257. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  1258. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1259. {
  1260. int ccur;
  1261. int cprv;
  1262. int i;
  1263. cprv = NR_CPUS;
  1264. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1265. ccur = rsp->levelcnt[i];
  1266. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  1267. cprv = ccur;
  1268. }
  1269. }
  1270. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  1271. /*
  1272. * Helper function for rcu_init() that initializes one rcu_state structure.
  1273. */
  1274. static void __init rcu_init_one(struct rcu_state *rsp)
  1275. {
  1276. int cpustride = 1;
  1277. int i;
  1278. int j;
  1279. struct rcu_node *rnp;
  1280. /* Initialize the level-tracking arrays. */
  1281. for (i = 1; i < NUM_RCU_LVLS; i++)
  1282. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  1283. rcu_init_levelspread(rsp);
  1284. /* Initialize the elements themselves, starting from the leaves. */
  1285. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1286. cpustride *= rsp->levelspread[i];
  1287. rnp = rsp->level[i];
  1288. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  1289. spin_lock_init(&rnp->lock);
  1290. rnp->qsmask = 0;
  1291. rnp->qsmaskinit = 0;
  1292. rnp->grplo = j * cpustride;
  1293. rnp->grphi = (j + 1) * cpustride - 1;
  1294. if (rnp->grphi >= NR_CPUS)
  1295. rnp->grphi = NR_CPUS - 1;
  1296. if (i == 0) {
  1297. rnp->grpnum = 0;
  1298. rnp->grpmask = 0;
  1299. rnp->parent = NULL;
  1300. } else {
  1301. rnp->grpnum = j % rsp->levelspread[i - 1];
  1302. rnp->grpmask = 1UL << rnp->grpnum;
  1303. rnp->parent = rsp->level[i - 1] +
  1304. j / rsp->levelspread[i - 1];
  1305. }
  1306. rnp->level = i;
  1307. }
  1308. }
  1309. }
  1310. /*
  1311. * Helper macro for __rcu_init(). To be used nowhere else!
  1312. * Assigns leaf node pointers into each CPU's rcu_data structure.
  1313. */
  1314. #define RCU_DATA_PTR_INIT(rsp, rcu_data) \
  1315. do { \
  1316. rnp = (rsp)->level[NUM_RCU_LVLS - 1]; \
  1317. j = 0; \
  1318. for_each_possible_cpu(i) { \
  1319. if (i > rnp[j].grphi) \
  1320. j++; \
  1321. per_cpu(rcu_data, i).mynode = &rnp[j]; \
  1322. (rsp)->rda[i] = &per_cpu(rcu_data, i); \
  1323. } \
  1324. } while (0)
  1325. static struct notifier_block __cpuinitdata rcu_nb = {
  1326. .notifier_call = rcu_cpu_notify,
  1327. };
  1328. void __init __rcu_init(void)
  1329. {
  1330. int i; /* All used by RCU_DATA_PTR_INIT(). */
  1331. int j;
  1332. struct rcu_node *rnp;
  1333. printk(KERN_WARNING "Experimental hierarchical RCU implementation.\n");
  1334. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  1335. printk(KERN_INFO "RCU-based detection of stalled CPUs is enabled.\n");
  1336. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  1337. rcu_init_one(&rcu_state);
  1338. RCU_DATA_PTR_INIT(&rcu_state, rcu_data);
  1339. rcu_init_one(&rcu_bh_state);
  1340. RCU_DATA_PTR_INIT(&rcu_bh_state, rcu_bh_data);
  1341. for_each_online_cpu(i)
  1342. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long)i);
  1343. /* Register notifier for non-boot CPUs */
  1344. register_cpu_notifier(&rcu_nb);
  1345. printk(KERN_WARNING "Experimental hierarchical RCU init done.\n");
  1346. }
  1347. module_param(blimit, int, 0);
  1348. module_param(qhimark, int, 0);
  1349. module_param(qlowmark, int, 0);