rcutree.c 44 KB

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