tree.c 116 KB

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