rcutree.c 46 KB

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