rcutree.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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/module.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 "rcutree.h"
  54. /* Data structures. */
  55. static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
  56. #define RCU_STATE_INITIALIZER(structname) { \
  57. .level = { &structname.node[0] }, \
  58. .levelcnt = { \
  59. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  60. NUM_RCU_LVL_1, \
  61. NUM_RCU_LVL_2, \
  62. NUM_RCU_LVL_3, \
  63. NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
  64. }, \
  65. .signaled = RCU_GP_IDLE, \
  66. .gpnum = -300, \
  67. .completed = -300, \
  68. .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&structname.onofflock), \
  69. .fqslock = __RAW_SPIN_LOCK_UNLOCKED(&structname.fqslock), \
  70. .n_force_qs = 0, \
  71. .n_force_qs_ngp = 0, \
  72. .name = #structname, \
  73. }
  74. struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched_state);
  75. DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
  76. struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh_state);
  77. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
  78. static struct rcu_state *rcu_state;
  79. /*
  80. * The rcu_scheduler_active variable transitions from zero to one just
  81. * before the first task is spawned. So when this variable is zero, RCU
  82. * can assume that there is but one task, allowing RCU to (for example)
  83. * optimized synchronize_sched() to a simple barrier(). When this variable
  84. * is one, RCU must actually do all the hard work required to detect real
  85. * grace periods. This variable is also used to suppress boot-time false
  86. * positives from lockdep-RCU error checking.
  87. */
  88. int rcu_scheduler_active __read_mostly;
  89. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  90. /*
  91. * The rcu_scheduler_fully_active variable transitions from zero to one
  92. * during the early_initcall() processing, which is after the scheduler
  93. * is capable of creating new tasks. So RCU processing (for example,
  94. * creating tasks for RCU priority boosting) must be delayed until after
  95. * rcu_scheduler_fully_active transitions from zero to one. We also
  96. * currently delay invocation of any RCU callbacks until after this point.
  97. *
  98. * It might later prove better for people registering RCU callbacks during
  99. * early boot to take responsibility for these callbacks, but one step at
  100. * a time.
  101. */
  102. static int rcu_scheduler_fully_active __read_mostly;
  103. #ifdef CONFIG_RCU_BOOST
  104. /*
  105. * Control variables for per-CPU and per-rcu_node kthreads. These
  106. * handle all flavors of RCU.
  107. */
  108. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  109. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  110. DEFINE_PER_CPU(int, rcu_cpu_kthread_cpu);
  111. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  112. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  113. #endif /* #ifdef CONFIG_RCU_BOOST */
  114. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  115. static void invoke_rcu_core(void);
  116. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  117. #define RCU_KTHREAD_PRIO 1 /* RT priority for per-CPU kthreads. */
  118. /*
  119. * Track the rcutorture test sequence number and the update version
  120. * number within a given test. The rcutorture_testseq is incremented
  121. * on every rcutorture module load and unload, so has an odd value
  122. * when a test is running. The rcutorture_vernum is set to zero
  123. * when rcutorture starts and is incremented on each rcutorture update.
  124. * These variables enable correlating rcutorture output with the
  125. * RCU tracing information.
  126. */
  127. unsigned long rcutorture_testseq;
  128. unsigned long rcutorture_vernum;
  129. /*
  130. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  131. * permit this function to be invoked without holding the root rcu_node
  132. * structure's ->lock, but of course results can be subject to change.
  133. */
  134. static int rcu_gp_in_progress(struct rcu_state *rsp)
  135. {
  136. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  137. }
  138. /*
  139. * Note a quiescent state. Because we do not need to know
  140. * how many quiescent states passed, just if there was at least
  141. * one since the start of the grace period, this just sets a flag.
  142. */
  143. void rcu_sched_qs(int cpu)
  144. {
  145. struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
  146. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  147. barrier();
  148. rdp->passed_quiesc = 1;
  149. }
  150. void rcu_bh_qs(int cpu)
  151. {
  152. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  153. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  154. barrier();
  155. rdp->passed_quiesc = 1;
  156. }
  157. /*
  158. * Note a context switch. This is a quiescent state for RCU-sched,
  159. * and requires special handling for preemptible RCU.
  160. */
  161. void rcu_note_context_switch(int cpu)
  162. {
  163. rcu_sched_qs(cpu);
  164. rcu_preempt_note_context_switch(cpu);
  165. }
  166. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  167. #ifdef CONFIG_NO_HZ
  168. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  169. .dynticks_nesting = 1,
  170. .dynticks = ATOMIC_INIT(1),
  171. };
  172. #endif /* #ifdef CONFIG_NO_HZ */
  173. static int blimit = 10; /* Maximum callbacks per softirq. */
  174. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  175. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  176. module_param(blimit, int, 0);
  177. module_param(qhimark, int, 0);
  178. module_param(qlowmark, int, 0);
  179. int rcu_cpu_stall_suppress __read_mostly;
  180. module_param(rcu_cpu_stall_suppress, int, 0644);
  181. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  182. static int rcu_pending(int cpu);
  183. /*
  184. * Return the number of RCU-sched batches processed thus far for debug & stats.
  185. */
  186. long rcu_batches_completed_sched(void)
  187. {
  188. return rcu_sched_state.completed;
  189. }
  190. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  191. /*
  192. * Return the number of RCU BH batches processed thus far for debug & stats.
  193. */
  194. long rcu_batches_completed_bh(void)
  195. {
  196. return rcu_bh_state.completed;
  197. }
  198. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  199. /*
  200. * Force a quiescent state for RCU BH.
  201. */
  202. void rcu_bh_force_quiescent_state(void)
  203. {
  204. force_quiescent_state(&rcu_bh_state, 0);
  205. }
  206. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  207. /*
  208. * Record the number of times rcutorture tests have been initiated and
  209. * terminated. This information allows the debugfs tracing stats to be
  210. * correlated to the rcutorture messages, even when the rcutorture module
  211. * is being repeatedly loaded and unloaded. In other words, we cannot
  212. * store this state in rcutorture itself.
  213. */
  214. void rcutorture_record_test_transition(void)
  215. {
  216. rcutorture_testseq++;
  217. rcutorture_vernum = 0;
  218. }
  219. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  220. /*
  221. * Record the number of writer passes through the current rcutorture test.
  222. * This is also used to correlate debugfs tracing stats with the rcutorture
  223. * messages.
  224. */
  225. void rcutorture_record_progress(unsigned long vernum)
  226. {
  227. rcutorture_vernum++;
  228. }
  229. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  230. /*
  231. * Force a quiescent state for RCU-sched.
  232. */
  233. void rcu_sched_force_quiescent_state(void)
  234. {
  235. force_quiescent_state(&rcu_sched_state, 0);
  236. }
  237. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  238. /*
  239. * Does the CPU have callbacks ready to be invoked?
  240. */
  241. static int
  242. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  243. {
  244. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  245. }
  246. /*
  247. * Does the current CPU require a yet-as-unscheduled grace period?
  248. */
  249. static int
  250. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  251. {
  252. return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
  253. }
  254. /*
  255. * Return the root node of the specified rcu_state structure.
  256. */
  257. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  258. {
  259. return &rsp->node[0];
  260. }
  261. #ifdef CONFIG_SMP
  262. /*
  263. * If the specified CPU is offline, tell the caller that it is in
  264. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  265. * Grace periods can end up waiting on an offline CPU when that
  266. * CPU is in the process of coming online -- it will be added to the
  267. * rcu_node bitmasks before it actually makes it online. The same thing
  268. * can happen while a CPU is in the process of coming online. Because this
  269. * race is quite rare, we check for it after detecting that the grace
  270. * period has been delayed rather than checking each and every CPU
  271. * each and every time we start a new grace period.
  272. */
  273. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  274. {
  275. /*
  276. * If the CPU is offline, it is in a quiescent state. We can
  277. * trust its state not to change because interrupts are disabled.
  278. */
  279. if (cpu_is_offline(rdp->cpu)) {
  280. rdp->offline_fqs++;
  281. return 1;
  282. }
  283. /* If preemptible RCU, no point in sending reschedule IPI. */
  284. if (rdp->preemptible)
  285. return 0;
  286. /* The CPU is online, so send it a reschedule IPI. */
  287. if (rdp->cpu != smp_processor_id())
  288. smp_send_reschedule(rdp->cpu);
  289. else
  290. set_need_resched();
  291. rdp->resched_ipi++;
  292. return 0;
  293. }
  294. #endif /* #ifdef CONFIG_SMP */
  295. #ifdef CONFIG_NO_HZ
  296. /**
  297. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  298. *
  299. * Enter nohz mode, in other words, -leave- the mode in which RCU
  300. * read-side critical sections can occur. (Though RCU read-side
  301. * critical sections can occur in irq handlers in nohz mode, a possibility
  302. * handled by rcu_irq_enter() and rcu_irq_exit()).
  303. */
  304. void rcu_enter_nohz(void)
  305. {
  306. unsigned long flags;
  307. struct rcu_dynticks *rdtp;
  308. local_irq_save(flags);
  309. rdtp = &__get_cpu_var(rcu_dynticks);
  310. if (--rdtp->dynticks_nesting) {
  311. local_irq_restore(flags);
  312. return;
  313. }
  314. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  315. smp_mb__before_atomic_inc(); /* See above. */
  316. atomic_inc(&rdtp->dynticks);
  317. smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
  318. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  319. local_irq_restore(flags);
  320. /* If the interrupt queued a callback, get out of dyntick mode. */
  321. if (in_irq() &&
  322. (__get_cpu_var(rcu_sched_data).nxtlist ||
  323. __get_cpu_var(rcu_bh_data).nxtlist ||
  324. rcu_preempt_needs_cpu(smp_processor_id())))
  325. set_need_resched();
  326. }
  327. /*
  328. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  329. *
  330. * Exit nohz mode, in other words, -enter- the mode in which RCU
  331. * read-side critical sections normally occur.
  332. */
  333. void rcu_exit_nohz(void)
  334. {
  335. unsigned long flags;
  336. struct rcu_dynticks *rdtp;
  337. local_irq_save(flags);
  338. rdtp = &__get_cpu_var(rcu_dynticks);
  339. if (rdtp->dynticks_nesting++) {
  340. local_irq_restore(flags);
  341. return;
  342. }
  343. smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
  344. atomic_inc(&rdtp->dynticks);
  345. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  346. smp_mb__after_atomic_inc(); /* See above. */
  347. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  348. local_irq_restore(flags);
  349. }
  350. /**
  351. * rcu_nmi_enter - inform RCU of entry to NMI context
  352. *
  353. * If the CPU was idle with dynamic ticks active, and there is no
  354. * irq handler running, this updates rdtp->dynticks_nmi to let the
  355. * RCU grace-period handling know that the CPU is active.
  356. */
  357. void rcu_nmi_enter(void)
  358. {
  359. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  360. if (rdtp->dynticks_nmi_nesting == 0 &&
  361. (atomic_read(&rdtp->dynticks) & 0x1))
  362. return;
  363. rdtp->dynticks_nmi_nesting++;
  364. smp_mb__before_atomic_inc(); /* Force delay from prior write. */
  365. atomic_inc(&rdtp->dynticks);
  366. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  367. smp_mb__after_atomic_inc(); /* See above. */
  368. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  369. }
  370. /**
  371. * rcu_nmi_exit - inform RCU of exit from NMI context
  372. *
  373. * If the CPU was idle with dynamic ticks active, and there is no
  374. * irq handler running, this updates rdtp->dynticks_nmi to let the
  375. * RCU grace-period handling know that the CPU is no longer active.
  376. */
  377. void rcu_nmi_exit(void)
  378. {
  379. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  380. if (rdtp->dynticks_nmi_nesting == 0 ||
  381. --rdtp->dynticks_nmi_nesting != 0)
  382. return;
  383. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  384. smp_mb__before_atomic_inc(); /* See above. */
  385. atomic_inc(&rdtp->dynticks);
  386. smp_mb__after_atomic_inc(); /* Force delay to next write. */
  387. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  388. }
  389. /**
  390. * rcu_irq_enter - inform RCU of entry to hard irq context
  391. *
  392. * If the CPU was idle with dynamic ticks active, this updates the
  393. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  394. */
  395. void rcu_irq_enter(void)
  396. {
  397. rcu_exit_nohz();
  398. }
  399. /**
  400. * rcu_irq_exit - inform RCU of exit from hard irq context
  401. *
  402. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  403. * to put let the RCU handling be aware that the CPU is going back to idle
  404. * with no ticks.
  405. */
  406. void rcu_irq_exit(void)
  407. {
  408. rcu_enter_nohz();
  409. }
  410. #ifdef CONFIG_SMP
  411. /*
  412. * Snapshot the specified CPU's dynticks counter so that we can later
  413. * credit them with an implicit quiescent state. Return 1 if this CPU
  414. * is in dynticks idle mode, which is an extended quiescent state.
  415. */
  416. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  417. {
  418. rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
  419. return 0;
  420. }
  421. /*
  422. * Return true if the specified CPU has passed through a quiescent
  423. * state by virtue of being in or having passed through an dynticks
  424. * idle state since the last call to dyntick_save_progress_counter()
  425. * for this same CPU.
  426. */
  427. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  428. {
  429. unsigned long curr;
  430. unsigned long snap;
  431. curr = (unsigned long)atomic_add_return(0, &rdp->dynticks->dynticks);
  432. snap = (unsigned long)rdp->dynticks_snap;
  433. /*
  434. * If the CPU passed through or entered a dynticks idle phase with
  435. * no active irq/NMI handlers, then we can safely pretend that the CPU
  436. * already acknowledged the request to pass through a quiescent
  437. * state. Either way, that CPU cannot possibly be in an RCU
  438. * read-side critical section that started before the beginning
  439. * of the current RCU grace period.
  440. */
  441. if ((curr & 0x1) == 0 || ULONG_CMP_GE(curr, snap + 2)) {
  442. rdp->dynticks_fqs++;
  443. return 1;
  444. }
  445. /* Go check for the CPU being offline. */
  446. return rcu_implicit_offline_qs(rdp);
  447. }
  448. #endif /* #ifdef CONFIG_SMP */
  449. #else /* #ifdef CONFIG_NO_HZ */
  450. #ifdef CONFIG_SMP
  451. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  452. {
  453. return 0;
  454. }
  455. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  456. {
  457. return rcu_implicit_offline_qs(rdp);
  458. }
  459. #endif /* #ifdef CONFIG_SMP */
  460. #endif /* #else #ifdef CONFIG_NO_HZ */
  461. int rcu_cpu_stall_suppress __read_mostly;
  462. static void record_gp_stall_check_time(struct rcu_state *rsp)
  463. {
  464. rsp->gp_start = jiffies;
  465. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  466. }
  467. static void print_other_cpu_stall(struct rcu_state *rsp)
  468. {
  469. int cpu;
  470. long delta;
  471. unsigned long flags;
  472. struct rcu_node *rnp = rcu_get_root(rsp);
  473. /* Only let one CPU complain about others per time interval. */
  474. raw_spin_lock_irqsave(&rnp->lock, flags);
  475. delta = jiffies - rsp->jiffies_stall;
  476. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  477. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  478. return;
  479. }
  480. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  481. /*
  482. * Now rat on any tasks that got kicked up to the root rcu_node
  483. * due to CPU offlining.
  484. */
  485. rcu_print_task_stall(rnp);
  486. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  487. /*
  488. * OK, time to rat on our buddy...
  489. * See Documentation/RCU/stallwarn.txt for info on how to debug
  490. * RCU CPU stall warnings.
  491. */
  492. printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks: {",
  493. rsp->name);
  494. rcu_for_each_leaf_node(rsp, rnp) {
  495. raw_spin_lock_irqsave(&rnp->lock, flags);
  496. rcu_print_task_stall(rnp);
  497. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  498. if (rnp->qsmask == 0)
  499. continue;
  500. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  501. if (rnp->qsmask & (1UL << cpu))
  502. printk(" %d", rnp->grplo + cpu);
  503. }
  504. printk("} (detected by %d, t=%ld jiffies)\n",
  505. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  506. trigger_all_cpu_backtrace();
  507. /* If so configured, complain about tasks blocking the grace period. */
  508. rcu_print_detail_task_stall(rsp);
  509. force_quiescent_state(rsp, 0); /* Kick them all. */
  510. }
  511. static void print_cpu_stall(struct rcu_state *rsp)
  512. {
  513. unsigned long flags;
  514. struct rcu_node *rnp = rcu_get_root(rsp);
  515. /*
  516. * OK, time to rat on ourselves...
  517. * See Documentation/RCU/stallwarn.txt for info on how to debug
  518. * RCU CPU stall warnings.
  519. */
  520. printk(KERN_ERR "INFO: %s detected stall on CPU %d (t=%lu jiffies)\n",
  521. rsp->name, smp_processor_id(), jiffies - rsp->gp_start);
  522. trigger_all_cpu_backtrace();
  523. raw_spin_lock_irqsave(&rnp->lock, flags);
  524. if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
  525. rsp->jiffies_stall =
  526. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  527. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  528. set_need_resched(); /* kick ourselves to get things going. */
  529. }
  530. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  531. {
  532. unsigned long j;
  533. unsigned long js;
  534. struct rcu_node *rnp;
  535. if (rcu_cpu_stall_suppress)
  536. return;
  537. j = ACCESS_ONCE(jiffies);
  538. js = ACCESS_ONCE(rsp->jiffies_stall);
  539. rnp = rdp->mynode;
  540. if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
  541. /* We haven't checked in, so go dump stack. */
  542. print_cpu_stall(rsp);
  543. } else if (rcu_gp_in_progress(rsp) &&
  544. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  545. /* They had a few time units to dump stack, so complain. */
  546. print_other_cpu_stall(rsp);
  547. }
  548. }
  549. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  550. {
  551. rcu_cpu_stall_suppress = 1;
  552. return NOTIFY_DONE;
  553. }
  554. /**
  555. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  556. *
  557. * Set the stall-warning timeout way off into the future, thus preventing
  558. * any RCU CPU stall-warning messages from appearing in the current set of
  559. * RCU grace periods.
  560. *
  561. * The caller must disable hard irqs.
  562. */
  563. void rcu_cpu_stall_reset(void)
  564. {
  565. rcu_sched_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  566. rcu_bh_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  567. rcu_preempt_stall_reset();
  568. }
  569. static struct notifier_block rcu_panic_block = {
  570. .notifier_call = rcu_panic,
  571. };
  572. static void __init check_cpu_stall_init(void)
  573. {
  574. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  575. }
  576. /*
  577. * Update CPU-local rcu_data state to record the newly noticed grace period.
  578. * This is used both when we started the grace period and when we notice
  579. * that someone else started the grace period. The caller must hold the
  580. * ->lock of the leaf rcu_node structure corresponding to the current CPU,
  581. * and must have irqs disabled.
  582. */
  583. static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  584. {
  585. if (rdp->gpnum != rnp->gpnum) {
  586. /*
  587. * If the current grace period is waiting for this CPU,
  588. * set up to detect a quiescent state, otherwise don't
  589. * go looking for one.
  590. */
  591. rdp->gpnum = rnp->gpnum;
  592. if (rnp->qsmask & rdp->grpmask) {
  593. rdp->qs_pending = 1;
  594. rdp->passed_quiesc = 0;
  595. } else
  596. rdp->qs_pending = 0;
  597. }
  598. }
  599. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  600. {
  601. unsigned long flags;
  602. struct rcu_node *rnp;
  603. local_irq_save(flags);
  604. rnp = rdp->mynode;
  605. if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
  606. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  607. local_irq_restore(flags);
  608. return;
  609. }
  610. __note_new_gpnum(rsp, rnp, rdp);
  611. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  612. }
  613. /*
  614. * Did someone else start a new RCU grace period start since we last
  615. * checked? Update local state appropriately if so. Must be called
  616. * on the CPU corresponding to rdp.
  617. */
  618. static int
  619. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  620. {
  621. unsigned long flags;
  622. int ret = 0;
  623. local_irq_save(flags);
  624. if (rdp->gpnum != rsp->gpnum) {
  625. note_new_gpnum(rsp, rdp);
  626. ret = 1;
  627. }
  628. local_irq_restore(flags);
  629. return ret;
  630. }
  631. /*
  632. * Advance this CPU's callbacks, but only if the current grace period
  633. * has ended. This may be called only from the CPU to whom the rdp
  634. * belongs. In addition, the corresponding leaf rcu_node structure's
  635. * ->lock must be held by the caller, with irqs disabled.
  636. */
  637. static void
  638. __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  639. {
  640. /* Did another grace period end? */
  641. if (rdp->completed != rnp->completed) {
  642. /* Advance callbacks. No harm if list empty. */
  643. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  644. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  645. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  646. /* Remember that we saw this grace-period completion. */
  647. rdp->completed = rnp->completed;
  648. /*
  649. * If we were in an extended quiescent state, we may have
  650. * missed some grace periods that others CPUs handled on
  651. * our behalf. Catch up with this state to avoid noting
  652. * spurious new grace periods. If another grace period
  653. * has started, then rnp->gpnum will have advanced, so
  654. * we will detect this later on.
  655. */
  656. if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
  657. rdp->gpnum = rdp->completed;
  658. /*
  659. * If RCU does not need a quiescent state from this CPU,
  660. * then make sure that this CPU doesn't go looking for one.
  661. */
  662. if ((rnp->qsmask & rdp->grpmask) == 0)
  663. rdp->qs_pending = 0;
  664. }
  665. }
  666. /*
  667. * Advance this CPU's callbacks, but only if the current grace period
  668. * has ended. This may be called only from the CPU to whom the rdp
  669. * belongs.
  670. */
  671. static void
  672. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  673. {
  674. unsigned long flags;
  675. struct rcu_node *rnp;
  676. local_irq_save(flags);
  677. rnp = rdp->mynode;
  678. if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
  679. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  680. local_irq_restore(flags);
  681. return;
  682. }
  683. __rcu_process_gp_end(rsp, rnp, rdp);
  684. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  685. }
  686. /*
  687. * Do per-CPU grace-period initialization for running CPU. The caller
  688. * must hold the lock of the leaf rcu_node structure corresponding to
  689. * this CPU.
  690. */
  691. static void
  692. rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  693. {
  694. /* Prior grace period ended, so advance callbacks for current CPU. */
  695. __rcu_process_gp_end(rsp, rnp, rdp);
  696. /*
  697. * Because this CPU just now started the new grace period, we know
  698. * that all of its callbacks will be covered by this upcoming grace
  699. * period, even the ones that were registered arbitrarily recently.
  700. * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
  701. *
  702. * Other CPUs cannot be sure exactly when the grace period started.
  703. * Therefore, their recently registered callbacks must pass through
  704. * an additional RCU_NEXT_READY stage, so that they will be handled
  705. * by the next RCU grace period.
  706. */
  707. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  708. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  709. /* Set state so that this CPU will detect the next quiescent state. */
  710. __note_new_gpnum(rsp, rnp, rdp);
  711. }
  712. /*
  713. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  714. * in preparation for detecting the next grace period. The caller must hold
  715. * the root node's ->lock, which is released before return. Hard irqs must
  716. * be disabled.
  717. */
  718. static void
  719. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  720. __releases(rcu_get_root(rsp)->lock)
  721. {
  722. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  723. struct rcu_node *rnp = rcu_get_root(rsp);
  724. if (!cpu_needs_another_gp(rsp, rdp) || rsp->fqs_active) {
  725. if (cpu_needs_another_gp(rsp, rdp))
  726. rsp->fqs_need_gp = 1;
  727. if (rnp->completed == rsp->completed) {
  728. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  729. return;
  730. }
  731. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  732. /*
  733. * Propagate new ->completed value to rcu_node structures
  734. * so that other CPUs don't have to wait until the start
  735. * of the next grace period to process their callbacks.
  736. */
  737. rcu_for_each_node_breadth_first(rsp, rnp) {
  738. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  739. rnp->completed = rsp->completed;
  740. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  741. }
  742. local_irq_restore(flags);
  743. return;
  744. }
  745. /* Advance to a new grace period and initialize state. */
  746. rsp->gpnum++;
  747. WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
  748. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  749. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  750. record_gp_stall_check_time(rsp);
  751. /* Special-case the common single-level case. */
  752. if (NUM_RCU_NODES == 1) {
  753. rcu_preempt_check_blocked_tasks(rnp);
  754. rnp->qsmask = rnp->qsmaskinit;
  755. rnp->gpnum = rsp->gpnum;
  756. rnp->completed = rsp->completed;
  757. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  758. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  759. rcu_preempt_boost_start_gp(rnp);
  760. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  761. return;
  762. }
  763. raw_spin_unlock(&rnp->lock); /* leave irqs disabled. */
  764. /* Exclude any concurrent CPU-hotplug operations. */
  765. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  766. /*
  767. * Set the quiescent-state-needed bits in all the rcu_node
  768. * structures for all currently online CPUs in breadth-first
  769. * order, starting from the root rcu_node structure. This
  770. * operation relies on the layout of the hierarchy within the
  771. * rsp->node[] array. Note that other CPUs will access only
  772. * the leaves of the hierarchy, which still indicate that no
  773. * grace period is in progress, at least until the corresponding
  774. * leaf node has been initialized. In addition, we have excluded
  775. * CPU-hotplug operations.
  776. *
  777. * Note that the grace period cannot complete until we finish
  778. * the initialization process, as there will be at least one
  779. * qsmask bit set in the root node until that time, namely the
  780. * one corresponding to this CPU, due to the fact that we have
  781. * irqs disabled.
  782. */
  783. rcu_for_each_node_breadth_first(rsp, rnp) {
  784. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  785. rcu_preempt_check_blocked_tasks(rnp);
  786. rnp->qsmask = rnp->qsmaskinit;
  787. rnp->gpnum = rsp->gpnum;
  788. rnp->completed = rsp->completed;
  789. if (rnp == rdp->mynode)
  790. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  791. rcu_preempt_boost_start_gp(rnp);
  792. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  793. }
  794. rnp = rcu_get_root(rsp);
  795. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  796. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  797. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  798. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  799. }
  800. /*
  801. * Report a full set of quiescent states to the specified rcu_state
  802. * data structure. This involves cleaning up after the prior grace
  803. * period and letting rcu_start_gp() start up the next grace period
  804. * if one is needed. Note that the caller must hold rnp->lock, as
  805. * required by rcu_start_gp(), which will release it.
  806. */
  807. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  808. __releases(rcu_get_root(rsp)->lock)
  809. {
  810. unsigned long gp_duration;
  811. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  812. /*
  813. * Ensure that all grace-period and pre-grace-period activity
  814. * is seen before the assignment to rsp->completed.
  815. */
  816. smp_mb(); /* See above block comment. */
  817. gp_duration = jiffies - rsp->gp_start;
  818. if (gp_duration > rsp->gp_max)
  819. rsp->gp_max = gp_duration;
  820. rsp->completed = rsp->gpnum;
  821. rsp->signaled = RCU_GP_IDLE;
  822. rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
  823. }
  824. /*
  825. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  826. * Allows quiescent states for a group of CPUs to be reported at one go
  827. * to the specified rcu_node structure, though all the CPUs in the group
  828. * must be represented by the same rcu_node structure (which need not be
  829. * a leaf rcu_node structure, though it often will be). That structure's
  830. * lock must be held upon entry, and it is released before return.
  831. */
  832. static void
  833. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  834. struct rcu_node *rnp, unsigned long flags)
  835. __releases(rnp->lock)
  836. {
  837. struct rcu_node *rnp_c;
  838. /* Walk up the rcu_node hierarchy. */
  839. for (;;) {
  840. if (!(rnp->qsmask & mask)) {
  841. /* Our bit has already been cleared, so done. */
  842. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  843. return;
  844. }
  845. rnp->qsmask &= ~mask;
  846. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  847. /* Other bits still set at this level, so done. */
  848. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  849. return;
  850. }
  851. mask = rnp->grpmask;
  852. if (rnp->parent == NULL) {
  853. /* No more levels. Exit loop holding root lock. */
  854. break;
  855. }
  856. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  857. rnp_c = rnp;
  858. rnp = rnp->parent;
  859. raw_spin_lock_irqsave(&rnp->lock, flags);
  860. WARN_ON_ONCE(rnp_c->qsmask);
  861. }
  862. /*
  863. * Get here if we are the last CPU to pass through a quiescent
  864. * state for this grace period. Invoke rcu_report_qs_rsp()
  865. * to clean up and start the next grace period if one is needed.
  866. */
  867. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  868. }
  869. /*
  870. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  871. * structure. This must be either called from the specified CPU, or
  872. * called when the specified CPU is known to be offline (and when it is
  873. * also known that no other CPU is concurrently trying to help the offline
  874. * CPU). The lastcomp argument is used to make sure we are still in the
  875. * grace period of interest. We don't want to end the current grace period
  876. * based on quiescent states detected in an earlier grace period!
  877. */
  878. static void
  879. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  880. {
  881. unsigned long flags;
  882. unsigned long mask;
  883. struct rcu_node *rnp;
  884. rnp = rdp->mynode;
  885. raw_spin_lock_irqsave(&rnp->lock, flags);
  886. if (lastcomp != rnp->completed) {
  887. /*
  888. * Someone beat us to it for this grace period, so leave.
  889. * The race with GP start is resolved by the fact that we
  890. * hold the leaf rcu_node lock, so that the per-CPU bits
  891. * cannot yet be initialized -- so we would simply find our
  892. * CPU's bit already cleared in rcu_report_qs_rnp() if this
  893. * race occurred.
  894. */
  895. rdp->passed_quiesc = 0; /* try again later! */
  896. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  897. return;
  898. }
  899. mask = rdp->grpmask;
  900. if ((rnp->qsmask & mask) == 0) {
  901. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  902. } else {
  903. rdp->qs_pending = 0;
  904. /*
  905. * This GP can't end until cpu checks in, so all of our
  906. * callbacks can be processed during the next GP.
  907. */
  908. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  909. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  910. }
  911. }
  912. /*
  913. * Check to see if there is a new grace period of which this CPU
  914. * is not yet aware, and if so, set up local rcu_data state for it.
  915. * Otherwise, see if this CPU has just passed through its first
  916. * quiescent state for this grace period, and record that fact if so.
  917. */
  918. static void
  919. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  920. {
  921. /* If there is now a new grace period, record and return. */
  922. if (check_for_new_grace_period(rsp, rdp))
  923. return;
  924. /*
  925. * Does this CPU still need to do its part for current grace period?
  926. * If no, return and let the other CPUs do their part as well.
  927. */
  928. if (!rdp->qs_pending)
  929. return;
  930. /*
  931. * Was there a quiescent state since the beginning of the grace
  932. * period? If no, then exit and wait for the next call.
  933. */
  934. if (!rdp->passed_quiesc)
  935. return;
  936. /*
  937. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  938. * judge of that).
  939. */
  940. rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  941. }
  942. #ifdef CONFIG_HOTPLUG_CPU
  943. /*
  944. * Move a dying CPU's RCU callbacks to online CPU's callback list.
  945. * Synchronization is not required because this function executes
  946. * in stop_machine() context.
  947. */
  948. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  949. {
  950. int i;
  951. /* current DYING CPU is cleared in the cpu_online_mask */
  952. int receive_cpu = cpumask_any(cpu_online_mask);
  953. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  954. struct rcu_data *receive_rdp = per_cpu_ptr(rsp->rda, receive_cpu);
  955. if (rdp->nxtlist == NULL)
  956. return; /* irqs disabled, so comparison is stable. */
  957. *receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
  958. receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  959. receive_rdp->qlen += rdp->qlen;
  960. receive_rdp->n_cbs_adopted += rdp->qlen;
  961. rdp->n_cbs_orphaned += rdp->qlen;
  962. rdp->nxtlist = NULL;
  963. for (i = 0; i < RCU_NEXT_SIZE; i++)
  964. rdp->nxttail[i] = &rdp->nxtlist;
  965. rdp->qlen = 0;
  966. }
  967. /*
  968. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  969. * and move all callbacks from the outgoing CPU to the current one.
  970. * There can only be one CPU hotplug operation at a time, so no other
  971. * CPU can be attempting to update rcu_cpu_kthread_task.
  972. */
  973. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  974. {
  975. unsigned long flags;
  976. unsigned long mask;
  977. int need_report = 0;
  978. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  979. struct rcu_node *rnp;
  980. rcu_stop_cpu_kthread(cpu);
  981. /* Exclude any attempts to start a new grace period. */
  982. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  983. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  984. rnp = rdp->mynode; /* this is the outgoing CPU's rnp. */
  985. mask = rdp->grpmask; /* rnp->grplo is constant. */
  986. do {
  987. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  988. rnp->qsmaskinit &= ~mask;
  989. if (rnp->qsmaskinit != 0) {
  990. if (rnp != rdp->mynode)
  991. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  992. break;
  993. }
  994. if (rnp == rdp->mynode)
  995. need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  996. else
  997. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  998. mask = rnp->grpmask;
  999. rnp = rnp->parent;
  1000. } while (rnp != NULL);
  1001. /*
  1002. * We still hold the leaf rcu_node structure lock here, and
  1003. * irqs are still disabled. The reason for this subterfuge is
  1004. * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
  1005. * held leads to deadlock.
  1006. */
  1007. raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  1008. rnp = rdp->mynode;
  1009. if (need_report & RCU_OFL_TASKS_NORM_GP)
  1010. rcu_report_unblock_qs_rnp(rnp, flags);
  1011. else
  1012. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1013. if (need_report & RCU_OFL_TASKS_EXP_GP)
  1014. rcu_report_exp_rnp(rsp, rnp);
  1015. rcu_node_kthread_setaffinity(rnp, -1);
  1016. }
  1017. /*
  1018. * Remove the specified CPU from the RCU hierarchy and move any pending
  1019. * callbacks that it might have to the current CPU. This code assumes
  1020. * that at least one CPU in the system will remain running at all times.
  1021. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  1022. */
  1023. static void rcu_offline_cpu(int cpu)
  1024. {
  1025. __rcu_offline_cpu(cpu, &rcu_sched_state);
  1026. __rcu_offline_cpu(cpu, &rcu_bh_state);
  1027. rcu_preempt_offline_cpu(cpu);
  1028. }
  1029. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1030. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  1031. {
  1032. }
  1033. static void rcu_offline_cpu(int cpu)
  1034. {
  1035. }
  1036. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1037. /*
  1038. * Invoke any RCU callbacks that have made it to the end of their grace
  1039. * period. Thottle as specified by rdp->blimit.
  1040. */
  1041. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  1042. {
  1043. unsigned long flags;
  1044. struct rcu_head *next, *list, **tail;
  1045. int count;
  1046. /* If no callbacks are ready, just return.*/
  1047. if (!cpu_has_callbacks_ready_to_invoke(rdp))
  1048. return;
  1049. /*
  1050. * Extract the list of ready callbacks, disabling to prevent
  1051. * races with call_rcu() from interrupt handlers.
  1052. */
  1053. local_irq_save(flags);
  1054. list = rdp->nxtlist;
  1055. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  1056. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1057. tail = rdp->nxttail[RCU_DONE_TAIL];
  1058. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  1059. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  1060. rdp->nxttail[count] = &rdp->nxtlist;
  1061. local_irq_restore(flags);
  1062. /* Invoke callbacks. */
  1063. count = 0;
  1064. while (list) {
  1065. next = list->next;
  1066. prefetch(next);
  1067. debug_rcu_head_unqueue(list);
  1068. __rcu_reclaim(list);
  1069. list = next;
  1070. if (++count >= rdp->blimit)
  1071. break;
  1072. }
  1073. local_irq_save(flags);
  1074. /* Update count, and requeue any remaining callbacks. */
  1075. rdp->qlen -= count;
  1076. rdp->n_cbs_invoked += count;
  1077. if (list != NULL) {
  1078. *tail = rdp->nxtlist;
  1079. rdp->nxtlist = list;
  1080. for (count = 0; count < RCU_NEXT_SIZE; count++)
  1081. if (&rdp->nxtlist == rdp->nxttail[count])
  1082. rdp->nxttail[count] = tail;
  1083. else
  1084. break;
  1085. }
  1086. /* Reinstate batch limit if we have worked down the excess. */
  1087. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  1088. rdp->blimit = blimit;
  1089. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  1090. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  1091. rdp->qlen_last_fqs_check = 0;
  1092. rdp->n_force_qs_snap = rsp->n_force_qs;
  1093. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  1094. rdp->qlen_last_fqs_check = rdp->qlen;
  1095. local_irq_restore(flags);
  1096. /* Re-raise the RCU softirq if there are callbacks remaining. */
  1097. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1098. invoke_rcu_core();
  1099. }
  1100. /*
  1101. * Check to see if this CPU is in a non-context-switch quiescent state
  1102. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  1103. * Also schedule the RCU softirq handler.
  1104. *
  1105. * This function must be called with hardirqs disabled. It is normally
  1106. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  1107. * false, there is no point in invoking rcu_check_callbacks().
  1108. */
  1109. void rcu_check_callbacks(int cpu, int user)
  1110. {
  1111. if (user ||
  1112. (idle_cpu(cpu) && rcu_scheduler_active &&
  1113. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  1114. /*
  1115. * Get here if this CPU took its interrupt from user
  1116. * mode or from the idle loop, and if this is not a
  1117. * nested interrupt. In this case, the CPU is in
  1118. * a quiescent state, so note it.
  1119. *
  1120. * No memory barrier is required here because both
  1121. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  1122. * variables that other CPUs neither access nor modify,
  1123. * at least not while the corresponding CPU is online.
  1124. */
  1125. rcu_sched_qs(cpu);
  1126. rcu_bh_qs(cpu);
  1127. } else if (!in_softirq()) {
  1128. /*
  1129. * Get here if this CPU did not take its interrupt from
  1130. * softirq, in other words, if it is not interrupting
  1131. * a rcu_bh read-side critical section. This is an _bh
  1132. * critical section, so note it.
  1133. */
  1134. rcu_bh_qs(cpu);
  1135. }
  1136. rcu_preempt_check_callbacks(cpu);
  1137. if (rcu_pending(cpu))
  1138. invoke_rcu_core();
  1139. }
  1140. #ifdef CONFIG_SMP
  1141. /*
  1142. * Scan the leaf rcu_node structures, processing dyntick state for any that
  1143. * have not yet encountered a quiescent state, using the function specified.
  1144. * Also initiate boosting for any threads blocked on the root rcu_node.
  1145. *
  1146. * The caller must have suppressed start of new grace periods.
  1147. */
  1148. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
  1149. {
  1150. unsigned long bit;
  1151. int cpu;
  1152. unsigned long flags;
  1153. unsigned long mask;
  1154. struct rcu_node *rnp;
  1155. rcu_for_each_leaf_node(rsp, rnp) {
  1156. mask = 0;
  1157. raw_spin_lock_irqsave(&rnp->lock, flags);
  1158. if (!rcu_gp_in_progress(rsp)) {
  1159. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1160. return;
  1161. }
  1162. if (rnp->qsmask == 0) {
  1163. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  1164. continue;
  1165. }
  1166. cpu = rnp->grplo;
  1167. bit = 1;
  1168. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1169. if ((rnp->qsmask & bit) != 0 &&
  1170. f(per_cpu_ptr(rsp->rda, cpu)))
  1171. mask |= bit;
  1172. }
  1173. if (mask != 0) {
  1174. /* rcu_report_qs_rnp() releases rnp->lock. */
  1175. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1176. continue;
  1177. }
  1178. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1179. }
  1180. rnp = rcu_get_root(rsp);
  1181. if (rnp->qsmask == 0) {
  1182. raw_spin_lock_irqsave(&rnp->lock, flags);
  1183. rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
  1184. }
  1185. }
  1186. /*
  1187. * Force quiescent states on reluctant CPUs, and also detect which
  1188. * CPUs are in dyntick-idle mode.
  1189. */
  1190. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1191. {
  1192. unsigned long flags;
  1193. struct rcu_node *rnp = rcu_get_root(rsp);
  1194. if (!rcu_gp_in_progress(rsp))
  1195. return; /* No grace period in progress, nothing to force. */
  1196. if (!raw_spin_trylock_irqsave(&rsp->fqslock, flags)) {
  1197. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  1198. return; /* Someone else is already on the job. */
  1199. }
  1200. if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
  1201. goto unlock_fqs_ret; /* no emergency and done recently. */
  1202. rsp->n_force_qs++;
  1203. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1204. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  1205. if(!rcu_gp_in_progress(rsp)) {
  1206. rsp->n_force_qs_ngp++;
  1207. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1208. goto unlock_fqs_ret; /* no GP in progress, time updated. */
  1209. }
  1210. rsp->fqs_active = 1;
  1211. switch (rsp->signaled) {
  1212. case RCU_GP_IDLE:
  1213. case RCU_GP_INIT:
  1214. break; /* grace period idle or initializing, ignore. */
  1215. case RCU_SAVE_DYNTICK:
  1216. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  1217. break; /* So gcc recognizes the dead code. */
  1218. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1219. /* Record dyntick-idle state. */
  1220. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1221. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1222. if (rcu_gp_in_progress(rsp))
  1223. rsp->signaled = RCU_FORCE_QS;
  1224. break;
  1225. case RCU_FORCE_QS:
  1226. /* Check dyntick-idle state, send IPI to laggarts. */
  1227. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1228. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1229. /* Leave state in case more forcing is required. */
  1230. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1231. break;
  1232. }
  1233. rsp->fqs_active = 0;
  1234. if (rsp->fqs_need_gp) {
  1235. raw_spin_unlock(&rsp->fqslock); /* irqs remain disabled */
  1236. rsp->fqs_need_gp = 0;
  1237. rcu_start_gp(rsp, flags); /* releases rnp->lock */
  1238. return;
  1239. }
  1240. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1241. unlock_fqs_ret:
  1242. raw_spin_unlock_irqrestore(&rsp->fqslock, flags);
  1243. }
  1244. #else /* #ifdef CONFIG_SMP */
  1245. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1246. {
  1247. set_need_resched();
  1248. }
  1249. #endif /* #else #ifdef CONFIG_SMP */
  1250. /*
  1251. * This does the RCU processing work from softirq context for the
  1252. * specified rcu_state and rcu_data structures. This may be called
  1253. * only from the CPU to whom the rdp belongs.
  1254. */
  1255. static void
  1256. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1257. {
  1258. unsigned long flags;
  1259. WARN_ON_ONCE(rdp->beenonline == 0);
  1260. /*
  1261. * If an RCU GP has gone long enough, go check for dyntick
  1262. * idle CPUs and, if needed, send resched IPIs.
  1263. */
  1264. if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1265. force_quiescent_state(rsp, 1);
  1266. /*
  1267. * Advance callbacks in response to end of earlier grace
  1268. * period that some other CPU ended.
  1269. */
  1270. rcu_process_gp_end(rsp, rdp);
  1271. /* Update RCU state based on any recent quiescent states. */
  1272. rcu_check_quiescent_state(rsp, rdp);
  1273. /* Does this CPU require a not-yet-started grace period? */
  1274. if (cpu_needs_another_gp(rsp, rdp)) {
  1275. raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1276. rcu_start_gp(rsp, flags); /* releases above lock */
  1277. }
  1278. /* If there are callbacks ready, invoke them. */
  1279. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1280. invoke_rcu_callbacks(rsp, rdp);
  1281. }
  1282. /*
  1283. * Do softirq processing for the current CPU.
  1284. */
  1285. static void rcu_process_callbacks(struct softirq_action *unused)
  1286. {
  1287. __rcu_process_callbacks(&rcu_sched_state,
  1288. &__get_cpu_var(rcu_sched_data));
  1289. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1290. rcu_preempt_process_callbacks();
  1291. /* If we are last CPU on way to dyntick-idle mode, accelerate it. */
  1292. rcu_needs_cpu_flush();
  1293. }
  1294. /*
  1295. * Wake up the current CPU's kthread. This replaces raise_softirq()
  1296. * in earlier versions of RCU. Note that because we are running on
  1297. * the current CPU with interrupts disabled, the rcu_cpu_kthread_task
  1298. * cannot disappear out from under us.
  1299. */
  1300. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1301. {
  1302. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  1303. return;
  1304. if (likely(!rsp->boost)) {
  1305. rcu_do_batch(rsp, rdp);
  1306. return;
  1307. }
  1308. invoke_rcu_callbacks_kthread();
  1309. }
  1310. static void invoke_rcu_core(void)
  1311. {
  1312. raise_softirq(RCU_SOFTIRQ);
  1313. }
  1314. static void
  1315. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1316. struct rcu_state *rsp)
  1317. {
  1318. unsigned long flags;
  1319. struct rcu_data *rdp;
  1320. debug_rcu_head_queue(head);
  1321. head->func = func;
  1322. head->next = NULL;
  1323. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1324. /*
  1325. * Opportunistically note grace-period endings and beginnings.
  1326. * Note that we might see a beginning right after we see an
  1327. * end, but never vice versa, since this CPU has to pass through
  1328. * a quiescent state betweentimes.
  1329. */
  1330. local_irq_save(flags);
  1331. rdp = this_cpu_ptr(rsp->rda);
  1332. /* Add the callback to our list. */
  1333. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1334. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1335. rdp->qlen++;
  1336. /* If interrupts were disabled, don't dive into RCU core. */
  1337. if (irqs_disabled_flags(flags)) {
  1338. local_irq_restore(flags);
  1339. return;
  1340. }
  1341. /*
  1342. * Force the grace period if too many callbacks or too long waiting.
  1343. * Enforce hysteresis, and don't invoke force_quiescent_state()
  1344. * if some other CPU has recently done so. Also, don't bother
  1345. * invoking force_quiescent_state() if the newly enqueued callback
  1346. * is the only one waiting for a grace period to complete.
  1347. */
  1348. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  1349. /* Are we ignoring a completed grace period? */
  1350. rcu_process_gp_end(rsp, rdp);
  1351. check_for_new_grace_period(rsp, rdp);
  1352. /* Start a new grace period if one not already started. */
  1353. if (!rcu_gp_in_progress(rsp)) {
  1354. unsigned long nestflag;
  1355. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1356. raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
  1357. rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
  1358. } else {
  1359. /* Give the grace period a kick. */
  1360. rdp->blimit = LONG_MAX;
  1361. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  1362. *rdp->nxttail[RCU_DONE_TAIL] != head)
  1363. force_quiescent_state(rsp, 0);
  1364. rdp->n_force_qs_snap = rsp->n_force_qs;
  1365. rdp->qlen_last_fqs_check = rdp->qlen;
  1366. }
  1367. } else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1368. force_quiescent_state(rsp, 1);
  1369. local_irq_restore(flags);
  1370. }
  1371. /*
  1372. * Queue an RCU-sched callback for invocation after a grace period.
  1373. */
  1374. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1375. {
  1376. __call_rcu(head, func, &rcu_sched_state);
  1377. }
  1378. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1379. /*
  1380. * Queue an RCU for invocation after a quicker grace period.
  1381. */
  1382. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1383. {
  1384. __call_rcu(head, func, &rcu_bh_state);
  1385. }
  1386. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1387. /**
  1388. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  1389. *
  1390. * Control will return to the caller some time after a full rcu-sched
  1391. * grace period has elapsed, in other words after all currently executing
  1392. * rcu-sched read-side critical sections have completed. These read-side
  1393. * critical sections are delimited by rcu_read_lock_sched() and
  1394. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  1395. * local_irq_disable(), and so on may be used in place of
  1396. * rcu_read_lock_sched().
  1397. *
  1398. * This means that all preempt_disable code sequences, including NMI and
  1399. * hardware-interrupt handlers, in progress on entry will have completed
  1400. * before this primitive returns. However, this does not guarantee that
  1401. * softirq handlers will have completed, since in some kernels, these
  1402. * handlers can run in process context, and can block.
  1403. *
  1404. * This primitive provides the guarantees made by the (now removed)
  1405. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  1406. * guarantees that rcu_read_lock() sections will have completed.
  1407. * In "classic RCU", these two guarantees happen to be one and
  1408. * the same, but can differ in realtime RCU implementations.
  1409. */
  1410. void synchronize_sched(void)
  1411. {
  1412. struct rcu_synchronize rcu;
  1413. if (rcu_blocking_is_gp())
  1414. return;
  1415. init_rcu_head_on_stack(&rcu.head);
  1416. init_completion(&rcu.completion);
  1417. /* Will wake me after RCU finished. */
  1418. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  1419. /* Wait for it. */
  1420. wait_for_completion(&rcu.completion);
  1421. destroy_rcu_head_on_stack(&rcu.head);
  1422. }
  1423. EXPORT_SYMBOL_GPL(synchronize_sched);
  1424. /**
  1425. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  1426. *
  1427. * Control will return to the caller some time after a full rcu_bh grace
  1428. * period has elapsed, in other words after all currently executing rcu_bh
  1429. * read-side critical sections have completed. RCU read-side critical
  1430. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  1431. * and may be nested.
  1432. */
  1433. void synchronize_rcu_bh(void)
  1434. {
  1435. struct rcu_synchronize rcu;
  1436. if (rcu_blocking_is_gp())
  1437. return;
  1438. init_rcu_head_on_stack(&rcu.head);
  1439. init_completion(&rcu.completion);
  1440. /* Will wake me after RCU finished. */
  1441. call_rcu_bh(&rcu.head, wakeme_after_rcu);
  1442. /* Wait for it. */
  1443. wait_for_completion(&rcu.completion);
  1444. destroy_rcu_head_on_stack(&rcu.head);
  1445. }
  1446. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  1447. /*
  1448. * Check to see if there is any immediate RCU-related work to be done
  1449. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1450. * The checks are in order of increasing expense: checks that can be
  1451. * carried out against CPU-local state are performed first. However,
  1452. * we must check for CPU stalls first, else we might not get a chance.
  1453. */
  1454. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1455. {
  1456. struct rcu_node *rnp = rdp->mynode;
  1457. rdp->n_rcu_pending++;
  1458. /* Check for CPU stalls, if enabled. */
  1459. check_cpu_stall(rsp, rdp);
  1460. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1461. if (rdp->qs_pending && !rdp->passed_quiesc) {
  1462. /*
  1463. * If force_quiescent_state() coming soon and this CPU
  1464. * needs a quiescent state, and this is either RCU-sched
  1465. * or RCU-bh, force a local reschedule.
  1466. */
  1467. rdp->n_rp_qs_pending++;
  1468. if (!rdp->preemptible &&
  1469. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs) - 1,
  1470. jiffies))
  1471. set_need_resched();
  1472. } else if (rdp->qs_pending && rdp->passed_quiesc) {
  1473. rdp->n_rp_report_qs++;
  1474. return 1;
  1475. }
  1476. /* Does this CPU have callbacks ready to invoke? */
  1477. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  1478. rdp->n_rp_cb_ready++;
  1479. return 1;
  1480. }
  1481. /* Has RCU gone idle with this CPU needing another grace period? */
  1482. if (cpu_needs_another_gp(rsp, rdp)) {
  1483. rdp->n_rp_cpu_needs_gp++;
  1484. return 1;
  1485. }
  1486. /* Has another RCU grace period completed? */
  1487. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  1488. rdp->n_rp_gp_completed++;
  1489. return 1;
  1490. }
  1491. /* Has a new RCU grace period started? */
  1492. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  1493. rdp->n_rp_gp_started++;
  1494. return 1;
  1495. }
  1496. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1497. if (rcu_gp_in_progress(rsp) &&
  1498. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
  1499. rdp->n_rp_need_fqs++;
  1500. return 1;
  1501. }
  1502. /* nothing to do */
  1503. rdp->n_rp_need_nothing++;
  1504. return 0;
  1505. }
  1506. /*
  1507. * Check to see if there is any immediate RCU-related work to be done
  1508. * by the current CPU, returning 1 if so. This function is part of the
  1509. * RCU implementation; it is -not- an exported member of the RCU API.
  1510. */
  1511. static int rcu_pending(int cpu)
  1512. {
  1513. return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
  1514. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
  1515. rcu_preempt_pending(cpu);
  1516. }
  1517. /*
  1518. * Check to see if any future RCU-related work will need to be done
  1519. * by the current CPU, even if none need be done immediately, returning
  1520. * 1 if so.
  1521. */
  1522. static int rcu_needs_cpu_quick_check(int cpu)
  1523. {
  1524. /* RCU callbacks either ready or pending? */
  1525. return per_cpu(rcu_sched_data, cpu).nxtlist ||
  1526. per_cpu(rcu_bh_data, cpu).nxtlist ||
  1527. rcu_preempt_needs_cpu(cpu);
  1528. }
  1529. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  1530. static atomic_t rcu_barrier_cpu_count;
  1531. static DEFINE_MUTEX(rcu_barrier_mutex);
  1532. static struct completion rcu_barrier_completion;
  1533. static void rcu_barrier_callback(struct rcu_head *notused)
  1534. {
  1535. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1536. complete(&rcu_barrier_completion);
  1537. }
  1538. /*
  1539. * Called with preemption disabled, and from cross-cpu IRQ context.
  1540. */
  1541. static void rcu_barrier_func(void *type)
  1542. {
  1543. int cpu = smp_processor_id();
  1544. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  1545. void (*call_rcu_func)(struct rcu_head *head,
  1546. void (*func)(struct rcu_head *head));
  1547. atomic_inc(&rcu_barrier_cpu_count);
  1548. call_rcu_func = type;
  1549. call_rcu_func(head, rcu_barrier_callback);
  1550. }
  1551. /*
  1552. * Orchestrate the specified type of RCU barrier, waiting for all
  1553. * RCU callbacks of the specified type to complete.
  1554. */
  1555. static void _rcu_barrier(struct rcu_state *rsp,
  1556. void (*call_rcu_func)(struct rcu_head *head,
  1557. void (*func)(struct rcu_head *head)))
  1558. {
  1559. BUG_ON(in_interrupt());
  1560. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  1561. mutex_lock(&rcu_barrier_mutex);
  1562. init_completion(&rcu_barrier_completion);
  1563. /*
  1564. * Initialize rcu_barrier_cpu_count to 1, then invoke
  1565. * rcu_barrier_func() on each CPU, so that each CPU also has
  1566. * incremented rcu_barrier_cpu_count. Only then is it safe to
  1567. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  1568. * might complete its grace period before all of the other CPUs
  1569. * did their increment, causing this function to return too
  1570. * early. Note that on_each_cpu() disables irqs, which prevents
  1571. * any CPUs from coming online or going offline until each online
  1572. * CPU has queued its RCU-barrier callback.
  1573. */
  1574. atomic_set(&rcu_barrier_cpu_count, 1);
  1575. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  1576. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1577. complete(&rcu_barrier_completion);
  1578. wait_for_completion(&rcu_barrier_completion);
  1579. mutex_unlock(&rcu_barrier_mutex);
  1580. }
  1581. /**
  1582. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  1583. */
  1584. void rcu_barrier_bh(void)
  1585. {
  1586. _rcu_barrier(&rcu_bh_state, call_rcu_bh);
  1587. }
  1588. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  1589. /**
  1590. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  1591. */
  1592. void rcu_barrier_sched(void)
  1593. {
  1594. _rcu_barrier(&rcu_sched_state, call_rcu_sched);
  1595. }
  1596. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  1597. /*
  1598. * Do boot-time initialization of a CPU's per-CPU RCU data.
  1599. */
  1600. static void __init
  1601. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  1602. {
  1603. unsigned long flags;
  1604. int i;
  1605. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1606. struct rcu_node *rnp = rcu_get_root(rsp);
  1607. /* Set up local state, ensuring consistent view of global state. */
  1608. raw_spin_lock_irqsave(&rnp->lock, flags);
  1609. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1610. rdp->nxtlist = NULL;
  1611. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1612. rdp->nxttail[i] = &rdp->nxtlist;
  1613. rdp->qlen = 0;
  1614. #ifdef CONFIG_NO_HZ
  1615. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1616. #endif /* #ifdef CONFIG_NO_HZ */
  1617. rdp->cpu = cpu;
  1618. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1619. }
  1620. /*
  1621. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  1622. * offline event can be happening at a given time. Note also that we
  1623. * can accept some slop in the rsp->completed access due to the fact
  1624. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  1625. */
  1626. static void __cpuinit
  1627. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
  1628. {
  1629. unsigned long flags;
  1630. unsigned long mask;
  1631. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1632. struct rcu_node *rnp = rcu_get_root(rsp);
  1633. /* Set up local state, ensuring consistent view of global state. */
  1634. raw_spin_lock_irqsave(&rnp->lock, flags);
  1635. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1636. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1637. rdp->beenonline = 1; /* We have now been online. */
  1638. rdp->preemptible = preemptible;
  1639. rdp->qlen_last_fqs_check = 0;
  1640. rdp->n_force_qs_snap = rsp->n_force_qs;
  1641. rdp->blimit = blimit;
  1642. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1643. /*
  1644. * A new grace period might start here. If so, we won't be part
  1645. * of it, but that is OK, as we are currently in a quiescent state.
  1646. */
  1647. /* Exclude any attempts to start a new GP on large systems. */
  1648. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1649. /* Add CPU to rcu_node bitmasks. */
  1650. rnp = rdp->mynode;
  1651. mask = rdp->grpmask;
  1652. do {
  1653. /* Exclude any attempts to start a new GP on small systems. */
  1654. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1655. rnp->qsmaskinit |= mask;
  1656. mask = rnp->grpmask;
  1657. if (rnp == rdp->mynode) {
  1658. rdp->gpnum = rnp->completed; /* if GP in progress... */
  1659. rdp->completed = rnp->completed;
  1660. rdp->passed_quiesc_completed = rnp->completed - 1;
  1661. }
  1662. raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
  1663. rnp = rnp->parent;
  1664. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1665. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  1666. }
  1667. static void __cpuinit rcu_prepare_cpu(int cpu)
  1668. {
  1669. rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
  1670. rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
  1671. rcu_preempt_init_percpu_data(cpu);
  1672. }
  1673. /*
  1674. * Handle CPU online/offline notification events.
  1675. */
  1676. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1677. unsigned long action, void *hcpu)
  1678. {
  1679. long cpu = (long)hcpu;
  1680. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1681. struct rcu_node *rnp = rdp->mynode;
  1682. switch (action) {
  1683. case CPU_UP_PREPARE:
  1684. case CPU_UP_PREPARE_FROZEN:
  1685. rcu_prepare_cpu(cpu);
  1686. rcu_prepare_kthreads(cpu);
  1687. break;
  1688. case CPU_ONLINE:
  1689. case CPU_DOWN_FAILED:
  1690. rcu_node_kthread_setaffinity(rnp, -1);
  1691. rcu_cpu_kthread_setrt(cpu, 1);
  1692. break;
  1693. case CPU_DOWN_PREPARE:
  1694. rcu_node_kthread_setaffinity(rnp, cpu);
  1695. rcu_cpu_kthread_setrt(cpu, 0);
  1696. break;
  1697. case CPU_DYING:
  1698. case CPU_DYING_FROZEN:
  1699. /*
  1700. * The whole machine is "stopped" except this CPU, so we can
  1701. * touch any data without introducing corruption. We send the
  1702. * dying CPU's callbacks to an arbitrarily chosen online CPU.
  1703. */
  1704. rcu_send_cbs_to_online(&rcu_bh_state);
  1705. rcu_send_cbs_to_online(&rcu_sched_state);
  1706. rcu_preempt_send_cbs_to_online();
  1707. break;
  1708. case CPU_DEAD:
  1709. case CPU_DEAD_FROZEN:
  1710. case CPU_UP_CANCELED:
  1711. case CPU_UP_CANCELED_FROZEN:
  1712. rcu_offline_cpu(cpu);
  1713. break;
  1714. default:
  1715. break;
  1716. }
  1717. return NOTIFY_OK;
  1718. }
  1719. /*
  1720. * This function is invoked towards the end of the scheduler's initialization
  1721. * process. Before this is called, the idle task might contain
  1722. * RCU read-side critical sections (during which time, this idle
  1723. * task is booting the system). After this function is called, the
  1724. * idle tasks are prohibited from containing RCU read-side critical
  1725. * sections. This function also enables RCU lockdep checking.
  1726. */
  1727. void rcu_scheduler_starting(void)
  1728. {
  1729. WARN_ON(num_online_cpus() != 1);
  1730. WARN_ON(nr_context_switches() > 0);
  1731. rcu_scheduler_active = 1;
  1732. }
  1733. /*
  1734. * Compute the per-level fanout, either using the exact fanout specified
  1735. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  1736. */
  1737. #ifdef CONFIG_RCU_FANOUT_EXACT
  1738. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1739. {
  1740. int i;
  1741. for (i = NUM_RCU_LVLS - 1; i > 0; i--)
  1742. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  1743. rsp->levelspread[0] = RCU_FANOUT_LEAF;
  1744. }
  1745. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  1746. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1747. {
  1748. int ccur;
  1749. int cprv;
  1750. int i;
  1751. cprv = NR_CPUS;
  1752. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1753. ccur = rsp->levelcnt[i];
  1754. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  1755. cprv = ccur;
  1756. }
  1757. }
  1758. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  1759. /*
  1760. * Helper function for rcu_init() that initializes one rcu_state structure.
  1761. */
  1762. static void __init rcu_init_one(struct rcu_state *rsp,
  1763. struct rcu_data __percpu *rda)
  1764. {
  1765. static char *buf[] = { "rcu_node_level_0",
  1766. "rcu_node_level_1",
  1767. "rcu_node_level_2",
  1768. "rcu_node_level_3" }; /* Match MAX_RCU_LVLS */
  1769. int cpustride = 1;
  1770. int i;
  1771. int j;
  1772. struct rcu_node *rnp;
  1773. BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  1774. /* Initialize the level-tracking arrays. */
  1775. for (i = 1; i < NUM_RCU_LVLS; i++)
  1776. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  1777. rcu_init_levelspread(rsp);
  1778. /* Initialize the elements themselves, starting from the leaves. */
  1779. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1780. cpustride *= rsp->levelspread[i];
  1781. rnp = rsp->level[i];
  1782. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  1783. raw_spin_lock_init(&rnp->lock);
  1784. lockdep_set_class_and_name(&rnp->lock,
  1785. &rcu_node_class[i], buf[i]);
  1786. rnp->gpnum = 0;
  1787. rnp->qsmask = 0;
  1788. rnp->qsmaskinit = 0;
  1789. rnp->grplo = j * cpustride;
  1790. rnp->grphi = (j + 1) * cpustride - 1;
  1791. if (rnp->grphi >= NR_CPUS)
  1792. rnp->grphi = NR_CPUS - 1;
  1793. if (i == 0) {
  1794. rnp->grpnum = 0;
  1795. rnp->grpmask = 0;
  1796. rnp->parent = NULL;
  1797. } else {
  1798. rnp->grpnum = j % rsp->levelspread[i - 1];
  1799. rnp->grpmask = 1UL << rnp->grpnum;
  1800. rnp->parent = rsp->level[i - 1] +
  1801. j / rsp->levelspread[i - 1];
  1802. }
  1803. rnp->level = i;
  1804. INIT_LIST_HEAD(&rnp->blkd_tasks);
  1805. }
  1806. }
  1807. rsp->rda = rda;
  1808. rnp = rsp->level[NUM_RCU_LVLS - 1];
  1809. for_each_possible_cpu(i) {
  1810. while (i > rnp->grphi)
  1811. rnp++;
  1812. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  1813. rcu_boot_init_percpu_data(i, rsp);
  1814. }
  1815. }
  1816. void __init rcu_init(void)
  1817. {
  1818. int cpu;
  1819. rcu_bootup_announce();
  1820. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  1821. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  1822. __rcu_init_preempt();
  1823. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1824. /*
  1825. * We don't need protection against CPU-hotplug here because
  1826. * this is called early in boot, before either interrupts
  1827. * or the scheduler are operational.
  1828. */
  1829. cpu_notifier(rcu_cpu_notify, 0);
  1830. for_each_online_cpu(cpu)
  1831. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  1832. check_cpu_stall_init();
  1833. }
  1834. #include "rcutree_plugin.h"