rcutree.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  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 <linux/delay.h>
  54. #include <linux/stop_machine.h>
  55. #include <linux/random.h>
  56. #include <linux/ftrace_event.h>
  57. #include "rcutree.h"
  58. #include <trace/events/rcu.h>
  59. #include "rcu.h"
  60. /*
  61. * Strings used in tracepoints need to be exported via the
  62. * tracing system such that tools like perf and trace-cmd can
  63. * translate the string address pointers to actual text.
  64. */
  65. #define TPS(x) tracepoint_string(x)
  66. /* Data structures. */
  67. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  68. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  69. /*
  70. * In order to export the rcu_state name to the tracing tools, it
  71. * needs to be added in the __tracepoint_string section.
  72. * This requires defining a separate variable tp_<sname>_varname
  73. * that points to the string being used, and this will allow
  74. * the tracing userspace tools to be able to decipher the string
  75. * address to the matching string.
  76. */
  77. #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
  78. static char sname##_varname[] = #sname; \
  79. static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname; \
  80. struct rcu_state sname##_state = { \
  81. .level = { &sname##_state.node[0] }, \
  82. .call = cr, \
  83. .fqs_state = RCU_GP_IDLE, \
  84. .gpnum = 0UL - 300UL, \
  85. .completed = 0UL - 300UL, \
  86. .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
  87. .orphan_nxttail = &sname##_state.orphan_nxtlist, \
  88. .orphan_donetail = &sname##_state.orphan_donelist, \
  89. .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
  90. .onoff_mutex = __MUTEX_INITIALIZER(sname##_state.onoff_mutex), \
  91. .name = sname##_varname, \
  92. .abbr = sabbr, \
  93. }; \
  94. DEFINE_PER_CPU(struct rcu_data, sname##_data)
  95. RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
  96. RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
  97. static struct rcu_state *rcu_state;
  98. LIST_HEAD(rcu_struct_flavors);
  99. /* Increase (but not decrease) the CONFIG_RCU_FANOUT_LEAF at boot time. */
  100. static int rcu_fanout_leaf = CONFIG_RCU_FANOUT_LEAF;
  101. module_param(rcu_fanout_leaf, int, 0444);
  102. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  103. static int num_rcu_lvl[] = { /* Number of rcu_nodes at specified level. */
  104. NUM_RCU_LVL_0,
  105. NUM_RCU_LVL_1,
  106. NUM_RCU_LVL_2,
  107. NUM_RCU_LVL_3,
  108. NUM_RCU_LVL_4,
  109. };
  110. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  111. /*
  112. * The rcu_scheduler_active variable transitions from zero to one just
  113. * before the first task is spawned. So when this variable is zero, RCU
  114. * can assume that there is but one task, allowing RCU to (for example)
  115. * optimize synchronize_sched() to a simple barrier(). When this variable
  116. * is one, RCU must actually do all the hard work required to detect real
  117. * grace periods. This variable is also used to suppress boot-time false
  118. * positives from lockdep-RCU error checking.
  119. */
  120. int rcu_scheduler_active __read_mostly;
  121. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  122. /*
  123. * The rcu_scheduler_fully_active variable transitions from zero to one
  124. * during the early_initcall() processing, which is after the scheduler
  125. * is capable of creating new tasks. So RCU processing (for example,
  126. * creating tasks for RCU priority boosting) must be delayed until after
  127. * rcu_scheduler_fully_active transitions from zero to one. We also
  128. * currently delay invocation of any RCU callbacks until after this point.
  129. *
  130. * It might later prove better for people registering RCU callbacks during
  131. * early boot to take responsibility for these callbacks, but one step at
  132. * a time.
  133. */
  134. static int rcu_scheduler_fully_active __read_mostly;
  135. #ifdef CONFIG_RCU_BOOST
  136. /*
  137. * Control variables for per-CPU and per-rcu_node kthreads. These
  138. * handle all flavors of RCU.
  139. */
  140. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  141. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  142. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  143. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  144. #endif /* #ifdef CONFIG_RCU_BOOST */
  145. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  146. static void invoke_rcu_core(void);
  147. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  148. /*
  149. * Track the rcutorture test sequence number and the update version
  150. * number within a given test. The rcutorture_testseq is incremented
  151. * on every rcutorture module load and unload, so has an odd value
  152. * when a test is running. The rcutorture_vernum is set to zero
  153. * when rcutorture starts and is incremented on each rcutorture update.
  154. * These variables enable correlating rcutorture output with the
  155. * RCU tracing information.
  156. */
  157. unsigned long rcutorture_testseq;
  158. unsigned long rcutorture_vernum;
  159. /*
  160. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  161. * permit this function to be invoked without holding the root rcu_node
  162. * structure's ->lock, but of course results can be subject to change.
  163. */
  164. static int rcu_gp_in_progress(struct rcu_state *rsp)
  165. {
  166. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  167. }
  168. /*
  169. * Note a quiescent state. Because we do not need to know
  170. * how many quiescent states passed, just if there was at least
  171. * one since the start of the grace period, this just sets a flag.
  172. * The caller must have disabled preemption.
  173. */
  174. void rcu_sched_qs(int cpu)
  175. {
  176. struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
  177. if (rdp->passed_quiesce == 0)
  178. trace_rcu_grace_period(TPS("rcu_sched"), rdp->gpnum, TPS("cpuqs"));
  179. rdp->passed_quiesce = 1;
  180. }
  181. void rcu_bh_qs(int cpu)
  182. {
  183. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  184. if (rdp->passed_quiesce == 0)
  185. trace_rcu_grace_period(TPS("rcu_bh"), rdp->gpnum, TPS("cpuqs"));
  186. rdp->passed_quiesce = 1;
  187. }
  188. /*
  189. * Note a context switch. This is a quiescent state for RCU-sched,
  190. * and requires special handling for preemptible RCU.
  191. * The caller must have disabled preemption.
  192. */
  193. void rcu_note_context_switch(int cpu)
  194. {
  195. trace_rcu_utilization(TPS("Start context switch"));
  196. rcu_sched_qs(cpu);
  197. rcu_preempt_note_context_switch(cpu);
  198. trace_rcu_utilization(TPS("End context switch"));
  199. }
  200. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  201. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  202. .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
  203. .dynticks = ATOMIC_INIT(1),
  204. };
  205. static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
  206. static long qhimark = 10000; /* If this many pending, ignore blimit. */
  207. static long qlowmark = 100; /* Once only this many pending, use blimit. */
  208. module_param(blimit, long, 0444);
  209. module_param(qhimark, long, 0444);
  210. module_param(qlowmark, long, 0444);
  211. static ulong jiffies_till_first_fqs = ULONG_MAX;
  212. static ulong jiffies_till_next_fqs = ULONG_MAX;
  213. module_param(jiffies_till_first_fqs, ulong, 0644);
  214. module_param(jiffies_till_next_fqs, ulong, 0644);
  215. static void rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  216. struct rcu_data *rdp);
  217. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *));
  218. static void force_quiescent_state(struct rcu_state *rsp);
  219. static int rcu_pending(int cpu);
  220. /*
  221. * Return the number of RCU-sched batches processed thus far for debug & stats.
  222. */
  223. long rcu_batches_completed_sched(void)
  224. {
  225. return rcu_sched_state.completed;
  226. }
  227. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  228. /*
  229. * Return the number of RCU BH batches processed thus far for debug & stats.
  230. */
  231. long rcu_batches_completed_bh(void)
  232. {
  233. return rcu_bh_state.completed;
  234. }
  235. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  236. /*
  237. * Force a quiescent state for RCU BH.
  238. */
  239. void rcu_bh_force_quiescent_state(void)
  240. {
  241. force_quiescent_state(&rcu_bh_state);
  242. }
  243. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  244. /*
  245. * Record the number of times rcutorture tests have been initiated and
  246. * terminated. This information allows the debugfs tracing stats to be
  247. * correlated to the rcutorture messages, even when the rcutorture module
  248. * is being repeatedly loaded and unloaded. In other words, we cannot
  249. * store this state in rcutorture itself.
  250. */
  251. void rcutorture_record_test_transition(void)
  252. {
  253. rcutorture_testseq++;
  254. rcutorture_vernum = 0;
  255. }
  256. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  257. /*
  258. * Record the number of writer passes through the current rcutorture test.
  259. * This is also used to correlate debugfs tracing stats with the rcutorture
  260. * messages.
  261. */
  262. void rcutorture_record_progress(unsigned long vernum)
  263. {
  264. rcutorture_vernum++;
  265. }
  266. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  267. /*
  268. * Force a quiescent state for RCU-sched.
  269. */
  270. void rcu_sched_force_quiescent_state(void)
  271. {
  272. force_quiescent_state(&rcu_sched_state);
  273. }
  274. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  275. /*
  276. * Does the CPU have callbacks ready to be invoked?
  277. */
  278. static int
  279. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  280. {
  281. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
  282. rdp->nxttail[RCU_DONE_TAIL] != NULL;
  283. }
  284. /*
  285. * Does the current CPU require a not-yet-started grace period?
  286. * The caller must have disabled interrupts to prevent races with
  287. * normal callback registry.
  288. */
  289. static int
  290. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  291. {
  292. int i;
  293. if (rcu_gp_in_progress(rsp))
  294. return 0; /* No, a grace period is already in progress. */
  295. if (rcu_nocb_needs_gp(rsp))
  296. return 1; /* Yes, a no-CBs CPU needs one. */
  297. if (!rdp->nxttail[RCU_NEXT_TAIL])
  298. return 0; /* No, this is a no-CBs (or offline) CPU. */
  299. if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
  300. return 1; /* Yes, this CPU has newly registered callbacks. */
  301. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
  302. if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
  303. ULONG_CMP_LT(ACCESS_ONCE(rsp->completed),
  304. rdp->nxtcompleted[i]))
  305. return 1; /* Yes, CBs for future grace period. */
  306. return 0; /* No grace period needed. */
  307. }
  308. /*
  309. * Return the root node of the specified rcu_state structure.
  310. */
  311. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  312. {
  313. return &rsp->node[0];
  314. }
  315. /*
  316. * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
  317. *
  318. * If the new value of the ->dynticks_nesting counter now is zero,
  319. * we really have entered idle, and must do the appropriate accounting.
  320. * The caller must have disabled interrupts.
  321. */
  322. static void rcu_eqs_enter_common(struct rcu_dynticks *rdtp, long long oldval,
  323. bool user)
  324. {
  325. trace_rcu_dyntick(TPS("Start"), oldval, rdtp->dynticks_nesting);
  326. if (!user && !is_idle_task(current)) {
  327. struct task_struct *idle = idle_task(smp_processor_id());
  328. trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval, 0);
  329. ftrace_dump(DUMP_ORIG);
  330. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  331. current->pid, current->comm,
  332. idle->pid, idle->comm); /* must be idle task! */
  333. }
  334. rcu_prepare_for_idle(smp_processor_id());
  335. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  336. smp_mb__before_atomic_inc(); /* See above. */
  337. atomic_inc(&rdtp->dynticks);
  338. smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
  339. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  340. /*
  341. * It is illegal to enter an extended quiescent state while
  342. * in an RCU read-side critical section.
  343. */
  344. rcu_lockdep_assert(!lock_is_held(&rcu_lock_map),
  345. "Illegal idle entry in RCU read-side critical section.");
  346. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),
  347. "Illegal idle entry in RCU-bh read-side critical section.");
  348. rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),
  349. "Illegal idle entry in RCU-sched read-side critical section.");
  350. }
  351. /*
  352. * Enter an RCU extended quiescent state, which can be either the
  353. * idle loop or adaptive-tickless usermode execution.
  354. */
  355. static void rcu_eqs_enter(bool user)
  356. {
  357. long long oldval;
  358. struct rcu_dynticks *rdtp;
  359. rdtp = &__get_cpu_var(rcu_dynticks);
  360. oldval = rdtp->dynticks_nesting;
  361. WARN_ON_ONCE((oldval & DYNTICK_TASK_NEST_MASK) == 0);
  362. if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE)
  363. rdtp->dynticks_nesting = 0;
  364. else
  365. rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
  366. rcu_eqs_enter_common(rdtp, oldval, user);
  367. }
  368. /**
  369. * rcu_idle_enter - inform RCU that current CPU is entering idle
  370. *
  371. * Enter idle mode, in other words, -leave- the mode in which RCU
  372. * read-side critical sections can occur. (Though RCU read-side
  373. * critical sections can occur in irq handlers in idle, a possibility
  374. * handled by irq_enter() and irq_exit().)
  375. *
  376. * We crowbar the ->dynticks_nesting field to zero to allow for
  377. * the possibility of usermode upcalls having messed up our count
  378. * of interrupt nesting level during the prior busy period.
  379. */
  380. void rcu_idle_enter(void)
  381. {
  382. unsigned long flags;
  383. local_irq_save(flags);
  384. rcu_eqs_enter(false);
  385. local_irq_restore(flags);
  386. }
  387. EXPORT_SYMBOL_GPL(rcu_idle_enter);
  388. #ifdef CONFIG_RCU_USER_QS
  389. /**
  390. * rcu_user_enter - inform RCU that we are resuming userspace.
  391. *
  392. * Enter RCU idle mode right before resuming userspace. No use of RCU
  393. * is permitted between this call and rcu_user_exit(). This way the
  394. * CPU doesn't need to maintain the tick for RCU maintenance purposes
  395. * when the CPU runs in userspace.
  396. */
  397. void rcu_user_enter(void)
  398. {
  399. rcu_eqs_enter(1);
  400. }
  401. #endif /* CONFIG_RCU_USER_QS */
  402. /**
  403. * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
  404. *
  405. * Exit from an interrupt handler, which might possibly result in entering
  406. * idle mode, in other words, leaving the mode in which read-side critical
  407. * sections can occur.
  408. *
  409. * This code assumes that the idle loop never does anything that might
  410. * result in unbalanced calls to irq_enter() and irq_exit(). If your
  411. * architecture violates this assumption, RCU will give you what you
  412. * deserve, good and hard. But very infrequently and irreproducibly.
  413. *
  414. * Use things like work queues to work around this limitation.
  415. *
  416. * You have been warned.
  417. */
  418. void rcu_irq_exit(void)
  419. {
  420. unsigned long flags;
  421. long long oldval;
  422. struct rcu_dynticks *rdtp;
  423. local_irq_save(flags);
  424. rdtp = &__get_cpu_var(rcu_dynticks);
  425. oldval = rdtp->dynticks_nesting;
  426. rdtp->dynticks_nesting--;
  427. WARN_ON_ONCE(rdtp->dynticks_nesting < 0);
  428. if (rdtp->dynticks_nesting)
  429. trace_rcu_dyntick(TPS("--="), oldval, rdtp->dynticks_nesting);
  430. else
  431. rcu_eqs_enter_common(rdtp, oldval, true);
  432. local_irq_restore(flags);
  433. }
  434. /*
  435. * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
  436. *
  437. * If the new value of the ->dynticks_nesting counter was previously zero,
  438. * we really have exited idle, and must do the appropriate accounting.
  439. * The caller must have disabled interrupts.
  440. */
  441. static void rcu_eqs_exit_common(struct rcu_dynticks *rdtp, long long oldval,
  442. int user)
  443. {
  444. smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
  445. atomic_inc(&rdtp->dynticks);
  446. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  447. smp_mb__after_atomic_inc(); /* See above. */
  448. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  449. rcu_cleanup_after_idle(smp_processor_id());
  450. trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
  451. if (!user && !is_idle_task(current)) {
  452. struct task_struct *idle = idle_task(smp_processor_id());
  453. trace_rcu_dyntick(TPS("Error on exit: not idle task"),
  454. oldval, rdtp->dynticks_nesting);
  455. ftrace_dump(DUMP_ORIG);
  456. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  457. current->pid, current->comm,
  458. idle->pid, idle->comm); /* must be idle task! */
  459. }
  460. }
  461. /*
  462. * Exit an RCU extended quiescent state, which can be either the
  463. * idle loop or adaptive-tickless usermode execution.
  464. */
  465. static void rcu_eqs_exit(bool user)
  466. {
  467. struct rcu_dynticks *rdtp;
  468. long long oldval;
  469. rdtp = &__get_cpu_var(rcu_dynticks);
  470. oldval = rdtp->dynticks_nesting;
  471. WARN_ON_ONCE(oldval < 0);
  472. if (oldval & DYNTICK_TASK_NEST_MASK)
  473. rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
  474. else
  475. rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  476. rcu_eqs_exit_common(rdtp, oldval, user);
  477. }
  478. /**
  479. * rcu_idle_exit - inform RCU that current CPU is leaving idle
  480. *
  481. * Exit idle mode, in other words, -enter- the mode in which RCU
  482. * read-side critical sections can occur.
  483. *
  484. * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
  485. * allow for the possibility of usermode upcalls messing up our count
  486. * of interrupt nesting level during the busy period that is just
  487. * now starting.
  488. */
  489. void rcu_idle_exit(void)
  490. {
  491. unsigned long flags;
  492. local_irq_save(flags);
  493. rcu_eqs_exit(false);
  494. local_irq_restore(flags);
  495. }
  496. EXPORT_SYMBOL_GPL(rcu_idle_exit);
  497. #ifdef CONFIG_RCU_USER_QS
  498. /**
  499. * rcu_user_exit - inform RCU that we are exiting userspace.
  500. *
  501. * Exit RCU idle mode while entering the kernel because it can
  502. * run a RCU read side critical section anytime.
  503. */
  504. void rcu_user_exit(void)
  505. {
  506. rcu_eqs_exit(1);
  507. }
  508. #endif /* CONFIG_RCU_USER_QS */
  509. /**
  510. * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
  511. *
  512. * Enter an interrupt handler, which might possibly result in exiting
  513. * idle mode, in other words, entering the mode in which read-side critical
  514. * sections can occur.
  515. *
  516. * Note that the Linux kernel is fully capable of entering an interrupt
  517. * handler that it never exits, for example when doing upcalls to
  518. * user mode! This code assumes that the idle loop never does upcalls to
  519. * user mode. If your architecture does do upcalls from the idle loop (or
  520. * does anything else that results in unbalanced calls to the irq_enter()
  521. * and irq_exit() functions), RCU will give you what you deserve, good
  522. * and hard. But very infrequently and irreproducibly.
  523. *
  524. * Use things like work queues to work around this limitation.
  525. *
  526. * You have been warned.
  527. */
  528. void rcu_irq_enter(void)
  529. {
  530. unsigned long flags;
  531. struct rcu_dynticks *rdtp;
  532. long long oldval;
  533. local_irq_save(flags);
  534. rdtp = &__get_cpu_var(rcu_dynticks);
  535. oldval = rdtp->dynticks_nesting;
  536. rdtp->dynticks_nesting++;
  537. WARN_ON_ONCE(rdtp->dynticks_nesting == 0);
  538. if (oldval)
  539. trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
  540. else
  541. rcu_eqs_exit_common(rdtp, oldval, true);
  542. local_irq_restore(flags);
  543. }
  544. /**
  545. * rcu_nmi_enter - inform RCU of entry to NMI context
  546. *
  547. * If the CPU was idle with dynamic ticks active, and there is no
  548. * irq handler running, this updates rdtp->dynticks_nmi to let the
  549. * RCU grace-period handling know that the CPU is active.
  550. */
  551. void rcu_nmi_enter(void)
  552. {
  553. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  554. if (rdtp->dynticks_nmi_nesting == 0 &&
  555. (atomic_read(&rdtp->dynticks) & 0x1))
  556. return;
  557. rdtp->dynticks_nmi_nesting++;
  558. smp_mb__before_atomic_inc(); /* Force delay from prior write. */
  559. atomic_inc(&rdtp->dynticks);
  560. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  561. smp_mb__after_atomic_inc(); /* See above. */
  562. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  563. }
  564. /**
  565. * rcu_nmi_exit - inform RCU of exit from NMI context
  566. *
  567. * If the CPU was idle with dynamic ticks active, and there is no
  568. * irq handler running, this updates rdtp->dynticks_nmi to let the
  569. * RCU grace-period handling know that the CPU is no longer active.
  570. */
  571. void rcu_nmi_exit(void)
  572. {
  573. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  574. if (rdtp->dynticks_nmi_nesting == 0 ||
  575. --rdtp->dynticks_nmi_nesting != 0)
  576. return;
  577. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  578. smp_mb__before_atomic_inc(); /* See above. */
  579. atomic_inc(&rdtp->dynticks);
  580. smp_mb__after_atomic_inc(); /* Force delay to next write. */
  581. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  582. }
  583. /**
  584. * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle
  585. *
  586. * If the current CPU is in its idle loop and is neither in an interrupt
  587. * or NMI handler, return true.
  588. */
  589. int rcu_is_cpu_idle(void)
  590. {
  591. int ret;
  592. preempt_disable();
  593. ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0;
  594. preempt_enable();
  595. return ret;
  596. }
  597. EXPORT_SYMBOL(rcu_is_cpu_idle);
  598. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  599. /*
  600. * Is the current CPU online? Disable preemption to avoid false positives
  601. * that could otherwise happen due to the current CPU number being sampled,
  602. * this task being preempted, its old CPU being taken offline, resuming
  603. * on some other CPU, then determining that its old CPU is now offline.
  604. * It is OK to use RCU on an offline processor during initial boot, hence
  605. * the check for rcu_scheduler_fully_active. Note also that it is OK
  606. * for a CPU coming online to use RCU for one jiffy prior to marking itself
  607. * online in the cpu_online_mask. Similarly, it is OK for a CPU going
  608. * offline to continue to use RCU for one jiffy after marking itself
  609. * offline in the cpu_online_mask. This leniency is necessary given the
  610. * non-atomic nature of the online and offline processing, for example,
  611. * the fact that a CPU enters the scheduler after completing the CPU_DYING
  612. * notifiers.
  613. *
  614. * This is also why RCU internally marks CPUs online during the
  615. * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
  616. *
  617. * Disable checking if in an NMI handler because we cannot safely report
  618. * errors from NMI handlers anyway.
  619. */
  620. bool rcu_lockdep_current_cpu_online(void)
  621. {
  622. struct rcu_data *rdp;
  623. struct rcu_node *rnp;
  624. bool ret;
  625. if (in_nmi())
  626. return 1;
  627. preempt_disable();
  628. rdp = &__get_cpu_var(rcu_sched_data);
  629. rnp = rdp->mynode;
  630. ret = (rdp->grpmask & rnp->qsmaskinit) ||
  631. !rcu_scheduler_fully_active;
  632. preempt_enable();
  633. return ret;
  634. }
  635. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  636. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  637. /**
  638. * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
  639. *
  640. * If the current CPU is idle or running at a first-level (not nested)
  641. * interrupt from idle, return true. The caller must have at least
  642. * disabled preemption.
  643. */
  644. static int rcu_is_cpu_rrupt_from_idle(void)
  645. {
  646. return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1;
  647. }
  648. /*
  649. * Snapshot the specified CPU's dynticks counter so that we can later
  650. * credit them with an implicit quiescent state. Return 1 if this CPU
  651. * is in dynticks idle mode, which is an extended quiescent state.
  652. */
  653. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  654. {
  655. rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
  656. return (rdp->dynticks_snap & 0x1) == 0;
  657. }
  658. /*
  659. * Return true if the specified CPU has passed through a quiescent
  660. * state by virtue of being in or having passed through an dynticks
  661. * idle state since the last call to dyntick_save_progress_counter()
  662. * for this same CPU, or by virtue of having been offline.
  663. */
  664. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  665. {
  666. unsigned int curr;
  667. unsigned int snap;
  668. curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
  669. snap = (unsigned int)rdp->dynticks_snap;
  670. /*
  671. * If the CPU passed through or entered a dynticks idle phase with
  672. * no active irq/NMI handlers, then we can safely pretend that the CPU
  673. * already acknowledged the request to pass through a quiescent
  674. * state. Either way, that CPU cannot possibly be in an RCU
  675. * read-side critical section that started before the beginning
  676. * of the current RCU grace period.
  677. */
  678. if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
  679. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  680. rdp->dynticks_fqs++;
  681. return 1;
  682. }
  683. /*
  684. * Check for the CPU being offline, but only if the grace period
  685. * is old enough. We don't need to worry about the CPU changing
  686. * state: If we see it offline even once, it has been through a
  687. * quiescent state.
  688. *
  689. * The reason for insisting that the grace period be at least
  690. * one jiffy old is that CPUs that are not quite online and that
  691. * have just gone offline can still execute RCU read-side critical
  692. * sections.
  693. */
  694. if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
  695. return 0; /* Grace period is not old enough. */
  696. barrier();
  697. if (cpu_is_offline(rdp->cpu)) {
  698. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
  699. rdp->offline_fqs++;
  700. return 1;
  701. }
  702. /*
  703. * There is a possibility that a CPU in adaptive-ticks state
  704. * might run in the kernel with the scheduling-clock tick disabled
  705. * for an extended time period. Invoke rcu_kick_nohz_cpu() to
  706. * force the CPU to restart the scheduling-clock tick in this
  707. * CPU is in this state.
  708. */
  709. rcu_kick_nohz_cpu(rdp->cpu);
  710. return 0;
  711. }
  712. static void record_gp_stall_check_time(struct rcu_state *rsp)
  713. {
  714. rsp->gp_start = jiffies;
  715. rsp->jiffies_stall = jiffies + rcu_jiffies_till_stall_check();
  716. }
  717. /*
  718. * Dump stacks of all tasks running on stalled CPUs. This is a fallback
  719. * for architectures that do not implement trigger_all_cpu_backtrace().
  720. * The NMI-triggered stack traces are more accurate because they are
  721. * printed by the target CPU.
  722. */
  723. static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
  724. {
  725. int cpu;
  726. unsigned long flags;
  727. struct rcu_node *rnp;
  728. rcu_for_each_leaf_node(rsp, rnp) {
  729. raw_spin_lock_irqsave(&rnp->lock, flags);
  730. if (rnp->qsmask != 0) {
  731. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  732. if (rnp->qsmask & (1UL << cpu))
  733. dump_cpu_task(rnp->grplo + cpu);
  734. }
  735. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  736. }
  737. }
  738. static void print_other_cpu_stall(struct rcu_state *rsp)
  739. {
  740. int cpu;
  741. long delta;
  742. unsigned long flags;
  743. int ndetected = 0;
  744. struct rcu_node *rnp = rcu_get_root(rsp);
  745. long totqlen = 0;
  746. /* Only let one CPU complain about others per time interval. */
  747. raw_spin_lock_irqsave(&rnp->lock, flags);
  748. delta = jiffies - rsp->jiffies_stall;
  749. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  750. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  751. return;
  752. }
  753. rsp->jiffies_stall = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
  754. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  755. /*
  756. * OK, time to rat on our buddy...
  757. * See Documentation/RCU/stallwarn.txt for info on how to debug
  758. * RCU CPU stall warnings.
  759. */
  760. pr_err("INFO: %s detected stalls on CPUs/tasks:",
  761. rsp->name);
  762. print_cpu_stall_info_begin();
  763. rcu_for_each_leaf_node(rsp, rnp) {
  764. raw_spin_lock_irqsave(&rnp->lock, flags);
  765. ndetected += rcu_print_task_stall(rnp);
  766. if (rnp->qsmask != 0) {
  767. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  768. if (rnp->qsmask & (1UL << cpu)) {
  769. print_cpu_stall_info(rsp,
  770. rnp->grplo + cpu);
  771. ndetected++;
  772. }
  773. }
  774. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  775. }
  776. /*
  777. * Now rat on any tasks that got kicked up to the root rcu_node
  778. * due to CPU offlining.
  779. */
  780. rnp = rcu_get_root(rsp);
  781. raw_spin_lock_irqsave(&rnp->lock, flags);
  782. ndetected += rcu_print_task_stall(rnp);
  783. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  784. print_cpu_stall_info_end();
  785. for_each_possible_cpu(cpu)
  786. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  787. pr_cont("(detected by %d, t=%ld jiffies, g=%lu, c=%lu, q=%lu)\n",
  788. smp_processor_id(), (long)(jiffies - rsp->gp_start),
  789. rsp->gpnum, rsp->completed, totqlen);
  790. if (ndetected == 0)
  791. pr_err("INFO: Stall ended before state dump start\n");
  792. else if (!trigger_all_cpu_backtrace())
  793. rcu_dump_cpu_stacks(rsp);
  794. /* Complain about tasks blocking the grace period. */
  795. rcu_print_detail_task_stall(rsp);
  796. force_quiescent_state(rsp); /* Kick them all. */
  797. }
  798. static void print_cpu_stall(struct rcu_state *rsp)
  799. {
  800. int cpu;
  801. unsigned long flags;
  802. struct rcu_node *rnp = rcu_get_root(rsp);
  803. long totqlen = 0;
  804. /*
  805. * OK, time to rat on ourselves...
  806. * See Documentation/RCU/stallwarn.txt for info on how to debug
  807. * RCU CPU stall warnings.
  808. */
  809. pr_err("INFO: %s self-detected stall on CPU", rsp->name);
  810. print_cpu_stall_info_begin();
  811. print_cpu_stall_info(rsp, smp_processor_id());
  812. print_cpu_stall_info_end();
  813. for_each_possible_cpu(cpu)
  814. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  815. pr_cont(" (t=%lu jiffies g=%lu c=%lu q=%lu)\n",
  816. jiffies - rsp->gp_start, rsp->gpnum, rsp->completed, totqlen);
  817. if (!trigger_all_cpu_backtrace())
  818. dump_stack();
  819. raw_spin_lock_irqsave(&rnp->lock, flags);
  820. if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
  821. rsp->jiffies_stall = jiffies +
  822. 3 * rcu_jiffies_till_stall_check() + 3;
  823. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  824. set_need_resched(); /* kick ourselves to get things going. */
  825. }
  826. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  827. {
  828. unsigned long j;
  829. unsigned long js;
  830. struct rcu_node *rnp;
  831. if (rcu_cpu_stall_suppress)
  832. return;
  833. j = ACCESS_ONCE(jiffies);
  834. js = ACCESS_ONCE(rsp->jiffies_stall);
  835. rnp = rdp->mynode;
  836. if (rcu_gp_in_progress(rsp) &&
  837. (ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
  838. /* We haven't checked in, so go dump stack. */
  839. print_cpu_stall(rsp);
  840. } else if (rcu_gp_in_progress(rsp) &&
  841. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  842. /* They had a few time units to dump stack, so complain. */
  843. print_other_cpu_stall(rsp);
  844. }
  845. }
  846. /**
  847. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  848. *
  849. * Set the stall-warning timeout way off into the future, thus preventing
  850. * any RCU CPU stall-warning messages from appearing in the current set of
  851. * RCU grace periods.
  852. *
  853. * The caller must disable hard irqs.
  854. */
  855. void rcu_cpu_stall_reset(void)
  856. {
  857. struct rcu_state *rsp;
  858. for_each_rcu_flavor(rsp)
  859. rsp->jiffies_stall = jiffies + ULONG_MAX / 2;
  860. }
  861. /*
  862. * Initialize the specified rcu_data structure's callback list to empty.
  863. */
  864. static void init_callback_list(struct rcu_data *rdp)
  865. {
  866. int i;
  867. if (init_nocb_callback_list(rdp))
  868. return;
  869. rdp->nxtlist = NULL;
  870. for (i = 0; i < RCU_NEXT_SIZE; i++)
  871. rdp->nxttail[i] = &rdp->nxtlist;
  872. }
  873. /*
  874. * Determine the value that ->completed will have at the end of the
  875. * next subsequent grace period. This is used to tag callbacks so that
  876. * a CPU can invoke callbacks in a timely fashion even if that CPU has
  877. * been dyntick-idle for an extended period with callbacks under the
  878. * influence of RCU_FAST_NO_HZ.
  879. *
  880. * The caller must hold rnp->lock with interrupts disabled.
  881. */
  882. static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
  883. struct rcu_node *rnp)
  884. {
  885. /*
  886. * If RCU is idle, we just wait for the next grace period.
  887. * But we can only be sure that RCU is idle if we are looking
  888. * at the root rcu_node structure -- otherwise, a new grace
  889. * period might have started, but just not yet gotten around
  890. * to initializing the current non-root rcu_node structure.
  891. */
  892. if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
  893. return rnp->completed + 1;
  894. /*
  895. * Otherwise, wait for a possible partial grace period and
  896. * then the subsequent full grace period.
  897. */
  898. return rnp->completed + 2;
  899. }
  900. /*
  901. * Trace-event helper function for rcu_start_future_gp() and
  902. * rcu_nocb_wait_gp().
  903. */
  904. static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  905. unsigned long c, const char *s)
  906. {
  907. trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
  908. rnp->completed, c, rnp->level,
  909. rnp->grplo, rnp->grphi, s);
  910. }
  911. /*
  912. * Start some future grace period, as needed to handle newly arrived
  913. * callbacks. The required future grace periods are recorded in each
  914. * rcu_node structure's ->need_future_gp field.
  915. *
  916. * The caller must hold the specified rcu_node structure's ->lock.
  917. */
  918. static unsigned long __maybe_unused
  919. rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp)
  920. {
  921. unsigned long c;
  922. int i;
  923. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  924. /*
  925. * Pick up grace-period number for new callbacks. If this
  926. * grace period is already marked as needed, return to the caller.
  927. */
  928. c = rcu_cbs_completed(rdp->rsp, rnp);
  929. trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
  930. if (rnp->need_future_gp[c & 0x1]) {
  931. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
  932. return c;
  933. }
  934. /*
  935. * If either this rcu_node structure or the root rcu_node structure
  936. * believe that a grace period is in progress, then we must wait
  937. * for the one following, which is in "c". Because our request
  938. * will be noticed at the end of the current grace period, we don't
  939. * need to explicitly start one.
  940. */
  941. if (rnp->gpnum != rnp->completed ||
  942. ACCESS_ONCE(rnp->gpnum) != ACCESS_ONCE(rnp->completed)) {
  943. rnp->need_future_gp[c & 0x1]++;
  944. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
  945. return c;
  946. }
  947. /*
  948. * There might be no grace period in progress. If we don't already
  949. * hold it, acquire the root rcu_node structure's lock in order to
  950. * start one (if needed).
  951. */
  952. if (rnp != rnp_root)
  953. raw_spin_lock(&rnp_root->lock);
  954. /*
  955. * Get a new grace-period number. If there really is no grace
  956. * period in progress, it will be smaller than the one we obtained
  957. * earlier. Adjust callbacks as needed. Note that even no-CBs
  958. * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
  959. */
  960. c = rcu_cbs_completed(rdp->rsp, rnp_root);
  961. for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
  962. if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
  963. rdp->nxtcompleted[i] = c;
  964. /*
  965. * If the needed for the required grace period is already
  966. * recorded, trace and leave.
  967. */
  968. if (rnp_root->need_future_gp[c & 0x1]) {
  969. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
  970. goto unlock_out;
  971. }
  972. /* Record the need for the future grace period. */
  973. rnp_root->need_future_gp[c & 0x1]++;
  974. /* If a grace period is not already in progress, start one. */
  975. if (rnp_root->gpnum != rnp_root->completed) {
  976. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
  977. } else {
  978. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
  979. rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
  980. }
  981. unlock_out:
  982. if (rnp != rnp_root)
  983. raw_spin_unlock(&rnp_root->lock);
  984. return c;
  985. }
  986. /*
  987. * Clean up any old requests for the just-ended grace period. Also return
  988. * whether any additional grace periods have been requested. Also invoke
  989. * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
  990. * waiting for this grace period to complete.
  991. */
  992. static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  993. {
  994. int c = rnp->completed;
  995. int needmore;
  996. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  997. rcu_nocb_gp_cleanup(rsp, rnp);
  998. rnp->need_future_gp[c & 0x1] = 0;
  999. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  1000. trace_rcu_future_gp(rnp, rdp, c,
  1001. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  1002. return needmore;
  1003. }
  1004. /*
  1005. * If there is room, assign a ->completed number to any callbacks on
  1006. * this CPU that have not already been assigned. Also accelerate any
  1007. * callbacks that were previously assigned a ->completed number that has
  1008. * since proven to be too conservative, which can happen if callbacks get
  1009. * assigned a ->completed number while RCU is idle, but with reference to
  1010. * a non-root rcu_node structure. This function is idempotent, so it does
  1011. * not hurt to call it repeatedly.
  1012. *
  1013. * The caller must hold rnp->lock with interrupts disabled.
  1014. */
  1015. static void rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1016. struct rcu_data *rdp)
  1017. {
  1018. unsigned long c;
  1019. int i;
  1020. /* If the CPU has no callbacks, nothing to do. */
  1021. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1022. return;
  1023. /*
  1024. * Starting from the sublist containing the callbacks most
  1025. * recently assigned a ->completed number and working down, find the
  1026. * first sublist that is not assignable to an upcoming grace period.
  1027. * Such a sublist has something in it (first two tests) and has
  1028. * a ->completed number assigned that will complete sooner than
  1029. * the ->completed number for newly arrived callbacks (last test).
  1030. *
  1031. * The key point is that any later sublist can be assigned the
  1032. * same ->completed number as the newly arrived callbacks, which
  1033. * means that the callbacks in any of these later sublist can be
  1034. * grouped into a single sublist, whether or not they have already
  1035. * been assigned a ->completed number.
  1036. */
  1037. c = rcu_cbs_completed(rsp, rnp);
  1038. for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
  1039. if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
  1040. !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
  1041. break;
  1042. /*
  1043. * If there are no sublist for unassigned callbacks, leave.
  1044. * At the same time, advance "i" one sublist, so that "i" will
  1045. * index into the sublist where all the remaining callbacks should
  1046. * be grouped into.
  1047. */
  1048. if (++i >= RCU_NEXT_TAIL)
  1049. return;
  1050. /*
  1051. * Assign all subsequent callbacks' ->completed number to the next
  1052. * full grace period and group them all in the sublist initially
  1053. * indexed by "i".
  1054. */
  1055. for (; i <= RCU_NEXT_TAIL; i++) {
  1056. rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
  1057. rdp->nxtcompleted[i] = c;
  1058. }
  1059. /* Record any needed additional grace periods. */
  1060. rcu_start_future_gp(rnp, rdp);
  1061. /* Trace depending on how much we were able to accelerate. */
  1062. if (!*rdp->nxttail[RCU_WAIT_TAIL])
  1063. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
  1064. else
  1065. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
  1066. }
  1067. /*
  1068. * Move any callbacks whose grace period has completed to the
  1069. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1070. * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
  1071. * sublist. This function is idempotent, so it does not hurt to
  1072. * invoke it repeatedly. As long as it is not invoked -too- often...
  1073. *
  1074. * The caller must hold rnp->lock with interrupts disabled.
  1075. */
  1076. static void rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1077. struct rcu_data *rdp)
  1078. {
  1079. int i, j;
  1080. /* If the CPU has no callbacks, nothing to do. */
  1081. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1082. return;
  1083. /*
  1084. * Find all callbacks whose ->completed numbers indicate that they
  1085. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1086. */
  1087. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  1088. if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
  1089. break;
  1090. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
  1091. }
  1092. /* Clean up any sublist tail pointers that were misordered above. */
  1093. for (j = RCU_WAIT_TAIL; j < i; j++)
  1094. rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
  1095. /* Copy down callbacks to fill in empty sublists. */
  1096. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  1097. if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
  1098. break;
  1099. rdp->nxttail[j] = rdp->nxttail[i];
  1100. rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
  1101. }
  1102. /* Classify any remaining callbacks. */
  1103. rcu_accelerate_cbs(rsp, rnp, rdp);
  1104. }
  1105. /*
  1106. * Update CPU-local rcu_data state to record the beginnings and ends of
  1107. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1108. * structure corresponding to the current CPU, and must have irqs disabled.
  1109. */
  1110. static void __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  1111. {
  1112. /* Handle the ends of any preceding grace periods first. */
  1113. if (rdp->completed == rnp->completed) {
  1114. /* No grace period end, so just accelerate recent callbacks. */
  1115. rcu_accelerate_cbs(rsp, rnp, rdp);
  1116. } else {
  1117. /* Advance callbacks. */
  1118. rcu_advance_cbs(rsp, rnp, rdp);
  1119. /* Remember that we saw this grace-period completion. */
  1120. rdp->completed = rnp->completed;
  1121. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
  1122. }
  1123. if (rdp->gpnum != rnp->gpnum) {
  1124. /*
  1125. * If the current grace period is waiting for this CPU,
  1126. * set up to detect a quiescent state, otherwise don't
  1127. * go looking for one.
  1128. */
  1129. rdp->gpnum = rnp->gpnum;
  1130. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
  1131. rdp->passed_quiesce = 0;
  1132. rdp->qs_pending = !!(rnp->qsmask & rdp->grpmask);
  1133. zero_cpu_stall_ticks(rdp);
  1134. }
  1135. }
  1136. static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
  1137. {
  1138. unsigned long flags;
  1139. struct rcu_node *rnp;
  1140. local_irq_save(flags);
  1141. rnp = rdp->mynode;
  1142. if ((rdp->gpnum == ACCESS_ONCE(rnp->gpnum) &&
  1143. rdp->completed == ACCESS_ONCE(rnp->completed)) || /* w/out lock. */
  1144. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  1145. local_irq_restore(flags);
  1146. return;
  1147. }
  1148. __note_gp_changes(rsp, rnp, rdp);
  1149. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1150. }
  1151. /*
  1152. * Initialize a new grace period.
  1153. */
  1154. static int rcu_gp_init(struct rcu_state *rsp)
  1155. {
  1156. struct rcu_data *rdp;
  1157. struct rcu_node *rnp = rcu_get_root(rsp);
  1158. raw_spin_lock_irq(&rnp->lock);
  1159. rsp->gp_flags = 0; /* Clear all flags: New grace period. */
  1160. if (rcu_gp_in_progress(rsp)) {
  1161. /* Grace period already in progress, don't start another. */
  1162. raw_spin_unlock_irq(&rnp->lock);
  1163. return 0;
  1164. }
  1165. /* Advance to a new grace period and initialize state. */
  1166. rsp->gpnum++;
  1167. trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
  1168. record_gp_stall_check_time(rsp);
  1169. raw_spin_unlock_irq(&rnp->lock);
  1170. /* Exclude any concurrent CPU-hotplug operations. */
  1171. mutex_lock(&rsp->onoff_mutex);
  1172. /*
  1173. * Set the quiescent-state-needed bits in all the rcu_node
  1174. * structures for all currently online CPUs in breadth-first order,
  1175. * starting from the root rcu_node structure, relying on the layout
  1176. * of the tree within the rsp->node[] array. Note that other CPUs
  1177. * will access only the leaves of the hierarchy, thus seeing that no
  1178. * grace period is in progress, at least until the corresponding
  1179. * leaf node has been initialized. In addition, we have excluded
  1180. * CPU-hotplug operations.
  1181. *
  1182. * The grace period cannot complete until the initialization
  1183. * process finishes, because this kthread handles both.
  1184. */
  1185. rcu_for_each_node_breadth_first(rsp, rnp) {
  1186. raw_spin_lock_irq(&rnp->lock);
  1187. rdp = this_cpu_ptr(rsp->rda);
  1188. rcu_preempt_check_blocked_tasks(rnp);
  1189. rnp->qsmask = rnp->qsmaskinit;
  1190. ACCESS_ONCE(rnp->gpnum) = rsp->gpnum;
  1191. WARN_ON_ONCE(rnp->completed != rsp->completed);
  1192. ACCESS_ONCE(rnp->completed) = rsp->completed;
  1193. if (rnp == rdp->mynode)
  1194. __note_gp_changes(rsp, rnp, rdp);
  1195. rcu_preempt_boost_start_gp(rnp);
  1196. trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
  1197. rnp->level, rnp->grplo,
  1198. rnp->grphi, rnp->qsmask);
  1199. raw_spin_unlock_irq(&rnp->lock);
  1200. #ifdef CONFIG_PROVE_RCU_DELAY
  1201. if ((prandom_u32() % (rcu_num_nodes + 1)) == 0 &&
  1202. system_state == SYSTEM_RUNNING)
  1203. udelay(200);
  1204. #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
  1205. cond_resched();
  1206. }
  1207. mutex_unlock(&rsp->onoff_mutex);
  1208. return 1;
  1209. }
  1210. /*
  1211. * Do one round of quiescent-state forcing.
  1212. */
  1213. int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in)
  1214. {
  1215. int fqs_state = fqs_state_in;
  1216. struct rcu_node *rnp = rcu_get_root(rsp);
  1217. rsp->n_force_qs++;
  1218. if (fqs_state == RCU_SAVE_DYNTICK) {
  1219. /* Collect dyntick-idle snapshots. */
  1220. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1221. fqs_state = RCU_FORCE_QS;
  1222. } else {
  1223. /* Handle dyntick-idle and offline CPUs. */
  1224. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1225. }
  1226. /* Clear flag to prevent immediate re-entry. */
  1227. if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1228. raw_spin_lock_irq(&rnp->lock);
  1229. rsp->gp_flags &= ~RCU_GP_FLAG_FQS;
  1230. raw_spin_unlock_irq(&rnp->lock);
  1231. }
  1232. return fqs_state;
  1233. }
  1234. /*
  1235. * Clean up after the old grace period.
  1236. */
  1237. static void rcu_gp_cleanup(struct rcu_state *rsp)
  1238. {
  1239. unsigned long gp_duration;
  1240. int nocb = 0;
  1241. struct rcu_data *rdp;
  1242. struct rcu_node *rnp = rcu_get_root(rsp);
  1243. raw_spin_lock_irq(&rnp->lock);
  1244. gp_duration = jiffies - rsp->gp_start;
  1245. if (gp_duration > rsp->gp_max)
  1246. rsp->gp_max = gp_duration;
  1247. /*
  1248. * We know the grace period is complete, but to everyone else
  1249. * it appears to still be ongoing. But it is also the case
  1250. * that to everyone else it looks like there is nothing that
  1251. * they can do to advance the grace period. It is therefore
  1252. * safe for us to drop the lock in order to mark the grace
  1253. * period as completed in all of the rcu_node structures.
  1254. */
  1255. raw_spin_unlock_irq(&rnp->lock);
  1256. /*
  1257. * Propagate new ->completed value to rcu_node structures so
  1258. * that other CPUs don't have to wait until the start of the next
  1259. * grace period to process their callbacks. This also avoids
  1260. * some nasty RCU grace-period initialization races by forcing
  1261. * the end of the current grace period to be completely recorded in
  1262. * all of the rcu_node structures before the beginning of the next
  1263. * grace period is recorded in any of the rcu_node structures.
  1264. */
  1265. rcu_for_each_node_breadth_first(rsp, rnp) {
  1266. raw_spin_lock_irq(&rnp->lock);
  1267. ACCESS_ONCE(rnp->completed) = rsp->gpnum;
  1268. rdp = this_cpu_ptr(rsp->rda);
  1269. if (rnp == rdp->mynode)
  1270. __note_gp_changes(rsp, rnp, rdp);
  1271. nocb += rcu_future_gp_cleanup(rsp, rnp);
  1272. raw_spin_unlock_irq(&rnp->lock);
  1273. cond_resched();
  1274. }
  1275. rnp = rcu_get_root(rsp);
  1276. raw_spin_lock_irq(&rnp->lock);
  1277. rcu_nocb_gp_set(rnp, nocb);
  1278. rsp->completed = rsp->gpnum; /* Declare grace period done. */
  1279. trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
  1280. rsp->fqs_state = RCU_GP_IDLE;
  1281. rdp = this_cpu_ptr(rsp->rda);
  1282. rcu_advance_cbs(rsp, rnp, rdp); /* Reduce false positives below. */
  1283. if (cpu_needs_another_gp(rsp, rdp))
  1284. rsp->gp_flags = 1;
  1285. raw_spin_unlock_irq(&rnp->lock);
  1286. }
  1287. /*
  1288. * Body of kthread that handles grace periods.
  1289. */
  1290. static int __noreturn rcu_gp_kthread(void *arg)
  1291. {
  1292. int fqs_state;
  1293. unsigned long j;
  1294. int ret;
  1295. struct rcu_state *rsp = arg;
  1296. struct rcu_node *rnp = rcu_get_root(rsp);
  1297. for (;;) {
  1298. /* Handle grace-period start. */
  1299. for (;;) {
  1300. wait_event_interruptible(rsp->gp_wq,
  1301. rsp->gp_flags &
  1302. RCU_GP_FLAG_INIT);
  1303. if ((rsp->gp_flags & RCU_GP_FLAG_INIT) &&
  1304. rcu_gp_init(rsp))
  1305. break;
  1306. cond_resched();
  1307. flush_signals(current);
  1308. }
  1309. /* Handle quiescent-state forcing. */
  1310. fqs_state = RCU_SAVE_DYNTICK;
  1311. j = jiffies_till_first_fqs;
  1312. if (j > HZ) {
  1313. j = HZ;
  1314. jiffies_till_first_fqs = HZ;
  1315. }
  1316. for (;;) {
  1317. rsp->jiffies_force_qs = jiffies + j;
  1318. ret = wait_event_interruptible_timeout(rsp->gp_wq,
  1319. (rsp->gp_flags & RCU_GP_FLAG_FQS) ||
  1320. (!ACCESS_ONCE(rnp->qsmask) &&
  1321. !rcu_preempt_blocked_readers_cgp(rnp)),
  1322. j);
  1323. /* If grace period done, leave loop. */
  1324. if (!ACCESS_ONCE(rnp->qsmask) &&
  1325. !rcu_preempt_blocked_readers_cgp(rnp))
  1326. break;
  1327. /* If time for quiescent-state forcing, do it. */
  1328. if (ret == 0 || (rsp->gp_flags & RCU_GP_FLAG_FQS)) {
  1329. fqs_state = rcu_gp_fqs(rsp, fqs_state);
  1330. cond_resched();
  1331. } else {
  1332. /* Deal with stray signal. */
  1333. cond_resched();
  1334. flush_signals(current);
  1335. }
  1336. j = jiffies_till_next_fqs;
  1337. if (j > HZ) {
  1338. j = HZ;
  1339. jiffies_till_next_fqs = HZ;
  1340. } else if (j < 1) {
  1341. j = 1;
  1342. jiffies_till_next_fqs = 1;
  1343. }
  1344. }
  1345. /* Handle grace-period end. */
  1346. rcu_gp_cleanup(rsp);
  1347. }
  1348. }
  1349. static void rsp_wakeup(struct irq_work *work)
  1350. {
  1351. struct rcu_state *rsp = container_of(work, struct rcu_state, wakeup_work);
  1352. /* Wake up rcu_gp_kthread() to start the grace period. */
  1353. wake_up(&rsp->gp_wq);
  1354. }
  1355. /*
  1356. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  1357. * in preparation for detecting the next grace period. The caller must hold
  1358. * the root node's ->lock and hard irqs must be disabled.
  1359. *
  1360. * Note that it is legal for a dying CPU (which is marked as offline) to
  1361. * invoke this function. This can happen when the dying CPU reports its
  1362. * quiescent state.
  1363. */
  1364. static void
  1365. rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  1366. struct rcu_data *rdp)
  1367. {
  1368. if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
  1369. /*
  1370. * Either we have not yet spawned the grace-period
  1371. * task, this CPU does not need another grace period,
  1372. * or a grace period is already in progress.
  1373. * Either way, don't start a new grace period.
  1374. */
  1375. return;
  1376. }
  1377. rsp->gp_flags = RCU_GP_FLAG_INIT;
  1378. /*
  1379. * We can't do wakeups while holding the rnp->lock, as that
  1380. * could cause possible deadlocks with the rq->lock. Deter
  1381. * the wakeup to interrupt context.
  1382. */
  1383. irq_work_queue(&rsp->wakeup_work);
  1384. }
  1385. /*
  1386. * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
  1387. * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
  1388. * is invoked indirectly from rcu_advance_cbs(), which would result in
  1389. * endless recursion -- or would do so if it wasn't for the self-deadlock
  1390. * that is encountered beforehand.
  1391. */
  1392. static void
  1393. rcu_start_gp(struct rcu_state *rsp)
  1394. {
  1395. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1396. struct rcu_node *rnp = rcu_get_root(rsp);
  1397. /*
  1398. * If there is no grace period in progress right now, any
  1399. * callbacks we have up to this point will be satisfied by the
  1400. * next grace period. Also, advancing the callbacks reduces the
  1401. * probability of false positives from cpu_needs_another_gp()
  1402. * resulting in pointless grace periods. So, advance callbacks
  1403. * then start the grace period!
  1404. */
  1405. rcu_advance_cbs(rsp, rnp, rdp);
  1406. rcu_start_gp_advanced(rsp, rnp, rdp);
  1407. }
  1408. /*
  1409. * Report a full set of quiescent states to the specified rcu_state
  1410. * data structure. This involves cleaning up after the prior grace
  1411. * period and letting rcu_start_gp() start up the next grace period
  1412. * if one is needed. Note that the caller must hold rnp->lock, which
  1413. * is released before return.
  1414. */
  1415. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  1416. __releases(rcu_get_root(rsp)->lock)
  1417. {
  1418. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  1419. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  1420. wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
  1421. }
  1422. /*
  1423. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  1424. * Allows quiescent states for a group of CPUs to be reported at one go
  1425. * to the specified rcu_node structure, though all the CPUs in the group
  1426. * must be represented by the same rcu_node structure (which need not be
  1427. * a leaf rcu_node structure, though it often will be). That structure's
  1428. * lock must be held upon entry, and it is released before return.
  1429. */
  1430. static void
  1431. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  1432. struct rcu_node *rnp, unsigned long flags)
  1433. __releases(rnp->lock)
  1434. {
  1435. struct rcu_node *rnp_c;
  1436. /* Walk up the rcu_node hierarchy. */
  1437. for (;;) {
  1438. if (!(rnp->qsmask & mask)) {
  1439. /* Our bit has already been cleared, so done. */
  1440. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1441. return;
  1442. }
  1443. rnp->qsmask &= ~mask;
  1444. trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
  1445. mask, rnp->qsmask, rnp->level,
  1446. rnp->grplo, rnp->grphi,
  1447. !!rnp->gp_tasks);
  1448. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  1449. /* Other bits still set at this level, so done. */
  1450. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1451. return;
  1452. }
  1453. mask = rnp->grpmask;
  1454. if (rnp->parent == NULL) {
  1455. /* No more levels. Exit loop holding root lock. */
  1456. break;
  1457. }
  1458. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1459. rnp_c = rnp;
  1460. rnp = rnp->parent;
  1461. raw_spin_lock_irqsave(&rnp->lock, flags);
  1462. WARN_ON_ONCE(rnp_c->qsmask);
  1463. }
  1464. /*
  1465. * Get here if we are the last CPU to pass through a quiescent
  1466. * state for this grace period. Invoke rcu_report_qs_rsp()
  1467. * to clean up and start the next grace period if one is needed.
  1468. */
  1469. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  1470. }
  1471. /*
  1472. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  1473. * structure. This must be either called from the specified CPU, or
  1474. * called when the specified CPU is known to be offline (and when it is
  1475. * also known that no other CPU is concurrently trying to help the offline
  1476. * CPU). The lastcomp argument is used to make sure we are still in the
  1477. * grace period of interest. We don't want to end the current grace period
  1478. * based on quiescent states detected in an earlier grace period!
  1479. */
  1480. static void
  1481. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
  1482. {
  1483. unsigned long flags;
  1484. unsigned long mask;
  1485. struct rcu_node *rnp;
  1486. rnp = rdp->mynode;
  1487. raw_spin_lock_irqsave(&rnp->lock, flags);
  1488. if (rdp->passed_quiesce == 0 || rdp->gpnum != rnp->gpnum ||
  1489. rnp->completed == rnp->gpnum) {
  1490. /*
  1491. * The grace period in which this quiescent state was
  1492. * recorded has ended, so don't report it upwards.
  1493. * We will instead need a new quiescent state that lies
  1494. * within the current grace period.
  1495. */
  1496. rdp->passed_quiesce = 0; /* need qs for new gp. */
  1497. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1498. return;
  1499. }
  1500. mask = rdp->grpmask;
  1501. if ((rnp->qsmask & mask) == 0) {
  1502. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1503. } else {
  1504. rdp->qs_pending = 0;
  1505. /*
  1506. * This GP can't end until cpu checks in, so all of our
  1507. * callbacks can be processed during the next GP.
  1508. */
  1509. rcu_accelerate_cbs(rsp, rnp, rdp);
  1510. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  1511. }
  1512. }
  1513. /*
  1514. * Check to see if there is a new grace period of which this CPU
  1515. * is not yet aware, and if so, set up local rcu_data state for it.
  1516. * Otherwise, see if this CPU has just passed through its first
  1517. * quiescent state for this grace period, and record that fact if so.
  1518. */
  1519. static void
  1520. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  1521. {
  1522. /* Check for grace-period ends and beginnings. */
  1523. note_gp_changes(rsp, rdp);
  1524. /*
  1525. * Does this CPU still need to do its part for current grace period?
  1526. * If no, return and let the other CPUs do their part as well.
  1527. */
  1528. if (!rdp->qs_pending)
  1529. return;
  1530. /*
  1531. * Was there a quiescent state since the beginning of the grace
  1532. * period? If no, then exit and wait for the next call.
  1533. */
  1534. if (!rdp->passed_quiesce)
  1535. return;
  1536. /*
  1537. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  1538. * judge of that).
  1539. */
  1540. rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
  1541. }
  1542. #ifdef CONFIG_HOTPLUG_CPU
  1543. /*
  1544. * Send the specified CPU's RCU callbacks to the orphanage. The
  1545. * specified CPU must be offline, and the caller must hold the
  1546. * ->orphan_lock.
  1547. */
  1548. static void
  1549. rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
  1550. struct rcu_node *rnp, struct rcu_data *rdp)
  1551. {
  1552. /* No-CBs CPUs do not have orphanable callbacks. */
  1553. if (rcu_is_nocb_cpu(rdp->cpu))
  1554. return;
  1555. /*
  1556. * Orphan the callbacks. First adjust the counts. This is safe
  1557. * because _rcu_barrier() excludes CPU-hotplug operations, so it
  1558. * cannot be running now. Thus no memory barrier is required.
  1559. */
  1560. if (rdp->nxtlist != NULL) {
  1561. rsp->qlen_lazy += rdp->qlen_lazy;
  1562. rsp->qlen += rdp->qlen;
  1563. rdp->n_cbs_orphaned += rdp->qlen;
  1564. rdp->qlen_lazy = 0;
  1565. ACCESS_ONCE(rdp->qlen) = 0;
  1566. }
  1567. /*
  1568. * Next, move those callbacks still needing a grace period to
  1569. * the orphanage, where some other CPU will pick them up.
  1570. * Some of the callbacks might have gone partway through a grace
  1571. * period, but that is too bad. They get to start over because we
  1572. * cannot assume that grace periods are synchronized across CPUs.
  1573. * We don't bother updating the ->nxttail[] array yet, instead
  1574. * we just reset the whole thing later on.
  1575. */
  1576. if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
  1577. *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
  1578. rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
  1579. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1580. }
  1581. /*
  1582. * Then move the ready-to-invoke callbacks to the orphanage,
  1583. * where some other CPU will pick them up. These will not be
  1584. * required to pass though another grace period: They are done.
  1585. */
  1586. if (rdp->nxtlist != NULL) {
  1587. *rsp->orphan_donetail = rdp->nxtlist;
  1588. rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
  1589. }
  1590. /* Finally, initialize the rcu_data structure's list to empty. */
  1591. init_callback_list(rdp);
  1592. }
  1593. /*
  1594. * Adopt the RCU callbacks from the specified rcu_state structure's
  1595. * orphanage. The caller must hold the ->orphan_lock.
  1596. */
  1597. static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
  1598. {
  1599. int i;
  1600. struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
  1601. /* No-CBs CPUs are handled specially. */
  1602. if (rcu_nocb_adopt_orphan_cbs(rsp, rdp))
  1603. return;
  1604. /* Do the accounting first. */
  1605. rdp->qlen_lazy += rsp->qlen_lazy;
  1606. rdp->qlen += rsp->qlen;
  1607. rdp->n_cbs_adopted += rsp->qlen;
  1608. if (rsp->qlen_lazy != rsp->qlen)
  1609. rcu_idle_count_callbacks_posted();
  1610. rsp->qlen_lazy = 0;
  1611. rsp->qlen = 0;
  1612. /*
  1613. * We do not need a memory barrier here because the only way we
  1614. * can get here if there is an rcu_barrier() in flight is if
  1615. * we are the task doing the rcu_barrier().
  1616. */
  1617. /* First adopt the ready-to-invoke callbacks. */
  1618. if (rsp->orphan_donelist != NULL) {
  1619. *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
  1620. *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
  1621. for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
  1622. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  1623. rdp->nxttail[i] = rsp->orphan_donetail;
  1624. rsp->orphan_donelist = NULL;
  1625. rsp->orphan_donetail = &rsp->orphan_donelist;
  1626. }
  1627. /* And then adopt the callbacks that still need a grace period. */
  1628. if (rsp->orphan_nxtlist != NULL) {
  1629. *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
  1630. rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
  1631. rsp->orphan_nxtlist = NULL;
  1632. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  1633. }
  1634. }
  1635. /*
  1636. * Trace the fact that this CPU is going offline.
  1637. */
  1638. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  1639. {
  1640. RCU_TRACE(unsigned long mask);
  1641. RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
  1642. RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
  1643. RCU_TRACE(mask = rdp->grpmask);
  1644. trace_rcu_grace_period(rsp->name,
  1645. rnp->gpnum + 1 - !!(rnp->qsmask & mask),
  1646. TPS("cpuofl"));
  1647. }
  1648. /*
  1649. * The CPU has been completely removed, and some other CPU is reporting
  1650. * this fact from process context. Do the remainder of the cleanup,
  1651. * including orphaning the outgoing CPU's RCU callbacks, and also
  1652. * adopting them. There can only be one CPU hotplug operation at a time,
  1653. * so no other CPU can be attempting to update rcu_cpu_kthread_task.
  1654. */
  1655. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  1656. {
  1657. unsigned long flags;
  1658. unsigned long mask;
  1659. int need_report = 0;
  1660. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1661. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  1662. /* Adjust any no-longer-needed kthreads. */
  1663. rcu_boost_kthread_setaffinity(rnp, -1);
  1664. /* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */
  1665. /* Exclude any attempts to start a new grace period. */
  1666. mutex_lock(&rsp->onoff_mutex);
  1667. raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
  1668. /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
  1669. rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
  1670. rcu_adopt_orphan_cbs(rsp);
  1671. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  1672. mask = rdp->grpmask; /* rnp->grplo is constant. */
  1673. do {
  1674. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1675. rnp->qsmaskinit &= ~mask;
  1676. if (rnp->qsmaskinit != 0) {
  1677. if (rnp != rdp->mynode)
  1678. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1679. break;
  1680. }
  1681. if (rnp == rdp->mynode)
  1682. need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  1683. else
  1684. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1685. mask = rnp->grpmask;
  1686. rnp = rnp->parent;
  1687. } while (rnp != NULL);
  1688. /*
  1689. * We still hold the leaf rcu_node structure lock here, and
  1690. * irqs are still disabled. The reason for this subterfuge is
  1691. * because invoking rcu_report_unblock_qs_rnp() with ->orphan_lock
  1692. * held leads to deadlock.
  1693. */
  1694. raw_spin_unlock(&rsp->orphan_lock); /* irqs remain disabled. */
  1695. rnp = rdp->mynode;
  1696. if (need_report & RCU_OFL_TASKS_NORM_GP)
  1697. rcu_report_unblock_qs_rnp(rnp, flags);
  1698. else
  1699. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1700. if (need_report & RCU_OFL_TASKS_EXP_GP)
  1701. rcu_report_exp_rnp(rsp, rnp, true);
  1702. WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
  1703. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
  1704. cpu, rdp->qlen, rdp->nxtlist);
  1705. init_callback_list(rdp);
  1706. /* Disallow further callbacks on this CPU. */
  1707. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  1708. mutex_unlock(&rsp->onoff_mutex);
  1709. }
  1710. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1711. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  1712. {
  1713. }
  1714. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  1715. {
  1716. }
  1717. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1718. /*
  1719. * Invoke any RCU callbacks that have made it to the end of their grace
  1720. * period. Thottle as specified by rdp->blimit.
  1721. */
  1722. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  1723. {
  1724. unsigned long flags;
  1725. struct rcu_head *next, *list, **tail;
  1726. long bl, count, count_lazy;
  1727. int i;
  1728. /* If no callbacks are ready, just return. */
  1729. if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
  1730. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
  1731. trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist),
  1732. need_resched(), is_idle_task(current),
  1733. rcu_is_callbacks_kthread());
  1734. return;
  1735. }
  1736. /*
  1737. * Extract the list of ready callbacks, disabling to prevent
  1738. * races with call_rcu() from interrupt handlers.
  1739. */
  1740. local_irq_save(flags);
  1741. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  1742. bl = rdp->blimit;
  1743. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
  1744. list = rdp->nxtlist;
  1745. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  1746. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1747. tail = rdp->nxttail[RCU_DONE_TAIL];
  1748. for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
  1749. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  1750. rdp->nxttail[i] = &rdp->nxtlist;
  1751. local_irq_restore(flags);
  1752. /* Invoke callbacks. */
  1753. count = count_lazy = 0;
  1754. while (list) {
  1755. next = list->next;
  1756. prefetch(next);
  1757. debug_rcu_head_unqueue(list);
  1758. if (__rcu_reclaim(rsp->name, list))
  1759. count_lazy++;
  1760. list = next;
  1761. /* Stop only if limit reached and CPU has something to do. */
  1762. if (++count >= bl &&
  1763. (need_resched() ||
  1764. (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
  1765. break;
  1766. }
  1767. local_irq_save(flags);
  1768. trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
  1769. is_idle_task(current),
  1770. rcu_is_callbacks_kthread());
  1771. /* Update count, and requeue any remaining callbacks. */
  1772. if (list != NULL) {
  1773. *tail = rdp->nxtlist;
  1774. rdp->nxtlist = list;
  1775. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1776. if (&rdp->nxtlist == rdp->nxttail[i])
  1777. rdp->nxttail[i] = tail;
  1778. else
  1779. break;
  1780. }
  1781. smp_mb(); /* List handling before counting for rcu_barrier(). */
  1782. rdp->qlen_lazy -= count_lazy;
  1783. ACCESS_ONCE(rdp->qlen) -= count;
  1784. rdp->n_cbs_invoked += count;
  1785. /* Reinstate batch limit if we have worked down the excess. */
  1786. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  1787. rdp->blimit = blimit;
  1788. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  1789. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  1790. rdp->qlen_last_fqs_check = 0;
  1791. rdp->n_force_qs_snap = rsp->n_force_qs;
  1792. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  1793. rdp->qlen_last_fqs_check = rdp->qlen;
  1794. WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
  1795. local_irq_restore(flags);
  1796. /* Re-invoke RCU core processing if there are callbacks remaining. */
  1797. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1798. invoke_rcu_core();
  1799. }
  1800. /*
  1801. * Check to see if this CPU is in a non-context-switch quiescent state
  1802. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  1803. * Also schedule RCU core processing.
  1804. *
  1805. * This function must be called from hardirq context. It is normally
  1806. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  1807. * false, there is no point in invoking rcu_check_callbacks().
  1808. */
  1809. void rcu_check_callbacks(int cpu, int user)
  1810. {
  1811. trace_rcu_utilization(TPS("Start scheduler-tick"));
  1812. increment_cpu_stall_ticks();
  1813. if (user || rcu_is_cpu_rrupt_from_idle()) {
  1814. /*
  1815. * Get here if this CPU took its interrupt from user
  1816. * mode or from the idle loop, and if this is not a
  1817. * nested interrupt. In this case, the CPU is in
  1818. * a quiescent state, so note it.
  1819. *
  1820. * No memory barrier is required here because both
  1821. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  1822. * variables that other CPUs neither access nor modify,
  1823. * at least not while the corresponding CPU is online.
  1824. */
  1825. rcu_sched_qs(cpu);
  1826. rcu_bh_qs(cpu);
  1827. } else if (!in_softirq()) {
  1828. /*
  1829. * Get here if this CPU did not take its interrupt from
  1830. * softirq, in other words, if it is not interrupting
  1831. * a rcu_bh read-side critical section. This is an _bh
  1832. * critical section, so note it.
  1833. */
  1834. rcu_bh_qs(cpu);
  1835. }
  1836. rcu_preempt_check_callbacks(cpu);
  1837. if (rcu_pending(cpu))
  1838. invoke_rcu_core();
  1839. trace_rcu_utilization(TPS("End scheduler-tick"));
  1840. }
  1841. /*
  1842. * Scan the leaf rcu_node structures, processing dyntick state for any that
  1843. * have not yet encountered a quiescent state, using the function specified.
  1844. * Also initiate boosting for any threads blocked on the root rcu_node.
  1845. *
  1846. * The caller must have suppressed start of new grace periods.
  1847. */
  1848. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
  1849. {
  1850. unsigned long bit;
  1851. int cpu;
  1852. unsigned long flags;
  1853. unsigned long mask;
  1854. struct rcu_node *rnp;
  1855. rcu_for_each_leaf_node(rsp, rnp) {
  1856. cond_resched();
  1857. mask = 0;
  1858. raw_spin_lock_irqsave(&rnp->lock, flags);
  1859. if (!rcu_gp_in_progress(rsp)) {
  1860. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1861. return;
  1862. }
  1863. if (rnp->qsmask == 0) {
  1864. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  1865. continue;
  1866. }
  1867. cpu = rnp->grplo;
  1868. bit = 1;
  1869. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1870. if ((rnp->qsmask & bit) != 0 &&
  1871. f(per_cpu_ptr(rsp->rda, cpu)))
  1872. mask |= bit;
  1873. }
  1874. if (mask != 0) {
  1875. /* rcu_report_qs_rnp() releases rnp->lock. */
  1876. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1877. continue;
  1878. }
  1879. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1880. }
  1881. rnp = rcu_get_root(rsp);
  1882. if (rnp->qsmask == 0) {
  1883. raw_spin_lock_irqsave(&rnp->lock, flags);
  1884. rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
  1885. }
  1886. }
  1887. /*
  1888. * Force quiescent states on reluctant CPUs, and also detect which
  1889. * CPUs are in dyntick-idle mode.
  1890. */
  1891. static void force_quiescent_state(struct rcu_state *rsp)
  1892. {
  1893. unsigned long flags;
  1894. bool ret;
  1895. struct rcu_node *rnp;
  1896. struct rcu_node *rnp_old = NULL;
  1897. /* Funnel through hierarchy to reduce memory contention. */
  1898. rnp = per_cpu_ptr(rsp->rda, raw_smp_processor_id())->mynode;
  1899. for (; rnp != NULL; rnp = rnp->parent) {
  1900. ret = (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
  1901. !raw_spin_trylock(&rnp->fqslock);
  1902. if (rnp_old != NULL)
  1903. raw_spin_unlock(&rnp_old->fqslock);
  1904. if (ret) {
  1905. rsp->n_force_qs_lh++;
  1906. return;
  1907. }
  1908. rnp_old = rnp;
  1909. }
  1910. /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
  1911. /* Reached the root of the rcu_node tree, acquire lock. */
  1912. raw_spin_lock_irqsave(&rnp_old->lock, flags);
  1913. raw_spin_unlock(&rnp_old->fqslock);
  1914. if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1915. rsp->n_force_qs_lh++;
  1916. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  1917. return; /* Someone beat us to it. */
  1918. }
  1919. rsp->gp_flags |= RCU_GP_FLAG_FQS;
  1920. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  1921. wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
  1922. }
  1923. /*
  1924. * This does the RCU core processing work for the specified rcu_state
  1925. * and rcu_data structures. This may be called only from the CPU to
  1926. * whom the rdp belongs.
  1927. */
  1928. static void
  1929. __rcu_process_callbacks(struct rcu_state *rsp)
  1930. {
  1931. unsigned long flags;
  1932. struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
  1933. WARN_ON_ONCE(rdp->beenonline == 0);
  1934. /* Update RCU state based on any recent quiescent states. */
  1935. rcu_check_quiescent_state(rsp, rdp);
  1936. /* Does this CPU require a not-yet-started grace period? */
  1937. local_irq_save(flags);
  1938. if (cpu_needs_another_gp(rsp, rdp)) {
  1939. raw_spin_lock(&rcu_get_root(rsp)->lock); /* irqs disabled. */
  1940. rcu_start_gp(rsp);
  1941. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  1942. } else {
  1943. local_irq_restore(flags);
  1944. }
  1945. /* If there are callbacks ready, invoke them. */
  1946. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1947. invoke_rcu_callbacks(rsp, rdp);
  1948. }
  1949. /*
  1950. * Do RCU core processing for the current CPU.
  1951. */
  1952. static void rcu_process_callbacks(struct softirq_action *unused)
  1953. {
  1954. struct rcu_state *rsp;
  1955. if (cpu_is_offline(smp_processor_id()))
  1956. return;
  1957. trace_rcu_utilization(TPS("Start RCU core"));
  1958. for_each_rcu_flavor(rsp)
  1959. __rcu_process_callbacks(rsp);
  1960. trace_rcu_utilization(TPS("End RCU core"));
  1961. }
  1962. /*
  1963. * Schedule RCU callback invocation. If the specified type of RCU
  1964. * does not support RCU priority boosting, just do a direct call,
  1965. * otherwise wake up the per-CPU kernel kthread. Note that because we
  1966. * are running on the current CPU with interrupts disabled, the
  1967. * rcu_cpu_kthread_task cannot disappear out from under us.
  1968. */
  1969. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1970. {
  1971. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  1972. return;
  1973. if (likely(!rsp->boost)) {
  1974. rcu_do_batch(rsp, rdp);
  1975. return;
  1976. }
  1977. invoke_rcu_callbacks_kthread();
  1978. }
  1979. static void invoke_rcu_core(void)
  1980. {
  1981. if (cpu_online(smp_processor_id()))
  1982. raise_softirq(RCU_SOFTIRQ);
  1983. }
  1984. /*
  1985. * Handle any core-RCU processing required by a call_rcu() invocation.
  1986. */
  1987. static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
  1988. struct rcu_head *head, unsigned long flags)
  1989. {
  1990. /*
  1991. * If called from an extended quiescent state, invoke the RCU
  1992. * core in order to force a re-evaluation of RCU's idleness.
  1993. */
  1994. if (rcu_is_cpu_idle() && cpu_online(smp_processor_id()))
  1995. invoke_rcu_core();
  1996. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  1997. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  1998. return;
  1999. /*
  2000. * Force the grace period if too many callbacks or too long waiting.
  2001. * Enforce hysteresis, and don't invoke force_quiescent_state()
  2002. * if some other CPU has recently done so. Also, don't bother
  2003. * invoking force_quiescent_state() if the newly enqueued callback
  2004. * is the only one waiting for a grace period to complete.
  2005. */
  2006. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  2007. /* Are we ignoring a completed grace period? */
  2008. note_gp_changes(rsp, rdp);
  2009. /* Start a new grace period if one not already started. */
  2010. if (!rcu_gp_in_progress(rsp)) {
  2011. struct rcu_node *rnp_root = rcu_get_root(rsp);
  2012. raw_spin_lock(&rnp_root->lock);
  2013. rcu_start_gp(rsp);
  2014. raw_spin_unlock(&rnp_root->lock);
  2015. } else {
  2016. /* Give the grace period a kick. */
  2017. rdp->blimit = LONG_MAX;
  2018. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  2019. *rdp->nxttail[RCU_DONE_TAIL] != head)
  2020. force_quiescent_state(rsp);
  2021. rdp->n_force_qs_snap = rsp->n_force_qs;
  2022. rdp->qlen_last_fqs_check = rdp->qlen;
  2023. }
  2024. }
  2025. }
  2026. /*
  2027. * Helper function for call_rcu() and friends. The cpu argument will
  2028. * normally be -1, indicating "currently running CPU". It may specify
  2029. * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
  2030. * is expected to specify a CPU.
  2031. */
  2032. static void
  2033. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  2034. struct rcu_state *rsp, int cpu, bool lazy)
  2035. {
  2036. unsigned long flags;
  2037. struct rcu_data *rdp;
  2038. WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */
  2039. debug_rcu_head_queue(head);
  2040. head->func = func;
  2041. head->next = NULL;
  2042. /*
  2043. * Opportunistically note grace-period endings and beginnings.
  2044. * Note that we might see a beginning right after we see an
  2045. * end, but never vice versa, since this CPU has to pass through
  2046. * a quiescent state betweentimes.
  2047. */
  2048. local_irq_save(flags);
  2049. rdp = this_cpu_ptr(rsp->rda);
  2050. /* Add the callback to our list. */
  2051. if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
  2052. int offline;
  2053. if (cpu != -1)
  2054. rdp = per_cpu_ptr(rsp->rda, cpu);
  2055. offline = !__call_rcu_nocb(rdp, head, lazy);
  2056. WARN_ON_ONCE(offline);
  2057. /* _call_rcu() is illegal on offline CPU; leak the callback. */
  2058. local_irq_restore(flags);
  2059. return;
  2060. }
  2061. ACCESS_ONCE(rdp->qlen)++;
  2062. if (lazy)
  2063. rdp->qlen_lazy++;
  2064. else
  2065. rcu_idle_count_callbacks_posted();
  2066. smp_mb(); /* Count before adding callback for rcu_barrier(). */
  2067. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  2068. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  2069. if (__is_kfree_rcu_offset((unsigned long)func))
  2070. trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
  2071. rdp->qlen_lazy, rdp->qlen);
  2072. else
  2073. trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
  2074. /* Go handle any RCU core processing required. */
  2075. __call_rcu_core(rsp, rdp, head, flags);
  2076. local_irq_restore(flags);
  2077. }
  2078. /*
  2079. * Queue an RCU-sched callback for invocation after a grace period.
  2080. */
  2081. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  2082. {
  2083. __call_rcu(head, func, &rcu_sched_state, -1, 0);
  2084. }
  2085. EXPORT_SYMBOL_GPL(call_rcu_sched);
  2086. /*
  2087. * Queue an RCU callback for invocation after a quicker grace period.
  2088. */
  2089. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  2090. {
  2091. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  2092. }
  2093. EXPORT_SYMBOL_GPL(call_rcu_bh);
  2094. /*
  2095. * Because a context switch is a grace period for RCU-sched and RCU-bh,
  2096. * any blocking grace-period wait automatically implies a grace period
  2097. * if there is only one CPU online at any point time during execution
  2098. * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
  2099. * occasionally incorrectly indicate that there are multiple CPUs online
  2100. * when there was in fact only one the whole time, as this just adds
  2101. * some overhead: RCU still operates correctly.
  2102. */
  2103. static inline int rcu_blocking_is_gp(void)
  2104. {
  2105. int ret;
  2106. might_sleep(); /* Check for RCU read-side critical section. */
  2107. preempt_disable();
  2108. ret = num_online_cpus() <= 1;
  2109. preempt_enable();
  2110. return ret;
  2111. }
  2112. /**
  2113. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  2114. *
  2115. * Control will return to the caller some time after a full rcu-sched
  2116. * grace period has elapsed, in other words after all currently executing
  2117. * rcu-sched read-side critical sections have completed. These read-side
  2118. * critical sections are delimited by rcu_read_lock_sched() and
  2119. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  2120. * local_irq_disable(), and so on may be used in place of
  2121. * rcu_read_lock_sched().
  2122. *
  2123. * This means that all preempt_disable code sequences, including NMI and
  2124. * non-threaded hardware-interrupt handlers, in progress on entry will
  2125. * have completed before this primitive returns. However, this does not
  2126. * guarantee that softirq handlers will have completed, since in some
  2127. * kernels, these handlers can run in process context, and can block.
  2128. *
  2129. * Note that this guarantee implies further memory-ordering guarantees.
  2130. * On systems with more than one CPU, when synchronize_sched() returns,
  2131. * each CPU is guaranteed to have executed a full memory barrier since the
  2132. * end of its last RCU-sched read-side critical section whose beginning
  2133. * preceded the call to synchronize_sched(). In addition, each CPU having
  2134. * an RCU read-side critical section that extends beyond the return from
  2135. * synchronize_sched() is guaranteed to have executed a full memory barrier
  2136. * after the beginning of synchronize_sched() and before the beginning of
  2137. * that RCU read-side critical section. Note that these guarantees include
  2138. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  2139. * that are executing in the kernel.
  2140. *
  2141. * Furthermore, if CPU A invoked synchronize_sched(), which returned
  2142. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  2143. * to have executed a full memory barrier during the execution of
  2144. * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
  2145. * again only if the system has more than one CPU).
  2146. *
  2147. * This primitive provides the guarantees made by the (now removed)
  2148. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  2149. * guarantees that rcu_read_lock() sections will have completed.
  2150. * In "classic RCU", these two guarantees happen to be one and
  2151. * the same, but can differ in realtime RCU implementations.
  2152. */
  2153. void synchronize_sched(void)
  2154. {
  2155. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  2156. !lock_is_held(&rcu_lock_map) &&
  2157. !lock_is_held(&rcu_sched_lock_map),
  2158. "Illegal synchronize_sched() in RCU-sched read-side critical section");
  2159. if (rcu_blocking_is_gp())
  2160. return;
  2161. if (rcu_expedited)
  2162. synchronize_sched_expedited();
  2163. else
  2164. wait_rcu_gp(call_rcu_sched);
  2165. }
  2166. EXPORT_SYMBOL_GPL(synchronize_sched);
  2167. /**
  2168. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  2169. *
  2170. * Control will return to the caller some time after a full rcu_bh grace
  2171. * period has elapsed, in other words after all currently executing rcu_bh
  2172. * read-side critical sections have completed. RCU read-side critical
  2173. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  2174. * and may be nested.
  2175. *
  2176. * See the description of synchronize_sched() for more detailed information
  2177. * on memory ordering guarantees.
  2178. */
  2179. void synchronize_rcu_bh(void)
  2180. {
  2181. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  2182. !lock_is_held(&rcu_lock_map) &&
  2183. !lock_is_held(&rcu_sched_lock_map),
  2184. "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
  2185. if (rcu_blocking_is_gp())
  2186. return;
  2187. if (rcu_expedited)
  2188. synchronize_rcu_bh_expedited();
  2189. else
  2190. wait_rcu_gp(call_rcu_bh);
  2191. }
  2192. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  2193. static int synchronize_sched_expedited_cpu_stop(void *data)
  2194. {
  2195. /*
  2196. * There must be a full memory barrier on each affected CPU
  2197. * between the time that try_stop_cpus() is called and the
  2198. * time that it returns.
  2199. *
  2200. * In the current initial implementation of cpu_stop, the
  2201. * above condition is already met when the control reaches
  2202. * this point and the following smp_mb() is not strictly
  2203. * necessary. Do smp_mb() anyway for documentation and
  2204. * robustness against future implementation changes.
  2205. */
  2206. smp_mb(); /* See above comment block. */
  2207. return 0;
  2208. }
  2209. /**
  2210. * synchronize_sched_expedited - Brute-force RCU-sched grace period
  2211. *
  2212. * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
  2213. * approach to force the grace period to end quickly. This consumes
  2214. * significant time on all CPUs and is unfriendly to real-time workloads,
  2215. * so is thus not recommended for any sort of common-case code. In fact,
  2216. * if you are using synchronize_sched_expedited() in a loop, please
  2217. * restructure your code to batch your updates, and then use a single
  2218. * synchronize_sched() instead.
  2219. *
  2220. * Note that it is illegal to call this function while holding any lock
  2221. * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
  2222. * to call this function from a CPU-hotplug notifier. Failing to observe
  2223. * these restriction will result in deadlock.
  2224. *
  2225. * This implementation can be thought of as an application of ticket
  2226. * locking to RCU, with sync_sched_expedited_started and
  2227. * sync_sched_expedited_done taking on the roles of the halves
  2228. * of the ticket-lock word. Each task atomically increments
  2229. * sync_sched_expedited_started upon entry, snapshotting the old value,
  2230. * then attempts to stop all the CPUs. If this succeeds, then each
  2231. * CPU will have executed a context switch, resulting in an RCU-sched
  2232. * grace period. We are then done, so we use atomic_cmpxchg() to
  2233. * update sync_sched_expedited_done to match our snapshot -- but
  2234. * only if someone else has not already advanced past our snapshot.
  2235. *
  2236. * On the other hand, if try_stop_cpus() fails, we check the value
  2237. * of sync_sched_expedited_done. If it has advanced past our
  2238. * initial snapshot, then someone else must have forced a grace period
  2239. * some time after we took our snapshot. In this case, our work is
  2240. * done for us, and we can simply return. Otherwise, we try again,
  2241. * but keep our initial snapshot for purposes of checking for someone
  2242. * doing our work for us.
  2243. *
  2244. * If we fail too many times in a row, we fall back to synchronize_sched().
  2245. */
  2246. void synchronize_sched_expedited(void)
  2247. {
  2248. long firstsnap, s, snap;
  2249. int trycount = 0;
  2250. struct rcu_state *rsp = &rcu_sched_state;
  2251. /*
  2252. * If we are in danger of counter wrap, just do synchronize_sched().
  2253. * By allowing sync_sched_expedited_started to advance no more than
  2254. * ULONG_MAX/8 ahead of sync_sched_expedited_done, we are ensuring
  2255. * that more than 3.5 billion CPUs would be required to force a
  2256. * counter wrap on a 32-bit system. Quite a few more CPUs would of
  2257. * course be required on a 64-bit system.
  2258. */
  2259. if (ULONG_CMP_GE((ulong)atomic_long_read(&rsp->expedited_start),
  2260. (ulong)atomic_long_read(&rsp->expedited_done) +
  2261. ULONG_MAX / 8)) {
  2262. synchronize_sched();
  2263. atomic_long_inc(&rsp->expedited_wrap);
  2264. return;
  2265. }
  2266. /*
  2267. * Take a ticket. Note that atomic_inc_return() implies a
  2268. * full memory barrier.
  2269. */
  2270. snap = atomic_long_inc_return(&rsp->expedited_start);
  2271. firstsnap = snap;
  2272. get_online_cpus();
  2273. WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
  2274. /*
  2275. * Each pass through the following loop attempts to force a
  2276. * context switch on each CPU.
  2277. */
  2278. while (try_stop_cpus(cpu_online_mask,
  2279. synchronize_sched_expedited_cpu_stop,
  2280. NULL) == -EAGAIN) {
  2281. put_online_cpus();
  2282. atomic_long_inc(&rsp->expedited_tryfail);
  2283. /* Check to see if someone else did our work for us. */
  2284. s = atomic_long_read(&rsp->expedited_done);
  2285. if (ULONG_CMP_GE((ulong)s, (ulong)firstsnap)) {
  2286. /* ensure test happens before caller kfree */
  2287. smp_mb__before_atomic_inc(); /* ^^^ */
  2288. atomic_long_inc(&rsp->expedited_workdone1);
  2289. return;
  2290. }
  2291. /* No joy, try again later. Or just synchronize_sched(). */
  2292. if (trycount++ < 10) {
  2293. udelay(trycount * num_online_cpus());
  2294. } else {
  2295. wait_rcu_gp(call_rcu_sched);
  2296. atomic_long_inc(&rsp->expedited_normal);
  2297. return;
  2298. }
  2299. /* Recheck to see if someone else did our work for us. */
  2300. s = atomic_long_read(&rsp->expedited_done);
  2301. if (ULONG_CMP_GE((ulong)s, (ulong)firstsnap)) {
  2302. /* ensure test happens before caller kfree */
  2303. smp_mb__before_atomic_inc(); /* ^^^ */
  2304. atomic_long_inc(&rsp->expedited_workdone2);
  2305. return;
  2306. }
  2307. /*
  2308. * Refetching sync_sched_expedited_started allows later
  2309. * callers to piggyback on our grace period. We retry
  2310. * after they started, so our grace period works for them,
  2311. * and they started after our first try, so their grace
  2312. * period works for us.
  2313. */
  2314. get_online_cpus();
  2315. snap = atomic_long_read(&rsp->expedited_start);
  2316. smp_mb(); /* ensure read is before try_stop_cpus(). */
  2317. }
  2318. atomic_long_inc(&rsp->expedited_stoppedcpus);
  2319. /*
  2320. * Everyone up to our most recent fetch is covered by our grace
  2321. * period. Update the counter, but only if our work is still
  2322. * relevant -- which it won't be if someone who started later
  2323. * than we did already did their update.
  2324. */
  2325. do {
  2326. atomic_long_inc(&rsp->expedited_done_tries);
  2327. s = atomic_long_read(&rsp->expedited_done);
  2328. if (ULONG_CMP_GE((ulong)s, (ulong)snap)) {
  2329. /* ensure test happens before caller kfree */
  2330. smp_mb__before_atomic_inc(); /* ^^^ */
  2331. atomic_long_inc(&rsp->expedited_done_lost);
  2332. break;
  2333. }
  2334. } while (atomic_long_cmpxchg(&rsp->expedited_done, s, snap) != s);
  2335. atomic_long_inc(&rsp->expedited_done_exit);
  2336. put_online_cpus();
  2337. }
  2338. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  2339. /*
  2340. * Check to see if there is any immediate RCU-related work to be done
  2341. * by the current CPU, for the specified type of RCU, returning 1 if so.
  2342. * The checks are in order of increasing expense: checks that can be
  2343. * carried out against CPU-local state are performed first. However,
  2344. * we must check for CPU stalls first, else we might not get a chance.
  2345. */
  2346. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  2347. {
  2348. struct rcu_node *rnp = rdp->mynode;
  2349. rdp->n_rcu_pending++;
  2350. /* Check for CPU stalls, if enabled. */
  2351. check_cpu_stall(rsp, rdp);
  2352. /* Is the RCU core waiting for a quiescent state from this CPU? */
  2353. if (rcu_scheduler_fully_active &&
  2354. rdp->qs_pending && !rdp->passed_quiesce) {
  2355. rdp->n_rp_qs_pending++;
  2356. } else if (rdp->qs_pending && rdp->passed_quiesce) {
  2357. rdp->n_rp_report_qs++;
  2358. return 1;
  2359. }
  2360. /* Does this CPU have callbacks ready to invoke? */
  2361. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  2362. rdp->n_rp_cb_ready++;
  2363. return 1;
  2364. }
  2365. /* Has RCU gone idle with this CPU needing another grace period? */
  2366. if (cpu_needs_another_gp(rsp, rdp)) {
  2367. rdp->n_rp_cpu_needs_gp++;
  2368. return 1;
  2369. }
  2370. /* Has another RCU grace period completed? */
  2371. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  2372. rdp->n_rp_gp_completed++;
  2373. return 1;
  2374. }
  2375. /* Has a new RCU grace period started? */
  2376. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  2377. rdp->n_rp_gp_started++;
  2378. return 1;
  2379. }
  2380. /* nothing to do */
  2381. rdp->n_rp_need_nothing++;
  2382. return 0;
  2383. }
  2384. /*
  2385. * Check to see if there is any immediate RCU-related work to be done
  2386. * by the current CPU, returning 1 if so. This function is part of the
  2387. * RCU implementation; it is -not- an exported member of the RCU API.
  2388. */
  2389. static int rcu_pending(int cpu)
  2390. {
  2391. struct rcu_state *rsp;
  2392. for_each_rcu_flavor(rsp)
  2393. if (__rcu_pending(rsp, per_cpu_ptr(rsp->rda, cpu)))
  2394. return 1;
  2395. return 0;
  2396. }
  2397. /*
  2398. * Return true if the specified CPU has any callback. If all_lazy is
  2399. * non-NULL, store an indication of whether all callbacks are lazy.
  2400. * (If there are no callbacks, all of them are deemed to be lazy.)
  2401. */
  2402. static int rcu_cpu_has_callbacks(int cpu, bool *all_lazy)
  2403. {
  2404. bool al = true;
  2405. bool hc = false;
  2406. struct rcu_data *rdp;
  2407. struct rcu_state *rsp;
  2408. for_each_rcu_flavor(rsp) {
  2409. rdp = per_cpu_ptr(rsp->rda, cpu);
  2410. if (rdp->qlen != rdp->qlen_lazy)
  2411. al = false;
  2412. if (rdp->nxtlist)
  2413. hc = true;
  2414. }
  2415. if (all_lazy)
  2416. *all_lazy = al;
  2417. return hc;
  2418. }
  2419. /*
  2420. * Helper function for _rcu_barrier() tracing. If tracing is disabled,
  2421. * the compiler is expected to optimize this away.
  2422. */
  2423. static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
  2424. int cpu, unsigned long done)
  2425. {
  2426. trace_rcu_barrier(rsp->name, s, cpu,
  2427. atomic_read(&rsp->barrier_cpu_count), done);
  2428. }
  2429. /*
  2430. * RCU callback function for _rcu_barrier(). If we are last, wake
  2431. * up the task executing _rcu_barrier().
  2432. */
  2433. static void rcu_barrier_callback(struct rcu_head *rhp)
  2434. {
  2435. struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
  2436. struct rcu_state *rsp = rdp->rsp;
  2437. if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
  2438. _rcu_barrier_trace(rsp, "LastCB", -1, rsp->n_barrier_done);
  2439. complete(&rsp->barrier_completion);
  2440. } else {
  2441. _rcu_barrier_trace(rsp, "CB", -1, rsp->n_barrier_done);
  2442. }
  2443. }
  2444. /*
  2445. * Called with preemption disabled, and from cross-cpu IRQ context.
  2446. */
  2447. static void rcu_barrier_func(void *type)
  2448. {
  2449. struct rcu_state *rsp = type;
  2450. struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
  2451. _rcu_barrier_trace(rsp, "IRQ", -1, rsp->n_barrier_done);
  2452. atomic_inc(&rsp->barrier_cpu_count);
  2453. rsp->call(&rdp->barrier_head, rcu_barrier_callback);
  2454. }
  2455. /*
  2456. * Orchestrate the specified type of RCU barrier, waiting for all
  2457. * RCU callbacks of the specified type to complete.
  2458. */
  2459. static void _rcu_barrier(struct rcu_state *rsp)
  2460. {
  2461. int cpu;
  2462. struct rcu_data *rdp;
  2463. unsigned long snap = ACCESS_ONCE(rsp->n_barrier_done);
  2464. unsigned long snap_done;
  2465. _rcu_barrier_trace(rsp, "Begin", -1, snap);
  2466. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  2467. mutex_lock(&rsp->barrier_mutex);
  2468. /*
  2469. * Ensure that all prior references, including to ->n_barrier_done,
  2470. * are ordered before the _rcu_barrier() machinery.
  2471. */
  2472. smp_mb(); /* See above block comment. */
  2473. /*
  2474. * Recheck ->n_barrier_done to see if others did our work for us.
  2475. * This means checking ->n_barrier_done for an even-to-odd-to-even
  2476. * transition. The "if" expression below therefore rounds the old
  2477. * value up to the next even number and adds two before comparing.
  2478. */
  2479. snap_done = ACCESS_ONCE(rsp->n_barrier_done);
  2480. _rcu_barrier_trace(rsp, "Check", -1, snap_done);
  2481. if (ULONG_CMP_GE(snap_done, ((snap + 1) & ~0x1) + 2)) {
  2482. _rcu_barrier_trace(rsp, "EarlyExit", -1, snap_done);
  2483. smp_mb(); /* caller's subsequent code after above check. */
  2484. mutex_unlock(&rsp->barrier_mutex);
  2485. return;
  2486. }
  2487. /*
  2488. * Increment ->n_barrier_done to avoid duplicate work. Use
  2489. * ACCESS_ONCE() to prevent the compiler from speculating
  2490. * the increment to precede the early-exit check.
  2491. */
  2492. ACCESS_ONCE(rsp->n_barrier_done)++;
  2493. WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 1);
  2494. _rcu_barrier_trace(rsp, "Inc1", -1, rsp->n_barrier_done);
  2495. smp_mb(); /* Order ->n_barrier_done increment with below mechanism. */
  2496. /*
  2497. * Initialize the count to one rather than to zero in order to
  2498. * avoid a too-soon return to zero in case of a short grace period
  2499. * (or preemption of this task). Exclude CPU-hotplug operations
  2500. * to ensure that no offline CPU has callbacks queued.
  2501. */
  2502. init_completion(&rsp->barrier_completion);
  2503. atomic_set(&rsp->barrier_cpu_count, 1);
  2504. get_online_cpus();
  2505. /*
  2506. * Force each CPU with callbacks to register a new callback.
  2507. * When that callback is invoked, we will know that all of the
  2508. * corresponding CPU's preceding callbacks have been invoked.
  2509. */
  2510. for_each_possible_cpu(cpu) {
  2511. if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
  2512. continue;
  2513. rdp = per_cpu_ptr(rsp->rda, cpu);
  2514. if (rcu_is_nocb_cpu(cpu)) {
  2515. _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
  2516. rsp->n_barrier_done);
  2517. atomic_inc(&rsp->barrier_cpu_count);
  2518. __call_rcu(&rdp->barrier_head, rcu_barrier_callback,
  2519. rsp, cpu, 0);
  2520. } else if (ACCESS_ONCE(rdp->qlen)) {
  2521. _rcu_barrier_trace(rsp, "OnlineQ", cpu,
  2522. rsp->n_barrier_done);
  2523. smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
  2524. } else {
  2525. _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
  2526. rsp->n_barrier_done);
  2527. }
  2528. }
  2529. put_online_cpus();
  2530. /*
  2531. * Now that we have an rcu_barrier_callback() callback on each
  2532. * CPU, and thus each counted, remove the initial count.
  2533. */
  2534. if (atomic_dec_and_test(&rsp->barrier_cpu_count))
  2535. complete(&rsp->barrier_completion);
  2536. /* Increment ->n_barrier_done to prevent duplicate work. */
  2537. smp_mb(); /* Keep increment after above mechanism. */
  2538. ACCESS_ONCE(rsp->n_barrier_done)++;
  2539. WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 0);
  2540. _rcu_barrier_trace(rsp, "Inc2", -1, rsp->n_barrier_done);
  2541. smp_mb(); /* Keep increment before caller's subsequent code. */
  2542. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  2543. wait_for_completion(&rsp->barrier_completion);
  2544. /* Other rcu_barrier() invocations can now safely proceed. */
  2545. mutex_unlock(&rsp->barrier_mutex);
  2546. }
  2547. /**
  2548. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  2549. */
  2550. void rcu_barrier_bh(void)
  2551. {
  2552. _rcu_barrier(&rcu_bh_state);
  2553. }
  2554. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  2555. /**
  2556. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  2557. */
  2558. void rcu_barrier_sched(void)
  2559. {
  2560. _rcu_barrier(&rcu_sched_state);
  2561. }
  2562. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  2563. /*
  2564. * Do boot-time initialization of a CPU's per-CPU RCU data.
  2565. */
  2566. static void __init
  2567. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  2568. {
  2569. unsigned long flags;
  2570. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2571. struct rcu_node *rnp = rcu_get_root(rsp);
  2572. /* Set up local state, ensuring consistent view of global state. */
  2573. raw_spin_lock_irqsave(&rnp->lock, flags);
  2574. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  2575. init_callback_list(rdp);
  2576. rdp->qlen_lazy = 0;
  2577. ACCESS_ONCE(rdp->qlen) = 0;
  2578. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  2579. WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
  2580. WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
  2581. rdp->cpu = cpu;
  2582. rdp->rsp = rsp;
  2583. rcu_boot_init_nocb_percpu_data(rdp);
  2584. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  2585. }
  2586. /*
  2587. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  2588. * offline event can be happening at a given time. Note also that we
  2589. * can accept some slop in the rsp->completed access due to the fact
  2590. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  2591. */
  2592. static void
  2593. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
  2594. {
  2595. unsigned long flags;
  2596. unsigned long mask;
  2597. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2598. struct rcu_node *rnp = rcu_get_root(rsp);
  2599. /* Exclude new grace periods. */
  2600. mutex_lock(&rsp->onoff_mutex);
  2601. /* Set up local state, ensuring consistent view of global state. */
  2602. raw_spin_lock_irqsave(&rnp->lock, flags);
  2603. rdp->beenonline = 1; /* We have now been online. */
  2604. rdp->preemptible = preemptible;
  2605. rdp->qlen_last_fqs_check = 0;
  2606. rdp->n_force_qs_snap = rsp->n_force_qs;
  2607. rdp->blimit = blimit;
  2608. init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
  2609. rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  2610. atomic_set(&rdp->dynticks->dynticks,
  2611. (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
  2612. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  2613. /* Add CPU to rcu_node bitmasks. */
  2614. rnp = rdp->mynode;
  2615. mask = rdp->grpmask;
  2616. do {
  2617. /* Exclude any attempts to start a new GP on small systems. */
  2618. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  2619. rnp->qsmaskinit |= mask;
  2620. mask = rnp->grpmask;
  2621. if (rnp == rdp->mynode) {
  2622. /*
  2623. * If there is a grace period in progress, we will
  2624. * set up to wait for it next time we run the
  2625. * RCU core code.
  2626. */
  2627. rdp->gpnum = rnp->completed;
  2628. rdp->completed = rnp->completed;
  2629. rdp->passed_quiesce = 0;
  2630. rdp->qs_pending = 0;
  2631. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
  2632. }
  2633. raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
  2634. rnp = rnp->parent;
  2635. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  2636. local_irq_restore(flags);
  2637. mutex_unlock(&rsp->onoff_mutex);
  2638. }
  2639. static void rcu_prepare_cpu(int cpu)
  2640. {
  2641. struct rcu_state *rsp;
  2642. for_each_rcu_flavor(rsp)
  2643. rcu_init_percpu_data(cpu, rsp,
  2644. strcmp(rsp->name, "rcu_preempt") == 0);
  2645. }
  2646. /*
  2647. * Handle CPU online/offline notification events.
  2648. */
  2649. static int rcu_cpu_notify(struct notifier_block *self,
  2650. unsigned long action, void *hcpu)
  2651. {
  2652. long cpu = (long)hcpu;
  2653. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  2654. struct rcu_node *rnp = rdp->mynode;
  2655. struct rcu_state *rsp;
  2656. trace_rcu_utilization(TPS("Start CPU hotplug"));
  2657. switch (action) {
  2658. case CPU_UP_PREPARE:
  2659. case CPU_UP_PREPARE_FROZEN:
  2660. rcu_prepare_cpu(cpu);
  2661. rcu_prepare_kthreads(cpu);
  2662. break;
  2663. case CPU_ONLINE:
  2664. case CPU_DOWN_FAILED:
  2665. rcu_boost_kthread_setaffinity(rnp, -1);
  2666. break;
  2667. case CPU_DOWN_PREPARE:
  2668. rcu_boost_kthread_setaffinity(rnp, cpu);
  2669. break;
  2670. case CPU_DYING:
  2671. case CPU_DYING_FROZEN:
  2672. for_each_rcu_flavor(rsp)
  2673. rcu_cleanup_dying_cpu(rsp);
  2674. break;
  2675. case CPU_DEAD:
  2676. case CPU_DEAD_FROZEN:
  2677. case CPU_UP_CANCELED:
  2678. case CPU_UP_CANCELED_FROZEN:
  2679. for_each_rcu_flavor(rsp)
  2680. rcu_cleanup_dead_cpu(cpu, rsp);
  2681. break;
  2682. default:
  2683. break;
  2684. }
  2685. trace_rcu_utilization(TPS("End CPU hotplug"));
  2686. return NOTIFY_OK;
  2687. }
  2688. /*
  2689. * Spawn the kthread that handles this RCU flavor's grace periods.
  2690. */
  2691. static int __init rcu_spawn_gp_kthread(void)
  2692. {
  2693. unsigned long flags;
  2694. struct rcu_node *rnp;
  2695. struct rcu_state *rsp;
  2696. struct task_struct *t;
  2697. for_each_rcu_flavor(rsp) {
  2698. t = kthread_run(rcu_gp_kthread, rsp, "%s", rsp->name);
  2699. BUG_ON(IS_ERR(t));
  2700. rnp = rcu_get_root(rsp);
  2701. raw_spin_lock_irqsave(&rnp->lock, flags);
  2702. rsp->gp_kthread = t;
  2703. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  2704. rcu_spawn_nocb_kthreads(rsp);
  2705. }
  2706. return 0;
  2707. }
  2708. early_initcall(rcu_spawn_gp_kthread);
  2709. /*
  2710. * This function is invoked towards the end of the scheduler's initialization
  2711. * process. Before this is called, the idle task might contain
  2712. * RCU read-side critical sections (during which time, this idle
  2713. * task is booting the system). After this function is called, the
  2714. * idle tasks are prohibited from containing RCU read-side critical
  2715. * sections. This function also enables RCU lockdep checking.
  2716. */
  2717. void rcu_scheduler_starting(void)
  2718. {
  2719. WARN_ON(num_online_cpus() != 1);
  2720. WARN_ON(nr_context_switches() > 0);
  2721. rcu_scheduler_active = 1;
  2722. }
  2723. /*
  2724. * Compute the per-level fanout, either using the exact fanout specified
  2725. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  2726. */
  2727. #ifdef CONFIG_RCU_FANOUT_EXACT
  2728. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  2729. {
  2730. int i;
  2731. for (i = rcu_num_lvls - 1; i > 0; i--)
  2732. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  2733. rsp->levelspread[0] = rcu_fanout_leaf;
  2734. }
  2735. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  2736. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  2737. {
  2738. int ccur;
  2739. int cprv;
  2740. int i;
  2741. cprv = nr_cpu_ids;
  2742. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  2743. ccur = rsp->levelcnt[i];
  2744. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  2745. cprv = ccur;
  2746. }
  2747. }
  2748. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  2749. /*
  2750. * Helper function for rcu_init() that initializes one rcu_state structure.
  2751. */
  2752. static void __init rcu_init_one(struct rcu_state *rsp,
  2753. struct rcu_data __percpu *rda)
  2754. {
  2755. static char *buf[] = { "rcu_node_0",
  2756. "rcu_node_1",
  2757. "rcu_node_2",
  2758. "rcu_node_3" }; /* Match MAX_RCU_LVLS */
  2759. static char *fqs[] = { "rcu_node_fqs_0",
  2760. "rcu_node_fqs_1",
  2761. "rcu_node_fqs_2",
  2762. "rcu_node_fqs_3" }; /* Match MAX_RCU_LVLS */
  2763. int cpustride = 1;
  2764. int i;
  2765. int j;
  2766. struct rcu_node *rnp;
  2767. BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  2768. /* Silence gcc 4.8 warning about array index out of range. */
  2769. if (rcu_num_lvls > RCU_NUM_LVLS)
  2770. panic("rcu_init_one: rcu_num_lvls overflow");
  2771. /* Initialize the level-tracking arrays. */
  2772. for (i = 0; i < rcu_num_lvls; i++)
  2773. rsp->levelcnt[i] = num_rcu_lvl[i];
  2774. for (i = 1; i < rcu_num_lvls; i++)
  2775. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  2776. rcu_init_levelspread(rsp);
  2777. /* Initialize the elements themselves, starting from the leaves. */
  2778. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  2779. cpustride *= rsp->levelspread[i];
  2780. rnp = rsp->level[i];
  2781. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  2782. raw_spin_lock_init(&rnp->lock);
  2783. lockdep_set_class_and_name(&rnp->lock,
  2784. &rcu_node_class[i], buf[i]);
  2785. raw_spin_lock_init(&rnp->fqslock);
  2786. lockdep_set_class_and_name(&rnp->fqslock,
  2787. &rcu_fqs_class[i], fqs[i]);
  2788. rnp->gpnum = rsp->gpnum;
  2789. rnp->completed = rsp->completed;
  2790. rnp->qsmask = 0;
  2791. rnp->qsmaskinit = 0;
  2792. rnp->grplo = j * cpustride;
  2793. rnp->grphi = (j + 1) * cpustride - 1;
  2794. if (rnp->grphi >= NR_CPUS)
  2795. rnp->grphi = NR_CPUS - 1;
  2796. if (i == 0) {
  2797. rnp->grpnum = 0;
  2798. rnp->grpmask = 0;
  2799. rnp->parent = NULL;
  2800. } else {
  2801. rnp->grpnum = j % rsp->levelspread[i - 1];
  2802. rnp->grpmask = 1UL << rnp->grpnum;
  2803. rnp->parent = rsp->level[i - 1] +
  2804. j / rsp->levelspread[i - 1];
  2805. }
  2806. rnp->level = i;
  2807. INIT_LIST_HEAD(&rnp->blkd_tasks);
  2808. rcu_init_one_nocb(rnp);
  2809. }
  2810. }
  2811. rsp->rda = rda;
  2812. init_waitqueue_head(&rsp->gp_wq);
  2813. init_irq_work(&rsp->wakeup_work, rsp_wakeup);
  2814. rnp = rsp->level[rcu_num_lvls - 1];
  2815. for_each_possible_cpu(i) {
  2816. while (i > rnp->grphi)
  2817. rnp++;
  2818. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  2819. rcu_boot_init_percpu_data(i, rsp);
  2820. }
  2821. list_add(&rsp->flavors, &rcu_struct_flavors);
  2822. }
  2823. /*
  2824. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  2825. * replace the definitions in rcutree.h because those are needed to size
  2826. * the ->node array in the rcu_state structure.
  2827. */
  2828. static void __init rcu_init_geometry(void)
  2829. {
  2830. ulong d;
  2831. int i;
  2832. int j;
  2833. int n = nr_cpu_ids;
  2834. int rcu_capacity[MAX_RCU_LVLS + 1];
  2835. /*
  2836. * Initialize any unspecified boot parameters.
  2837. * The default values of jiffies_till_first_fqs and
  2838. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  2839. * value, which is a function of HZ, then adding one for each
  2840. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  2841. */
  2842. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  2843. if (jiffies_till_first_fqs == ULONG_MAX)
  2844. jiffies_till_first_fqs = d;
  2845. if (jiffies_till_next_fqs == ULONG_MAX)
  2846. jiffies_till_next_fqs = d;
  2847. /* If the compile-time values are accurate, just leave. */
  2848. if (rcu_fanout_leaf == CONFIG_RCU_FANOUT_LEAF &&
  2849. nr_cpu_ids == NR_CPUS)
  2850. return;
  2851. /*
  2852. * Compute number of nodes that can be handled an rcu_node tree
  2853. * with the given number of levels. Setting rcu_capacity[0] makes
  2854. * some of the arithmetic easier.
  2855. */
  2856. rcu_capacity[0] = 1;
  2857. rcu_capacity[1] = rcu_fanout_leaf;
  2858. for (i = 2; i <= MAX_RCU_LVLS; i++)
  2859. rcu_capacity[i] = rcu_capacity[i - 1] * CONFIG_RCU_FANOUT;
  2860. /*
  2861. * The boot-time rcu_fanout_leaf parameter is only permitted
  2862. * to increase the leaf-level fanout, not decrease it. Of course,
  2863. * the leaf-level fanout cannot exceed the number of bits in
  2864. * the rcu_node masks. Finally, the tree must be able to accommodate
  2865. * the configured number of CPUs. Complain and fall back to the
  2866. * compile-time values if these limits are exceeded.
  2867. */
  2868. if (rcu_fanout_leaf < CONFIG_RCU_FANOUT_LEAF ||
  2869. rcu_fanout_leaf > sizeof(unsigned long) * 8 ||
  2870. n > rcu_capacity[MAX_RCU_LVLS]) {
  2871. WARN_ON(1);
  2872. return;
  2873. }
  2874. /* Calculate the number of rcu_nodes at each level of the tree. */
  2875. for (i = 1; i <= MAX_RCU_LVLS; i++)
  2876. if (n <= rcu_capacity[i]) {
  2877. for (j = 0; j <= i; j++)
  2878. num_rcu_lvl[j] =
  2879. DIV_ROUND_UP(n, rcu_capacity[i - j]);
  2880. rcu_num_lvls = i;
  2881. for (j = i + 1; j <= MAX_RCU_LVLS; j++)
  2882. num_rcu_lvl[j] = 0;
  2883. break;
  2884. }
  2885. /* Calculate the total number of rcu_node structures. */
  2886. rcu_num_nodes = 0;
  2887. for (i = 0; i <= MAX_RCU_LVLS; i++)
  2888. rcu_num_nodes += num_rcu_lvl[i];
  2889. rcu_num_nodes -= n;
  2890. }
  2891. void __init rcu_init(void)
  2892. {
  2893. int cpu;
  2894. rcu_bootup_announce();
  2895. rcu_init_geometry();
  2896. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  2897. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  2898. __rcu_init_preempt();
  2899. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  2900. /*
  2901. * We don't need protection against CPU-hotplug here because
  2902. * this is called early in boot, before either interrupts
  2903. * or the scheduler are operational.
  2904. */
  2905. cpu_notifier(rcu_cpu_notify, 0);
  2906. for_each_online_cpu(cpu)
  2907. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  2908. }
  2909. #include "rcutree_plugin.h"