tree.c 132 KB

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