tree.c 132 KB

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