rcutree.c 47 KB

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