rcutree.c 88 KB

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