rcutree.c 72 KB

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