rcutree.c 48 KB

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