rcutree.c 103 KB

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