rcutree.c 71 KB

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