rcutree.c 70 KB

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