tree.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  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_wait.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <linux/sched/debug.h>
  39. #include <linux/nmi.h>
  40. #include <linux/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/export.h>
  43. #include <linux/completion.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/percpu.h>
  46. #include <linux/notifier.h>
  47. #include <linux/cpu.h>
  48. #include <linux/mutex.h>
  49. #include <linux/time.h>
  50. #include <linux/kernel_stat.h>
  51. #include <linux/wait.h>
  52. #include <linux/kthread.h>
  53. #include <uapi/linux/sched/types.h>
  54. #include <linux/prefetch.h>
  55. #include <linux/delay.h>
  56. #include <linux/stop_machine.h>
  57. #include <linux/random.h>
  58. #include <linux/trace_events.h>
  59. #include <linux/suspend.h>
  60. #include <linux/ftrace.h>
  61. #include "tree.h"
  62. #include "rcu.h"
  63. #ifdef MODULE_PARAM_PREFIX
  64. #undef MODULE_PARAM_PREFIX
  65. #endif
  66. #define MODULE_PARAM_PREFIX "rcutree."
  67. /* Data structures. */
  68. /*
  69. * In order to export the rcu_state name to the tracing tools, it
  70. * needs to be added in the __tracepoint_string section.
  71. * This requires defining a separate variable tp_<sname>_varname
  72. * that points to the string being used, and this will allow
  73. * the tracing userspace tools to be able to decipher the string
  74. * address to the matching string.
  75. */
  76. #ifdef CONFIG_TRACING
  77. # define DEFINE_RCU_TPS(sname) \
  78. static char sname##_varname[] = #sname; \
  79. static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
  80. # define RCU_STATE_NAME(sname) sname##_varname
  81. #else
  82. # define DEFINE_RCU_TPS(sname)
  83. # define RCU_STATE_NAME(sname) __stringify(sname)
  84. #endif
  85. #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
  86. DEFINE_RCU_TPS(sname) \
  87. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
  88. struct rcu_state sname##_state = { \
  89. .level = { &sname##_state.node[0] }, \
  90. .rda = &sname##_data, \
  91. .call = cr, \
  92. .gp_state = RCU_GP_IDLE, \
  93. .gpnum = 0UL - 300UL, \
  94. .completed = 0UL - 300UL, \
  95. .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
  96. .name = RCU_STATE_NAME(sname), \
  97. .abbr = sabbr, \
  98. .exp_mutex = __MUTEX_INITIALIZER(sname##_state.exp_mutex), \
  99. .exp_wake_mutex = __MUTEX_INITIALIZER(sname##_state.exp_wake_mutex), \
  100. }
  101. RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
  102. RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
  103. static struct rcu_state *const rcu_state_p;
  104. LIST_HEAD(rcu_struct_flavors);
  105. /* Dump rcu_node combining tree at boot to verify correct setup. */
  106. static bool dump_tree;
  107. module_param(dump_tree, bool, 0444);
  108. /* Control rcu_node-tree auto-balancing at boot time. */
  109. static bool rcu_fanout_exact;
  110. module_param(rcu_fanout_exact, bool, 0444);
  111. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  112. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  113. module_param(rcu_fanout_leaf, int, 0444);
  114. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  115. /* Number of rcu_nodes at specified level. */
  116. int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  117. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  118. /* panic() on RCU Stall sysctl. */
  119. int sysctl_panic_on_rcu_stall __read_mostly;
  120. /*
  121. * The rcu_scheduler_active variable is initialized to the value
  122. * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
  123. * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
  124. * RCU can assume that there is but one task, allowing RCU to (for example)
  125. * optimize synchronize_rcu() to a simple barrier(). When this variable
  126. * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
  127. * to detect real grace periods. This variable is also used to suppress
  128. * boot-time false positives from lockdep-RCU error checking. Finally, it
  129. * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
  130. * is fully initialized, including all of its kthreads having been spawned.
  131. */
  132. int rcu_scheduler_active __read_mostly;
  133. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  134. /*
  135. * The rcu_scheduler_fully_active variable transitions from zero to one
  136. * during the early_initcall() processing, which is after the scheduler
  137. * is capable of creating new tasks. So RCU processing (for example,
  138. * creating tasks for RCU priority boosting) must be delayed until after
  139. * rcu_scheduler_fully_active transitions from zero to one. We also
  140. * currently delay invocation of any RCU callbacks until after this point.
  141. *
  142. * It might later prove better for people registering RCU callbacks during
  143. * early boot to take responsibility for these callbacks, but one step at
  144. * a time.
  145. */
  146. static int rcu_scheduler_fully_active __read_mostly;
  147. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  148. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  149. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  150. static void invoke_rcu_core(void);
  151. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  152. static void rcu_report_exp_rdp(struct rcu_state *rsp,
  153. struct rcu_data *rdp, bool wake);
  154. static void sync_sched_exp_online_cleanup(int cpu);
  155. /* rcuc/rcub kthread realtime priority */
  156. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  157. module_param(kthread_prio, int, 0644);
  158. /* Delay in jiffies for grace-period initialization delays, debug only. */
  159. static int gp_preinit_delay;
  160. module_param(gp_preinit_delay, int, 0444);
  161. static int gp_init_delay;
  162. module_param(gp_init_delay, int, 0444);
  163. static int gp_cleanup_delay;
  164. module_param(gp_cleanup_delay, int, 0444);
  165. /*
  166. * Number of grace periods between delays, normalized by the duration of
  167. * the delay. The longer the delay, the more the grace periods between
  168. * each delay. The reason for this normalization is that it means that,
  169. * for non-zero delays, the overall slowdown of grace periods is constant
  170. * regardless of the duration of the delay. This arrangement balances
  171. * the need for long delays to increase some race probabilities with the
  172. * need for fast grace periods to increase other race probabilities.
  173. */
  174. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
  175. /*
  176. * Track the rcutorture test sequence number and the update version
  177. * number within a given test. The rcutorture_testseq is incremented
  178. * on every rcutorture module load and unload, so has an odd value
  179. * when a test is running. The rcutorture_vernum is set to zero
  180. * when rcutorture starts and is incremented on each rcutorture update.
  181. * These variables enable correlating rcutorture output with the
  182. * RCU tracing information.
  183. */
  184. unsigned long rcutorture_testseq;
  185. unsigned long rcutorture_vernum;
  186. /*
  187. * Compute the mask of online CPUs for the specified rcu_node structure.
  188. * This will not be stable unless the rcu_node structure's ->lock is
  189. * held, but the bit corresponding to the current CPU will be stable
  190. * in most contexts.
  191. */
  192. unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  193. {
  194. return READ_ONCE(rnp->qsmaskinitnext);
  195. }
  196. /*
  197. * Return true if an RCU grace period is in progress. The READ_ONCE()s
  198. * permit this function to be invoked without holding the root rcu_node
  199. * structure's ->lock, but of course results can be subject to change.
  200. */
  201. static int rcu_gp_in_progress(struct rcu_state *rsp)
  202. {
  203. return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
  204. }
  205. /*
  206. * Note a quiescent state. Because we do not need to know
  207. * how many quiescent states passed, just if there was at least
  208. * one since the start of the grace period, this just sets a flag.
  209. * The caller must have disabled preemption.
  210. */
  211. void rcu_sched_qs(void)
  212. {
  213. RCU_LOCKDEP_WARN(preemptible(), "rcu_sched_qs() invoked with preemption enabled!!!");
  214. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
  215. return;
  216. trace_rcu_grace_period(TPS("rcu_sched"),
  217. __this_cpu_read(rcu_sched_data.gpnum),
  218. TPS("cpuqs"));
  219. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
  220. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
  221. return;
  222. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
  223. rcu_report_exp_rdp(&rcu_sched_state,
  224. this_cpu_ptr(&rcu_sched_data), true);
  225. }
  226. void rcu_bh_qs(void)
  227. {
  228. RCU_LOCKDEP_WARN(preemptible(), "rcu_bh_qs() invoked with preemption enabled!!!");
  229. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  230. trace_rcu_grace_period(TPS("rcu_bh"),
  231. __this_cpu_read(rcu_bh_data.gpnum),
  232. TPS("cpuqs"));
  233. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  234. }
  235. }
  236. /*
  237. * Steal a bit from the bottom of ->dynticks for idle entry/exit
  238. * control. Initially this is for TLB flushing.
  239. */
  240. #define RCU_DYNTICK_CTRL_MASK 0x1
  241. #define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
  242. #ifndef rcu_eqs_special_exit
  243. #define rcu_eqs_special_exit() do { } while (0)
  244. #endif
  245. static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  246. .dynticks_nesting = 1,
  247. .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
  248. .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
  249. };
  250. /*
  251. * Record entry into an extended quiescent state. This is only to be
  252. * called when not already in an extended quiescent state.
  253. */
  254. static void rcu_dynticks_eqs_enter(void)
  255. {
  256. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  257. int seq;
  258. /*
  259. * CPUs seeing atomic_add_return() must see prior RCU read-side
  260. * critical sections, and we also must force ordering with the
  261. * next idle sojourn.
  262. */
  263. seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  264. /* Better be in an extended quiescent state! */
  265. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  266. (seq & RCU_DYNTICK_CTRL_CTR));
  267. /* Better not have special action (TLB flush) pending! */
  268. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  269. (seq & RCU_DYNTICK_CTRL_MASK));
  270. }
  271. /*
  272. * Record exit from an extended quiescent state. This is only to be
  273. * called from an extended quiescent state.
  274. */
  275. static void rcu_dynticks_eqs_exit(void)
  276. {
  277. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  278. int seq;
  279. /*
  280. * CPUs seeing atomic_add_return() must see prior idle sojourns,
  281. * and we also must force ordering with the next RCU read-side
  282. * critical section.
  283. */
  284. seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  285. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  286. !(seq & RCU_DYNTICK_CTRL_CTR));
  287. if (seq & RCU_DYNTICK_CTRL_MASK) {
  288. atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdtp->dynticks);
  289. smp_mb__after_atomic(); /* _exit after clearing mask. */
  290. /* Prefer duplicate flushes to losing a flush. */
  291. rcu_eqs_special_exit();
  292. }
  293. }
  294. /*
  295. * Reset the current CPU's ->dynticks counter to indicate that the
  296. * newly onlined CPU is no longer in an extended quiescent state.
  297. * This will either leave the counter unchanged, or increment it
  298. * to the next non-quiescent value.
  299. *
  300. * The non-atomic test/increment sequence works because the upper bits
  301. * of the ->dynticks counter are manipulated only by the corresponding CPU,
  302. * or when the corresponding CPU is offline.
  303. */
  304. static void rcu_dynticks_eqs_online(void)
  305. {
  306. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  307. if (atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR)
  308. return;
  309. atomic_add(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  310. }
  311. /*
  312. * Is the current CPU in an extended quiescent state?
  313. *
  314. * No ordering, as we are sampling CPU-local information.
  315. */
  316. bool rcu_dynticks_curr_cpu_in_eqs(void)
  317. {
  318. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  319. return !(atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR);
  320. }
  321. /*
  322. * Snapshot the ->dynticks counter with full ordering so as to allow
  323. * stable comparison of this counter with past and future snapshots.
  324. */
  325. int rcu_dynticks_snap(struct rcu_dynticks *rdtp)
  326. {
  327. int snap = atomic_add_return(0, &rdtp->dynticks);
  328. return snap & ~RCU_DYNTICK_CTRL_MASK;
  329. }
  330. /*
  331. * Return true if the snapshot returned from rcu_dynticks_snap()
  332. * indicates that RCU is in an extended quiescent state.
  333. */
  334. static bool rcu_dynticks_in_eqs(int snap)
  335. {
  336. return !(snap & RCU_DYNTICK_CTRL_CTR);
  337. }
  338. /*
  339. * Return true if the CPU corresponding to the specified rcu_dynticks
  340. * structure has spent some time in an extended quiescent state since
  341. * rcu_dynticks_snap() returned the specified snapshot.
  342. */
  343. static bool rcu_dynticks_in_eqs_since(struct rcu_dynticks *rdtp, int snap)
  344. {
  345. return snap != rcu_dynticks_snap(rdtp);
  346. }
  347. /*
  348. * Do a double-increment of the ->dynticks counter to emulate a
  349. * momentary idle-CPU quiescent state.
  350. */
  351. static void rcu_dynticks_momentary_idle(void)
  352. {
  353. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  354. int special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
  355. &rdtp->dynticks);
  356. /* It is illegal to call this from idle state. */
  357. WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
  358. }
  359. /*
  360. * Set the special (bottom) bit of the specified CPU so that it
  361. * will take special action (such as flushing its TLB) on the
  362. * next exit from an extended quiescent state. Returns true if
  363. * the bit was successfully set, or false if the CPU was not in
  364. * an extended quiescent state.
  365. */
  366. bool rcu_eqs_special_set(int cpu)
  367. {
  368. int old;
  369. int new;
  370. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  371. do {
  372. old = atomic_read(&rdtp->dynticks);
  373. if (old & RCU_DYNTICK_CTRL_CTR)
  374. return false;
  375. new = old | RCU_DYNTICK_CTRL_MASK;
  376. } while (atomic_cmpxchg(&rdtp->dynticks, old, new) != old);
  377. return true;
  378. }
  379. /*
  380. * Let the RCU core know that this CPU has gone through the scheduler,
  381. * which is a quiescent state. This is called when the need for a
  382. * quiescent state is urgent, so we burn an atomic operation and full
  383. * memory barriers to let the RCU core know about it, regardless of what
  384. * this CPU might (or might not) do in the near future.
  385. *
  386. * We inform the RCU core by emulating a zero-duration dyntick-idle period.
  387. *
  388. * The caller must have disabled interrupts.
  389. */
  390. static void rcu_momentary_dyntick_idle(void)
  391. {
  392. raw_cpu_write(rcu_dynticks.rcu_need_heavy_qs, false);
  393. rcu_dynticks_momentary_idle();
  394. }
  395. /*
  396. * Note a context switch. This is a quiescent state for RCU-sched,
  397. * and requires special handling for preemptible RCU.
  398. * The caller must have disabled interrupts.
  399. */
  400. void rcu_note_context_switch(bool preempt)
  401. {
  402. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  403. trace_rcu_utilization(TPS("Start context switch"));
  404. rcu_sched_qs();
  405. rcu_preempt_note_context_switch(preempt);
  406. /* Load rcu_urgent_qs before other flags. */
  407. if (!smp_load_acquire(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs)))
  408. goto out;
  409. this_cpu_write(rcu_dynticks.rcu_urgent_qs, false);
  410. if (unlikely(raw_cpu_read(rcu_dynticks.rcu_need_heavy_qs)))
  411. rcu_momentary_dyntick_idle();
  412. this_cpu_inc(rcu_dynticks.rcu_qs_ctr);
  413. if (!preempt)
  414. rcu_note_voluntary_context_switch_lite(current);
  415. out:
  416. trace_rcu_utilization(TPS("End context switch"));
  417. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  418. }
  419. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  420. /*
  421. * Register a quiescent state for all RCU flavors. If there is an
  422. * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
  423. * dyntick-idle quiescent state visible to other CPUs (but only for those
  424. * RCU flavors in desperate need of a quiescent state, which will normally
  425. * be none of them). Either way, do a lightweight quiescent state for
  426. * all RCU flavors.
  427. *
  428. * The barrier() calls are redundant in the common case when this is
  429. * called externally, but just in case this is called from within this
  430. * file.
  431. *
  432. */
  433. void rcu_all_qs(void)
  434. {
  435. unsigned long flags;
  436. if (!raw_cpu_read(rcu_dynticks.rcu_urgent_qs))
  437. return;
  438. preempt_disable();
  439. /* Load rcu_urgent_qs before other flags. */
  440. if (!smp_load_acquire(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs))) {
  441. preempt_enable();
  442. return;
  443. }
  444. this_cpu_write(rcu_dynticks.rcu_urgent_qs, false);
  445. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  446. if (unlikely(raw_cpu_read(rcu_dynticks.rcu_need_heavy_qs))) {
  447. local_irq_save(flags);
  448. rcu_momentary_dyntick_idle();
  449. local_irq_restore(flags);
  450. }
  451. if (unlikely(raw_cpu_read(rcu_sched_data.cpu_no_qs.b.exp)))
  452. rcu_sched_qs();
  453. this_cpu_inc(rcu_dynticks.rcu_qs_ctr);
  454. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  455. preempt_enable();
  456. }
  457. EXPORT_SYMBOL_GPL(rcu_all_qs);
  458. #define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch. */
  459. static long blimit = DEFAULT_RCU_BLIMIT;
  460. #define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
  461. static long qhimark = DEFAULT_RCU_QHIMARK;
  462. #define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
  463. static long qlowmark = DEFAULT_RCU_QLOMARK;
  464. module_param(blimit, long, 0444);
  465. module_param(qhimark, long, 0444);
  466. module_param(qlowmark, long, 0444);
  467. static ulong jiffies_till_first_fqs = ULONG_MAX;
  468. static ulong jiffies_till_next_fqs = ULONG_MAX;
  469. static bool rcu_kick_kthreads;
  470. module_param(jiffies_till_first_fqs, ulong, 0644);
  471. module_param(jiffies_till_next_fqs, ulong, 0644);
  472. module_param(rcu_kick_kthreads, bool, 0644);
  473. /*
  474. * How long the grace period must be before we start recruiting
  475. * quiescent-state help from rcu_note_context_switch().
  476. */
  477. static ulong jiffies_till_sched_qs = HZ / 10;
  478. module_param(jiffies_till_sched_qs, ulong, 0444);
  479. static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  480. struct rcu_data *rdp);
  481. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *rsp));
  482. static void force_quiescent_state(struct rcu_state *rsp);
  483. static int rcu_pending(void);
  484. /*
  485. * Return the number of RCU batches started thus far for debug & stats.
  486. */
  487. unsigned long rcu_batches_started(void)
  488. {
  489. return rcu_state_p->gpnum;
  490. }
  491. EXPORT_SYMBOL_GPL(rcu_batches_started);
  492. /*
  493. * Return the number of RCU-sched batches started thus far for debug & stats.
  494. */
  495. unsigned long rcu_batches_started_sched(void)
  496. {
  497. return rcu_sched_state.gpnum;
  498. }
  499. EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  500. /*
  501. * Return the number of RCU BH batches started thus far for debug & stats.
  502. */
  503. unsigned long rcu_batches_started_bh(void)
  504. {
  505. return rcu_bh_state.gpnum;
  506. }
  507. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  508. /*
  509. * Return the number of RCU batches completed thus far for debug & stats.
  510. */
  511. unsigned long rcu_batches_completed(void)
  512. {
  513. return rcu_state_p->completed;
  514. }
  515. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  516. /*
  517. * Return the number of RCU-sched batches completed thus far for debug & stats.
  518. */
  519. unsigned long rcu_batches_completed_sched(void)
  520. {
  521. return rcu_sched_state.completed;
  522. }
  523. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  524. /*
  525. * Return the number of RCU BH batches completed thus far for debug & stats.
  526. */
  527. unsigned long rcu_batches_completed_bh(void)
  528. {
  529. return rcu_bh_state.completed;
  530. }
  531. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  532. /*
  533. * Return the number of RCU expedited batches completed thus far for
  534. * debug & stats. Odd numbers mean that a batch is in progress, even
  535. * numbers mean idle. The value returned will thus be roughly double
  536. * the cumulative batches since boot.
  537. */
  538. unsigned long rcu_exp_batches_completed(void)
  539. {
  540. return rcu_state_p->expedited_sequence;
  541. }
  542. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
  543. /*
  544. * Return the number of RCU-sched expedited batches completed thus far
  545. * for debug & stats. Similar to rcu_exp_batches_completed().
  546. */
  547. unsigned long rcu_exp_batches_completed_sched(void)
  548. {
  549. return rcu_sched_state.expedited_sequence;
  550. }
  551. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
  552. /*
  553. * Force a quiescent state.
  554. */
  555. void rcu_force_quiescent_state(void)
  556. {
  557. force_quiescent_state(rcu_state_p);
  558. }
  559. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  560. /*
  561. * Force a quiescent state for RCU BH.
  562. */
  563. void rcu_bh_force_quiescent_state(void)
  564. {
  565. force_quiescent_state(&rcu_bh_state);
  566. }
  567. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  568. /*
  569. * Force a quiescent state for RCU-sched.
  570. */
  571. void rcu_sched_force_quiescent_state(void)
  572. {
  573. force_quiescent_state(&rcu_sched_state);
  574. }
  575. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  576. /*
  577. * Show the state of the grace-period kthreads.
  578. */
  579. void show_rcu_gp_kthreads(void)
  580. {
  581. struct rcu_state *rsp;
  582. for_each_rcu_flavor(rsp) {
  583. pr_info("%s: wait state: %d ->state: %#lx\n",
  584. rsp->name, rsp->gp_state, rsp->gp_kthread->state);
  585. /* sched_show_task(rsp->gp_kthread); */
  586. }
  587. }
  588. EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
  589. /*
  590. * Record the number of times rcutorture tests have been initiated and
  591. * terminated. This information allows the debugfs tracing stats to be
  592. * correlated to the rcutorture messages, even when the rcutorture module
  593. * is being repeatedly loaded and unloaded. In other words, we cannot
  594. * store this state in rcutorture itself.
  595. */
  596. void rcutorture_record_test_transition(void)
  597. {
  598. rcutorture_testseq++;
  599. rcutorture_vernum = 0;
  600. }
  601. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  602. /*
  603. * Send along grace-period-related data for rcutorture diagnostics.
  604. */
  605. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  606. unsigned long *gpnum, unsigned long *completed)
  607. {
  608. struct rcu_state *rsp = NULL;
  609. switch (test_type) {
  610. case RCU_FLAVOR:
  611. rsp = rcu_state_p;
  612. break;
  613. case RCU_BH_FLAVOR:
  614. rsp = &rcu_bh_state;
  615. break;
  616. case RCU_SCHED_FLAVOR:
  617. rsp = &rcu_sched_state;
  618. break;
  619. default:
  620. break;
  621. }
  622. if (rsp == NULL)
  623. return;
  624. *flags = READ_ONCE(rsp->gp_flags);
  625. *gpnum = READ_ONCE(rsp->gpnum);
  626. *completed = READ_ONCE(rsp->completed);
  627. }
  628. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  629. /*
  630. * Record the number of writer passes through the current rcutorture test.
  631. * This is also used to correlate debugfs tracing stats with the rcutorture
  632. * messages.
  633. */
  634. void rcutorture_record_progress(unsigned long vernum)
  635. {
  636. rcutorture_vernum++;
  637. }
  638. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  639. /*
  640. * Return the root node of the specified rcu_state structure.
  641. */
  642. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  643. {
  644. return &rsp->node[0];
  645. }
  646. /*
  647. * Is there any need for future grace periods?
  648. * Interrupts must be disabled. If the caller does not hold the root
  649. * rnp_node structure's ->lock, the results are advisory only.
  650. */
  651. static int rcu_future_needs_gp(struct rcu_state *rsp)
  652. {
  653. struct rcu_node *rnp = rcu_get_root(rsp);
  654. int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
  655. int *fp = &rnp->need_future_gp[idx];
  656. lockdep_assert_irqs_disabled();
  657. return READ_ONCE(*fp);
  658. }
  659. /*
  660. * Does the current CPU require a not-yet-started grace period?
  661. * The caller must have disabled interrupts to prevent races with
  662. * normal callback registry.
  663. */
  664. static bool
  665. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  666. {
  667. lockdep_assert_irqs_disabled();
  668. if (rcu_gp_in_progress(rsp))
  669. return false; /* No, a grace period is already in progress. */
  670. if (rcu_future_needs_gp(rsp))
  671. return true; /* Yes, a no-CBs CPU needs one. */
  672. if (!rcu_segcblist_is_enabled(&rdp->cblist))
  673. return false; /* No, this is a no-CBs (or offline) CPU. */
  674. if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  675. return true; /* Yes, CPU has newly registered callbacks. */
  676. if (rcu_segcblist_future_gp_needed(&rdp->cblist,
  677. READ_ONCE(rsp->completed)))
  678. return true; /* Yes, CBs for future grace period. */
  679. return false; /* No grace period needed. */
  680. }
  681. /*
  682. * Enter an RCU extended quiescent state, which can be either the
  683. * idle loop or adaptive-tickless usermode execution.
  684. *
  685. * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
  686. * the possibility of usermode upcalls having messed up our count
  687. * of interrupt nesting level during the prior busy period.
  688. */
  689. static void rcu_eqs_enter(bool user)
  690. {
  691. struct rcu_state *rsp;
  692. struct rcu_data *rdp;
  693. struct rcu_dynticks *rdtp;
  694. rdtp = this_cpu_ptr(&rcu_dynticks);
  695. WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0);
  696. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  697. rdtp->dynticks_nesting == 0);
  698. if (rdtp->dynticks_nesting != 1) {
  699. rdtp->dynticks_nesting--;
  700. return;
  701. }
  702. lockdep_assert_irqs_disabled();
  703. trace_rcu_dyntick(TPS("Start"), rdtp->dynticks_nesting, 0, rdtp->dynticks);
  704. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
  705. for_each_rcu_flavor(rsp) {
  706. rdp = this_cpu_ptr(rsp->rda);
  707. do_nocb_deferred_wakeup(rdp);
  708. }
  709. rcu_prepare_for_idle();
  710. WRITE_ONCE(rdtp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
  711. rcu_dynticks_eqs_enter();
  712. rcu_dynticks_task_enter();
  713. }
  714. /**
  715. * rcu_idle_enter - inform RCU that current CPU is entering idle
  716. *
  717. * Enter idle mode, in other words, -leave- the mode in which RCU
  718. * read-side critical sections can occur. (Though RCU read-side
  719. * critical sections can occur in irq handlers in idle, a possibility
  720. * handled by irq_enter() and irq_exit().)
  721. *
  722. * If you add or remove a call to rcu_idle_enter(), be sure to test with
  723. * CONFIG_RCU_EQS_DEBUG=y.
  724. */
  725. void rcu_idle_enter(void)
  726. {
  727. lockdep_assert_irqs_disabled();
  728. rcu_eqs_enter(false);
  729. }
  730. #ifdef CONFIG_NO_HZ_FULL
  731. /**
  732. * rcu_user_enter - inform RCU that we are resuming userspace.
  733. *
  734. * Enter RCU idle mode right before resuming userspace. No use of RCU
  735. * is permitted between this call and rcu_user_exit(). This way the
  736. * CPU doesn't need to maintain the tick for RCU maintenance purposes
  737. * when the CPU runs in userspace.
  738. *
  739. * If you add or remove a call to rcu_user_enter(), be sure to test with
  740. * CONFIG_RCU_EQS_DEBUG=y.
  741. */
  742. void rcu_user_enter(void)
  743. {
  744. lockdep_assert_irqs_disabled();
  745. rcu_eqs_enter(true);
  746. }
  747. #endif /* CONFIG_NO_HZ_FULL */
  748. /**
  749. * rcu_nmi_exit - inform RCU of exit from NMI context
  750. *
  751. * If we are returning from the outermost NMI handler that interrupted an
  752. * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
  753. * to let the RCU grace-period handling know that the CPU is back to
  754. * being RCU-idle.
  755. *
  756. * If you add or remove a call to rcu_nmi_exit(), be sure to test
  757. * with CONFIG_RCU_EQS_DEBUG=y.
  758. */
  759. void rcu_nmi_exit(void)
  760. {
  761. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  762. /*
  763. * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
  764. * (We are exiting an NMI handler, so RCU better be paying attention
  765. * to us!)
  766. */
  767. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
  768. WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
  769. /*
  770. * If the nesting level is not 1, the CPU wasn't RCU-idle, so
  771. * leave it in non-RCU-idle state.
  772. */
  773. if (rdtp->dynticks_nmi_nesting != 1) {
  774. trace_rcu_dyntick(TPS("--="), rdtp->dynticks_nmi_nesting, rdtp->dynticks_nmi_nesting - 2, rdtp->dynticks);
  775. WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* No store tearing. */
  776. rdtp->dynticks_nmi_nesting - 2);
  777. return;
  778. }
  779. /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
  780. trace_rcu_dyntick(TPS("Startirq"), rdtp->dynticks_nmi_nesting, 0, rdtp->dynticks);
  781. WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
  782. rcu_dynticks_eqs_enter();
  783. }
  784. /**
  785. * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
  786. *
  787. * Exit from an interrupt handler, which might possibly result in entering
  788. * idle mode, in other words, leaving the mode in which read-side critical
  789. * sections can occur. The caller must have disabled interrupts.
  790. *
  791. * This code assumes that the idle loop never does anything that might
  792. * result in unbalanced calls to irq_enter() and irq_exit(). If your
  793. * architecture's idle loop violates this assumption, RCU will give you what
  794. * you deserve, good and hard. But very infrequently and irreproducibly.
  795. *
  796. * Use things like work queues to work around this limitation.
  797. *
  798. * You have been warned.
  799. *
  800. * If you add or remove a call to rcu_irq_exit(), be sure to test with
  801. * CONFIG_RCU_EQS_DEBUG=y.
  802. */
  803. void rcu_irq_exit(void)
  804. {
  805. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  806. lockdep_assert_irqs_disabled();
  807. if (rdtp->dynticks_nmi_nesting == 1)
  808. rcu_prepare_for_idle();
  809. rcu_nmi_exit();
  810. if (rdtp->dynticks_nmi_nesting == 0)
  811. rcu_dynticks_task_enter();
  812. }
  813. /*
  814. * Wrapper for rcu_irq_exit() where interrupts are enabled.
  815. *
  816. * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
  817. * with CONFIG_RCU_EQS_DEBUG=y.
  818. */
  819. void rcu_irq_exit_irqson(void)
  820. {
  821. unsigned long flags;
  822. local_irq_save(flags);
  823. rcu_irq_exit();
  824. local_irq_restore(flags);
  825. }
  826. /*
  827. * Exit an RCU extended quiescent state, which can be either the
  828. * idle loop or adaptive-tickless usermode execution.
  829. *
  830. * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
  831. * allow for the possibility of usermode upcalls messing up our count of
  832. * interrupt nesting level during the busy period that is just now starting.
  833. */
  834. static void rcu_eqs_exit(bool user)
  835. {
  836. struct rcu_dynticks *rdtp;
  837. long oldval;
  838. lockdep_assert_irqs_disabled();
  839. rdtp = this_cpu_ptr(&rcu_dynticks);
  840. oldval = rdtp->dynticks_nesting;
  841. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
  842. if (oldval) {
  843. rdtp->dynticks_nesting++;
  844. return;
  845. }
  846. rcu_dynticks_task_exit();
  847. rcu_dynticks_eqs_exit();
  848. rcu_cleanup_after_idle();
  849. trace_rcu_dyntick(TPS("End"), rdtp->dynticks_nesting, 1, rdtp->dynticks);
  850. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
  851. WRITE_ONCE(rdtp->dynticks_nesting, 1);
  852. WRITE_ONCE(rdtp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
  853. }
  854. /**
  855. * rcu_idle_exit - inform RCU that current CPU is leaving idle
  856. *
  857. * Exit idle mode, in other words, -enter- the mode in which RCU
  858. * read-side critical sections can occur.
  859. *
  860. * If you add or remove a call to rcu_idle_exit(), be sure to test with
  861. * CONFIG_RCU_EQS_DEBUG=y.
  862. */
  863. void rcu_idle_exit(void)
  864. {
  865. unsigned long flags;
  866. local_irq_save(flags);
  867. rcu_eqs_exit(false);
  868. local_irq_restore(flags);
  869. }
  870. #ifdef CONFIG_NO_HZ_FULL
  871. /**
  872. * rcu_user_exit - inform RCU that we are exiting userspace.
  873. *
  874. * Exit RCU idle mode while entering the kernel because it can
  875. * run a RCU read side critical section anytime.
  876. *
  877. * If you add or remove a call to rcu_user_exit(), be sure to test with
  878. * CONFIG_RCU_EQS_DEBUG=y.
  879. */
  880. void rcu_user_exit(void)
  881. {
  882. rcu_eqs_exit(1);
  883. }
  884. #endif /* CONFIG_NO_HZ_FULL */
  885. /**
  886. * rcu_nmi_enter - inform RCU of entry to NMI context
  887. *
  888. * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
  889. * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
  890. * that the CPU is active. This implementation permits nested NMIs, as
  891. * long as the nesting level does not overflow an int. (You will probably
  892. * run out of stack space first.)
  893. *
  894. * If you add or remove a call to rcu_nmi_enter(), be sure to test
  895. * with CONFIG_RCU_EQS_DEBUG=y.
  896. */
  897. void rcu_nmi_enter(void)
  898. {
  899. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  900. long incby = 2;
  901. /* Complain about underflow. */
  902. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
  903. /*
  904. * If idle from RCU viewpoint, atomically increment ->dynticks
  905. * to mark non-idle and increment ->dynticks_nmi_nesting by one.
  906. * Otherwise, increment ->dynticks_nmi_nesting by two. This means
  907. * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
  908. * to be in the outermost NMI handler that interrupted an RCU-idle
  909. * period (observation due to Andy Lutomirski).
  910. */
  911. if (rcu_dynticks_curr_cpu_in_eqs()) {
  912. rcu_dynticks_eqs_exit();
  913. incby = 1;
  914. }
  915. trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
  916. rdtp->dynticks_nmi_nesting,
  917. rdtp->dynticks_nmi_nesting + incby, rdtp->dynticks);
  918. WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* Prevent store tearing. */
  919. rdtp->dynticks_nmi_nesting + incby);
  920. barrier();
  921. }
  922. /**
  923. * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
  924. *
  925. * Enter an interrupt handler, which might possibly result in exiting
  926. * idle mode, in other words, entering the mode in which read-side critical
  927. * sections can occur. The caller must have disabled interrupts.
  928. *
  929. * Note that the Linux kernel is fully capable of entering an interrupt
  930. * handler that it never exits, for example when doing upcalls to user mode!
  931. * This code assumes that the idle loop never does upcalls to user mode.
  932. * If your architecture's idle loop does do upcalls to user mode (or does
  933. * anything else that results in unbalanced calls to the irq_enter() and
  934. * irq_exit() functions), RCU will give you what you deserve, good and hard.
  935. * But very infrequently and irreproducibly.
  936. *
  937. * Use things like work queues to work around this limitation.
  938. *
  939. * You have been warned.
  940. *
  941. * If you add or remove a call to rcu_irq_enter(), be sure to test with
  942. * CONFIG_RCU_EQS_DEBUG=y.
  943. */
  944. void rcu_irq_enter(void)
  945. {
  946. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  947. lockdep_assert_irqs_disabled();
  948. if (rdtp->dynticks_nmi_nesting == 0)
  949. rcu_dynticks_task_exit();
  950. rcu_nmi_enter();
  951. if (rdtp->dynticks_nmi_nesting == 1)
  952. rcu_cleanup_after_idle();
  953. }
  954. /*
  955. * Wrapper for rcu_irq_enter() where interrupts are enabled.
  956. *
  957. * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
  958. * with CONFIG_RCU_EQS_DEBUG=y.
  959. */
  960. void rcu_irq_enter_irqson(void)
  961. {
  962. unsigned long flags;
  963. local_irq_save(flags);
  964. rcu_irq_enter();
  965. local_irq_restore(flags);
  966. }
  967. /**
  968. * rcu_is_watching - see if RCU thinks that the current CPU is idle
  969. *
  970. * Return true if RCU is watching the running CPU, which means that this
  971. * CPU can safely enter RCU read-side critical sections. In other words,
  972. * if the current CPU is in its idle loop and is neither in an interrupt
  973. * or NMI handler, return true.
  974. */
  975. bool notrace rcu_is_watching(void)
  976. {
  977. bool ret;
  978. preempt_disable_notrace();
  979. ret = !rcu_dynticks_curr_cpu_in_eqs();
  980. preempt_enable_notrace();
  981. return ret;
  982. }
  983. EXPORT_SYMBOL_GPL(rcu_is_watching);
  984. /*
  985. * If a holdout task is actually running, request an urgent quiescent
  986. * state from its CPU. This is unsynchronized, so migrations can cause
  987. * the request to go to the wrong CPU. Which is OK, all that will happen
  988. * is that the CPU's next context switch will be a bit slower and next
  989. * time around this task will generate another request.
  990. */
  991. void rcu_request_urgent_qs_task(struct task_struct *t)
  992. {
  993. int cpu;
  994. barrier();
  995. cpu = task_cpu(t);
  996. if (!task_curr(t))
  997. return; /* This task is not running on that CPU. */
  998. smp_store_release(per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, cpu), true);
  999. }
  1000. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  1001. /*
  1002. * Is the current CPU online? Disable preemption to avoid false positives
  1003. * that could otherwise happen due to the current CPU number being sampled,
  1004. * this task being preempted, its old CPU being taken offline, resuming
  1005. * on some other CPU, then determining that its old CPU is now offline.
  1006. * It is OK to use RCU on an offline processor during initial boot, hence
  1007. * the check for rcu_scheduler_fully_active. Note also that it is OK
  1008. * for a CPU coming online to use RCU for one jiffy prior to marking itself
  1009. * online in the cpu_online_mask. Similarly, it is OK for a CPU going
  1010. * offline to continue to use RCU for one jiffy after marking itself
  1011. * offline in the cpu_online_mask. This leniency is necessary given the
  1012. * non-atomic nature of the online and offline processing, for example,
  1013. * the fact that a CPU enters the scheduler after completing the teardown
  1014. * of the CPU.
  1015. *
  1016. * This is also why RCU internally marks CPUs online during in the
  1017. * preparation phase and offline after the CPU has been taken down.
  1018. *
  1019. * Disable checking if in an NMI handler because we cannot safely report
  1020. * errors from NMI handlers anyway.
  1021. */
  1022. bool rcu_lockdep_current_cpu_online(void)
  1023. {
  1024. struct rcu_data *rdp;
  1025. struct rcu_node *rnp;
  1026. bool ret;
  1027. if (in_nmi())
  1028. return true;
  1029. preempt_disable();
  1030. rdp = this_cpu_ptr(&rcu_sched_data);
  1031. rnp = rdp->mynode;
  1032. ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
  1033. !rcu_scheduler_fully_active;
  1034. preempt_enable();
  1035. return ret;
  1036. }
  1037. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  1038. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  1039. /**
  1040. * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
  1041. *
  1042. * If the current CPU is idle or running at a first-level (not nested)
  1043. * interrupt from idle, return true. The caller must have at least
  1044. * disabled preemption.
  1045. */
  1046. static int rcu_is_cpu_rrupt_from_idle(void)
  1047. {
  1048. return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 0 &&
  1049. __this_cpu_read(rcu_dynticks.dynticks_nmi_nesting) <= 1;
  1050. }
  1051. /*
  1052. * We are reporting a quiescent state on behalf of some other CPU, so
  1053. * it is our responsibility to check for and handle potential overflow
  1054. * of the rcu_node ->gpnum counter with respect to the rcu_data counters.
  1055. * After all, the CPU might be in deep idle state, and thus executing no
  1056. * code whatsoever.
  1057. */
  1058. static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
  1059. {
  1060. raw_lockdep_assert_held_rcu_node(rnp);
  1061. if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4, rnp->gpnum))
  1062. WRITE_ONCE(rdp->gpwrap, true);
  1063. if (ULONG_CMP_LT(rdp->rcu_iw_gpnum + ULONG_MAX / 4, rnp->gpnum))
  1064. rdp->rcu_iw_gpnum = rnp->gpnum + ULONG_MAX / 4;
  1065. }
  1066. /*
  1067. * Snapshot the specified CPU's dynticks counter so that we can later
  1068. * credit them with an implicit quiescent state. Return 1 if this CPU
  1069. * is in dynticks idle mode, which is an extended quiescent state.
  1070. */
  1071. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  1072. {
  1073. rdp->dynticks_snap = rcu_dynticks_snap(rdp->dynticks);
  1074. if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
  1075. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1076. rcu_gpnum_ovf(rdp->mynode, rdp);
  1077. return 1;
  1078. }
  1079. return 0;
  1080. }
  1081. /*
  1082. * Handler for the irq_work request posted when a grace period has
  1083. * gone on for too long, but not yet long enough for an RCU CPU
  1084. * stall warning. Set state appropriately, but just complain if
  1085. * there is unexpected state on entry.
  1086. */
  1087. static void rcu_iw_handler(struct irq_work *iwp)
  1088. {
  1089. struct rcu_data *rdp;
  1090. struct rcu_node *rnp;
  1091. rdp = container_of(iwp, struct rcu_data, rcu_iw);
  1092. rnp = rdp->mynode;
  1093. raw_spin_lock_rcu_node(rnp);
  1094. if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
  1095. rdp->rcu_iw_gpnum = rnp->gpnum;
  1096. rdp->rcu_iw_pending = false;
  1097. }
  1098. raw_spin_unlock_rcu_node(rnp);
  1099. }
  1100. /*
  1101. * Return true if the specified CPU has passed through a quiescent
  1102. * state by virtue of being in or having passed through an dynticks
  1103. * idle state since the last call to dyntick_save_progress_counter()
  1104. * for this same CPU, or by virtue of having been offline.
  1105. */
  1106. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  1107. {
  1108. unsigned long jtsq;
  1109. bool *rnhqp;
  1110. bool *ruqp;
  1111. struct rcu_node *rnp = rdp->mynode;
  1112. /*
  1113. * If the CPU passed through or entered a dynticks idle phase with
  1114. * no active irq/NMI handlers, then we can safely pretend that the CPU
  1115. * already acknowledged the request to pass through a quiescent
  1116. * state. Either way, that CPU cannot possibly be in an RCU
  1117. * read-side critical section that started before the beginning
  1118. * of the current RCU grace period.
  1119. */
  1120. if (rcu_dynticks_in_eqs_since(rdp->dynticks, rdp->dynticks_snap)) {
  1121. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1122. rdp->dynticks_fqs++;
  1123. rcu_gpnum_ovf(rnp, rdp);
  1124. return 1;
  1125. }
  1126. /*
  1127. * Has this CPU encountered a cond_resched_rcu_qs() since the
  1128. * beginning of the grace period? For this to be the case,
  1129. * the CPU has to have noticed the current grace period. This
  1130. * might not be the case for nohz_full CPUs looping in the kernel.
  1131. */
  1132. jtsq = jiffies_till_sched_qs;
  1133. ruqp = per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, rdp->cpu);
  1134. if (time_after(jiffies, rdp->rsp->gp_start + jtsq) &&
  1135. READ_ONCE(rdp->rcu_qs_ctr_snap) != per_cpu(rcu_dynticks.rcu_qs_ctr, rdp->cpu) &&
  1136. READ_ONCE(rdp->gpnum) == rnp->gpnum && !rdp->gpwrap) {
  1137. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("rqc"));
  1138. rcu_gpnum_ovf(rnp, rdp);
  1139. return 1;
  1140. } else if (time_after(jiffies, rdp->rsp->gp_start + jtsq)) {
  1141. /* Load rcu_qs_ctr before store to rcu_urgent_qs. */
  1142. smp_store_release(ruqp, true);
  1143. }
  1144. /* Check for the CPU being offline. */
  1145. if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp))) {
  1146. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
  1147. rdp->offline_fqs++;
  1148. rcu_gpnum_ovf(rnp, rdp);
  1149. return 1;
  1150. }
  1151. /*
  1152. * A CPU running for an extended time within the kernel can
  1153. * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
  1154. * even context-switching back and forth between a pair of
  1155. * in-kernel CPU-bound tasks cannot advance grace periods.
  1156. * So if the grace period is old enough, make the CPU pay attention.
  1157. * Note that the unsynchronized assignments to the per-CPU
  1158. * rcu_need_heavy_qs variable are safe. Yes, setting of
  1159. * bits can be lost, but they will be set again on the next
  1160. * force-quiescent-state pass. So lost bit sets do not result
  1161. * in incorrect behavior, merely in a grace period lasting
  1162. * a few jiffies longer than it might otherwise. Because
  1163. * there are at most four threads involved, and because the
  1164. * updates are only once every few jiffies, the probability of
  1165. * lossage (and thus of slight grace-period extension) is
  1166. * quite low.
  1167. */
  1168. rnhqp = &per_cpu(rcu_dynticks.rcu_need_heavy_qs, rdp->cpu);
  1169. if (!READ_ONCE(*rnhqp) &&
  1170. (time_after(jiffies, rdp->rsp->gp_start + jtsq) ||
  1171. time_after(jiffies, rdp->rsp->jiffies_resched))) {
  1172. WRITE_ONCE(*rnhqp, true);
  1173. /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
  1174. smp_store_release(ruqp, true);
  1175. rdp->rsp->jiffies_resched += jtsq; /* Re-enable beating. */
  1176. }
  1177. /*
  1178. * If more than halfway to RCU CPU stall-warning time, do a
  1179. * resched_cpu() to try to loosen things up a bit. Also check to
  1180. * see if the CPU is getting hammered with interrupts, but only
  1181. * once per grace period, just to keep the IPIs down to a dull roar.
  1182. */
  1183. if (jiffies - rdp->rsp->gp_start > rcu_jiffies_till_stall_check() / 2) {
  1184. resched_cpu(rdp->cpu);
  1185. if (IS_ENABLED(CONFIG_IRQ_WORK) &&
  1186. !rdp->rcu_iw_pending && rdp->rcu_iw_gpnum != rnp->gpnum &&
  1187. (rnp->ffmask & rdp->grpmask)) {
  1188. init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
  1189. rdp->rcu_iw_pending = true;
  1190. rdp->rcu_iw_gpnum = rnp->gpnum;
  1191. irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
  1192. }
  1193. }
  1194. return 0;
  1195. }
  1196. static void record_gp_stall_check_time(struct rcu_state *rsp)
  1197. {
  1198. unsigned long j = jiffies;
  1199. unsigned long j1;
  1200. rsp->gp_start = j;
  1201. smp_wmb(); /* Record start time before stall time. */
  1202. j1 = rcu_jiffies_till_stall_check();
  1203. WRITE_ONCE(rsp->jiffies_stall, j + j1);
  1204. rsp->jiffies_resched = j + j1 / 2;
  1205. rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
  1206. }
  1207. /*
  1208. * Convert a ->gp_state value to a character string.
  1209. */
  1210. static const char *gp_state_getname(short gs)
  1211. {
  1212. if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
  1213. return "???";
  1214. return gp_state_names[gs];
  1215. }
  1216. /*
  1217. * Complain about starvation of grace-period kthread.
  1218. */
  1219. static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
  1220. {
  1221. unsigned long gpa;
  1222. unsigned long j;
  1223. j = jiffies;
  1224. gpa = READ_ONCE(rsp->gp_activity);
  1225. if (j - gpa > 2 * HZ) {
  1226. pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx ->cpu=%d\n",
  1227. rsp->name, j - gpa,
  1228. rsp->gpnum, rsp->completed,
  1229. rsp->gp_flags,
  1230. gp_state_getname(rsp->gp_state), rsp->gp_state,
  1231. rsp->gp_kthread ? rsp->gp_kthread->state : ~0,
  1232. rsp->gp_kthread ? task_cpu(rsp->gp_kthread) : -1);
  1233. if (rsp->gp_kthread) {
  1234. pr_err("RCU grace-period kthread stack dump:\n");
  1235. sched_show_task(rsp->gp_kthread);
  1236. wake_up_process(rsp->gp_kthread);
  1237. }
  1238. }
  1239. }
  1240. /*
  1241. * Dump stacks of all tasks running on stalled CPUs. First try using
  1242. * NMIs, but fall back to manual remote stack tracing on architectures
  1243. * that don't support NMI-based stack dumps. The NMI-triggered stack
  1244. * traces are more accurate because they are printed by the target CPU.
  1245. */
  1246. static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
  1247. {
  1248. int cpu;
  1249. unsigned long flags;
  1250. struct rcu_node *rnp;
  1251. rcu_for_each_leaf_node(rsp, rnp) {
  1252. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1253. for_each_leaf_node_possible_cpu(rnp, cpu)
  1254. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
  1255. if (!trigger_single_cpu_backtrace(cpu))
  1256. dump_cpu_task(cpu);
  1257. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1258. }
  1259. }
  1260. /*
  1261. * If too much time has passed in the current grace period, and if
  1262. * so configured, go kick the relevant kthreads.
  1263. */
  1264. static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
  1265. {
  1266. unsigned long j;
  1267. if (!rcu_kick_kthreads)
  1268. return;
  1269. j = READ_ONCE(rsp->jiffies_kick_kthreads);
  1270. if (time_after(jiffies, j) && rsp->gp_kthread &&
  1271. (rcu_gp_in_progress(rsp) || READ_ONCE(rsp->gp_flags))) {
  1272. WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
  1273. rcu_ftrace_dump(DUMP_ALL);
  1274. wake_up_process(rsp->gp_kthread);
  1275. WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
  1276. }
  1277. }
  1278. static inline void panic_on_rcu_stall(void)
  1279. {
  1280. if (sysctl_panic_on_rcu_stall)
  1281. panic("RCU Stall\n");
  1282. }
  1283. static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
  1284. {
  1285. int cpu;
  1286. long delta;
  1287. unsigned long flags;
  1288. unsigned long gpa;
  1289. unsigned long j;
  1290. int ndetected = 0;
  1291. struct rcu_node *rnp = rcu_get_root(rsp);
  1292. long totqlen = 0;
  1293. /* Kick and suppress, if so configured. */
  1294. rcu_stall_kick_kthreads(rsp);
  1295. if (rcu_cpu_stall_suppress)
  1296. return;
  1297. /* Only let one CPU complain about others per time interval. */
  1298. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1299. delta = jiffies - READ_ONCE(rsp->jiffies_stall);
  1300. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  1301. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1302. return;
  1303. }
  1304. WRITE_ONCE(rsp->jiffies_stall,
  1305. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1306. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1307. /*
  1308. * OK, time to rat on our buddy...
  1309. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1310. * RCU CPU stall warnings.
  1311. */
  1312. pr_err("INFO: %s detected stalls on CPUs/tasks:",
  1313. rsp->name);
  1314. print_cpu_stall_info_begin();
  1315. rcu_for_each_leaf_node(rsp, rnp) {
  1316. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1317. ndetected += rcu_print_task_stall(rnp);
  1318. if (rnp->qsmask != 0) {
  1319. for_each_leaf_node_possible_cpu(rnp, cpu)
  1320. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
  1321. print_cpu_stall_info(rsp, cpu);
  1322. ndetected++;
  1323. }
  1324. }
  1325. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1326. }
  1327. print_cpu_stall_info_end();
  1328. for_each_possible_cpu(cpu)
  1329. totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(rsp->rda,
  1330. cpu)->cblist);
  1331. pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
  1332. smp_processor_id(), (long)(jiffies - rsp->gp_start),
  1333. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1334. if (ndetected) {
  1335. rcu_dump_cpu_stacks(rsp);
  1336. /* Complain about tasks blocking the grace period. */
  1337. rcu_print_detail_task_stall(rsp);
  1338. } else {
  1339. if (READ_ONCE(rsp->gpnum) != gpnum ||
  1340. READ_ONCE(rsp->completed) == gpnum) {
  1341. pr_err("INFO: Stall ended before state dump start\n");
  1342. } else {
  1343. j = jiffies;
  1344. gpa = READ_ONCE(rsp->gp_activity);
  1345. pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
  1346. rsp->name, j - gpa, j, gpa,
  1347. jiffies_till_next_fqs,
  1348. rcu_get_root(rsp)->qsmask);
  1349. /* In this case, the current CPU might be at fault. */
  1350. sched_show_task(current);
  1351. }
  1352. }
  1353. rcu_check_gp_kthread_starvation(rsp);
  1354. panic_on_rcu_stall();
  1355. force_quiescent_state(rsp); /* Kick them all. */
  1356. }
  1357. static void print_cpu_stall(struct rcu_state *rsp)
  1358. {
  1359. int cpu;
  1360. unsigned long flags;
  1361. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1362. struct rcu_node *rnp = rcu_get_root(rsp);
  1363. long totqlen = 0;
  1364. /* Kick and suppress, if so configured. */
  1365. rcu_stall_kick_kthreads(rsp);
  1366. if (rcu_cpu_stall_suppress)
  1367. return;
  1368. /*
  1369. * OK, time to rat on ourselves...
  1370. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1371. * RCU CPU stall warnings.
  1372. */
  1373. pr_err("INFO: %s self-detected stall on CPU", rsp->name);
  1374. print_cpu_stall_info_begin();
  1375. raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
  1376. print_cpu_stall_info(rsp, smp_processor_id());
  1377. raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
  1378. print_cpu_stall_info_end();
  1379. for_each_possible_cpu(cpu)
  1380. totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(rsp->rda,
  1381. cpu)->cblist);
  1382. pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
  1383. jiffies - rsp->gp_start,
  1384. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1385. rcu_check_gp_kthread_starvation(rsp);
  1386. rcu_dump_cpu_stacks(rsp);
  1387. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1388. if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
  1389. WRITE_ONCE(rsp->jiffies_stall,
  1390. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1391. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1392. panic_on_rcu_stall();
  1393. /*
  1394. * Attempt to revive the RCU machinery by forcing a context switch.
  1395. *
  1396. * A context switch would normally allow the RCU state machine to make
  1397. * progress and it could be we're stuck in kernel space without context
  1398. * switches for an entirely unreasonable amount of time.
  1399. */
  1400. resched_cpu(smp_processor_id());
  1401. }
  1402. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  1403. {
  1404. unsigned long completed;
  1405. unsigned long gpnum;
  1406. unsigned long gps;
  1407. unsigned long j;
  1408. unsigned long js;
  1409. struct rcu_node *rnp;
  1410. if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
  1411. !rcu_gp_in_progress(rsp))
  1412. return;
  1413. rcu_stall_kick_kthreads(rsp);
  1414. j = jiffies;
  1415. /*
  1416. * Lots of memory barriers to reject false positives.
  1417. *
  1418. * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
  1419. * then rsp->gp_start, and finally rsp->completed. These values
  1420. * are updated in the opposite order with memory barriers (or
  1421. * equivalent) during grace-period initialization and cleanup.
  1422. * Now, a false positive can occur if we get an new value of
  1423. * rsp->gp_start and a old value of rsp->jiffies_stall. But given
  1424. * the memory barriers, the only way that this can happen is if one
  1425. * grace period ends and another starts between these two fetches.
  1426. * Detect this by comparing rsp->completed with the previous fetch
  1427. * from rsp->gpnum.
  1428. *
  1429. * Given this check, comparisons of jiffies, rsp->jiffies_stall,
  1430. * and rsp->gp_start suffice to forestall false positives.
  1431. */
  1432. gpnum = READ_ONCE(rsp->gpnum);
  1433. smp_rmb(); /* Pick up ->gpnum first... */
  1434. js = READ_ONCE(rsp->jiffies_stall);
  1435. smp_rmb(); /* ...then ->jiffies_stall before the rest... */
  1436. gps = READ_ONCE(rsp->gp_start);
  1437. smp_rmb(); /* ...and finally ->gp_start before ->completed. */
  1438. completed = READ_ONCE(rsp->completed);
  1439. if (ULONG_CMP_GE(completed, gpnum) ||
  1440. ULONG_CMP_LT(j, js) ||
  1441. ULONG_CMP_GE(gps, js))
  1442. return; /* No stall or GP completed since entering function. */
  1443. rnp = rdp->mynode;
  1444. if (rcu_gp_in_progress(rsp) &&
  1445. (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
  1446. /* We haven't checked in, so go dump stack. */
  1447. print_cpu_stall(rsp);
  1448. } else if (rcu_gp_in_progress(rsp) &&
  1449. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  1450. /* They had a few time units to dump stack, so complain. */
  1451. print_other_cpu_stall(rsp, gpnum);
  1452. }
  1453. }
  1454. /**
  1455. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  1456. *
  1457. * Set the stall-warning timeout way off into the future, thus preventing
  1458. * any RCU CPU stall-warning messages from appearing in the current set of
  1459. * RCU grace periods.
  1460. *
  1461. * The caller must disable hard irqs.
  1462. */
  1463. void rcu_cpu_stall_reset(void)
  1464. {
  1465. struct rcu_state *rsp;
  1466. for_each_rcu_flavor(rsp)
  1467. WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
  1468. }
  1469. /*
  1470. * Determine the value that ->completed will have at the end of the
  1471. * next subsequent grace period. This is used to tag callbacks so that
  1472. * a CPU can invoke callbacks in a timely fashion even if that CPU has
  1473. * been dyntick-idle for an extended period with callbacks under the
  1474. * influence of RCU_FAST_NO_HZ.
  1475. *
  1476. * The caller must hold rnp->lock with interrupts disabled.
  1477. */
  1478. static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
  1479. struct rcu_node *rnp)
  1480. {
  1481. raw_lockdep_assert_held_rcu_node(rnp);
  1482. /*
  1483. * If RCU is idle, we just wait for the next grace period.
  1484. * But we can only be sure that RCU is idle if we are looking
  1485. * at the root rcu_node structure -- otherwise, a new grace
  1486. * period might have started, but just not yet gotten around
  1487. * to initializing the current non-root rcu_node structure.
  1488. */
  1489. if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
  1490. return rnp->completed + 1;
  1491. /*
  1492. * Otherwise, wait for a possible partial grace period and
  1493. * then the subsequent full grace period.
  1494. */
  1495. return rnp->completed + 2;
  1496. }
  1497. /*
  1498. * Trace-event helper function for rcu_start_future_gp() and
  1499. * rcu_nocb_wait_gp().
  1500. */
  1501. static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1502. unsigned long c, const char *s)
  1503. {
  1504. trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
  1505. rnp->completed, c, rnp->level,
  1506. rnp->grplo, rnp->grphi, s);
  1507. }
  1508. /*
  1509. * Start some future grace period, as needed to handle newly arrived
  1510. * callbacks. The required future grace periods are recorded in each
  1511. * rcu_node structure's ->need_future_gp field. Returns true if there
  1512. * is reason to awaken the grace-period kthread.
  1513. *
  1514. * The caller must hold the specified rcu_node structure's ->lock.
  1515. */
  1516. static bool __maybe_unused
  1517. rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1518. unsigned long *c_out)
  1519. {
  1520. unsigned long c;
  1521. bool ret = false;
  1522. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  1523. raw_lockdep_assert_held_rcu_node(rnp);
  1524. /*
  1525. * Pick up grace-period number for new callbacks. If this
  1526. * grace period is already marked as needed, return to the caller.
  1527. */
  1528. c = rcu_cbs_completed(rdp->rsp, rnp);
  1529. trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
  1530. if (rnp->need_future_gp[c & 0x1]) {
  1531. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
  1532. goto out;
  1533. }
  1534. /*
  1535. * If either this rcu_node structure or the root rcu_node structure
  1536. * believe that a grace period is in progress, then we must wait
  1537. * for the one following, which is in "c". Because our request
  1538. * will be noticed at the end of the current grace period, we don't
  1539. * need to explicitly start one. We only do the lockless check
  1540. * of rnp_root's fields if the current rcu_node structure thinks
  1541. * there is no grace period in flight, and because we hold rnp->lock,
  1542. * the only possible change is when rnp_root's two fields are
  1543. * equal, in which case rnp_root->gpnum might be concurrently
  1544. * incremented. But that is OK, as it will just result in our
  1545. * doing some extra useless work.
  1546. */
  1547. if (rnp->gpnum != rnp->completed ||
  1548. READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
  1549. rnp->need_future_gp[c & 0x1]++;
  1550. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
  1551. goto out;
  1552. }
  1553. /*
  1554. * There might be no grace period in progress. If we don't already
  1555. * hold it, acquire the root rcu_node structure's lock in order to
  1556. * start one (if needed).
  1557. */
  1558. if (rnp != rnp_root)
  1559. raw_spin_lock_rcu_node(rnp_root);
  1560. /*
  1561. * Get a new grace-period number. If there really is no grace
  1562. * period in progress, it will be smaller than the one we obtained
  1563. * earlier. Adjust callbacks as needed.
  1564. */
  1565. c = rcu_cbs_completed(rdp->rsp, rnp_root);
  1566. if (!rcu_is_nocb_cpu(rdp->cpu))
  1567. (void)rcu_segcblist_accelerate(&rdp->cblist, c);
  1568. /*
  1569. * If the needed for the required grace period is already
  1570. * recorded, trace and leave.
  1571. */
  1572. if (rnp_root->need_future_gp[c & 0x1]) {
  1573. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
  1574. goto unlock_out;
  1575. }
  1576. /* Record the need for the future grace period. */
  1577. rnp_root->need_future_gp[c & 0x1]++;
  1578. /* If a grace period is not already in progress, start one. */
  1579. if (rnp_root->gpnum != rnp_root->completed) {
  1580. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
  1581. } else {
  1582. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
  1583. ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
  1584. }
  1585. unlock_out:
  1586. if (rnp != rnp_root)
  1587. raw_spin_unlock_rcu_node(rnp_root);
  1588. out:
  1589. if (c_out != NULL)
  1590. *c_out = c;
  1591. return ret;
  1592. }
  1593. /*
  1594. * Clean up any old requests for the just-ended grace period. Also return
  1595. * whether any additional grace periods have been requested.
  1596. */
  1597. static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1598. {
  1599. int c = rnp->completed;
  1600. int needmore;
  1601. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1602. rnp->need_future_gp[c & 0x1] = 0;
  1603. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  1604. trace_rcu_future_gp(rnp, rdp, c,
  1605. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  1606. return needmore;
  1607. }
  1608. /*
  1609. * Awaken the grace-period kthread for the specified flavor of RCU.
  1610. * Don't do a self-awaken, and don't bother awakening when there is
  1611. * nothing for the grace-period kthread to do (as in several CPUs
  1612. * raced to awaken, and we lost), and finally don't try to awaken
  1613. * a kthread that has not yet been created.
  1614. */
  1615. static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  1616. {
  1617. if (current == rsp->gp_kthread ||
  1618. !READ_ONCE(rsp->gp_flags) ||
  1619. !rsp->gp_kthread)
  1620. return;
  1621. swake_up(&rsp->gp_wq);
  1622. }
  1623. /*
  1624. * If there is room, assign a ->completed number to any callbacks on
  1625. * this CPU that have not already been assigned. Also accelerate any
  1626. * callbacks that were previously assigned a ->completed number that has
  1627. * since proven to be too conservative, which can happen if callbacks get
  1628. * assigned a ->completed number while RCU is idle, but with reference to
  1629. * a non-root rcu_node structure. This function is idempotent, so it does
  1630. * not hurt to call it repeatedly. Returns an flag saying that we should
  1631. * awaken the RCU grace-period kthread.
  1632. *
  1633. * The caller must hold rnp->lock with interrupts disabled.
  1634. */
  1635. static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1636. struct rcu_data *rdp)
  1637. {
  1638. bool ret = false;
  1639. raw_lockdep_assert_held_rcu_node(rnp);
  1640. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1641. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1642. return false;
  1643. /*
  1644. * Callbacks are often registered with incomplete grace-period
  1645. * information. Something about the fact that getting exact
  1646. * information requires acquiring a global lock... RCU therefore
  1647. * makes a conservative estimate of the grace period number at which
  1648. * a given callback will become ready to invoke. The following
  1649. * code checks this estimate and improves it when possible, thus
  1650. * accelerating callback invocation to an earlier grace-period
  1651. * number.
  1652. */
  1653. if (rcu_segcblist_accelerate(&rdp->cblist, rcu_cbs_completed(rsp, rnp)))
  1654. ret = rcu_start_future_gp(rnp, rdp, NULL);
  1655. /* Trace depending on how much we were able to accelerate. */
  1656. if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
  1657. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
  1658. else
  1659. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
  1660. return ret;
  1661. }
  1662. /*
  1663. * Move any callbacks whose grace period has completed to the
  1664. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1665. * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
  1666. * sublist. This function is idempotent, so it does not hurt to
  1667. * invoke it repeatedly. As long as it is not invoked -too- often...
  1668. * Returns true if the RCU grace-period kthread needs to be awakened.
  1669. *
  1670. * The caller must hold rnp->lock with interrupts disabled.
  1671. */
  1672. static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1673. struct rcu_data *rdp)
  1674. {
  1675. raw_lockdep_assert_held_rcu_node(rnp);
  1676. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1677. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1678. return false;
  1679. /*
  1680. * Find all callbacks whose ->completed numbers indicate that they
  1681. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1682. */
  1683. rcu_segcblist_advance(&rdp->cblist, rnp->completed);
  1684. /* Classify any remaining callbacks. */
  1685. return rcu_accelerate_cbs(rsp, rnp, rdp);
  1686. }
  1687. /*
  1688. * Update CPU-local rcu_data state to record the beginnings and ends of
  1689. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1690. * structure corresponding to the current CPU, and must have irqs disabled.
  1691. * Returns true if the grace-period kthread needs to be awakened.
  1692. */
  1693. static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
  1694. struct rcu_data *rdp)
  1695. {
  1696. bool ret;
  1697. bool need_gp;
  1698. raw_lockdep_assert_held_rcu_node(rnp);
  1699. /* Handle the ends of any preceding grace periods first. */
  1700. if (rdp->completed == rnp->completed &&
  1701. !unlikely(READ_ONCE(rdp->gpwrap))) {
  1702. /* No grace period end, so just accelerate recent callbacks. */
  1703. ret = rcu_accelerate_cbs(rsp, rnp, rdp);
  1704. } else {
  1705. /* Advance callbacks. */
  1706. ret = rcu_advance_cbs(rsp, rnp, rdp);
  1707. /* Remember that we saw this grace-period completion. */
  1708. rdp->completed = rnp->completed;
  1709. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
  1710. }
  1711. if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
  1712. /*
  1713. * If the current grace period is waiting for this CPU,
  1714. * set up to detect a quiescent state, otherwise don't
  1715. * go looking for one.
  1716. */
  1717. rdp->gpnum = rnp->gpnum;
  1718. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
  1719. need_gp = !!(rnp->qsmask & rdp->grpmask);
  1720. rdp->cpu_no_qs.b.norm = need_gp;
  1721. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
  1722. rdp->core_needs_qs = need_gp;
  1723. zero_cpu_stall_ticks(rdp);
  1724. WRITE_ONCE(rdp->gpwrap, false);
  1725. rcu_gpnum_ovf(rnp, rdp);
  1726. }
  1727. return ret;
  1728. }
  1729. static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
  1730. {
  1731. unsigned long flags;
  1732. bool needwake;
  1733. struct rcu_node *rnp;
  1734. local_irq_save(flags);
  1735. rnp = rdp->mynode;
  1736. if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
  1737. rdp->completed == READ_ONCE(rnp->completed) &&
  1738. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1739. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1740. local_irq_restore(flags);
  1741. return;
  1742. }
  1743. needwake = __note_gp_changes(rsp, rnp, rdp);
  1744. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1745. if (needwake)
  1746. rcu_gp_kthread_wake(rsp);
  1747. }
  1748. static void rcu_gp_slow(struct rcu_state *rsp, int delay)
  1749. {
  1750. if (delay > 0 &&
  1751. !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1752. schedule_timeout_uninterruptible(delay);
  1753. }
  1754. /*
  1755. * Initialize a new grace period. Return false if no grace period required.
  1756. */
  1757. static bool rcu_gp_init(struct rcu_state *rsp)
  1758. {
  1759. unsigned long oldmask;
  1760. struct rcu_data *rdp;
  1761. struct rcu_node *rnp = rcu_get_root(rsp);
  1762. WRITE_ONCE(rsp->gp_activity, jiffies);
  1763. raw_spin_lock_irq_rcu_node(rnp);
  1764. if (!READ_ONCE(rsp->gp_flags)) {
  1765. /* Spurious wakeup, tell caller to go back to sleep. */
  1766. raw_spin_unlock_irq_rcu_node(rnp);
  1767. return false;
  1768. }
  1769. WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
  1770. if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
  1771. /*
  1772. * Grace period already in progress, don't start another.
  1773. * Not supposed to be able to happen.
  1774. */
  1775. raw_spin_unlock_irq_rcu_node(rnp);
  1776. return false;
  1777. }
  1778. /* Advance to a new grace period and initialize state. */
  1779. record_gp_stall_check_time(rsp);
  1780. /* Record GP times before starting GP, hence smp_store_release(). */
  1781. smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
  1782. trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
  1783. raw_spin_unlock_irq_rcu_node(rnp);
  1784. /*
  1785. * Apply per-leaf buffered online and offline operations to the
  1786. * rcu_node tree. Note that this new grace period need not wait
  1787. * for subsequent online CPUs, and that quiescent-state forcing
  1788. * will handle subsequent offline CPUs.
  1789. */
  1790. rcu_for_each_leaf_node(rsp, rnp) {
  1791. rcu_gp_slow(rsp, gp_preinit_delay);
  1792. raw_spin_lock_irq_rcu_node(rnp);
  1793. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1794. !rnp->wait_blkd_tasks) {
  1795. /* Nothing to do on this leaf rcu_node structure. */
  1796. raw_spin_unlock_irq_rcu_node(rnp);
  1797. continue;
  1798. }
  1799. /* Record old state, apply changes to ->qsmaskinit field. */
  1800. oldmask = rnp->qsmaskinit;
  1801. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1802. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1803. if (!oldmask != !rnp->qsmaskinit) {
  1804. if (!oldmask) /* First online CPU for this rcu_node. */
  1805. rcu_init_new_rnp(rnp);
  1806. else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
  1807. rnp->wait_blkd_tasks = true;
  1808. else /* Last offline CPU and can propagate. */
  1809. rcu_cleanup_dead_rnp(rnp);
  1810. }
  1811. /*
  1812. * If all waited-on tasks from prior grace period are
  1813. * done, and if all this rcu_node structure's CPUs are
  1814. * still offline, propagate up the rcu_node tree and
  1815. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1816. * rcu_node structure's CPUs has since come back online,
  1817. * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
  1818. * checks for this, so just call it unconditionally).
  1819. */
  1820. if (rnp->wait_blkd_tasks &&
  1821. (!rcu_preempt_has_tasks(rnp) ||
  1822. rnp->qsmaskinit)) {
  1823. rnp->wait_blkd_tasks = false;
  1824. rcu_cleanup_dead_rnp(rnp);
  1825. }
  1826. raw_spin_unlock_irq_rcu_node(rnp);
  1827. }
  1828. /*
  1829. * Set the quiescent-state-needed bits in all the rcu_node
  1830. * structures for all currently online CPUs in breadth-first order,
  1831. * starting from the root rcu_node structure, relying on the layout
  1832. * of the tree within the rsp->node[] array. Note that other CPUs
  1833. * will access only the leaves of the hierarchy, thus seeing that no
  1834. * grace period is in progress, at least until the corresponding
  1835. * leaf node has been initialized.
  1836. *
  1837. * The grace period cannot complete until the initialization
  1838. * process finishes, because this kthread handles both.
  1839. */
  1840. rcu_for_each_node_breadth_first(rsp, rnp) {
  1841. rcu_gp_slow(rsp, gp_init_delay);
  1842. raw_spin_lock_irq_rcu_node(rnp);
  1843. rdp = this_cpu_ptr(rsp->rda);
  1844. rcu_preempt_check_blocked_tasks(rnp);
  1845. rnp->qsmask = rnp->qsmaskinit;
  1846. WRITE_ONCE(rnp->gpnum, rsp->gpnum);
  1847. if (WARN_ON_ONCE(rnp->completed != rsp->completed))
  1848. WRITE_ONCE(rnp->completed, rsp->completed);
  1849. if (rnp == rdp->mynode)
  1850. (void)__note_gp_changes(rsp, rnp, rdp);
  1851. rcu_preempt_boost_start_gp(rnp);
  1852. trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
  1853. rnp->level, rnp->grplo,
  1854. rnp->grphi, rnp->qsmask);
  1855. raw_spin_unlock_irq_rcu_node(rnp);
  1856. cond_resched_rcu_qs();
  1857. WRITE_ONCE(rsp->gp_activity, jiffies);
  1858. }
  1859. return true;
  1860. }
  1861. /*
  1862. * Helper function for swait_event_idle() wakeup at force-quiescent-state
  1863. * time.
  1864. */
  1865. static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
  1866. {
  1867. struct rcu_node *rnp = rcu_get_root(rsp);
  1868. /* Someone like call_rcu() requested a force-quiescent-state scan. */
  1869. *gfp = READ_ONCE(rsp->gp_flags);
  1870. if (*gfp & RCU_GP_FLAG_FQS)
  1871. return true;
  1872. /* The current grace period has completed. */
  1873. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1874. return true;
  1875. return false;
  1876. }
  1877. /*
  1878. * Do one round of quiescent-state forcing.
  1879. */
  1880. static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
  1881. {
  1882. struct rcu_node *rnp = rcu_get_root(rsp);
  1883. WRITE_ONCE(rsp->gp_activity, jiffies);
  1884. rsp->n_force_qs++;
  1885. if (first_time) {
  1886. /* Collect dyntick-idle snapshots. */
  1887. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1888. } else {
  1889. /* Handle dyntick-idle and offline CPUs. */
  1890. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1891. }
  1892. /* Clear flag to prevent immediate re-entry. */
  1893. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1894. raw_spin_lock_irq_rcu_node(rnp);
  1895. WRITE_ONCE(rsp->gp_flags,
  1896. READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
  1897. raw_spin_unlock_irq_rcu_node(rnp);
  1898. }
  1899. }
  1900. /*
  1901. * Clean up after the old grace period.
  1902. */
  1903. static void rcu_gp_cleanup(struct rcu_state *rsp)
  1904. {
  1905. unsigned long gp_duration;
  1906. bool needgp = false;
  1907. int nocb = 0;
  1908. struct rcu_data *rdp;
  1909. struct rcu_node *rnp = rcu_get_root(rsp);
  1910. struct swait_queue_head *sq;
  1911. WRITE_ONCE(rsp->gp_activity, jiffies);
  1912. raw_spin_lock_irq_rcu_node(rnp);
  1913. gp_duration = jiffies - rsp->gp_start;
  1914. if (gp_duration > rsp->gp_max)
  1915. rsp->gp_max = gp_duration;
  1916. /*
  1917. * We know the grace period is complete, but to everyone else
  1918. * it appears to still be ongoing. But it is also the case
  1919. * that to everyone else it looks like there is nothing that
  1920. * they can do to advance the grace period. It is therefore
  1921. * safe for us to drop the lock in order to mark the grace
  1922. * period as completed in all of the rcu_node structures.
  1923. */
  1924. raw_spin_unlock_irq_rcu_node(rnp);
  1925. /*
  1926. * Propagate new ->completed value to rcu_node structures so
  1927. * that other CPUs don't have to wait until the start of the next
  1928. * grace period to process their callbacks. This also avoids
  1929. * some nasty RCU grace-period initialization races by forcing
  1930. * the end of the current grace period to be completely recorded in
  1931. * all of the rcu_node structures before the beginning of the next
  1932. * grace period is recorded in any of the rcu_node structures.
  1933. */
  1934. rcu_for_each_node_breadth_first(rsp, rnp) {
  1935. raw_spin_lock_irq_rcu_node(rnp);
  1936. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  1937. WARN_ON_ONCE(rnp->qsmask);
  1938. WRITE_ONCE(rnp->completed, rsp->gpnum);
  1939. rdp = this_cpu_ptr(rsp->rda);
  1940. if (rnp == rdp->mynode)
  1941. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  1942. /* smp_mb() provided by prior unlock-lock pair. */
  1943. nocb += rcu_future_gp_cleanup(rsp, rnp);
  1944. sq = rcu_nocb_gp_get(rnp);
  1945. raw_spin_unlock_irq_rcu_node(rnp);
  1946. rcu_nocb_gp_cleanup(sq);
  1947. cond_resched_rcu_qs();
  1948. WRITE_ONCE(rsp->gp_activity, jiffies);
  1949. rcu_gp_slow(rsp, gp_cleanup_delay);
  1950. }
  1951. rnp = rcu_get_root(rsp);
  1952. raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
  1953. rcu_nocb_gp_set(rnp, nocb);
  1954. /* Declare grace period done. */
  1955. WRITE_ONCE(rsp->completed, rsp->gpnum);
  1956. trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
  1957. rsp->gp_state = RCU_GP_IDLE;
  1958. rdp = this_cpu_ptr(rsp->rda);
  1959. /* Advance CBs to reduce false positives below. */
  1960. needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
  1961. if (needgp || cpu_needs_another_gp(rsp, rdp)) {
  1962. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  1963. trace_rcu_grace_period(rsp->name,
  1964. READ_ONCE(rsp->gpnum),
  1965. TPS("newreq"));
  1966. }
  1967. raw_spin_unlock_irq_rcu_node(rnp);
  1968. }
  1969. /*
  1970. * Body of kthread that handles grace periods.
  1971. */
  1972. static int __noreturn rcu_gp_kthread(void *arg)
  1973. {
  1974. bool first_gp_fqs;
  1975. int gf;
  1976. unsigned long j;
  1977. int ret;
  1978. struct rcu_state *rsp = arg;
  1979. struct rcu_node *rnp = rcu_get_root(rsp);
  1980. rcu_bind_gp_kthread();
  1981. for (;;) {
  1982. /* Handle grace-period start. */
  1983. for (;;) {
  1984. trace_rcu_grace_period(rsp->name,
  1985. READ_ONCE(rsp->gpnum),
  1986. TPS("reqwait"));
  1987. rsp->gp_state = RCU_GP_WAIT_GPS;
  1988. swait_event_idle(rsp->gp_wq, READ_ONCE(rsp->gp_flags) &
  1989. RCU_GP_FLAG_INIT);
  1990. rsp->gp_state = RCU_GP_DONE_GPS;
  1991. /* Locking provides needed memory barrier. */
  1992. if (rcu_gp_init(rsp))
  1993. break;
  1994. cond_resched_rcu_qs();
  1995. WRITE_ONCE(rsp->gp_activity, jiffies);
  1996. WARN_ON(signal_pending(current));
  1997. trace_rcu_grace_period(rsp->name,
  1998. READ_ONCE(rsp->gpnum),
  1999. TPS("reqwaitsig"));
  2000. }
  2001. /* Handle quiescent-state forcing. */
  2002. first_gp_fqs = true;
  2003. j = jiffies_till_first_fqs;
  2004. if (j > HZ) {
  2005. j = HZ;
  2006. jiffies_till_first_fqs = HZ;
  2007. }
  2008. ret = 0;
  2009. for (;;) {
  2010. if (!ret) {
  2011. rsp->jiffies_force_qs = jiffies + j;
  2012. WRITE_ONCE(rsp->jiffies_kick_kthreads,
  2013. jiffies + 3 * j);
  2014. }
  2015. trace_rcu_grace_period(rsp->name,
  2016. READ_ONCE(rsp->gpnum),
  2017. TPS("fqswait"));
  2018. rsp->gp_state = RCU_GP_WAIT_FQS;
  2019. ret = swait_event_idle_timeout(rsp->gp_wq,
  2020. rcu_gp_fqs_check_wake(rsp, &gf), j);
  2021. rsp->gp_state = RCU_GP_DOING_FQS;
  2022. /* Locking provides needed memory barriers. */
  2023. /* If grace period done, leave loop. */
  2024. if (!READ_ONCE(rnp->qsmask) &&
  2025. !rcu_preempt_blocked_readers_cgp(rnp))
  2026. break;
  2027. /* If time for quiescent-state forcing, do it. */
  2028. if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
  2029. (gf & RCU_GP_FLAG_FQS)) {
  2030. trace_rcu_grace_period(rsp->name,
  2031. READ_ONCE(rsp->gpnum),
  2032. TPS("fqsstart"));
  2033. rcu_gp_fqs(rsp, first_gp_fqs);
  2034. first_gp_fqs = false;
  2035. trace_rcu_grace_period(rsp->name,
  2036. READ_ONCE(rsp->gpnum),
  2037. TPS("fqsend"));
  2038. cond_resched_rcu_qs();
  2039. WRITE_ONCE(rsp->gp_activity, jiffies);
  2040. ret = 0; /* Force full wait till next FQS. */
  2041. j = jiffies_till_next_fqs;
  2042. if (j > HZ) {
  2043. j = HZ;
  2044. jiffies_till_next_fqs = HZ;
  2045. } else if (j < 1) {
  2046. j = 1;
  2047. jiffies_till_next_fqs = 1;
  2048. }
  2049. } else {
  2050. /* Deal with stray signal. */
  2051. cond_resched_rcu_qs();
  2052. WRITE_ONCE(rsp->gp_activity, jiffies);
  2053. WARN_ON(signal_pending(current));
  2054. trace_rcu_grace_period(rsp->name,
  2055. READ_ONCE(rsp->gpnum),
  2056. TPS("fqswaitsig"));
  2057. ret = 1; /* Keep old FQS timing. */
  2058. j = jiffies;
  2059. if (time_after(jiffies, rsp->jiffies_force_qs))
  2060. j = 1;
  2061. else
  2062. j = rsp->jiffies_force_qs - j;
  2063. }
  2064. }
  2065. /* Handle grace-period end. */
  2066. rsp->gp_state = RCU_GP_CLEANUP;
  2067. rcu_gp_cleanup(rsp);
  2068. rsp->gp_state = RCU_GP_CLEANED;
  2069. }
  2070. }
  2071. /*
  2072. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  2073. * in preparation for detecting the next grace period. The caller must hold
  2074. * the root node's ->lock and hard irqs must be disabled.
  2075. *
  2076. * Note that it is legal for a dying CPU (which is marked as offline) to
  2077. * invoke this function. This can happen when the dying CPU reports its
  2078. * quiescent state.
  2079. *
  2080. * Returns true if the grace-period kthread must be awakened.
  2081. */
  2082. static bool
  2083. rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  2084. struct rcu_data *rdp)
  2085. {
  2086. raw_lockdep_assert_held_rcu_node(rnp);
  2087. if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
  2088. /*
  2089. * Either we have not yet spawned the grace-period
  2090. * task, this CPU does not need another grace period,
  2091. * or a grace period is already in progress.
  2092. * Either way, don't start a new grace period.
  2093. */
  2094. return false;
  2095. }
  2096. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  2097. trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
  2098. TPS("newreq"));
  2099. /*
  2100. * We can't do wakeups while holding the rnp->lock, as that
  2101. * could cause possible deadlocks with the rq->lock. Defer
  2102. * the wakeup to our caller.
  2103. */
  2104. return true;
  2105. }
  2106. /*
  2107. * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
  2108. * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
  2109. * is invoked indirectly from rcu_advance_cbs(), which would result in
  2110. * endless recursion -- or would do so if it wasn't for the self-deadlock
  2111. * that is encountered beforehand.
  2112. *
  2113. * Returns true if the grace-period kthread needs to be awakened.
  2114. */
  2115. static bool rcu_start_gp(struct rcu_state *rsp)
  2116. {
  2117. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  2118. struct rcu_node *rnp = rcu_get_root(rsp);
  2119. bool ret = false;
  2120. /*
  2121. * If there is no grace period in progress right now, any
  2122. * callbacks we have up to this point will be satisfied by the
  2123. * next grace period. Also, advancing the callbacks reduces the
  2124. * probability of false positives from cpu_needs_another_gp()
  2125. * resulting in pointless grace periods. So, advance callbacks
  2126. * then start the grace period!
  2127. */
  2128. ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
  2129. ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
  2130. return ret;
  2131. }
  2132. /*
  2133. * Report a full set of quiescent states to the specified rcu_state data
  2134. * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
  2135. * kthread if another grace period is required. Whether we wake
  2136. * the grace-period kthread or it awakens itself for the next round
  2137. * of quiescent-state forcing, that kthread will clean up after the
  2138. * just-completed grace period. Note that the caller must hold rnp->lock,
  2139. * which is released before return.
  2140. */
  2141. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  2142. __releases(rcu_get_root(rsp)->lock)
  2143. {
  2144. raw_lockdep_assert_held_rcu_node(rcu_get_root(rsp));
  2145. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  2146. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2147. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2148. rcu_gp_kthread_wake(rsp);
  2149. }
  2150. /*
  2151. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  2152. * Allows quiescent states for a group of CPUs to be reported at one go
  2153. * to the specified rcu_node structure, though all the CPUs in the group
  2154. * must be represented by the same rcu_node structure (which need not be a
  2155. * leaf rcu_node structure, though it often will be). The gps parameter
  2156. * is the grace-period snapshot, which means that the quiescent states
  2157. * are valid only if rnp->gpnum is equal to gps. That structure's lock
  2158. * must be held upon entry, and it is released before return.
  2159. */
  2160. static void
  2161. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  2162. struct rcu_node *rnp, unsigned long gps, unsigned long flags)
  2163. __releases(rnp->lock)
  2164. {
  2165. unsigned long oldmask = 0;
  2166. struct rcu_node *rnp_c;
  2167. raw_lockdep_assert_held_rcu_node(rnp);
  2168. /* Walk up the rcu_node hierarchy. */
  2169. for (;;) {
  2170. if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
  2171. /*
  2172. * Our bit has already been cleared, or the
  2173. * relevant grace period is already over, so done.
  2174. */
  2175. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2176. return;
  2177. }
  2178. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  2179. WARN_ON_ONCE(rnp->level != rcu_num_lvls - 1 &&
  2180. rcu_preempt_blocked_readers_cgp(rnp));
  2181. rnp->qsmask &= ~mask;
  2182. trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
  2183. mask, rnp->qsmask, rnp->level,
  2184. rnp->grplo, rnp->grphi,
  2185. !!rnp->gp_tasks);
  2186. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2187. /* Other bits still set at this level, so done. */
  2188. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2189. return;
  2190. }
  2191. mask = rnp->grpmask;
  2192. if (rnp->parent == NULL) {
  2193. /* No more levels. Exit loop holding root lock. */
  2194. break;
  2195. }
  2196. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2197. rnp_c = rnp;
  2198. rnp = rnp->parent;
  2199. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2200. oldmask = rnp_c->qsmask;
  2201. }
  2202. /*
  2203. * Get here if we are the last CPU to pass through a quiescent
  2204. * state for this grace period. Invoke rcu_report_qs_rsp()
  2205. * to clean up and start the next grace period if one is needed.
  2206. */
  2207. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  2208. }
  2209. /*
  2210. * Record a quiescent state for all tasks that were previously queued
  2211. * on the specified rcu_node structure and that were blocking the current
  2212. * RCU grace period. The caller must hold the specified rnp->lock with
  2213. * irqs disabled, and this lock is released upon return, but irqs remain
  2214. * disabled.
  2215. */
  2216. static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
  2217. struct rcu_node *rnp, unsigned long flags)
  2218. __releases(rnp->lock)
  2219. {
  2220. unsigned long gps;
  2221. unsigned long mask;
  2222. struct rcu_node *rnp_p;
  2223. raw_lockdep_assert_held_rcu_node(rnp);
  2224. if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
  2225. rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2226. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2227. return; /* Still need more quiescent states! */
  2228. }
  2229. rnp_p = rnp->parent;
  2230. if (rnp_p == NULL) {
  2231. /*
  2232. * Only one rcu_node structure in the tree, so don't
  2233. * try to report up to its nonexistent parent!
  2234. */
  2235. rcu_report_qs_rsp(rsp, flags);
  2236. return;
  2237. }
  2238. /* Report up the rest of the hierarchy, tracking current ->gpnum. */
  2239. gps = rnp->gpnum;
  2240. mask = rnp->grpmask;
  2241. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2242. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  2243. rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
  2244. }
  2245. /*
  2246. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  2247. * structure. This must be called from the specified CPU.
  2248. */
  2249. static void
  2250. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
  2251. {
  2252. unsigned long flags;
  2253. unsigned long mask;
  2254. bool needwake;
  2255. struct rcu_node *rnp;
  2256. rnp = rdp->mynode;
  2257. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2258. if (rdp->cpu_no_qs.b.norm || rdp->gpnum != rnp->gpnum ||
  2259. rnp->completed == rnp->gpnum || rdp->gpwrap) {
  2260. /*
  2261. * The grace period in which this quiescent state was
  2262. * recorded has ended, so don't report it upwards.
  2263. * We will instead need a new quiescent state that lies
  2264. * within the current grace period.
  2265. */
  2266. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  2267. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
  2268. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2269. return;
  2270. }
  2271. mask = rdp->grpmask;
  2272. if ((rnp->qsmask & mask) == 0) {
  2273. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2274. } else {
  2275. rdp->core_needs_qs = false;
  2276. /*
  2277. * This GP can't end until cpu checks in, so all of our
  2278. * callbacks can be processed during the next GP.
  2279. */
  2280. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  2281. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2282. /* ^^^ Released rnp->lock */
  2283. if (needwake)
  2284. rcu_gp_kthread_wake(rsp);
  2285. }
  2286. }
  2287. /*
  2288. * Check to see if there is a new grace period of which this CPU
  2289. * is not yet aware, and if so, set up local rcu_data state for it.
  2290. * Otherwise, see if this CPU has just passed through its first
  2291. * quiescent state for this grace period, and record that fact if so.
  2292. */
  2293. static void
  2294. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  2295. {
  2296. /* Check for grace-period ends and beginnings. */
  2297. note_gp_changes(rsp, rdp);
  2298. /*
  2299. * Does this CPU still need to do its part for current grace period?
  2300. * If no, return and let the other CPUs do their part as well.
  2301. */
  2302. if (!rdp->core_needs_qs)
  2303. return;
  2304. /*
  2305. * Was there a quiescent state since the beginning of the grace
  2306. * period? If no, then exit and wait for the next call.
  2307. */
  2308. if (rdp->cpu_no_qs.b.norm)
  2309. return;
  2310. /*
  2311. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  2312. * judge of that).
  2313. */
  2314. rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
  2315. }
  2316. /*
  2317. * Trace the fact that this CPU is going offline.
  2318. */
  2319. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  2320. {
  2321. RCU_TRACE(unsigned long mask;)
  2322. RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda);)
  2323. RCU_TRACE(struct rcu_node *rnp = rdp->mynode;)
  2324. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2325. return;
  2326. RCU_TRACE(mask = rdp->grpmask;)
  2327. trace_rcu_grace_period(rsp->name,
  2328. rnp->gpnum + 1 - !!(rnp->qsmask & mask),
  2329. TPS("cpuofl"));
  2330. }
  2331. /*
  2332. * All CPUs for the specified rcu_node structure have gone offline,
  2333. * and all tasks that were preempted within an RCU read-side critical
  2334. * section while running on one of those CPUs have since exited their RCU
  2335. * read-side critical section. Some other CPU is reporting this fact with
  2336. * the specified rcu_node structure's ->lock held and interrupts disabled.
  2337. * This function therefore goes up the tree of rcu_node structures,
  2338. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  2339. * the leaf rcu_node structure's ->qsmaskinit field has already been
  2340. * updated
  2341. *
  2342. * This function does check that the specified rcu_node structure has
  2343. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  2344. * prematurely. That said, invoking it after the fact will cost you
  2345. * a needless lock acquisition. So once it has done its work, don't
  2346. * invoke it again.
  2347. */
  2348. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  2349. {
  2350. long mask;
  2351. struct rcu_node *rnp = rnp_leaf;
  2352. raw_lockdep_assert_held_rcu_node(rnp);
  2353. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2354. rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
  2355. return;
  2356. for (;;) {
  2357. mask = rnp->grpmask;
  2358. rnp = rnp->parent;
  2359. if (!rnp)
  2360. break;
  2361. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  2362. rnp->qsmaskinit &= ~mask;
  2363. rnp->qsmask &= ~mask;
  2364. if (rnp->qsmaskinit) {
  2365. raw_spin_unlock_rcu_node(rnp);
  2366. /* irqs remain disabled. */
  2367. return;
  2368. }
  2369. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2370. }
  2371. }
  2372. /*
  2373. * The CPU has been completely removed, and some other CPU is reporting
  2374. * this fact from process context. Do the remainder of the cleanup.
  2375. * There can only be one CPU hotplug operation at a time, so no need for
  2376. * explicit locking.
  2377. */
  2378. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  2379. {
  2380. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2381. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  2382. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2383. return;
  2384. /* Adjust any no-longer-needed kthreads. */
  2385. rcu_boost_kthread_setaffinity(rnp, -1);
  2386. }
  2387. /*
  2388. * Invoke any RCU callbacks that have made it to the end of their grace
  2389. * period. Thottle as specified by rdp->blimit.
  2390. */
  2391. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  2392. {
  2393. unsigned long flags;
  2394. struct rcu_head *rhp;
  2395. struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
  2396. long bl, count;
  2397. /* If no callbacks are ready, just return. */
  2398. if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
  2399. trace_rcu_batch_start(rsp->name,
  2400. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2401. rcu_segcblist_n_cbs(&rdp->cblist), 0);
  2402. trace_rcu_batch_end(rsp->name, 0,
  2403. !rcu_segcblist_empty(&rdp->cblist),
  2404. need_resched(), is_idle_task(current),
  2405. rcu_is_callbacks_kthread());
  2406. return;
  2407. }
  2408. /*
  2409. * Extract the list of ready callbacks, disabling to prevent
  2410. * races with call_rcu() from interrupt handlers. Leave the
  2411. * callback counts, as rcu_barrier() needs to be conservative.
  2412. */
  2413. local_irq_save(flags);
  2414. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2415. bl = rdp->blimit;
  2416. trace_rcu_batch_start(rsp->name, rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2417. rcu_segcblist_n_cbs(&rdp->cblist), bl);
  2418. rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  2419. local_irq_restore(flags);
  2420. /* Invoke callbacks. */
  2421. rhp = rcu_cblist_dequeue(&rcl);
  2422. for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  2423. debug_rcu_head_unqueue(rhp);
  2424. if (__rcu_reclaim(rsp->name, rhp))
  2425. rcu_cblist_dequeued_lazy(&rcl);
  2426. /*
  2427. * Stop only if limit reached and CPU has something to do.
  2428. * Note: The rcl structure counts down from zero.
  2429. */
  2430. if (-rcl.len >= bl &&
  2431. (need_resched() ||
  2432. (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
  2433. break;
  2434. }
  2435. local_irq_save(flags);
  2436. count = -rcl.len;
  2437. trace_rcu_batch_end(rsp->name, count, !!rcl.head, need_resched(),
  2438. is_idle_task(current), rcu_is_callbacks_kthread());
  2439. /* Update counts and requeue any remaining callbacks. */
  2440. rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
  2441. smp_mb(); /* List handling before counting for rcu_barrier(). */
  2442. rcu_segcblist_insert_count(&rdp->cblist, &rcl);
  2443. /* Reinstate batch limit if we have worked down the excess. */
  2444. count = rcu_segcblist_n_cbs(&rdp->cblist);
  2445. if (rdp->blimit == LONG_MAX && count <= qlowmark)
  2446. rdp->blimit = blimit;
  2447. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2448. if (count == 0 && rdp->qlen_last_fqs_check != 0) {
  2449. rdp->qlen_last_fqs_check = 0;
  2450. rdp->n_force_qs_snap = rsp->n_force_qs;
  2451. } else if (count < rdp->qlen_last_fqs_check - qhimark)
  2452. rdp->qlen_last_fqs_check = count;
  2453. /*
  2454. * The following usually indicates a double call_rcu(). To track
  2455. * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
  2456. */
  2457. WARN_ON_ONCE(rcu_segcblist_empty(&rdp->cblist) != (count == 0));
  2458. local_irq_restore(flags);
  2459. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2460. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2461. invoke_rcu_core();
  2462. }
  2463. /*
  2464. * Check to see if this CPU is in a non-context-switch quiescent state
  2465. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  2466. * Also schedule RCU core processing.
  2467. *
  2468. * This function must be called from hardirq context. It is normally
  2469. * invoked from the scheduling-clock interrupt.
  2470. */
  2471. void rcu_check_callbacks(int user)
  2472. {
  2473. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2474. increment_cpu_stall_ticks();
  2475. if (user || rcu_is_cpu_rrupt_from_idle()) {
  2476. /*
  2477. * Get here if this CPU took its interrupt from user
  2478. * mode or from the idle loop, and if this is not a
  2479. * nested interrupt. In this case, the CPU is in
  2480. * a quiescent state, so note it.
  2481. *
  2482. * No memory barrier is required here because both
  2483. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  2484. * variables that other CPUs neither access nor modify,
  2485. * at least not while the corresponding CPU is online.
  2486. */
  2487. rcu_sched_qs();
  2488. rcu_bh_qs();
  2489. } else if (!in_softirq()) {
  2490. /*
  2491. * Get here if this CPU did not take its interrupt from
  2492. * softirq, in other words, if it is not interrupting
  2493. * a rcu_bh read-side critical section. This is an _bh
  2494. * critical section, so note it.
  2495. */
  2496. rcu_bh_qs();
  2497. }
  2498. rcu_preempt_check_callbacks();
  2499. if (rcu_pending())
  2500. invoke_rcu_core();
  2501. if (user)
  2502. rcu_note_voluntary_context_switch(current);
  2503. trace_rcu_utilization(TPS("End scheduler-tick"));
  2504. }
  2505. /*
  2506. * Scan the leaf rcu_node structures, processing dyntick state for any that
  2507. * have not yet encountered a quiescent state, using the function specified.
  2508. * Also initiate boosting for any threads blocked on the root rcu_node.
  2509. *
  2510. * The caller must have suppressed start of new grace periods.
  2511. */
  2512. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *rsp))
  2513. {
  2514. int cpu;
  2515. unsigned long flags;
  2516. unsigned long mask;
  2517. struct rcu_node *rnp;
  2518. rcu_for_each_leaf_node(rsp, rnp) {
  2519. cond_resched_rcu_qs();
  2520. mask = 0;
  2521. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2522. if (rnp->qsmask == 0) {
  2523. if (rcu_state_p == &rcu_sched_state ||
  2524. rsp != rcu_state_p ||
  2525. rcu_preempt_blocked_readers_cgp(rnp)) {
  2526. /*
  2527. * No point in scanning bits because they
  2528. * are all zero. But we might need to
  2529. * priority-boost blocked readers.
  2530. */
  2531. rcu_initiate_boost(rnp, flags);
  2532. /* rcu_initiate_boost() releases rnp->lock */
  2533. continue;
  2534. }
  2535. if (rnp->parent &&
  2536. (rnp->parent->qsmask & rnp->grpmask)) {
  2537. /*
  2538. * Race between grace-period
  2539. * initialization and task exiting RCU
  2540. * read-side critical section: Report.
  2541. */
  2542. rcu_report_unblock_qs_rnp(rsp, rnp, flags);
  2543. /* rcu_report_unblock_qs_rnp() rlses ->lock */
  2544. continue;
  2545. }
  2546. }
  2547. for_each_leaf_node_possible_cpu(rnp, cpu) {
  2548. unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
  2549. if ((rnp->qsmask & bit) != 0) {
  2550. if (f(per_cpu_ptr(rsp->rda, cpu)))
  2551. mask |= bit;
  2552. }
  2553. }
  2554. if (mask != 0) {
  2555. /* Idle/offline CPUs, report (releases rnp->lock. */
  2556. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2557. } else {
  2558. /* Nothing to do here, so just drop the lock. */
  2559. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2560. }
  2561. }
  2562. }
  2563. /*
  2564. * Force quiescent states on reluctant CPUs, and also detect which
  2565. * CPUs are in dyntick-idle mode.
  2566. */
  2567. static void force_quiescent_state(struct rcu_state *rsp)
  2568. {
  2569. unsigned long flags;
  2570. bool ret;
  2571. struct rcu_node *rnp;
  2572. struct rcu_node *rnp_old = NULL;
  2573. /* Funnel through hierarchy to reduce memory contention. */
  2574. rnp = __this_cpu_read(rsp->rda->mynode);
  2575. for (; rnp != NULL; rnp = rnp->parent) {
  2576. ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
  2577. !raw_spin_trylock(&rnp->fqslock);
  2578. if (rnp_old != NULL)
  2579. raw_spin_unlock(&rnp_old->fqslock);
  2580. if (ret)
  2581. return;
  2582. rnp_old = rnp;
  2583. }
  2584. /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
  2585. /* Reached the root of the rcu_node tree, acquire lock. */
  2586. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2587. raw_spin_unlock(&rnp_old->fqslock);
  2588. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  2589. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2590. return; /* Someone beat us to it. */
  2591. }
  2592. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2593. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2594. rcu_gp_kthread_wake(rsp);
  2595. }
  2596. /*
  2597. * This does the RCU core processing work for the specified rcu_state
  2598. * and rcu_data structures. This may be called only from the CPU to
  2599. * whom the rdp belongs.
  2600. */
  2601. static void
  2602. __rcu_process_callbacks(struct rcu_state *rsp)
  2603. {
  2604. unsigned long flags;
  2605. bool needwake;
  2606. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2607. WARN_ON_ONCE(!rdp->beenonline);
  2608. /* Update RCU state based on any recent quiescent states. */
  2609. rcu_check_quiescent_state(rsp, rdp);
  2610. /* Does this CPU require a not-yet-started grace period? */
  2611. local_irq_save(flags);
  2612. if (cpu_needs_another_gp(rsp, rdp)) {
  2613. raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
  2614. needwake = rcu_start_gp(rsp);
  2615. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2616. if (needwake)
  2617. rcu_gp_kthread_wake(rsp);
  2618. } else {
  2619. local_irq_restore(flags);
  2620. }
  2621. /* If there are callbacks ready, invoke them. */
  2622. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2623. invoke_rcu_callbacks(rsp, rdp);
  2624. /* Do any needed deferred wakeups of rcuo kthreads. */
  2625. do_nocb_deferred_wakeup(rdp);
  2626. }
  2627. /*
  2628. * Do RCU core processing for the current CPU.
  2629. */
  2630. static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
  2631. {
  2632. struct rcu_state *rsp;
  2633. if (cpu_is_offline(smp_processor_id()))
  2634. return;
  2635. trace_rcu_utilization(TPS("Start RCU core"));
  2636. for_each_rcu_flavor(rsp)
  2637. __rcu_process_callbacks(rsp);
  2638. trace_rcu_utilization(TPS("End RCU core"));
  2639. }
  2640. /*
  2641. * Schedule RCU callback invocation. If the specified type of RCU
  2642. * does not support RCU priority boosting, just do a direct call,
  2643. * otherwise wake up the per-CPU kernel kthread. Note that because we
  2644. * are running on the current CPU with softirqs disabled, the
  2645. * rcu_cpu_kthread_task cannot disappear out from under us.
  2646. */
  2647. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  2648. {
  2649. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  2650. return;
  2651. if (likely(!rsp->boost)) {
  2652. rcu_do_batch(rsp, rdp);
  2653. return;
  2654. }
  2655. invoke_rcu_callbacks_kthread();
  2656. }
  2657. static void invoke_rcu_core(void)
  2658. {
  2659. if (cpu_online(smp_processor_id()))
  2660. raise_softirq(RCU_SOFTIRQ);
  2661. }
  2662. /*
  2663. * Handle any core-RCU processing required by a call_rcu() invocation.
  2664. */
  2665. static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
  2666. struct rcu_head *head, unsigned long flags)
  2667. {
  2668. bool needwake;
  2669. /*
  2670. * If called from an extended quiescent state, invoke the RCU
  2671. * core in order to force a re-evaluation of RCU's idleness.
  2672. */
  2673. if (!rcu_is_watching())
  2674. invoke_rcu_core();
  2675. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2676. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2677. return;
  2678. /*
  2679. * Force the grace period if too many callbacks or too long waiting.
  2680. * Enforce hysteresis, and don't invoke force_quiescent_state()
  2681. * if some other CPU has recently done so. Also, don't bother
  2682. * invoking force_quiescent_state() if the newly enqueued callback
  2683. * is the only one waiting for a grace period to complete.
  2684. */
  2685. if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
  2686. rdp->qlen_last_fqs_check + qhimark)) {
  2687. /* Are we ignoring a completed grace period? */
  2688. note_gp_changes(rsp, rdp);
  2689. /* Start a new grace period if one not already started. */
  2690. if (!rcu_gp_in_progress(rsp)) {
  2691. struct rcu_node *rnp_root = rcu_get_root(rsp);
  2692. raw_spin_lock_rcu_node(rnp_root);
  2693. needwake = rcu_start_gp(rsp);
  2694. raw_spin_unlock_rcu_node(rnp_root);
  2695. if (needwake)
  2696. rcu_gp_kthread_wake(rsp);
  2697. } else {
  2698. /* Give the grace period a kick. */
  2699. rdp->blimit = LONG_MAX;
  2700. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  2701. rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
  2702. force_quiescent_state(rsp);
  2703. rdp->n_force_qs_snap = rsp->n_force_qs;
  2704. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2705. }
  2706. }
  2707. }
  2708. /*
  2709. * RCU callback function to leak a callback.
  2710. */
  2711. static void rcu_leak_callback(struct rcu_head *rhp)
  2712. {
  2713. }
  2714. /*
  2715. * Helper function for call_rcu() and friends. The cpu argument will
  2716. * normally be -1, indicating "currently running CPU". It may specify
  2717. * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
  2718. * is expected to specify a CPU.
  2719. */
  2720. static void
  2721. __call_rcu(struct rcu_head *head, rcu_callback_t func,
  2722. struct rcu_state *rsp, int cpu, bool lazy)
  2723. {
  2724. unsigned long flags;
  2725. struct rcu_data *rdp;
  2726. /* Misaligned rcu_head! */
  2727. WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
  2728. if (debug_rcu_head_queue(head)) {
  2729. /*
  2730. * Probable double call_rcu(), so leak the callback.
  2731. * Use rcu:rcu_callback trace event to find the previous
  2732. * time callback was passed to __call_rcu().
  2733. */
  2734. WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pF()!!!\n",
  2735. head, head->func);
  2736. WRITE_ONCE(head->func, rcu_leak_callback);
  2737. return;
  2738. }
  2739. head->func = func;
  2740. head->next = NULL;
  2741. local_irq_save(flags);
  2742. rdp = this_cpu_ptr(rsp->rda);
  2743. /* Add the callback to our list. */
  2744. if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist)) || cpu != -1) {
  2745. int offline;
  2746. if (cpu != -1)
  2747. rdp = per_cpu_ptr(rsp->rda, cpu);
  2748. if (likely(rdp->mynode)) {
  2749. /* Post-boot, so this should be for a no-CBs CPU. */
  2750. offline = !__call_rcu_nocb(rdp, head, lazy, flags);
  2751. WARN_ON_ONCE(offline);
  2752. /* Offline CPU, _call_rcu() illegal, leak callback. */
  2753. local_irq_restore(flags);
  2754. return;
  2755. }
  2756. /*
  2757. * Very early boot, before rcu_init(). Initialize if needed
  2758. * and then drop through to queue the callback.
  2759. */
  2760. BUG_ON(cpu != -1);
  2761. WARN_ON_ONCE(!rcu_is_watching());
  2762. if (rcu_segcblist_empty(&rdp->cblist))
  2763. rcu_segcblist_init(&rdp->cblist);
  2764. }
  2765. rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
  2766. if (!lazy)
  2767. rcu_idle_count_callbacks_posted();
  2768. if (__is_kfree_rcu_offset((unsigned long)func))
  2769. trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
  2770. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2771. rcu_segcblist_n_cbs(&rdp->cblist));
  2772. else
  2773. trace_rcu_callback(rsp->name, head,
  2774. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2775. rcu_segcblist_n_cbs(&rdp->cblist));
  2776. /* Go handle any RCU core processing required. */
  2777. __call_rcu_core(rsp, rdp, head, flags);
  2778. local_irq_restore(flags);
  2779. }
  2780. /**
  2781. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  2782. * @head: structure to be used for queueing the RCU updates.
  2783. * @func: actual callback function to be invoked after the grace period
  2784. *
  2785. * The callback function will be invoked some time after a full grace
  2786. * period elapses, in other words after all currently executing RCU
  2787. * read-side critical sections have completed. call_rcu_sched() assumes
  2788. * that the read-side critical sections end on enabling of preemption
  2789. * or on voluntary preemption.
  2790. * RCU read-side critical sections are delimited by:
  2791. *
  2792. * - rcu_read_lock_sched() and rcu_read_unlock_sched(), OR
  2793. * - anything that disables preemption.
  2794. *
  2795. * These may be nested.
  2796. *
  2797. * See the description of call_rcu() for more detailed information on
  2798. * memory ordering guarantees.
  2799. */
  2800. void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  2801. {
  2802. __call_rcu(head, func, &rcu_sched_state, -1, 0);
  2803. }
  2804. EXPORT_SYMBOL_GPL(call_rcu_sched);
  2805. /**
  2806. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  2807. * @head: structure to be used for queueing the RCU updates.
  2808. * @func: actual callback function to be invoked after the grace period
  2809. *
  2810. * The callback function will be invoked some time after a full grace
  2811. * period elapses, in other words after all currently executing RCU
  2812. * read-side critical sections have completed. call_rcu_bh() assumes
  2813. * that the read-side critical sections end on completion of a softirq
  2814. * handler. This means that read-side critical sections in process
  2815. * context must not be interrupted by softirqs. This interface is to be
  2816. * used when most of the read-side critical sections are in softirq context.
  2817. * RCU read-side critical sections are delimited by:
  2818. *
  2819. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context, OR
  2820. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  2821. *
  2822. * These may be nested.
  2823. *
  2824. * See the description of call_rcu() for more detailed information on
  2825. * memory ordering guarantees.
  2826. */
  2827. void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  2828. {
  2829. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  2830. }
  2831. EXPORT_SYMBOL_GPL(call_rcu_bh);
  2832. /*
  2833. * Queue an RCU callback for lazy invocation after a grace period.
  2834. * This will likely be later named something like "call_rcu_lazy()",
  2835. * but this change will require some way of tagging the lazy RCU
  2836. * callbacks in the list of pending callbacks. Until then, this
  2837. * function may only be called from __kfree_rcu().
  2838. */
  2839. void kfree_call_rcu(struct rcu_head *head,
  2840. rcu_callback_t func)
  2841. {
  2842. __call_rcu(head, func, rcu_state_p, -1, 1);
  2843. }
  2844. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  2845. /*
  2846. * Because a context switch is a grace period for RCU-sched and RCU-bh,
  2847. * any blocking grace-period wait automatically implies a grace period
  2848. * if there is only one CPU online at any point time during execution
  2849. * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
  2850. * occasionally incorrectly indicate that there are multiple CPUs online
  2851. * when there was in fact only one the whole time, as this just adds
  2852. * some overhead: RCU still operates correctly.
  2853. */
  2854. static inline int rcu_blocking_is_gp(void)
  2855. {
  2856. int ret;
  2857. might_sleep(); /* Check for RCU read-side critical section. */
  2858. preempt_disable();
  2859. ret = num_online_cpus() <= 1;
  2860. preempt_enable();
  2861. return ret;
  2862. }
  2863. /**
  2864. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  2865. *
  2866. * Control will return to the caller some time after a full rcu-sched
  2867. * grace period has elapsed, in other words after all currently executing
  2868. * rcu-sched read-side critical sections have completed. These read-side
  2869. * critical sections are delimited by rcu_read_lock_sched() and
  2870. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  2871. * local_irq_disable(), and so on may be used in place of
  2872. * rcu_read_lock_sched().
  2873. *
  2874. * This means that all preempt_disable code sequences, including NMI and
  2875. * non-threaded hardware-interrupt handlers, in progress on entry will
  2876. * have completed before this primitive returns. However, this does not
  2877. * guarantee that softirq handlers will have completed, since in some
  2878. * kernels, these handlers can run in process context, and can block.
  2879. *
  2880. * Note that this guarantee implies further memory-ordering guarantees.
  2881. * On systems with more than one CPU, when synchronize_sched() returns,
  2882. * each CPU is guaranteed to have executed a full memory barrier since the
  2883. * end of its last RCU-sched read-side critical section whose beginning
  2884. * preceded the call to synchronize_sched(). In addition, each CPU having
  2885. * an RCU read-side critical section that extends beyond the return from
  2886. * synchronize_sched() is guaranteed to have executed a full memory barrier
  2887. * after the beginning of synchronize_sched() and before the beginning of
  2888. * that RCU read-side critical section. Note that these guarantees include
  2889. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  2890. * that are executing in the kernel.
  2891. *
  2892. * Furthermore, if CPU A invoked synchronize_sched(), which returned
  2893. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  2894. * to have executed a full memory barrier during the execution of
  2895. * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
  2896. * again only if the system has more than one CPU).
  2897. */
  2898. void synchronize_sched(void)
  2899. {
  2900. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2901. lock_is_held(&rcu_lock_map) ||
  2902. lock_is_held(&rcu_sched_lock_map),
  2903. "Illegal synchronize_sched() in RCU-sched read-side critical section");
  2904. if (rcu_blocking_is_gp())
  2905. return;
  2906. if (rcu_gp_is_expedited())
  2907. synchronize_sched_expedited();
  2908. else
  2909. wait_rcu_gp(call_rcu_sched);
  2910. }
  2911. EXPORT_SYMBOL_GPL(synchronize_sched);
  2912. /**
  2913. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  2914. *
  2915. * Control will return to the caller some time after a full rcu_bh grace
  2916. * period has elapsed, in other words after all currently executing rcu_bh
  2917. * read-side critical sections have completed. RCU read-side critical
  2918. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  2919. * and may be nested.
  2920. *
  2921. * See the description of synchronize_sched() for more detailed information
  2922. * on memory ordering guarantees.
  2923. */
  2924. void synchronize_rcu_bh(void)
  2925. {
  2926. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2927. lock_is_held(&rcu_lock_map) ||
  2928. lock_is_held(&rcu_sched_lock_map),
  2929. "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
  2930. if (rcu_blocking_is_gp())
  2931. return;
  2932. if (rcu_gp_is_expedited())
  2933. synchronize_rcu_bh_expedited();
  2934. else
  2935. wait_rcu_gp(call_rcu_bh);
  2936. }
  2937. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  2938. /**
  2939. * get_state_synchronize_rcu - Snapshot current RCU state
  2940. *
  2941. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  2942. * to determine whether or not a full grace period has elapsed in the
  2943. * meantime.
  2944. */
  2945. unsigned long get_state_synchronize_rcu(void)
  2946. {
  2947. /*
  2948. * Any prior manipulation of RCU-protected data must happen
  2949. * before the load from ->gpnum.
  2950. */
  2951. smp_mb(); /* ^^^ */
  2952. /*
  2953. * Make sure this load happens before the purportedly
  2954. * time-consuming work between get_state_synchronize_rcu()
  2955. * and cond_synchronize_rcu().
  2956. */
  2957. return smp_load_acquire(&rcu_state_p->gpnum);
  2958. }
  2959. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  2960. /**
  2961. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  2962. *
  2963. * @oldstate: return value from earlier call to get_state_synchronize_rcu()
  2964. *
  2965. * If a full RCU grace period has elapsed since the earlier call to
  2966. * get_state_synchronize_rcu(), just return. Otherwise, invoke
  2967. * synchronize_rcu() to wait for a full grace period.
  2968. *
  2969. * Yes, this function does not take counter wrap into account. But
  2970. * counter wrap is harmless. If the counter wraps, we have waited for
  2971. * more than 2 billion grace periods (and way more on a 64-bit system!),
  2972. * so waiting for one additional grace period should be just fine.
  2973. */
  2974. void cond_synchronize_rcu(unsigned long oldstate)
  2975. {
  2976. unsigned long newstate;
  2977. /*
  2978. * Ensure that this load happens before any RCU-destructive
  2979. * actions the caller might carry out after we return.
  2980. */
  2981. newstate = smp_load_acquire(&rcu_state_p->completed);
  2982. if (ULONG_CMP_GE(oldstate, newstate))
  2983. synchronize_rcu();
  2984. }
  2985. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  2986. /**
  2987. * get_state_synchronize_sched - Snapshot current RCU-sched state
  2988. *
  2989. * Returns a cookie that is used by a later call to cond_synchronize_sched()
  2990. * to determine whether or not a full grace period has elapsed in the
  2991. * meantime.
  2992. */
  2993. unsigned long get_state_synchronize_sched(void)
  2994. {
  2995. /*
  2996. * Any prior manipulation of RCU-protected data must happen
  2997. * before the load from ->gpnum.
  2998. */
  2999. smp_mb(); /* ^^^ */
  3000. /*
  3001. * Make sure this load happens before the purportedly
  3002. * time-consuming work between get_state_synchronize_sched()
  3003. * and cond_synchronize_sched().
  3004. */
  3005. return smp_load_acquire(&rcu_sched_state.gpnum);
  3006. }
  3007. EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
  3008. /**
  3009. * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
  3010. *
  3011. * @oldstate: return value from earlier call to get_state_synchronize_sched()
  3012. *
  3013. * If a full RCU-sched grace period has elapsed since the earlier call to
  3014. * get_state_synchronize_sched(), just return. Otherwise, invoke
  3015. * synchronize_sched() to wait for a full grace period.
  3016. *
  3017. * Yes, this function does not take counter wrap into account. But
  3018. * counter wrap is harmless. If the counter wraps, we have waited for
  3019. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3020. * so waiting for one additional grace period should be just fine.
  3021. */
  3022. void cond_synchronize_sched(unsigned long oldstate)
  3023. {
  3024. unsigned long newstate;
  3025. /*
  3026. * Ensure that this load happens before any RCU-destructive
  3027. * actions the caller might carry out after we return.
  3028. */
  3029. newstate = smp_load_acquire(&rcu_sched_state.completed);
  3030. if (ULONG_CMP_GE(oldstate, newstate))
  3031. synchronize_sched();
  3032. }
  3033. EXPORT_SYMBOL_GPL(cond_synchronize_sched);
  3034. /*
  3035. * Check to see if there is any immediate RCU-related work to be done
  3036. * by the current CPU, for the specified type of RCU, returning 1 if so.
  3037. * The checks are in order of increasing expense: checks that can be
  3038. * carried out against CPU-local state are performed first. However,
  3039. * we must check for CPU stalls first, else we might not get a chance.
  3040. */
  3041. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  3042. {
  3043. struct rcu_node *rnp = rdp->mynode;
  3044. /* Check for CPU stalls, if enabled. */
  3045. check_cpu_stall(rsp, rdp);
  3046. /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
  3047. if (rcu_nohz_full_cpu(rsp))
  3048. return 0;
  3049. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3050. if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
  3051. return 1;
  3052. /* Does this CPU have callbacks ready to invoke? */
  3053. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  3054. return 1;
  3055. /* Has RCU gone idle with this CPU needing another grace period? */
  3056. if (cpu_needs_another_gp(rsp, rdp))
  3057. return 1;
  3058. /* Has another RCU grace period completed? */
  3059. if (READ_ONCE(rnp->completed) != rdp->completed) /* outside lock */
  3060. return 1;
  3061. /* Has a new RCU grace period started? */
  3062. if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
  3063. unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
  3064. return 1;
  3065. /* Does this CPU need a deferred NOCB wakeup? */
  3066. if (rcu_nocb_need_deferred_wakeup(rdp))
  3067. return 1;
  3068. /* nothing to do */
  3069. return 0;
  3070. }
  3071. /*
  3072. * Check to see if there is any immediate RCU-related work to be done
  3073. * by the current CPU, returning 1 if so. This function is part of the
  3074. * RCU implementation; it is -not- an exported member of the RCU API.
  3075. */
  3076. static int rcu_pending(void)
  3077. {
  3078. struct rcu_state *rsp;
  3079. for_each_rcu_flavor(rsp)
  3080. if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
  3081. return 1;
  3082. return 0;
  3083. }
  3084. /*
  3085. * Return true if the specified CPU has any callback. If all_lazy is
  3086. * non-NULL, store an indication of whether all callbacks are lazy.
  3087. * (If there are no callbacks, all of them are deemed to be lazy.)
  3088. */
  3089. static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
  3090. {
  3091. bool al = true;
  3092. bool hc = false;
  3093. struct rcu_data *rdp;
  3094. struct rcu_state *rsp;
  3095. for_each_rcu_flavor(rsp) {
  3096. rdp = this_cpu_ptr(rsp->rda);
  3097. if (rcu_segcblist_empty(&rdp->cblist))
  3098. continue;
  3099. hc = true;
  3100. if (rcu_segcblist_n_nonlazy_cbs(&rdp->cblist) || !all_lazy) {
  3101. al = false;
  3102. break;
  3103. }
  3104. }
  3105. if (all_lazy)
  3106. *all_lazy = al;
  3107. return hc;
  3108. }
  3109. /*
  3110. * Helper function for _rcu_barrier() tracing. If tracing is disabled,
  3111. * the compiler is expected to optimize this away.
  3112. */
  3113. static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
  3114. int cpu, unsigned long done)
  3115. {
  3116. trace_rcu_barrier(rsp->name, s, cpu,
  3117. atomic_read(&rsp->barrier_cpu_count), done);
  3118. }
  3119. /*
  3120. * RCU callback function for _rcu_barrier(). If we are last, wake
  3121. * up the task executing _rcu_barrier().
  3122. */
  3123. static void rcu_barrier_callback(struct rcu_head *rhp)
  3124. {
  3125. struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
  3126. struct rcu_state *rsp = rdp->rsp;
  3127. if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
  3128. _rcu_barrier_trace(rsp, TPS("LastCB"), -1,
  3129. rsp->barrier_sequence);
  3130. complete(&rsp->barrier_completion);
  3131. } else {
  3132. _rcu_barrier_trace(rsp, TPS("CB"), -1, rsp->barrier_sequence);
  3133. }
  3134. }
  3135. /*
  3136. * Called with preemption disabled, and from cross-cpu IRQ context.
  3137. */
  3138. static void rcu_barrier_func(void *type)
  3139. {
  3140. struct rcu_state *rsp = type;
  3141. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  3142. _rcu_barrier_trace(rsp, TPS("IRQ"), -1, rsp->barrier_sequence);
  3143. rdp->barrier_head.func = rcu_barrier_callback;
  3144. debug_rcu_head_queue(&rdp->barrier_head);
  3145. if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
  3146. atomic_inc(&rsp->barrier_cpu_count);
  3147. } else {
  3148. debug_rcu_head_unqueue(&rdp->barrier_head);
  3149. _rcu_barrier_trace(rsp, TPS("IRQNQ"), -1,
  3150. rsp->barrier_sequence);
  3151. }
  3152. }
  3153. /*
  3154. * Orchestrate the specified type of RCU barrier, waiting for all
  3155. * RCU callbacks of the specified type to complete.
  3156. */
  3157. static void _rcu_barrier(struct rcu_state *rsp)
  3158. {
  3159. int cpu;
  3160. struct rcu_data *rdp;
  3161. unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
  3162. _rcu_barrier_trace(rsp, TPS("Begin"), -1, s);
  3163. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  3164. mutex_lock(&rsp->barrier_mutex);
  3165. /* Did someone else do our work for us? */
  3166. if (rcu_seq_done(&rsp->barrier_sequence, s)) {
  3167. _rcu_barrier_trace(rsp, TPS("EarlyExit"), -1,
  3168. rsp->barrier_sequence);
  3169. smp_mb(); /* caller's subsequent code after above check. */
  3170. mutex_unlock(&rsp->barrier_mutex);
  3171. return;
  3172. }
  3173. /* Mark the start of the barrier operation. */
  3174. rcu_seq_start(&rsp->barrier_sequence);
  3175. _rcu_barrier_trace(rsp, TPS("Inc1"), -1, rsp->barrier_sequence);
  3176. /*
  3177. * Initialize the count to one rather than to zero in order to
  3178. * avoid a too-soon return to zero in case of a short grace period
  3179. * (or preemption of this task). Exclude CPU-hotplug operations
  3180. * to ensure that no offline CPU has callbacks queued.
  3181. */
  3182. init_completion(&rsp->barrier_completion);
  3183. atomic_set(&rsp->barrier_cpu_count, 1);
  3184. get_online_cpus();
  3185. /*
  3186. * Force each CPU with callbacks to register a new callback.
  3187. * When that callback is invoked, we will know that all of the
  3188. * corresponding CPU's preceding callbacks have been invoked.
  3189. */
  3190. for_each_possible_cpu(cpu) {
  3191. if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
  3192. continue;
  3193. rdp = per_cpu_ptr(rsp->rda, cpu);
  3194. if (rcu_is_nocb_cpu(cpu)) {
  3195. if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
  3196. _rcu_barrier_trace(rsp, TPS("OfflineNoCB"), cpu,
  3197. rsp->barrier_sequence);
  3198. } else {
  3199. _rcu_barrier_trace(rsp, TPS("OnlineNoCB"), cpu,
  3200. rsp->barrier_sequence);
  3201. smp_mb__before_atomic();
  3202. atomic_inc(&rsp->barrier_cpu_count);
  3203. __call_rcu(&rdp->barrier_head,
  3204. rcu_barrier_callback, rsp, cpu, 0);
  3205. }
  3206. } else if (rcu_segcblist_n_cbs(&rdp->cblist)) {
  3207. _rcu_barrier_trace(rsp, TPS("OnlineQ"), cpu,
  3208. rsp->barrier_sequence);
  3209. smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
  3210. } else {
  3211. _rcu_barrier_trace(rsp, TPS("OnlineNQ"), cpu,
  3212. rsp->barrier_sequence);
  3213. }
  3214. }
  3215. put_online_cpus();
  3216. /*
  3217. * Now that we have an rcu_barrier_callback() callback on each
  3218. * CPU, and thus each counted, remove the initial count.
  3219. */
  3220. if (atomic_dec_and_test(&rsp->barrier_cpu_count))
  3221. complete(&rsp->barrier_completion);
  3222. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  3223. wait_for_completion(&rsp->barrier_completion);
  3224. /* Mark the end of the barrier operation. */
  3225. _rcu_barrier_trace(rsp, TPS("Inc2"), -1, rsp->barrier_sequence);
  3226. rcu_seq_end(&rsp->barrier_sequence);
  3227. /* Other rcu_barrier() invocations can now safely proceed. */
  3228. mutex_unlock(&rsp->barrier_mutex);
  3229. }
  3230. /**
  3231. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  3232. */
  3233. void rcu_barrier_bh(void)
  3234. {
  3235. _rcu_barrier(&rcu_bh_state);
  3236. }
  3237. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  3238. /**
  3239. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  3240. */
  3241. void rcu_barrier_sched(void)
  3242. {
  3243. _rcu_barrier(&rcu_sched_state);
  3244. }
  3245. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  3246. /*
  3247. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  3248. * first CPU in a given leaf rcu_node structure coming online. The caller
  3249. * must hold the corresponding leaf rcu_node ->lock with interrrupts
  3250. * disabled.
  3251. */
  3252. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  3253. {
  3254. long mask;
  3255. struct rcu_node *rnp = rnp_leaf;
  3256. raw_lockdep_assert_held_rcu_node(rnp);
  3257. for (;;) {
  3258. mask = rnp->grpmask;
  3259. rnp = rnp->parent;
  3260. if (rnp == NULL)
  3261. return;
  3262. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  3263. rnp->qsmaskinit |= mask;
  3264. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  3265. }
  3266. }
  3267. /*
  3268. * Do boot-time initialization of a CPU's per-CPU RCU data.
  3269. */
  3270. static void __init
  3271. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  3272. {
  3273. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3274. /* Set up local state, ensuring consistent view of global state. */
  3275. rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
  3276. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  3277. WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != 1);
  3278. WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp->dynticks)));
  3279. rdp->cpu = cpu;
  3280. rdp->rsp = rsp;
  3281. rcu_boot_init_nocb_percpu_data(rdp);
  3282. }
  3283. /*
  3284. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  3285. * offline event can be happening at a given time. Note also that we
  3286. * can accept some slop in the rsp->completed access due to the fact
  3287. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  3288. */
  3289. static void
  3290. rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
  3291. {
  3292. unsigned long flags;
  3293. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3294. struct rcu_node *rnp = rcu_get_root(rsp);
  3295. /* Set up local state, ensuring consistent view of global state. */
  3296. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3297. rdp->qlen_last_fqs_check = 0;
  3298. rdp->n_force_qs_snap = rsp->n_force_qs;
  3299. rdp->blimit = blimit;
  3300. if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
  3301. !init_nocb_callback_list(rdp))
  3302. rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
  3303. rdp->dynticks->dynticks_nesting = 1; /* CPU not up, no tearing. */
  3304. rcu_dynticks_eqs_online();
  3305. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  3306. /*
  3307. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  3308. * propagation up the rcu_node tree will happen at the beginning
  3309. * of the next grace period.
  3310. */
  3311. rnp = rdp->mynode;
  3312. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  3313. rdp->beenonline = true; /* We have now been online. */
  3314. rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
  3315. rdp->completed = rnp->completed;
  3316. rdp->cpu_no_qs.b.norm = true;
  3317. rdp->rcu_qs_ctr_snap = per_cpu(rcu_dynticks.rcu_qs_ctr, cpu);
  3318. rdp->core_needs_qs = false;
  3319. rdp->rcu_iw_pending = false;
  3320. rdp->rcu_iw_gpnum = rnp->gpnum - 1;
  3321. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
  3322. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3323. }
  3324. /*
  3325. * Invoked early in the CPU-online process, when pretty much all
  3326. * services are available. The incoming CPU is not present.
  3327. */
  3328. int rcutree_prepare_cpu(unsigned int cpu)
  3329. {
  3330. struct rcu_state *rsp;
  3331. for_each_rcu_flavor(rsp)
  3332. rcu_init_percpu_data(cpu, rsp);
  3333. rcu_prepare_kthreads(cpu);
  3334. rcu_spawn_all_nocb_kthreads(cpu);
  3335. return 0;
  3336. }
  3337. /*
  3338. * Update RCU priority boot kthread affinity for CPU-hotplug changes.
  3339. */
  3340. static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
  3341. {
  3342. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  3343. rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
  3344. }
  3345. /*
  3346. * Near the end of the CPU-online process. Pretty much all services
  3347. * enabled, and the CPU is now very much alive.
  3348. */
  3349. int rcutree_online_cpu(unsigned int cpu)
  3350. {
  3351. unsigned long flags;
  3352. struct rcu_data *rdp;
  3353. struct rcu_node *rnp;
  3354. struct rcu_state *rsp;
  3355. for_each_rcu_flavor(rsp) {
  3356. rdp = per_cpu_ptr(rsp->rda, cpu);
  3357. rnp = rdp->mynode;
  3358. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3359. rnp->ffmask |= rdp->grpmask;
  3360. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3361. }
  3362. if (IS_ENABLED(CONFIG_TREE_SRCU))
  3363. srcu_online_cpu(cpu);
  3364. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  3365. return 0; /* Too early in boot for scheduler work. */
  3366. sync_sched_exp_online_cleanup(cpu);
  3367. rcutree_affinity_setting(cpu, -1);
  3368. return 0;
  3369. }
  3370. /*
  3371. * Near the beginning of the process. The CPU is still very much alive
  3372. * with pretty much all services enabled.
  3373. */
  3374. int rcutree_offline_cpu(unsigned int cpu)
  3375. {
  3376. unsigned long flags;
  3377. struct rcu_data *rdp;
  3378. struct rcu_node *rnp;
  3379. struct rcu_state *rsp;
  3380. for_each_rcu_flavor(rsp) {
  3381. rdp = per_cpu_ptr(rsp->rda, cpu);
  3382. rnp = rdp->mynode;
  3383. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3384. rnp->ffmask &= ~rdp->grpmask;
  3385. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3386. }
  3387. rcutree_affinity_setting(cpu, cpu);
  3388. if (IS_ENABLED(CONFIG_TREE_SRCU))
  3389. srcu_offline_cpu(cpu);
  3390. return 0;
  3391. }
  3392. /*
  3393. * Near the end of the offline process. We do only tracing here.
  3394. */
  3395. int rcutree_dying_cpu(unsigned int cpu)
  3396. {
  3397. struct rcu_state *rsp;
  3398. for_each_rcu_flavor(rsp)
  3399. rcu_cleanup_dying_cpu(rsp);
  3400. return 0;
  3401. }
  3402. /*
  3403. * The outgoing CPU is gone and we are running elsewhere.
  3404. */
  3405. int rcutree_dead_cpu(unsigned int cpu)
  3406. {
  3407. struct rcu_state *rsp;
  3408. for_each_rcu_flavor(rsp) {
  3409. rcu_cleanup_dead_cpu(cpu, rsp);
  3410. do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
  3411. }
  3412. return 0;
  3413. }
  3414. /*
  3415. * Mark the specified CPU as being online so that subsequent grace periods
  3416. * (both expedited and normal) will wait on it. Note that this means that
  3417. * incoming CPUs are not allowed to use RCU read-side critical sections
  3418. * until this function is called. Failing to observe this restriction
  3419. * will result in lockdep splats.
  3420. *
  3421. * Note that this function is special in that it is invoked directly
  3422. * from the incoming CPU rather than from the cpuhp_step mechanism.
  3423. * This is because this function must be invoked at a precise location.
  3424. */
  3425. void rcu_cpu_starting(unsigned int cpu)
  3426. {
  3427. unsigned long flags;
  3428. unsigned long mask;
  3429. int nbits;
  3430. unsigned long oldmask;
  3431. struct rcu_data *rdp;
  3432. struct rcu_node *rnp;
  3433. struct rcu_state *rsp;
  3434. for_each_rcu_flavor(rsp) {
  3435. rdp = per_cpu_ptr(rsp->rda, cpu);
  3436. rnp = rdp->mynode;
  3437. mask = rdp->grpmask;
  3438. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3439. rnp->qsmaskinitnext |= mask;
  3440. oldmask = rnp->expmaskinitnext;
  3441. rnp->expmaskinitnext |= mask;
  3442. oldmask ^= rnp->expmaskinitnext;
  3443. nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
  3444. /* Allow lockless access for expedited grace periods. */
  3445. smp_store_release(&rsp->ncpus, rsp->ncpus + nbits); /* ^^^ */
  3446. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3447. }
  3448. smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
  3449. }
  3450. #ifdef CONFIG_HOTPLUG_CPU
  3451. /*
  3452. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3453. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3454. * bit masks.
  3455. */
  3456. static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
  3457. {
  3458. unsigned long flags;
  3459. unsigned long mask;
  3460. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3461. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  3462. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  3463. mask = rdp->grpmask;
  3464. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  3465. rnp->qsmaskinitnext &= ~mask;
  3466. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3467. }
  3468. /*
  3469. * The outgoing function has no further need of RCU, so remove it from
  3470. * the list of CPUs that RCU must track.
  3471. *
  3472. * Note that this function is special in that it is invoked directly
  3473. * from the outgoing CPU rather than from the cpuhp_step mechanism.
  3474. * This is because this function must be invoked at a precise location.
  3475. */
  3476. void rcu_report_dead(unsigned int cpu)
  3477. {
  3478. struct rcu_state *rsp;
  3479. /* QS for any half-done expedited RCU-sched GP. */
  3480. preempt_disable();
  3481. rcu_report_exp_rdp(&rcu_sched_state,
  3482. this_cpu_ptr(rcu_sched_state.rda), true);
  3483. preempt_enable();
  3484. for_each_rcu_flavor(rsp)
  3485. rcu_cleanup_dying_idle_cpu(cpu, rsp);
  3486. }
  3487. /* Migrate the dead CPU's callbacks to the current CPU. */
  3488. static void rcu_migrate_callbacks(int cpu, struct rcu_state *rsp)
  3489. {
  3490. unsigned long flags;
  3491. struct rcu_data *my_rdp;
  3492. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3493. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  3494. if (rcu_is_nocb_cpu(cpu) || rcu_segcblist_empty(&rdp->cblist))
  3495. return; /* No callbacks to migrate. */
  3496. local_irq_save(flags);
  3497. my_rdp = this_cpu_ptr(rsp->rda);
  3498. if (rcu_nocb_adopt_orphan_cbs(my_rdp, rdp, flags)) {
  3499. local_irq_restore(flags);
  3500. return;
  3501. }
  3502. raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
  3503. rcu_advance_cbs(rsp, rnp_root, rdp); /* Leverage recent GPs. */
  3504. rcu_advance_cbs(rsp, rnp_root, my_rdp); /* Assign GP to pending CBs. */
  3505. rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
  3506. WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
  3507. !rcu_segcblist_n_cbs(&my_rdp->cblist));
  3508. raw_spin_unlock_irqrestore_rcu_node(rnp_root, flags);
  3509. WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
  3510. !rcu_segcblist_empty(&rdp->cblist),
  3511. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
  3512. cpu, rcu_segcblist_n_cbs(&rdp->cblist),
  3513. rcu_segcblist_first_cb(&rdp->cblist));
  3514. }
  3515. /*
  3516. * The outgoing CPU has just passed through the dying-idle state,
  3517. * and we are being invoked from the CPU that was IPIed to continue the
  3518. * offline operation. We need to migrate the outgoing CPU's callbacks.
  3519. */
  3520. void rcutree_migrate_callbacks(int cpu)
  3521. {
  3522. struct rcu_state *rsp;
  3523. for_each_rcu_flavor(rsp)
  3524. rcu_migrate_callbacks(cpu, rsp);
  3525. }
  3526. #endif
  3527. /*
  3528. * On non-huge systems, use expedited RCU grace periods to make suspend
  3529. * and hibernation run faster.
  3530. */
  3531. static int rcu_pm_notify(struct notifier_block *self,
  3532. unsigned long action, void *hcpu)
  3533. {
  3534. switch (action) {
  3535. case PM_HIBERNATION_PREPARE:
  3536. case PM_SUSPEND_PREPARE:
  3537. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3538. rcu_expedite_gp();
  3539. break;
  3540. case PM_POST_HIBERNATION:
  3541. case PM_POST_SUSPEND:
  3542. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3543. rcu_unexpedite_gp();
  3544. break;
  3545. default:
  3546. break;
  3547. }
  3548. return NOTIFY_OK;
  3549. }
  3550. /*
  3551. * Spawn the kthreads that handle each RCU flavor's grace periods.
  3552. */
  3553. static int __init rcu_spawn_gp_kthread(void)
  3554. {
  3555. unsigned long flags;
  3556. int kthread_prio_in = kthread_prio;
  3557. struct rcu_node *rnp;
  3558. struct rcu_state *rsp;
  3559. struct sched_param sp;
  3560. struct task_struct *t;
  3561. /* Force priority into range. */
  3562. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  3563. kthread_prio = 1;
  3564. else if (kthread_prio < 0)
  3565. kthread_prio = 0;
  3566. else if (kthread_prio > 99)
  3567. kthread_prio = 99;
  3568. if (kthread_prio != kthread_prio_in)
  3569. pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
  3570. kthread_prio, kthread_prio_in);
  3571. rcu_scheduler_fully_active = 1;
  3572. for_each_rcu_flavor(rsp) {
  3573. t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
  3574. BUG_ON(IS_ERR(t));
  3575. rnp = rcu_get_root(rsp);
  3576. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3577. rsp->gp_kthread = t;
  3578. if (kthread_prio) {
  3579. sp.sched_priority = kthread_prio;
  3580. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  3581. }
  3582. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3583. wake_up_process(t);
  3584. }
  3585. rcu_spawn_nocb_kthreads();
  3586. rcu_spawn_boost_kthreads();
  3587. return 0;
  3588. }
  3589. early_initcall(rcu_spawn_gp_kthread);
  3590. /*
  3591. * This function is invoked towards the end of the scheduler's
  3592. * initialization process. Before this is called, the idle task might
  3593. * contain synchronous grace-period primitives (during which time, this idle
  3594. * task is booting the system, and such primitives are no-ops). After this
  3595. * function is called, any synchronous grace-period primitives are run as
  3596. * expedited, with the requesting task driving the grace period forward.
  3597. * A later core_initcall() rcu_set_runtime_mode() will switch to full
  3598. * runtime RCU functionality.
  3599. */
  3600. void rcu_scheduler_starting(void)
  3601. {
  3602. WARN_ON(num_online_cpus() != 1);
  3603. WARN_ON(nr_context_switches() > 0);
  3604. rcu_test_sync_prims();
  3605. rcu_scheduler_active = RCU_SCHEDULER_INIT;
  3606. rcu_test_sync_prims();
  3607. }
  3608. /*
  3609. * Helper function for rcu_init() that initializes one rcu_state structure.
  3610. */
  3611. static void __init rcu_init_one(struct rcu_state *rsp)
  3612. {
  3613. static const char * const buf[] = RCU_NODE_NAME_INIT;
  3614. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  3615. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  3616. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  3617. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  3618. int cpustride = 1;
  3619. int i;
  3620. int j;
  3621. struct rcu_node *rnp;
  3622. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  3623. /* Silence gcc 4.8 false positive about array index out of range. */
  3624. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  3625. panic("rcu_init_one: rcu_num_lvls out of range");
  3626. /* Initialize the level-tracking arrays. */
  3627. for (i = 1; i < rcu_num_lvls; i++)
  3628. rsp->level[i] = rsp->level[i - 1] + num_rcu_lvl[i - 1];
  3629. rcu_init_levelspread(levelspread, num_rcu_lvl);
  3630. /* Initialize the elements themselves, starting from the leaves. */
  3631. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  3632. cpustride *= levelspread[i];
  3633. rnp = rsp->level[i];
  3634. for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
  3635. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  3636. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  3637. &rcu_node_class[i], buf[i]);
  3638. raw_spin_lock_init(&rnp->fqslock);
  3639. lockdep_set_class_and_name(&rnp->fqslock,
  3640. &rcu_fqs_class[i], fqs[i]);
  3641. rnp->gpnum = rsp->gpnum;
  3642. rnp->completed = rsp->completed;
  3643. rnp->qsmask = 0;
  3644. rnp->qsmaskinit = 0;
  3645. rnp->grplo = j * cpustride;
  3646. rnp->grphi = (j + 1) * cpustride - 1;
  3647. if (rnp->grphi >= nr_cpu_ids)
  3648. rnp->grphi = nr_cpu_ids - 1;
  3649. if (i == 0) {
  3650. rnp->grpnum = 0;
  3651. rnp->grpmask = 0;
  3652. rnp->parent = NULL;
  3653. } else {
  3654. rnp->grpnum = j % levelspread[i - 1];
  3655. rnp->grpmask = 1UL << rnp->grpnum;
  3656. rnp->parent = rsp->level[i - 1] +
  3657. j / levelspread[i - 1];
  3658. }
  3659. rnp->level = i;
  3660. INIT_LIST_HEAD(&rnp->blkd_tasks);
  3661. rcu_init_one_nocb(rnp);
  3662. init_waitqueue_head(&rnp->exp_wq[0]);
  3663. init_waitqueue_head(&rnp->exp_wq[1]);
  3664. init_waitqueue_head(&rnp->exp_wq[2]);
  3665. init_waitqueue_head(&rnp->exp_wq[3]);
  3666. spin_lock_init(&rnp->exp_lock);
  3667. }
  3668. }
  3669. init_swait_queue_head(&rsp->gp_wq);
  3670. init_swait_queue_head(&rsp->expedited_wq);
  3671. rnp = rsp->level[rcu_num_lvls - 1];
  3672. for_each_possible_cpu(i) {
  3673. while (i > rnp->grphi)
  3674. rnp++;
  3675. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  3676. rcu_boot_init_percpu_data(i, rsp);
  3677. }
  3678. list_add(&rsp->flavors, &rcu_struct_flavors);
  3679. }
  3680. /*
  3681. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  3682. * replace the definitions in tree.h because those are needed to size
  3683. * the ->node array in the rcu_state structure.
  3684. */
  3685. static void __init rcu_init_geometry(void)
  3686. {
  3687. ulong d;
  3688. int i;
  3689. int rcu_capacity[RCU_NUM_LVLS];
  3690. /*
  3691. * Initialize any unspecified boot parameters.
  3692. * The default values of jiffies_till_first_fqs and
  3693. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  3694. * value, which is a function of HZ, then adding one for each
  3695. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  3696. */
  3697. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  3698. if (jiffies_till_first_fqs == ULONG_MAX)
  3699. jiffies_till_first_fqs = d;
  3700. if (jiffies_till_next_fqs == ULONG_MAX)
  3701. jiffies_till_next_fqs = d;
  3702. /* If the compile-time values are accurate, just leave. */
  3703. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  3704. nr_cpu_ids == NR_CPUS)
  3705. return;
  3706. pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
  3707. rcu_fanout_leaf, nr_cpu_ids);
  3708. /*
  3709. * The boot-time rcu_fanout_leaf parameter must be at least two
  3710. * and cannot exceed the number of bits in the rcu_node masks.
  3711. * Complain and fall back to the compile-time values if this
  3712. * limit is exceeded.
  3713. */
  3714. if (rcu_fanout_leaf < 2 ||
  3715. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  3716. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3717. WARN_ON(1);
  3718. return;
  3719. }
  3720. /*
  3721. * Compute number of nodes that can be handled an rcu_node tree
  3722. * with the given number of levels.
  3723. */
  3724. rcu_capacity[0] = rcu_fanout_leaf;
  3725. for (i = 1; i < RCU_NUM_LVLS; i++)
  3726. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  3727. /*
  3728. * The tree must be able to accommodate the configured number of CPUs.
  3729. * If this limit is exceeded, fall back to the compile-time values.
  3730. */
  3731. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  3732. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3733. WARN_ON(1);
  3734. return;
  3735. }
  3736. /* Calculate the number of levels in the tree. */
  3737. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  3738. }
  3739. rcu_num_lvls = i + 1;
  3740. /* Calculate the number of rcu_nodes at each level of the tree. */
  3741. for (i = 0; i < rcu_num_lvls; i++) {
  3742. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  3743. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  3744. }
  3745. /* Calculate the total number of rcu_node structures. */
  3746. rcu_num_nodes = 0;
  3747. for (i = 0; i < rcu_num_lvls; i++)
  3748. rcu_num_nodes += num_rcu_lvl[i];
  3749. }
  3750. /*
  3751. * Dump out the structure of the rcu_node combining tree associated
  3752. * with the rcu_state structure referenced by rsp.
  3753. */
  3754. static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
  3755. {
  3756. int level = 0;
  3757. struct rcu_node *rnp;
  3758. pr_info("rcu_node tree layout dump\n");
  3759. pr_info(" ");
  3760. rcu_for_each_node_breadth_first(rsp, rnp) {
  3761. if (rnp->level != level) {
  3762. pr_cont("\n");
  3763. pr_info(" ");
  3764. level = rnp->level;
  3765. }
  3766. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  3767. }
  3768. pr_cont("\n");
  3769. }
  3770. struct workqueue_struct *rcu_gp_wq;
  3771. void __init rcu_init(void)
  3772. {
  3773. int cpu;
  3774. rcu_early_boot_tests();
  3775. rcu_bootup_announce();
  3776. rcu_init_geometry();
  3777. rcu_init_one(&rcu_bh_state);
  3778. rcu_init_one(&rcu_sched_state);
  3779. if (dump_tree)
  3780. rcu_dump_rcu_node_tree(&rcu_sched_state);
  3781. __rcu_init_preempt();
  3782. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  3783. /*
  3784. * We don't need protection against CPU-hotplug here because
  3785. * this is called early in boot, before either interrupts
  3786. * or the scheduler are operational.
  3787. */
  3788. pm_notifier(rcu_pm_notify, 0);
  3789. for_each_online_cpu(cpu) {
  3790. rcutree_prepare_cpu(cpu);
  3791. rcu_cpu_starting(cpu);
  3792. rcutree_online_cpu(cpu);
  3793. }
  3794. /* Create workqueue for expedited GPs and for Tree SRCU. */
  3795. rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
  3796. WARN_ON(!rcu_gp_wq);
  3797. }
  3798. #include "tree_exp.h"
  3799. #include "tree_plugin.h"