tree.c 129 KB

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