tree.c 127 KB

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