tree.c 128 KB

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