rcutree.c 101 KB

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