rcutree.c 51 KB

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