tree.c 106 KB

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