rcutree.c 47 KB

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