rcutree.c 46 KB

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