tree.c 133 KB

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