rcutree.c 46 KB

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