rcutree.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  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 <linux/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 <linux/kernel_stat.h>
  50. #include <linux/wait.h>
  51. #include <linux/kthread.h>
  52. #include <linux/prefetch.h>
  53. #include "rcutree.h"
  54. #include <trace/events/rcu.h>
  55. #include "rcu.h"
  56. /* Data structures. */
  57. static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
  58. #define RCU_STATE_INITIALIZER(structname) { \
  59. .level = { &structname##_state.node[0] }, \
  60. .levelcnt = { \
  61. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  62. NUM_RCU_LVL_1, \
  63. NUM_RCU_LVL_2, \
  64. NUM_RCU_LVL_3, \
  65. NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
  66. }, \
  67. .signaled = RCU_GP_IDLE, \
  68. .gpnum = -300, \
  69. .completed = -300, \
  70. .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&structname##_state.onofflock), \
  71. .fqslock = __RAW_SPIN_LOCK_UNLOCKED(&structname##_state.fqslock), \
  72. .n_force_qs = 0, \
  73. .n_force_qs_ngp = 0, \
  74. .name = #structname, \
  75. }
  76. struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched);
  77. DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
  78. struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh);
  79. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
  80. static struct rcu_state *rcu_state;
  81. /*
  82. * The rcu_scheduler_active variable transitions from zero to one just
  83. * before the first task is spawned. So when this variable is zero, RCU
  84. * can assume that there is but one task, allowing RCU to (for example)
  85. * optimized synchronize_sched() to a simple barrier(). When this variable
  86. * is one, RCU must actually do all the hard work required to detect real
  87. * grace periods. This variable is also used to suppress boot-time false
  88. * positives from lockdep-RCU error checking.
  89. */
  90. int rcu_scheduler_active __read_mostly;
  91. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  92. /*
  93. * The rcu_scheduler_fully_active variable transitions from zero to one
  94. * during the early_initcall() processing, which is after the scheduler
  95. * is capable of creating new tasks. So RCU processing (for example,
  96. * creating tasks for RCU priority boosting) must be delayed until after
  97. * rcu_scheduler_fully_active transitions from zero to one. We also
  98. * currently delay invocation of any RCU callbacks until after this point.
  99. *
  100. * It might later prove better for people registering RCU callbacks during
  101. * early boot to take responsibility for these callbacks, but one step at
  102. * a time.
  103. */
  104. static int rcu_scheduler_fully_active __read_mostly;
  105. #ifdef CONFIG_RCU_BOOST
  106. /*
  107. * Control variables for per-CPU and per-rcu_node kthreads. These
  108. * handle all flavors of RCU.
  109. */
  110. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  111. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  112. DEFINE_PER_CPU(int, rcu_cpu_kthread_cpu);
  113. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  114. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  115. #endif /* #ifdef CONFIG_RCU_BOOST */
  116. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  117. static void invoke_rcu_core(void);
  118. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  119. #define RCU_KTHREAD_PRIO 1 /* RT priority for per-CPU kthreads. */
  120. /*
  121. * Track the rcutorture test sequence number and the update version
  122. * number within a given test. The rcutorture_testseq is incremented
  123. * on every rcutorture module load and unload, so has an odd value
  124. * when a test is running. The rcutorture_vernum is set to zero
  125. * when rcutorture starts and is incremented on each rcutorture update.
  126. * These variables enable correlating rcutorture output with the
  127. * RCU tracing information.
  128. */
  129. unsigned long rcutorture_testseq;
  130. unsigned long rcutorture_vernum;
  131. /*
  132. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  133. * permit this function to be invoked without holding the root rcu_node
  134. * structure's ->lock, but of course results can be subject to change.
  135. */
  136. static int rcu_gp_in_progress(struct rcu_state *rsp)
  137. {
  138. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  139. }
  140. /*
  141. * Note a quiescent state. Because we do not need to know
  142. * how many quiescent states passed, just if there was at least
  143. * one since the start of the grace period, this just sets a flag.
  144. */
  145. void rcu_sched_qs(int cpu)
  146. {
  147. struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
  148. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  149. barrier();
  150. rdp->passed_quiesc = 1;
  151. }
  152. void rcu_bh_qs(int cpu)
  153. {
  154. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  155. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  156. barrier();
  157. rdp->passed_quiesc = 1;
  158. }
  159. /*
  160. * Note a context switch. This is a quiescent state for RCU-sched,
  161. * and requires special handling for preemptible RCU.
  162. */
  163. void rcu_note_context_switch(int cpu)
  164. {
  165. trace_rcu_utilization("Start context switch");
  166. rcu_sched_qs(cpu);
  167. rcu_preempt_note_context_switch(cpu);
  168. trace_rcu_utilization("End context switch");
  169. }
  170. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  171. #ifdef CONFIG_NO_HZ
  172. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  173. .dynticks_nesting = 1,
  174. .dynticks = ATOMIC_INIT(1),
  175. };
  176. #endif /* #ifdef CONFIG_NO_HZ */
  177. static int blimit = 10; /* Maximum callbacks per softirq. */
  178. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  179. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  180. module_param(blimit, int, 0);
  181. module_param(qhimark, int, 0);
  182. module_param(qlowmark, int, 0);
  183. int rcu_cpu_stall_suppress __read_mostly;
  184. module_param(rcu_cpu_stall_suppress, int, 0644);
  185. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  186. static int rcu_pending(int cpu);
  187. /*
  188. * Return the number of RCU-sched batches processed thus far for debug & stats.
  189. */
  190. long rcu_batches_completed_sched(void)
  191. {
  192. return rcu_sched_state.completed;
  193. }
  194. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  195. /*
  196. * Return the number of RCU BH batches processed thus far for debug & stats.
  197. */
  198. long rcu_batches_completed_bh(void)
  199. {
  200. return rcu_bh_state.completed;
  201. }
  202. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  203. /*
  204. * Force a quiescent state for RCU BH.
  205. */
  206. void rcu_bh_force_quiescent_state(void)
  207. {
  208. force_quiescent_state(&rcu_bh_state, 0);
  209. }
  210. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  211. /*
  212. * Record the number of times rcutorture tests have been initiated and
  213. * terminated. This information allows the debugfs tracing stats to be
  214. * correlated to the rcutorture messages, even when the rcutorture module
  215. * is being repeatedly loaded and unloaded. In other words, we cannot
  216. * store this state in rcutorture itself.
  217. */
  218. void rcutorture_record_test_transition(void)
  219. {
  220. rcutorture_testseq++;
  221. rcutorture_vernum = 0;
  222. }
  223. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  224. /*
  225. * Record the number of writer passes through the current rcutorture test.
  226. * This is also used to correlate debugfs tracing stats with the rcutorture
  227. * messages.
  228. */
  229. void rcutorture_record_progress(unsigned long vernum)
  230. {
  231. rcutorture_vernum++;
  232. }
  233. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  234. /*
  235. * Force a quiescent state for RCU-sched.
  236. */
  237. void rcu_sched_force_quiescent_state(void)
  238. {
  239. force_quiescent_state(&rcu_sched_state, 0);
  240. }
  241. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  242. /*
  243. * Does the CPU have callbacks ready to be invoked?
  244. */
  245. static int
  246. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  247. {
  248. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  249. }
  250. /*
  251. * Does the current CPU require a yet-as-unscheduled grace period?
  252. */
  253. static int
  254. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  255. {
  256. return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
  257. }
  258. /*
  259. * Return the root node of the specified rcu_state structure.
  260. */
  261. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  262. {
  263. return &rsp->node[0];
  264. }
  265. #ifdef CONFIG_SMP
  266. /*
  267. * If the specified CPU is offline, tell the caller that it is in
  268. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  269. * Grace periods can end up waiting on an offline CPU when that
  270. * CPU is in the process of coming online -- it will be added to the
  271. * rcu_node bitmasks before it actually makes it online. The same thing
  272. * can happen while a CPU is in the process of coming online. Because this
  273. * race is quite rare, we check for it after detecting that the grace
  274. * period has been delayed rather than checking each and every CPU
  275. * each and every time we start a new grace period.
  276. */
  277. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  278. {
  279. /*
  280. * If the CPU is offline, it is in a quiescent state. We can
  281. * trust its state not to change because interrupts are disabled.
  282. */
  283. if (cpu_is_offline(rdp->cpu)) {
  284. rdp->offline_fqs++;
  285. return 1;
  286. }
  287. /* If preemptible RCU, no point in sending reschedule IPI. */
  288. if (rdp->preemptible)
  289. return 0;
  290. /* The CPU is online, so send it a reschedule IPI. */
  291. if (rdp->cpu != smp_processor_id())
  292. smp_send_reschedule(rdp->cpu);
  293. else
  294. set_need_resched();
  295. rdp->resched_ipi++;
  296. return 0;
  297. }
  298. #endif /* #ifdef CONFIG_SMP */
  299. #ifdef CONFIG_NO_HZ
  300. /**
  301. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  302. *
  303. * Enter nohz mode, in other words, -leave- the mode in which RCU
  304. * read-side critical sections can occur. (Though RCU read-side
  305. * critical sections can occur in irq handlers in nohz mode, a possibility
  306. * handled by rcu_irq_enter() and rcu_irq_exit()).
  307. */
  308. void rcu_enter_nohz(void)
  309. {
  310. unsigned long flags;
  311. struct rcu_dynticks *rdtp;
  312. local_irq_save(flags);
  313. rdtp = &__get_cpu_var(rcu_dynticks);
  314. if (--rdtp->dynticks_nesting) {
  315. local_irq_restore(flags);
  316. return;
  317. }
  318. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  319. smp_mb__before_atomic_inc(); /* See above. */
  320. atomic_inc(&rdtp->dynticks);
  321. smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
  322. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  323. local_irq_restore(flags);
  324. /* If the interrupt queued a callback, get out of dyntick mode. */
  325. if (in_irq() &&
  326. (__get_cpu_var(rcu_sched_data).nxtlist ||
  327. __get_cpu_var(rcu_bh_data).nxtlist ||
  328. rcu_preempt_needs_cpu(smp_processor_id())))
  329. set_need_resched();
  330. }
  331. /*
  332. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  333. *
  334. * Exit nohz mode, in other words, -enter- the mode in which RCU
  335. * read-side critical sections normally occur.
  336. */
  337. void rcu_exit_nohz(void)
  338. {
  339. unsigned long flags;
  340. struct rcu_dynticks *rdtp;
  341. local_irq_save(flags);
  342. rdtp = &__get_cpu_var(rcu_dynticks);
  343. if (rdtp->dynticks_nesting++) {
  344. local_irq_restore(flags);
  345. return;
  346. }
  347. smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
  348. atomic_inc(&rdtp->dynticks);
  349. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  350. smp_mb__after_atomic_inc(); /* See above. */
  351. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  352. local_irq_restore(flags);
  353. }
  354. /**
  355. * rcu_nmi_enter - inform RCU of entry to NMI context
  356. *
  357. * If the CPU was idle with dynamic ticks active, and there is no
  358. * irq handler running, this updates rdtp->dynticks_nmi to let the
  359. * RCU grace-period handling know that the CPU is active.
  360. */
  361. void rcu_nmi_enter(void)
  362. {
  363. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  364. if (rdtp->dynticks_nmi_nesting == 0 &&
  365. (atomic_read(&rdtp->dynticks) & 0x1))
  366. return;
  367. rdtp->dynticks_nmi_nesting++;
  368. smp_mb__before_atomic_inc(); /* Force delay from prior write. */
  369. atomic_inc(&rdtp->dynticks);
  370. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  371. smp_mb__after_atomic_inc(); /* See above. */
  372. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  373. }
  374. /**
  375. * rcu_nmi_exit - inform RCU of exit from NMI context
  376. *
  377. * If the CPU was idle with dynamic ticks active, and there is no
  378. * irq handler running, this updates rdtp->dynticks_nmi to let the
  379. * RCU grace-period handling know that the CPU is no longer active.
  380. */
  381. void rcu_nmi_exit(void)
  382. {
  383. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  384. if (rdtp->dynticks_nmi_nesting == 0 ||
  385. --rdtp->dynticks_nmi_nesting != 0)
  386. return;
  387. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  388. smp_mb__before_atomic_inc(); /* See above. */
  389. atomic_inc(&rdtp->dynticks);
  390. smp_mb__after_atomic_inc(); /* Force delay to next write. */
  391. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  392. }
  393. /**
  394. * rcu_irq_enter - inform RCU of entry to hard irq context
  395. *
  396. * If the CPU was idle with dynamic ticks active, this updates the
  397. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  398. */
  399. void rcu_irq_enter(void)
  400. {
  401. rcu_exit_nohz();
  402. }
  403. /**
  404. * rcu_irq_exit - inform RCU of exit from hard irq context
  405. *
  406. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  407. * to put let the RCU handling be aware that the CPU is going back to idle
  408. * with no ticks.
  409. */
  410. void rcu_irq_exit(void)
  411. {
  412. rcu_enter_nohz();
  413. }
  414. #ifdef CONFIG_SMP
  415. /*
  416. * Snapshot the specified CPU's dynticks counter so that we can later
  417. * credit them with an implicit quiescent state. Return 1 if this CPU
  418. * is in dynticks idle mode, which is an extended quiescent state.
  419. */
  420. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  421. {
  422. rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
  423. return 0;
  424. }
  425. /*
  426. * Return true if the specified CPU has passed through a quiescent
  427. * state by virtue of being in or having passed through an dynticks
  428. * idle state since the last call to dyntick_save_progress_counter()
  429. * for this same CPU.
  430. */
  431. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  432. {
  433. unsigned long curr;
  434. unsigned long snap;
  435. curr = (unsigned long)atomic_add_return(0, &rdp->dynticks->dynticks);
  436. snap = (unsigned long)rdp->dynticks_snap;
  437. /*
  438. * If the CPU passed through or entered a dynticks idle phase with
  439. * no active irq/NMI handlers, then we can safely pretend that the CPU
  440. * already acknowledged the request to pass through a quiescent
  441. * state. Either way, that CPU cannot possibly be in an RCU
  442. * read-side critical section that started before the beginning
  443. * of the current RCU grace period.
  444. */
  445. if ((curr & 0x1) == 0 || ULONG_CMP_GE(curr, snap + 2)) {
  446. rdp->dynticks_fqs++;
  447. return 1;
  448. }
  449. /* Go check for the CPU being offline. */
  450. return rcu_implicit_offline_qs(rdp);
  451. }
  452. #endif /* #ifdef CONFIG_SMP */
  453. #else /* #ifdef CONFIG_NO_HZ */
  454. #ifdef CONFIG_SMP
  455. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  456. {
  457. return 0;
  458. }
  459. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  460. {
  461. return rcu_implicit_offline_qs(rdp);
  462. }
  463. #endif /* #ifdef CONFIG_SMP */
  464. #endif /* #else #ifdef CONFIG_NO_HZ */
  465. int rcu_cpu_stall_suppress __read_mostly;
  466. static void record_gp_stall_check_time(struct rcu_state *rsp)
  467. {
  468. rsp->gp_start = jiffies;
  469. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  470. }
  471. static void print_other_cpu_stall(struct rcu_state *rsp)
  472. {
  473. int cpu;
  474. long delta;
  475. unsigned long flags;
  476. struct rcu_node *rnp = rcu_get_root(rsp);
  477. /* Only let one CPU complain about others per time interval. */
  478. raw_spin_lock_irqsave(&rnp->lock, flags);
  479. delta = jiffies - rsp->jiffies_stall;
  480. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  481. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  482. return;
  483. }
  484. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  485. /*
  486. * Now rat on any tasks that got kicked up to the root rcu_node
  487. * due to CPU offlining.
  488. */
  489. rcu_print_task_stall(rnp);
  490. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  491. /*
  492. * OK, time to rat on our buddy...
  493. * See Documentation/RCU/stallwarn.txt for info on how to debug
  494. * RCU CPU stall warnings.
  495. */
  496. printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks: {",
  497. rsp->name);
  498. rcu_for_each_leaf_node(rsp, rnp) {
  499. raw_spin_lock_irqsave(&rnp->lock, flags);
  500. rcu_print_task_stall(rnp);
  501. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  502. if (rnp->qsmask == 0)
  503. continue;
  504. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  505. if (rnp->qsmask & (1UL << cpu))
  506. printk(" %d", rnp->grplo + cpu);
  507. }
  508. printk("} (detected by %d, t=%ld jiffies)\n",
  509. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  510. trigger_all_cpu_backtrace();
  511. /* If so configured, complain about tasks blocking the grace period. */
  512. rcu_print_detail_task_stall(rsp);
  513. force_quiescent_state(rsp, 0); /* Kick them all. */
  514. }
  515. static void print_cpu_stall(struct rcu_state *rsp)
  516. {
  517. unsigned long flags;
  518. struct rcu_node *rnp = rcu_get_root(rsp);
  519. /*
  520. * OK, time to rat on ourselves...
  521. * See Documentation/RCU/stallwarn.txt for info on how to debug
  522. * RCU CPU stall warnings.
  523. */
  524. printk(KERN_ERR "INFO: %s detected stall on CPU %d (t=%lu jiffies)\n",
  525. rsp->name, smp_processor_id(), jiffies - rsp->gp_start);
  526. trigger_all_cpu_backtrace();
  527. raw_spin_lock_irqsave(&rnp->lock, flags);
  528. if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
  529. rsp->jiffies_stall =
  530. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  531. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  532. set_need_resched(); /* kick ourselves to get things going. */
  533. }
  534. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  535. {
  536. unsigned long j;
  537. unsigned long js;
  538. struct rcu_node *rnp;
  539. if (rcu_cpu_stall_suppress)
  540. return;
  541. j = ACCESS_ONCE(jiffies);
  542. js = ACCESS_ONCE(rsp->jiffies_stall);
  543. rnp = rdp->mynode;
  544. if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
  545. /* We haven't checked in, so go dump stack. */
  546. print_cpu_stall(rsp);
  547. } else if (rcu_gp_in_progress(rsp) &&
  548. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  549. /* They had a few time units to dump stack, so complain. */
  550. print_other_cpu_stall(rsp);
  551. }
  552. }
  553. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  554. {
  555. rcu_cpu_stall_suppress = 1;
  556. return NOTIFY_DONE;
  557. }
  558. /**
  559. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  560. *
  561. * Set the stall-warning timeout way off into the future, thus preventing
  562. * any RCU CPU stall-warning messages from appearing in the current set of
  563. * RCU grace periods.
  564. *
  565. * The caller must disable hard irqs.
  566. */
  567. void rcu_cpu_stall_reset(void)
  568. {
  569. rcu_sched_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  570. rcu_bh_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  571. rcu_preempt_stall_reset();
  572. }
  573. static struct notifier_block rcu_panic_block = {
  574. .notifier_call = rcu_panic,
  575. };
  576. static void __init check_cpu_stall_init(void)
  577. {
  578. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  579. }
  580. /*
  581. * Update CPU-local rcu_data state to record the newly noticed grace period.
  582. * This is used both when we started the grace period and when we notice
  583. * that someone else started the grace period. The caller must hold the
  584. * ->lock of the leaf rcu_node structure corresponding to the current CPU,
  585. * and must have irqs disabled.
  586. */
  587. static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  588. {
  589. if (rdp->gpnum != rnp->gpnum) {
  590. /*
  591. * If the current grace period is waiting for this CPU,
  592. * set up to detect a quiescent state, otherwise don't
  593. * go looking for one.
  594. */
  595. rdp->gpnum = rnp->gpnum;
  596. if (rnp->qsmask & rdp->grpmask) {
  597. rdp->qs_pending = 1;
  598. rdp->passed_quiesc = 0;
  599. } else
  600. rdp->qs_pending = 0;
  601. }
  602. }
  603. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  604. {
  605. unsigned long flags;
  606. struct rcu_node *rnp;
  607. local_irq_save(flags);
  608. rnp = rdp->mynode;
  609. if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
  610. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  611. local_irq_restore(flags);
  612. return;
  613. }
  614. __note_new_gpnum(rsp, rnp, rdp);
  615. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  616. }
  617. /*
  618. * Did someone else start a new RCU grace period start since we last
  619. * checked? Update local state appropriately if so. Must be called
  620. * on the CPU corresponding to rdp.
  621. */
  622. static int
  623. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  624. {
  625. unsigned long flags;
  626. int ret = 0;
  627. local_irq_save(flags);
  628. if (rdp->gpnum != rsp->gpnum) {
  629. note_new_gpnum(rsp, rdp);
  630. ret = 1;
  631. }
  632. local_irq_restore(flags);
  633. return ret;
  634. }
  635. /*
  636. * Advance this CPU's callbacks, but only if the current grace period
  637. * has ended. This may be called only from the CPU to whom the rdp
  638. * belongs. In addition, the corresponding leaf rcu_node structure's
  639. * ->lock must be held by the caller, with irqs disabled.
  640. */
  641. static void
  642. __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  643. {
  644. /* Did another grace period end? */
  645. if (rdp->completed != rnp->completed) {
  646. /* Advance callbacks. No harm if list empty. */
  647. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  648. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  649. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  650. /* Remember that we saw this grace-period completion. */
  651. rdp->completed = rnp->completed;
  652. /*
  653. * If we were in an extended quiescent state, we may have
  654. * missed some grace periods that others CPUs handled on
  655. * our behalf. Catch up with this state to avoid noting
  656. * spurious new grace periods. If another grace period
  657. * has started, then rnp->gpnum will have advanced, so
  658. * we will detect this later on.
  659. */
  660. if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
  661. rdp->gpnum = rdp->completed;
  662. /*
  663. * If RCU does not need a quiescent state from this CPU,
  664. * then make sure that this CPU doesn't go looking for one.
  665. */
  666. if ((rnp->qsmask & rdp->grpmask) == 0)
  667. rdp->qs_pending = 0;
  668. }
  669. }
  670. /*
  671. * Advance this CPU's callbacks, but only if the current grace period
  672. * has ended. This may be called only from the CPU to whom the rdp
  673. * belongs.
  674. */
  675. static void
  676. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  677. {
  678. unsigned long flags;
  679. struct rcu_node *rnp;
  680. local_irq_save(flags);
  681. rnp = rdp->mynode;
  682. if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
  683. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  684. local_irq_restore(flags);
  685. return;
  686. }
  687. __rcu_process_gp_end(rsp, rnp, rdp);
  688. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  689. }
  690. /*
  691. * Do per-CPU grace-period initialization for running CPU. The caller
  692. * must hold the lock of the leaf rcu_node structure corresponding to
  693. * this CPU.
  694. */
  695. static void
  696. rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  697. {
  698. /* Prior grace period ended, so advance callbacks for current CPU. */
  699. __rcu_process_gp_end(rsp, rnp, rdp);
  700. /*
  701. * Because this CPU just now started the new grace period, we know
  702. * that all of its callbacks will be covered by this upcoming grace
  703. * period, even the ones that were registered arbitrarily recently.
  704. * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
  705. *
  706. * Other CPUs cannot be sure exactly when the grace period started.
  707. * Therefore, their recently registered callbacks must pass through
  708. * an additional RCU_NEXT_READY stage, so that they will be handled
  709. * by the next RCU grace period.
  710. */
  711. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  712. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  713. /* Set state so that this CPU will detect the next quiescent state. */
  714. __note_new_gpnum(rsp, rnp, rdp);
  715. }
  716. /*
  717. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  718. * in preparation for detecting the next grace period. The caller must hold
  719. * the root node's ->lock, which is released before return. Hard irqs must
  720. * be disabled.
  721. */
  722. static void
  723. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  724. __releases(rcu_get_root(rsp)->lock)
  725. {
  726. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  727. struct rcu_node *rnp = rcu_get_root(rsp);
  728. if (!cpu_needs_another_gp(rsp, rdp) || rsp->fqs_active) {
  729. if (cpu_needs_another_gp(rsp, rdp))
  730. rsp->fqs_need_gp = 1;
  731. if (rnp->completed == rsp->completed) {
  732. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  733. return;
  734. }
  735. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  736. /*
  737. * Propagate new ->completed value to rcu_node structures
  738. * so that other CPUs don't have to wait until the start
  739. * of the next grace period to process their callbacks.
  740. */
  741. rcu_for_each_node_breadth_first(rsp, rnp) {
  742. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  743. rnp->completed = rsp->completed;
  744. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  745. }
  746. local_irq_restore(flags);
  747. return;
  748. }
  749. /* Advance to a new grace period and initialize state. */
  750. rsp->gpnum++;
  751. WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
  752. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  753. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  754. record_gp_stall_check_time(rsp);
  755. /* Special-case the common single-level case. */
  756. if (NUM_RCU_NODES == 1) {
  757. rcu_preempt_check_blocked_tasks(rnp);
  758. rnp->qsmask = rnp->qsmaskinit;
  759. rnp->gpnum = rsp->gpnum;
  760. rnp->completed = rsp->completed;
  761. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  762. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  763. rcu_preempt_boost_start_gp(rnp);
  764. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  765. return;
  766. }
  767. raw_spin_unlock(&rnp->lock); /* leave irqs disabled. */
  768. /* Exclude any concurrent CPU-hotplug operations. */
  769. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  770. /*
  771. * Set the quiescent-state-needed bits in all the rcu_node
  772. * structures for all currently online CPUs in breadth-first
  773. * order, starting from the root rcu_node structure. This
  774. * operation relies on the layout of the hierarchy within the
  775. * rsp->node[] array. Note that other CPUs will access only
  776. * the leaves of the hierarchy, which still indicate that no
  777. * grace period is in progress, at least until the corresponding
  778. * leaf node has been initialized. In addition, we have excluded
  779. * CPU-hotplug operations.
  780. *
  781. * Note that the grace period cannot complete until we finish
  782. * the initialization process, as there will be at least one
  783. * qsmask bit set in the root node until that time, namely the
  784. * one corresponding to this CPU, due to the fact that we have
  785. * irqs disabled.
  786. */
  787. rcu_for_each_node_breadth_first(rsp, rnp) {
  788. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  789. rcu_preempt_check_blocked_tasks(rnp);
  790. rnp->qsmask = rnp->qsmaskinit;
  791. rnp->gpnum = rsp->gpnum;
  792. rnp->completed = rsp->completed;
  793. if (rnp == rdp->mynode)
  794. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  795. rcu_preempt_boost_start_gp(rnp);
  796. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  797. }
  798. rnp = rcu_get_root(rsp);
  799. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  800. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  801. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  802. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  803. }
  804. /*
  805. * Report a full set of quiescent states to the specified rcu_state
  806. * data structure. This involves cleaning up after the prior grace
  807. * period and letting rcu_start_gp() start up the next grace period
  808. * if one is needed. Note that the caller must hold rnp->lock, as
  809. * required by rcu_start_gp(), which will release it.
  810. */
  811. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  812. __releases(rcu_get_root(rsp)->lock)
  813. {
  814. unsigned long gp_duration;
  815. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  816. /*
  817. * Ensure that all grace-period and pre-grace-period activity
  818. * is seen before the assignment to rsp->completed.
  819. */
  820. smp_mb(); /* See above block comment. */
  821. gp_duration = jiffies - rsp->gp_start;
  822. if (gp_duration > rsp->gp_max)
  823. rsp->gp_max = gp_duration;
  824. rsp->completed = rsp->gpnum;
  825. rsp->signaled = RCU_GP_IDLE;
  826. rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
  827. }
  828. /*
  829. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  830. * Allows quiescent states for a group of CPUs to be reported at one go
  831. * to the specified rcu_node structure, though all the CPUs in the group
  832. * must be represented by the same rcu_node structure (which need not be
  833. * a leaf rcu_node structure, though it often will be). That structure's
  834. * lock must be held upon entry, and it is released before return.
  835. */
  836. static void
  837. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  838. struct rcu_node *rnp, unsigned long flags)
  839. __releases(rnp->lock)
  840. {
  841. struct rcu_node *rnp_c;
  842. /* Walk up the rcu_node hierarchy. */
  843. for (;;) {
  844. if (!(rnp->qsmask & mask)) {
  845. /* Our bit has already been cleared, so done. */
  846. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  847. return;
  848. }
  849. rnp->qsmask &= ~mask;
  850. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  851. /* Other bits still set at this level, so done. */
  852. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  853. return;
  854. }
  855. mask = rnp->grpmask;
  856. if (rnp->parent == NULL) {
  857. /* No more levels. Exit loop holding root lock. */
  858. break;
  859. }
  860. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  861. rnp_c = rnp;
  862. rnp = rnp->parent;
  863. raw_spin_lock_irqsave(&rnp->lock, flags);
  864. WARN_ON_ONCE(rnp_c->qsmask);
  865. }
  866. /*
  867. * Get here if we are the last CPU to pass through a quiescent
  868. * state for this grace period. Invoke rcu_report_qs_rsp()
  869. * to clean up and start the next grace period if one is needed.
  870. */
  871. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  872. }
  873. /*
  874. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  875. * structure. This must be either called from the specified CPU, or
  876. * called when the specified CPU is known to be offline (and when it is
  877. * also known that no other CPU is concurrently trying to help the offline
  878. * CPU). The lastcomp argument is used to make sure we are still in the
  879. * grace period of interest. We don't want to end the current grace period
  880. * based on quiescent states detected in an earlier grace period!
  881. */
  882. static void
  883. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  884. {
  885. unsigned long flags;
  886. unsigned long mask;
  887. struct rcu_node *rnp;
  888. rnp = rdp->mynode;
  889. raw_spin_lock_irqsave(&rnp->lock, flags);
  890. if (lastcomp != rnp->completed) {
  891. /*
  892. * Someone beat us to it for this grace period, so leave.
  893. * The race with GP start is resolved by the fact that we
  894. * hold the leaf rcu_node lock, so that the per-CPU bits
  895. * cannot yet be initialized -- so we would simply find our
  896. * CPU's bit already cleared in rcu_report_qs_rnp() if this
  897. * race occurred.
  898. */
  899. rdp->passed_quiesc = 0; /* try again later! */
  900. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  901. return;
  902. }
  903. mask = rdp->grpmask;
  904. if ((rnp->qsmask & mask) == 0) {
  905. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  906. } else {
  907. rdp->qs_pending = 0;
  908. /*
  909. * This GP can't end until cpu checks in, so all of our
  910. * callbacks can be processed during the next GP.
  911. */
  912. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  913. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  914. }
  915. }
  916. /*
  917. * Check to see if there is a new grace period of which this CPU
  918. * is not yet aware, and if so, set up local rcu_data state for it.
  919. * Otherwise, see if this CPU has just passed through its first
  920. * quiescent state for this grace period, and record that fact if so.
  921. */
  922. static void
  923. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  924. {
  925. /* If there is now a new grace period, record and return. */
  926. if (check_for_new_grace_period(rsp, rdp))
  927. return;
  928. /*
  929. * Does this CPU still need to do its part for current grace period?
  930. * If no, return and let the other CPUs do their part as well.
  931. */
  932. if (!rdp->qs_pending)
  933. return;
  934. /*
  935. * Was there a quiescent state since the beginning of the grace
  936. * period? If no, then exit and wait for the next call.
  937. */
  938. if (!rdp->passed_quiesc)
  939. return;
  940. /*
  941. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  942. * judge of that).
  943. */
  944. rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  945. }
  946. #ifdef CONFIG_HOTPLUG_CPU
  947. /*
  948. * Move a dying CPU's RCU callbacks to online CPU's callback list.
  949. * Synchronization is not required because this function executes
  950. * in stop_machine() context.
  951. */
  952. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  953. {
  954. int i;
  955. /* current DYING CPU is cleared in the cpu_online_mask */
  956. int receive_cpu = cpumask_any(cpu_online_mask);
  957. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  958. struct rcu_data *receive_rdp = per_cpu_ptr(rsp->rda, receive_cpu);
  959. if (rdp->nxtlist == NULL)
  960. return; /* irqs disabled, so comparison is stable. */
  961. *receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
  962. receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  963. receive_rdp->qlen += rdp->qlen;
  964. receive_rdp->n_cbs_adopted += rdp->qlen;
  965. rdp->n_cbs_orphaned += rdp->qlen;
  966. rdp->nxtlist = NULL;
  967. for (i = 0; i < RCU_NEXT_SIZE; i++)
  968. rdp->nxttail[i] = &rdp->nxtlist;
  969. rdp->qlen = 0;
  970. }
  971. /*
  972. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  973. * and move all callbacks from the outgoing CPU to the current one.
  974. * There can only be one CPU hotplug operation at a time, so no other
  975. * CPU can be attempting to update rcu_cpu_kthread_task.
  976. */
  977. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  978. {
  979. unsigned long flags;
  980. unsigned long mask;
  981. int need_report = 0;
  982. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  983. struct rcu_node *rnp;
  984. rcu_stop_cpu_kthread(cpu);
  985. /* Exclude any attempts to start a new grace period. */
  986. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  987. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  988. rnp = rdp->mynode; /* this is the outgoing CPU's rnp. */
  989. mask = rdp->grpmask; /* rnp->grplo is constant. */
  990. do {
  991. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  992. rnp->qsmaskinit &= ~mask;
  993. if (rnp->qsmaskinit != 0) {
  994. if (rnp != rdp->mynode)
  995. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  996. break;
  997. }
  998. if (rnp == rdp->mynode)
  999. need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  1000. else
  1001. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1002. mask = rnp->grpmask;
  1003. rnp = rnp->parent;
  1004. } while (rnp != NULL);
  1005. /*
  1006. * We still hold the leaf rcu_node structure lock here, and
  1007. * irqs are still disabled. The reason for this subterfuge is
  1008. * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
  1009. * held leads to deadlock.
  1010. */
  1011. raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  1012. rnp = rdp->mynode;
  1013. if (need_report & RCU_OFL_TASKS_NORM_GP)
  1014. rcu_report_unblock_qs_rnp(rnp, flags);
  1015. else
  1016. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1017. if (need_report & RCU_OFL_TASKS_EXP_GP)
  1018. rcu_report_exp_rnp(rsp, rnp);
  1019. rcu_node_kthread_setaffinity(rnp, -1);
  1020. }
  1021. /*
  1022. * Remove the specified CPU from the RCU hierarchy and move any pending
  1023. * callbacks that it might have to the current CPU. This code assumes
  1024. * that at least one CPU in the system will remain running at all times.
  1025. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  1026. */
  1027. static void rcu_offline_cpu(int cpu)
  1028. {
  1029. __rcu_offline_cpu(cpu, &rcu_sched_state);
  1030. __rcu_offline_cpu(cpu, &rcu_bh_state);
  1031. rcu_preempt_offline_cpu(cpu);
  1032. }
  1033. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1034. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  1035. {
  1036. }
  1037. static void rcu_offline_cpu(int cpu)
  1038. {
  1039. }
  1040. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1041. /*
  1042. * Invoke any RCU callbacks that have made it to the end of their grace
  1043. * period. Thottle as specified by rdp->blimit.
  1044. */
  1045. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  1046. {
  1047. unsigned long flags;
  1048. struct rcu_head *next, *list, **tail;
  1049. int bl, count;
  1050. /* If no callbacks are ready, just return.*/
  1051. if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
  1052. trace_rcu_batch_start(rsp->name, 0, 0);
  1053. trace_rcu_batch_end(rsp->name, 0);
  1054. return;
  1055. }
  1056. /*
  1057. * Extract the list of ready callbacks, disabling to prevent
  1058. * races with call_rcu() from interrupt handlers.
  1059. */
  1060. local_irq_save(flags);
  1061. bl = rdp->blimit;
  1062. trace_rcu_batch_start(rsp->name, rdp->qlen, bl);
  1063. list = rdp->nxtlist;
  1064. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  1065. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1066. tail = rdp->nxttail[RCU_DONE_TAIL];
  1067. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  1068. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  1069. rdp->nxttail[count] = &rdp->nxtlist;
  1070. local_irq_restore(flags);
  1071. /* Invoke callbacks. */
  1072. count = 0;
  1073. while (list) {
  1074. next = list->next;
  1075. prefetch(next);
  1076. debug_rcu_head_unqueue(list);
  1077. __rcu_reclaim(list);
  1078. list = next;
  1079. if (++count >= bl)
  1080. break;
  1081. }
  1082. local_irq_save(flags);
  1083. trace_rcu_batch_end(rsp->name, count);
  1084. /* Update count, and requeue any remaining callbacks. */
  1085. rdp->qlen -= count;
  1086. rdp->n_cbs_invoked += count;
  1087. if (list != NULL) {
  1088. *tail = rdp->nxtlist;
  1089. rdp->nxtlist = list;
  1090. for (count = 0; count < RCU_NEXT_SIZE; count++)
  1091. if (&rdp->nxtlist == rdp->nxttail[count])
  1092. rdp->nxttail[count] = tail;
  1093. else
  1094. break;
  1095. }
  1096. /* Reinstate batch limit if we have worked down the excess. */
  1097. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  1098. rdp->blimit = blimit;
  1099. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  1100. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  1101. rdp->qlen_last_fqs_check = 0;
  1102. rdp->n_force_qs_snap = rsp->n_force_qs;
  1103. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  1104. rdp->qlen_last_fqs_check = rdp->qlen;
  1105. local_irq_restore(flags);
  1106. /* Re-raise the RCU softirq if there are callbacks remaining. */
  1107. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1108. invoke_rcu_core();
  1109. }
  1110. /*
  1111. * Check to see if this CPU is in a non-context-switch quiescent state
  1112. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  1113. * Also schedule the RCU softirq handler.
  1114. *
  1115. * This function must be called with hardirqs disabled. It is normally
  1116. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  1117. * false, there is no point in invoking rcu_check_callbacks().
  1118. */
  1119. void rcu_check_callbacks(int cpu, int user)
  1120. {
  1121. trace_rcu_utilization("Start scheduler-tick");
  1122. if (user ||
  1123. (idle_cpu(cpu) && rcu_scheduler_active &&
  1124. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  1125. /*
  1126. * Get here if this CPU took its interrupt from user
  1127. * mode or from the idle loop, and if this is not a
  1128. * nested interrupt. In this case, the CPU is in
  1129. * a quiescent state, so note it.
  1130. *
  1131. * No memory barrier is required here because both
  1132. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  1133. * variables that other CPUs neither access nor modify,
  1134. * at least not while the corresponding CPU is online.
  1135. */
  1136. rcu_sched_qs(cpu);
  1137. rcu_bh_qs(cpu);
  1138. } else if (!in_softirq()) {
  1139. /*
  1140. * Get here if this CPU did not take its interrupt from
  1141. * softirq, in other words, if it is not interrupting
  1142. * a rcu_bh read-side critical section. This is an _bh
  1143. * critical section, so note it.
  1144. */
  1145. rcu_bh_qs(cpu);
  1146. }
  1147. rcu_preempt_check_callbacks(cpu);
  1148. if (rcu_pending(cpu))
  1149. invoke_rcu_core();
  1150. trace_rcu_utilization("End scheduler-tick");
  1151. }
  1152. #ifdef CONFIG_SMP
  1153. /*
  1154. * Scan the leaf rcu_node structures, processing dyntick state for any that
  1155. * have not yet encountered a quiescent state, using the function specified.
  1156. * Also initiate boosting for any threads blocked on the root rcu_node.
  1157. *
  1158. * The caller must have suppressed start of new grace periods.
  1159. */
  1160. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
  1161. {
  1162. unsigned long bit;
  1163. int cpu;
  1164. unsigned long flags;
  1165. unsigned long mask;
  1166. struct rcu_node *rnp;
  1167. rcu_for_each_leaf_node(rsp, rnp) {
  1168. mask = 0;
  1169. raw_spin_lock_irqsave(&rnp->lock, flags);
  1170. if (!rcu_gp_in_progress(rsp)) {
  1171. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1172. return;
  1173. }
  1174. if (rnp->qsmask == 0) {
  1175. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  1176. continue;
  1177. }
  1178. cpu = rnp->grplo;
  1179. bit = 1;
  1180. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1181. if ((rnp->qsmask & bit) != 0 &&
  1182. f(per_cpu_ptr(rsp->rda, cpu)))
  1183. mask |= bit;
  1184. }
  1185. if (mask != 0) {
  1186. /* rcu_report_qs_rnp() releases rnp->lock. */
  1187. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1188. continue;
  1189. }
  1190. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1191. }
  1192. rnp = rcu_get_root(rsp);
  1193. if (rnp->qsmask == 0) {
  1194. raw_spin_lock_irqsave(&rnp->lock, flags);
  1195. rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
  1196. }
  1197. }
  1198. /*
  1199. * Force quiescent states on reluctant CPUs, and also detect which
  1200. * CPUs are in dyntick-idle mode.
  1201. */
  1202. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1203. {
  1204. unsigned long flags;
  1205. struct rcu_node *rnp = rcu_get_root(rsp);
  1206. trace_rcu_utilization("Start fqs");
  1207. if (!rcu_gp_in_progress(rsp)) {
  1208. trace_rcu_utilization("End fqs");
  1209. return; /* No grace period in progress, nothing to force. */
  1210. }
  1211. if (!raw_spin_trylock_irqsave(&rsp->fqslock, flags)) {
  1212. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  1213. trace_rcu_utilization("End fqs");
  1214. return; /* Someone else is already on the job. */
  1215. }
  1216. if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
  1217. goto unlock_fqs_ret; /* no emergency and done recently. */
  1218. rsp->n_force_qs++;
  1219. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1220. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  1221. if(!rcu_gp_in_progress(rsp)) {
  1222. rsp->n_force_qs_ngp++;
  1223. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1224. goto unlock_fqs_ret; /* no GP in progress, time updated. */
  1225. }
  1226. rsp->fqs_active = 1;
  1227. switch (rsp->signaled) {
  1228. case RCU_GP_IDLE:
  1229. case RCU_GP_INIT:
  1230. break; /* grace period idle or initializing, ignore. */
  1231. case RCU_SAVE_DYNTICK:
  1232. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  1233. break; /* So gcc recognizes the dead code. */
  1234. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1235. /* Record dyntick-idle state. */
  1236. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1237. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1238. if (rcu_gp_in_progress(rsp))
  1239. rsp->signaled = RCU_FORCE_QS;
  1240. break;
  1241. case RCU_FORCE_QS:
  1242. /* Check dyntick-idle state, send IPI to laggarts. */
  1243. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1244. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1245. /* Leave state in case more forcing is required. */
  1246. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1247. break;
  1248. }
  1249. rsp->fqs_active = 0;
  1250. if (rsp->fqs_need_gp) {
  1251. raw_spin_unlock(&rsp->fqslock); /* irqs remain disabled */
  1252. rsp->fqs_need_gp = 0;
  1253. rcu_start_gp(rsp, flags); /* releases rnp->lock */
  1254. trace_rcu_utilization("End fqs");
  1255. return;
  1256. }
  1257. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1258. unlock_fqs_ret:
  1259. raw_spin_unlock_irqrestore(&rsp->fqslock, flags);
  1260. trace_rcu_utilization("End fqs");
  1261. }
  1262. #else /* #ifdef CONFIG_SMP */
  1263. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1264. {
  1265. set_need_resched();
  1266. }
  1267. #endif /* #else #ifdef CONFIG_SMP */
  1268. /*
  1269. * This does the RCU processing work from softirq context for the
  1270. * specified rcu_state and rcu_data structures. This may be called
  1271. * only from the CPU to whom the rdp belongs.
  1272. */
  1273. static void
  1274. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1275. {
  1276. unsigned long flags;
  1277. WARN_ON_ONCE(rdp->beenonline == 0);
  1278. /*
  1279. * If an RCU GP has gone long enough, go check for dyntick
  1280. * idle CPUs and, if needed, send resched IPIs.
  1281. */
  1282. if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1283. force_quiescent_state(rsp, 1);
  1284. /*
  1285. * Advance callbacks in response to end of earlier grace
  1286. * period that some other CPU ended.
  1287. */
  1288. rcu_process_gp_end(rsp, rdp);
  1289. /* Update RCU state based on any recent quiescent states. */
  1290. rcu_check_quiescent_state(rsp, rdp);
  1291. /* Does this CPU require a not-yet-started grace period? */
  1292. if (cpu_needs_another_gp(rsp, rdp)) {
  1293. raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1294. rcu_start_gp(rsp, flags); /* releases above lock */
  1295. }
  1296. /* If there are callbacks ready, invoke them. */
  1297. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1298. invoke_rcu_callbacks(rsp, rdp);
  1299. }
  1300. /*
  1301. * Do softirq processing for the current CPU.
  1302. */
  1303. static void rcu_process_callbacks(struct softirq_action *unused)
  1304. {
  1305. trace_rcu_utilization("Start RCU core");
  1306. __rcu_process_callbacks(&rcu_sched_state,
  1307. &__get_cpu_var(rcu_sched_data));
  1308. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1309. rcu_preempt_process_callbacks();
  1310. /* If we are last CPU on way to dyntick-idle mode, accelerate it. */
  1311. rcu_needs_cpu_flush();
  1312. trace_rcu_utilization("End RCU core");
  1313. }
  1314. /*
  1315. * Wake up the current CPU's kthread. This replaces raise_softirq()
  1316. * in earlier versions of RCU. Note that because we are running on
  1317. * the current CPU with interrupts disabled, the rcu_cpu_kthread_task
  1318. * cannot disappear out from under us.
  1319. */
  1320. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1321. {
  1322. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  1323. return;
  1324. if (likely(!rsp->boost)) {
  1325. rcu_do_batch(rsp, rdp);
  1326. return;
  1327. }
  1328. invoke_rcu_callbacks_kthread();
  1329. }
  1330. static void invoke_rcu_core(void)
  1331. {
  1332. raise_softirq(RCU_SOFTIRQ);
  1333. }
  1334. static void
  1335. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1336. struct rcu_state *rsp)
  1337. {
  1338. unsigned long flags;
  1339. struct rcu_data *rdp;
  1340. debug_rcu_head_queue(head);
  1341. head->func = func;
  1342. head->next = NULL;
  1343. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1344. /*
  1345. * Opportunistically note grace-period endings and beginnings.
  1346. * Note that we might see a beginning right after we see an
  1347. * end, but never vice versa, since this CPU has to pass through
  1348. * a quiescent state betweentimes.
  1349. */
  1350. local_irq_save(flags);
  1351. rdp = this_cpu_ptr(rsp->rda);
  1352. /* Add the callback to our list. */
  1353. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1354. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1355. rdp->qlen++;
  1356. /* If interrupts were disabled, don't dive into RCU core. */
  1357. if (irqs_disabled_flags(flags)) {
  1358. local_irq_restore(flags);
  1359. return;
  1360. }
  1361. /*
  1362. * Force the grace period if too many callbacks or too long waiting.
  1363. * Enforce hysteresis, and don't invoke force_quiescent_state()
  1364. * if some other CPU has recently done so. Also, don't bother
  1365. * invoking force_quiescent_state() if the newly enqueued callback
  1366. * is the only one waiting for a grace period to complete.
  1367. */
  1368. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  1369. /* Are we ignoring a completed grace period? */
  1370. rcu_process_gp_end(rsp, rdp);
  1371. check_for_new_grace_period(rsp, rdp);
  1372. /* Start a new grace period if one not already started. */
  1373. if (!rcu_gp_in_progress(rsp)) {
  1374. unsigned long nestflag;
  1375. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1376. raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
  1377. rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
  1378. } else {
  1379. /* Give the grace period a kick. */
  1380. rdp->blimit = LONG_MAX;
  1381. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  1382. *rdp->nxttail[RCU_DONE_TAIL] != head)
  1383. force_quiescent_state(rsp, 0);
  1384. rdp->n_force_qs_snap = rsp->n_force_qs;
  1385. rdp->qlen_last_fqs_check = rdp->qlen;
  1386. }
  1387. } else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1388. force_quiescent_state(rsp, 1);
  1389. local_irq_restore(flags);
  1390. }
  1391. /*
  1392. * Queue an RCU-sched callback for invocation after a grace period.
  1393. */
  1394. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1395. {
  1396. __call_rcu(head, func, &rcu_sched_state);
  1397. }
  1398. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1399. /*
  1400. * Queue an RCU for invocation after a quicker grace period.
  1401. */
  1402. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1403. {
  1404. __call_rcu(head, func, &rcu_bh_state);
  1405. }
  1406. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1407. /**
  1408. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  1409. *
  1410. * Control will return to the caller some time after a full rcu-sched
  1411. * grace period has elapsed, in other words after all currently executing
  1412. * rcu-sched read-side critical sections have completed. These read-side
  1413. * critical sections are delimited by rcu_read_lock_sched() and
  1414. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  1415. * local_irq_disable(), and so on may be used in place of
  1416. * rcu_read_lock_sched().
  1417. *
  1418. * This means that all preempt_disable code sequences, including NMI and
  1419. * hardware-interrupt handlers, in progress on entry will have completed
  1420. * before this primitive returns. However, this does not guarantee that
  1421. * softirq handlers will have completed, since in some kernels, these
  1422. * handlers can run in process context, and can block.
  1423. *
  1424. * This primitive provides the guarantees made by the (now removed)
  1425. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  1426. * guarantees that rcu_read_lock() sections will have completed.
  1427. * In "classic RCU", these two guarantees happen to be one and
  1428. * the same, but can differ in realtime RCU implementations.
  1429. */
  1430. void synchronize_sched(void)
  1431. {
  1432. if (rcu_blocking_is_gp())
  1433. return;
  1434. wait_rcu_gp(call_rcu_sched);
  1435. }
  1436. EXPORT_SYMBOL_GPL(synchronize_sched);
  1437. /**
  1438. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  1439. *
  1440. * Control will return to the caller some time after a full rcu_bh grace
  1441. * period has elapsed, in other words after all currently executing rcu_bh
  1442. * read-side critical sections have completed. RCU read-side critical
  1443. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  1444. * and may be nested.
  1445. */
  1446. void synchronize_rcu_bh(void)
  1447. {
  1448. if (rcu_blocking_is_gp())
  1449. return;
  1450. wait_rcu_gp(call_rcu_bh);
  1451. }
  1452. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  1453. /*
  1454. * Check to see if there is any immediate RCU-related work to be done
  1455. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1456. * The checks are in order of increasing expense: checks that can be
  1457. * carried out against CPU-local state are performed first. However,
  1458. * we must check for CPU stalls first, else we might not get a chance.
  1459. */
  1460. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1461. {
  1462. struct rcu_node *rnp = rdp->mynode;
  1463. rdp->n_rcu_pending++;
  1464. /* Check for CPU stalls, if enabled. */
  1465. check_cpu_stall(rsp, rdp);
  1466. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1467. if (rdp->qs_pending && !rdp->passed_quiesc) {
  1468. /*
  1469. * If force_quiescent_state() coming soon and this CPU
  1470. * needs a quiescent state, and this is either RCU-sched
  1471. * or RCU-bh, force a local reschedule.
  1472. */
  1473. rdp->n_rp_qs_pending++;
  1474. if (!rdp->preemptible &&
  1475. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs) - 1,
  1476. jiffies))
  1477. set_need_resched();
  1478. } else if (rdp->qs_pending && rdp->passed_quiesc) {
  1479. rdp->n_rp_report_qs++;
  1480. return 1;
  1481. }
  1482. /* Does this CPU have callbacks ready to invoke? */
  1483. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  1484. rdp->n_rp_cb_ready++;
  1485. return 1;
  1486. }
  1487. /* Has RCU gone idle with this CPU needing another grace period? */
  1488. if (cpu_needs_another_gp(rsp, rdp)) {
  1489. rdp->n_rp_cpu_needs_gp++;
  1490. return 1;
  1491. }
  1492. /* Has another RCU grace period completed? */
  1493. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  1494. rdp->n_rp_gp_completed++;
  1495. return 1;
  1496. }
  1497. /* Has a new RCU grace period started? */
  1498. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  1499. rdp->n_rp_gp_started++;
  1500. return 1;
  1501. }
  1502. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1503. if (rcu_gp_in_progress(rsp) &&
  1504. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
  1505. rdp->n_rp_need_fqs++;
  1506. return 1;
  1507. }
  1508. /* nothing to do */
  1509. rdp->n_rp_need_nothing++;
  1510. return 0;
  1511. }
  1512. /*
  1513. * Check to see if there is any immediate RCU-related work to be done
  1514. * by the current CPU, returning 1 if so. This function is part of the
  1515. * RCU implementation; it is -not- an exported member of the RCU API.
  1516. */
  1517. static int rcu_pending(int cpu)
  1518. {
  1519. return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
  1520. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
  1521. rcu_preempt_pending(cpu);
  1522. }
  1523. /*
  1524. * Check to see if any future RCU-related work will need to be done
  1525. * by the current CPU, even if none need be done immediately, returning
  1526. * 1 if so.
  1527. */
  1528. static int rcu_needs_cpu_quick_check(int cpu)
  1529. {
  1530. /* RCU callbacks either ready or pending? */
  1531. return per_cpu(rcu_sched_data, cpu).nxtlist ||
  1532. per_cpu(rcu_bh_data, cpu).nxtlist ||
  1533. rcu_preempt_needs_cpu(cpu);
  1534. }
  1535. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  1536. static atomic_t rcu_barrier_cpu_count;
  1537. static DEFINE_MUTEX(rcu_barrier_mutex);
  1538. static struct completion rcu_barrier_completion;
  1539. static void rcu_barrier_callback(struct rcu_head *notused)
  1540. {
  1541. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1542. complete(&rcu_barrier_completion);
  1543. }
  1544. /*
  1545. * Called with preemption disabled, and from cross-cpu IRQ context.
  1546. */
  1547. static void rcu_barrier_func(void *type)
  1548. {
  1549. int cpu = smp_processor_id();
  1550. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  1551. void (*call_rcu_func)(struct rcu_head *head,
  1552. void (*func)(struct rcu_head *head));
  1553. atomic_inc(&rcu_barrier_cpu_count);
  1554. call_rcu_func = type;
  1555. call_rcu_func(head, rcu_barrier_callback);
  1556. }
  1557. /*
  1558. * Orchestrate the specified type of RCU barrier, waiting for all
  1559. * RCU callbacks of the specified type to complete.
  1560. */
  1561. static void _rcu_barrier(struct rcu_state *rsp,
  1562. void (*call_rcu_func)(struct rcu_head *head,
  1563. void (*func)(struct rcu_head *head)))
  1564. {
  1565. BUG_ON(in_interrupt());
  1566. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  1567. mutex_lock(&rcu_barrier_mutex);
  1568. init_completion(&rcu_barrier_completion);
  1569. /*
  1570. * Initialize rcu_barrier_cpu_count to 1, then invoke
  1571. * rcu_barrier_func() on each CPU, so that each CPU also has
  1572. * incremented rcu_barrier_cpu_count. Only then is it safe to
  1573. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  1574. * might complete its grace period before all of the other CPUs
  1575. * did their increment, causing this function to return too
  1576. * early. Note that on_each_cpu() disables irqs, which prevents
  1577. * any CPUs from coming online or going offline until each online
  1578. * CPU has queued its RCU-barrier callback.
  1579. */
  1580. atomic_set(&rcu_barrier_cpu_count, 1);
  1581. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  1582. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1583. complete(&rcu_barrier_completion);
  1584. wait_for_completion(&rcu_barrier_completion);
  1585. mutex_unlock(&rcu_barrier_mutex);
  1586. }
  1587. /**
  1588. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  1589. */
  1590. void rcu_barrier_bh(void)
  1591. {
  1592. _rcu_barrier(&rcu_bh_state, call_rcu_bh);
  1593. }
  1594. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  1595. /**
  1596. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  1597. */
  1598. void rcu_barrier_sched(void)
  1599. {
  1600. _rcu_barrier(&rcu_sched_state, call_rcu_sched);
  1601. }
  1602. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  1603. /*
  1604. * Do boot-time initialization of a CPU's per-CPU RCU data.
  1605. */
  1606. static void __init
  1607. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  1608. {
  1609. unsigned long flags;
  1610. int i;
  1611. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1612. struct rcu_node *rnp = rcu_get_root(rsp);
  1613. /* Set up local state, ensuring consistent view of global state. */
  1614. raw_spin_lock_irqsave(&rnp->lock, flags);
  1615. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1616. rdp->nxtlist = NULL;
  1617. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1618. rdp->nxttail[i] = &rdp->nxtlist;
  1619. rdp->qlen = 0;
  1620. #ifdef CONFIG_NO_HZ
  1621. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1622. #endif /* #ifdef CONFIG_NO_HZ */
  1623. rdp->cpu = cpu;
  1624. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1625. }
  1626. /*
  1627. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  1628. * offline event can be happening at a given time. Note also that we
  1629. * can accept some slop in the rsp->completed access due to the fact
  1630. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  1631. */
  1632. static void __cpuinit
  1633. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
  1634. {
  1635. unsigned long flags;
  1636. unsigned long mask;
  1637. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1638. struct rcu_node *rnp = rcu_get_root(rsp);
  1639. /* Set up local state, ensuring consistent view of global state. */
  1640. raw_spin_lock_irqsave(&rnp->lock, flags);
  1641. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1642. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1643. rdp->beenonline = 1; /* We have now been online. */
  1644. rdp->preemptible = preemptible;
  1645. rdp->qlen_last_fqs_check = 0;
  1646. rdp->n_force_qs_snap = rsp->n_force_qs;
  1647. rdp->blimit = blimit;
  1648. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1649. /*
  1650. * A new grace period might start here. If so, we won't be part
  1651. * of it, but that is OK, as we are currently in a quiescent state.
  1652. */
  1653. /* Exclude any attempts to start a new GP on large systems. */
  1654. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1655. /* Add CPU to rcu_node bitmasks. */
  1656. rnp = rdp->mynode;
  1657. mask = rdp->grpmask;
  1658. do {
  1659. /* Exclude any attempts to start a new GP on small systems. */
  1660. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1661. rnp->qsmaskinit |= mask;
  1662. mask = rnp->grpmask;
  1663. if (rnp == rdp->mynode) {
  1664. rdp->gpnum = rnp->completed; /* if GP in progress... */
  1665. rdp->completed = rnp->completed;
  1666. rdp->passed_quiesc_completed = rnp->completed - 1;
  1667. }
  1668. raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
  1669. rnp = rnp->parent;
  1670. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1671. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  1672. }
  1673. static void __cpuinit rcu_prepare_cpu(int cpu)
  1674. {
  1675. rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
  1676. rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
  1677. rcu_preempt_init_percpu_data(cpu);
  1678. }
  1679. /*
  1680. * Handle CPU online/offline notification events.
  1681. */
  1682. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1683. unsigned long action, void *hcpu)
  1684. {
  1685. long cpu = (long)hcpu;
  1686. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1687. struct rcu_node *rnp = rdp->mynode;
  1688. trace_rcu_utilization("Start CPU hotplug");
  1689. switch (action) {
  1690. case CPU_UP_PREPARE:
  1691. case CPU_UP_PREPARE_FROZEN:
  1692. rcu_prepare_cpu(cpu);
  1693. rcu_prepare_kthreads(cpu);
  1694. break;
  1695. case CPU_ONLINE:
  1696. case CPU_DOWN_FAILED:
  1697. rcu_node_kthread_setaffinity(rnp, -1);
  1698. rcu_cpu_kthread_setrt(cpu, 1);
  1699. break;
  1700. case CPU_DOWN_PREPARE:
  1701. rcu_node_kthread_setaffinity(rnp, cpu);
  1702. rcu_cpu_kthread_setrt(cpu, 0);
  1703. break;
  1704. case CPU_DYING:
  1705. case CPU_DYING_FROZEN:
  1706. /*
  1707. * The whole machine is "stopped" except this CPU, so we can
  1708. * touch any data without introducing corruption. We send the
  1709. * dying CPU's callbacks to an arbitrarily chosen online CPU.
  1710. */
  1711. rcu_send_cbs_to_online(&rcu_bh_state);
  1712. rcu_send_cbs_to_online(&rcu_sched_state);
  1713. rcu_preempt_send_cbs_to_online();
  1714. break;
  1715. case CPU_DEAD:
  1716. case CPU_DEAD_FROZEN:
  1717. case CPU_UP_CANCELED:
  1718. case CPU_UP_CANCELED_FROZEN:
  1719. rcu_offline_cpu(cpu);
  1720. break;
  1721. default:
  1722. break;
  1723. }
  1724. trace_rcu_utilization("End CPU hotplug");
  1725. return NOTIFY_OK;
  1726. }
  1727. /*
  1728. * This function is invoked towards the end of the scheduler's initialization
  1729. * process. Before this is called, the idle task might contain
  1730. * RCU read-side critical sections (during which time, this idle
  1731. * task is booting the system). After this function is called, the
  1732. * idle tasks are prohibited from containing RCU read-side critical
  1733. * sections. This function also enables RCU lockdep checking.
  1734. */
  1735. void rcu_scheduler_starting(void)
  1736. {
  1737. WARN_ON(num_online_cpus() != 1);
  1738. WARN_ON(nr_context_switches() > 0);
  1739. rcu_scheduler_active = 1;
  1740. }
  1741. /*
  1742. * Compute the per-level fanout, either using the exact fanout specified
  1743. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  1744. */
  1745. #ifdef CONFIG_RCU_FANOUT_EXACT
  1746. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1747. {
  1748. int i;
  1749. for (i = NUM_RCU_LVLS - 1; i > 0; i--)
  1750. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  1751. rsp->levelspread[0] = RCU_FANOUT_LEAF;
  1752. }
  1753. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  1754. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1755. {
  1756. int ccur;
  1757. int cprv;
  1758. int i;
  1759. cprv = NR_CPUS;
  1760. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1761. ccur = rsp->levelcnt[i];
  1762. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  1763. cprv = ccur;
  1764. }
  1765. }
  1766. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  1767. /*
  1768. * Helper function for rcu_init() that initializes one rcu_state structure.
  1769. */
  1770. static void __init rcu_init_one(struct rcu_state *rsp,
  1771. struct rcu_data __percpu *rda)
  1772. {
  1773. static char *buf[] = { "rcu_node_level_0",
  1774. "rcu_node_level_1",
  1775. "rcu_node_level_2",
  1776. "rcu_node_level_3" }; /* Match MAX_RCU_LVLS */
  1777. int cpustride = 1;
  1778. int i;
  1779. int j;
  1780. struct rcu_node *rnp;
  1781. BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  1782. /* Initialize the level-tracking arrays. */
  1783. for (i = 1; i < NUM_RCU_LVLS; i++)
  1784. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  1785. rcu_init_levelspread(rsp);
  1786. /* Initialize the elements themselves, starting from the leaves. */
  1787. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1788. cpustride *= rsp->levelspread[i];
  1789. rnp = rsp->level[i];
  1790. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  1791. raw_spin_lock_init(&rnp->lock);
  1792. lockdep_set_class_and_name(&rnp->lock,
  1793. &rcu_node_class[i], buf[i]);
  1794. rnp->gpnum = 0;
  1795. rnp->qsmask = 0;
  1796. rnp->qsmaskinit = 0;
  1797. rnp->grplo = j * cpustride;
  1798. rnp->grphi = (j + 1) * cpustride - 1;
  1799. if (rnp->grphi >= NR_CPUS)
  1800. rnp->grphi = NR_CPUS - 1;
  1801. if (i == 0) {
  1802. rnp->grpnum = 0;
  1803. rnp->grpmask = 0;
  1804. rnp->parent = NULL;
  1805. } else {
  1806. rnp->grpnum = j % rsp->levelspread[i - 1];
  1807. rnp->grpmask = 1UL << rnp->grpnum;
  1808. rnp->parent = rsp->level[i - 1] +
  1809. j / rsp->levelspread[i - 1];
  1810. }
  1811. rnp->level = i;
  1812. INIT_LIST_HEAD(&rnp->blkd_tasks);
  1813. }
  1814. }
  1815. rsp->rda = rda;
  1816. rnp = rsp->level[NUM_RCU_LVLS - 1];
  1817. for_each_possible_cpu(i) {
  1818. while (i > rnp->grphi)
  1819. rnp++;
  1820. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  1821. rcu_boot_init_percpu_data(i, rsp);
  1822. }
  1823. }
  1824. void __init rcu_init(void)
  1825. {
  1826. int cpu;
  1827. rcu_bootup_announce();
  1828. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  1829. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  1830. __rcu_init_preempt();
  1831. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1832. /*
  1833. * We don't need protection against CPU-hotplug here because
  1834. * this is called early in boot, before either interrupts
  1835. * or the scheduler are operational.
  1836. */
  1837. cpu_notifier(rcu_cpu_notify, 0);
  1838. for_each_online_cpu(cpu)
  1839. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  1840. check_cpu_stall_init();
  1841. }
  1842. #include "rcutree_plugin.h"