rcutree.c 105 KB

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