tree.c 130 KB

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