rcutree.c 55 KB

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