tree.c 133 KB

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