rcutree.c 98 KB

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