rcutree.c 44 KB

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