rcutree.c 89 KB

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