rcutree.c 57 KB

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