tree.c 117 KB

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