rcutree.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
  23. *
  24. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  25. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  26. *
  27. * For detailed explanation of Read-Copy Update mechanism see -
  28. * Documentation/RCU
  29. */
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/smp.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <linux/nmi.h>
  39. #include <asm/atomic.h>
  40. #include <linux/bitops.h>
  41. #include <linux/module.h>
  42. #include <linux/completion.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/percpu.h>
  45. #include <linux/notifier.h>
  46. #include <linux/cpu.h>
  47. #include <linux/mutex.h>
  48. #include <linux/time.h>
  49. #include <linux/kernel_stat.h>
  50. #include <linux/wait.h>
  51. #include <linux/kthread.h>
  52. #include <linux/prefetch.h>
  53. #include "rcutree.h"
  54. /* Data structures. */
  55. static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
  56. #define RCU_STATE_INITIALIZER(structname) { \
  57. .level = { &structname.node[0] }, \
  58. .levelcnt = { \
  59. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  60. NUM_RCU_LVL_1, \
  61. NUM_RCU_LVL_2, \
  62. NUM_RCU_LVL_3, \
  63. NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
  64. }, \
  65. .signaled = RCU_GP_IDLE, \
  66. .gpnum = -300, \
  67. .completed = -300, \
  68. .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&structname.onofflock), \
  69. .fqslock = __RAW_SPIN_LOCK_UNLOCKED(&structname.fqslock), \
  70. .n_force_qs = 0, \
  71. .n_force_qs_ngp = 0, \
  72. .name = #structname, \
  73. }
  74. struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched_state);
  75. DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
  76. struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh_state);
  77. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
  78. static struct rcu_state *rcu_state;
  79. int rcu_scheduler_active __read_mostly;
  80. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  81. /*
  82. * Control variables for per-CPU and per-rcu_node kthreads. These
  83. * handle all flavors of RCU.
  84. */
  85. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  86. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  87. DEFINE_PER_CPU(int, rcu_cpu_kthread_cpu);
  88. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  89. static DEFINE_PER_CPU(wait_queue_head_t, rcu_cpu_wq);
  90. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  91. static char rcu_kthreads_spawnable;
  92. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  93. static void invoke_rcu_cpu_kthread(void);
  94. #define RCU_KTHREAD_PRIO 1 /* RT priority for per-CPU kthreads. */
  95. /*
  96. * Track the rcutorture test sequence number and the update version
  97. * number within a given test. The rcutorture_testseq is incremented
  98. * on every rcutorture module load and unload, so has an odd value
  99. * when a test is running. The rcutorture_vernum is set to zero
  100. * when rcutorture starts and is incremented on each rcutorture update.
  101. * These variables enable correlating rcutorture output with the
  102. * RCU tracing information.
  103. */
  104. unsigned long rcutorture_testseq;
  105. unsigned long rcutorture_vernum;
  106. /*
  107. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  108. * permit this function to be invoked without holding the root rcu_node
  109. * structure's ->lock, but of course results can be subject to change.
  110. */
  111. static int rcu_gp_in_progress(struct rcu_state *rsp)
  112. {
  113. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  114. }
  115. /*
  116. * Note a quiescent state. Because we do not need to know
  117. * how many quiescent states passed, just if there was at least
  118. * one since the start of the grace period, this just sets a flag.
  119. */
  120. void rcu_sched_qs(int cpu)
  121. {
  122. struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
  123. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  124. barrier();
  125. rdp->passed_quiesc = 1;
  126. }
  127. void rcu_bh_qs(int cpu)
  128. {
  129. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  130. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  131. barrier();
  132. rdp->passed_quiesc = 1;
  133. }
  134. /*
  135. * Note a context switch. This is a quiescent state for RCU-sched,
  136. * and requires special handling for preemptible RCU.
  137. */
  138. void rcu_note_context_switch(int cpu)
  139. {
  140. rcu_sched_qs(cpu);
  141. rcu_preempt_note_context_switch(cpu);
  142. }
  143. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  144. #ifdef CONFIG_NO_HZ
  145. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  146. .dynticks_nesting = 1,
  147. .dynticks = 1,
  148. };
  149. #endif /* #ifdef CONFIG_NO_HZ */
  150. static int blimit = 10; /* Maximum callbacks per softirq. */
  151. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  152. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  153. module_param(blimit, int, 0);
  154. module_param(qhimark, int, 0);
  155. module_param(qlowmark, int, 0);
  156. int rcu_cpu_stall_suppress __read_mostly;
  157. module_param(rcu_cpu_stall_suppress, int, 0644);
  158. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  159. static int rcu_pending(int cpu);
  160. /*
  161. * Return the number of RCU-sched batches processed thus far for debug & stats.
  162. */
  163. long rcu_batches_completed_sched(void)
  164. {
  165. return rcu_sched_state.completed;
  166. }
  167. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  168. /*
  169. * Return the number of RCU BH batches processed thus far for debug & stats.
  170. */
  171. long rcu_batches_completed_bh(void)
  172. {
  173. return rcu_bh_state.completed;
  174. }
  175. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  176. /*
  177. * Force a quiescent state for RCU BH.
  178. */
  179. void rcu_bh_force_quiescent_state(void)
  180. {
  181. force_quiescent_state(&rcu_bh_state, 0);
  182. }
  183. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  184. /*
  185. * Record the number of times rcutorture tests have been initiated and
  186. * terminated. This information allows the debugfs tracing stats to be
  187. * correlated to the rcutorture messages, even when the rcutorture module
  188. * is being repeatedly loaded and unloaded. In other words, we cannot
  189. * store this state in rcutorture itself.
  190. */
  191. void rcutorture_record_test_transition(void)
  192. {
  193. rcutorture_testseq++;
  194. rcutorture_vernum = 0;
  195. }
  196. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  197. /*
  198. * Record the number of writer passes through the current rcutorture test.
  199. * This is also used to correlate debugfs tracing stats with the rcutorture
  200. * messages.
  201. */
  202. void rcutorture_record_progress(unsigned long vernum)
  203. {
  204. rcutorture_vernum++;
  205. }
  206. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  207. /*
  208. * Force a quiescent state for RCU-sched.
  209. */
  210. void rcu_sched_force_quiescent_state(void)
  211. {
  212. force_quiescent_state(&rcu_sched_state, 0);
  213. }
  214. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  215. /*
  216. * Does the CPU have callbacks ready to be invoked?
  217. */
  218. static int
  219. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  220. {
  221. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  222. }
  223. /*
  224. * Does the current CPU require a yet-as-unscheduled grace period?
  225. */
  226. static int
  227. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  228. {
  229. return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
  230. }
  231. /*
  232. * Return the root node of the specified rcu_state structure.
  233. */
  234. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  235. {
  236. return &rsp->node[0];
  237. }
  238. #ifdef CONFIG_SMP
  239. /*
  240. * If the specified CPU is offline, tell the caller that it is in
  241. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  242. * Grace periods can end up waiting on an offline CPU when that
  243. * CPU is in the process of coming online -- it will be added to the
  244. * rcu_node bitmasks before it actually makes it online. The same thing
  245. * can happen while a CPU is in the process of coming online. Because this
  246. * race is quite rare, we check for it after detecting that the grace
  247. * period has been delayed rather than checking each and every CPU
  248. * each and every time we start a new grace period.
  249. */
  250. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  251. {
  252. /*
  253. * If the CPU is offline, it is in a quiescent state. We can
  254. * trust its state not to change because interrupts are disabled.
  255. */
  256. if (cpu_is_offline(rdp->cpu)) {
  257. rdp->offline_fqs++;
  258. return 1;
  259. }
  260. /* If preemptible RCU, no point in sending reschedule IPI. */
  261. if (rdp->preemptible)
  262. return 0;
  263. /* The CPU is online, so send it a reschedule IPI. */
  264. if (rdp->cpu != smp_processor_id())
  265. smp_send_reschedule(rdp->cpu);
  266. else
  267. set_need_resched();
  268. rdp->resched_ipi++;
  269. return 0;
  270. }
  271. #endif /* #ifdef CONFIG_SMP */
  272. #ifdef CONFIG_NO_HZ
  273. /**
  274. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  275. *
  276. * Enter nohz mode, in other words, -leave- the mode in which RCU
  277. * read-side critical sections can occur. (Though RCU read-side
  278. * critical sections can occur in irq handlers in nohz mode, a possibility
  279. * handled by rcu_irq_enter() and rcu_irq_exit()).
  280. */
  281. void rcu_enter_nohz(void)
  282. {
  283. unsigned long flags;
  284. struct rcu_dynticks *rdtp;
  285. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  286. local_irq_save(flags);
  287. rdtp = &__get_cpu_var(rcu_dynticks);
  288. rdtp->dynticks++;
  289. rdtp->dynticks_nesting--;
  290. WARN_ON_ONCE(rdtp->dynticks & 0x1);
  291. local_irq_restore(flags);
  292. }
  293. /*
  294. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  295. *
  296. * Exit nohz mode, in other words, -enter- the mode in which RCU
  297. * read-side critical sections normally occur.
  298. */
  299. void rcu_exit_nohz(void)
  300. {
  301. unsigned long flags;
  302. struct rcu_dynticks *rdtp;
  303. local_irq_save(flags);
  304. rdtp = &__get_cpu_var(rcu_dynticks);
  305. rdtp->dynticks++;
  306. rdtp->dynticks_nesting++;
  307. WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
  308. local_irq_restore(flags);
  309. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  310. }
  311. /**
  312. * rcu_nmi_enter - inform RCU of entry to NMI context
  313. *
  314. * If the CPU was idle with dynamic ticks active, and there is no
  315. * irq handler running, this updates rdtp->dynticks_nmi to let the
  316. * RCU grace-period handling know that the CPU is active.
  317. */
  318. void rcu_nmi_enter(void)
  319. {
  320. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  321. if (rdtp->dynticks & 0x1)
  322. return;
  323. rdtp->dynticks_nmi++;
  324. WARN_ON_ONCE(!(rdtp->dynticks_nmi & 0x1));
  325. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  326. }
  327. /**
  328. * rcu_nmi_exit - inform RCU of exit from NMI context
  329. *
  330. * If the CPU was idle with dynamic ticks active, and there is no
  331. * irq handler running, this updates rdtp->dynticks_nmi to let the
  332. * RCU grace-period handling know that the CPU is no longer active.
  333. */
  334. void rcu_nmi_exit(void)
  335. {
  336. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  337. if (rdtp->dynticks & 0x1)
  338. return;
  339. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  340. rdtp->dynticks_nmi++;
  341. WARN_ON_ONCE(rdtp->dynticks_nmi & 0x1);
  342. }
  343. /**
  344. * rcu_irq_enter - inform RCU of entry to hard irq context
  345. *
  346. * If the CPU was idle with dynamic ticks active, this updates the
  347. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  348. */
  349. void rcu_irq_enter(void)
  350. {
  351. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  352. if (rdtp->dynticks_nesting++)
  353. return;
  354. rdtp->dynticks++;
  355. WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
  356. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  357. }
  358. /**
  359. * rcu_irq_exit - inform RCU of exit from hard irq context
  360. *
  361. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  362. * to put let the RCU handling be aware that the CPU is going back to idle
  363. * with no ticks.
  364. */
  365. void rcu_irq_exit(void)
  366. {
  367. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  368. if (--rdtp->dynticks_nesting)
  369. return;
  370. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  371. rdtp->dynticks++;
  372. WARN_ON_ONCE(rdtp->dynticks & 0x1);
  373. /* If the interrupt queued a callback, get out of dyntick mode. */
  374. if (__this_cpu_read(rcu_sched_data.nxtlist) ||
  375. __this_cpu_read(rcu_bh_data.nxtlist))
  376. set_need_resched();
  377. }
  378. #ifdef CONFIG_SMP
  379. /*
  380. * Snapshot the specified CPU's dynticks counter so that we can later
  381. * credit them with an implicit quiescent state. Return 1 if this CPU
  382. * is in dynticks idle mode, which is an extended quiescent state.
  383. */
  384. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  385. {
  386. int ret;
  387. int snap;
  388. int snap_nmi;
  389. snap = rdp->dynticks->dynticks;
  390. snap_nmi = rdp->dynticks->dynticks_nmi;
  391. smp_mb(); /* Order sampling of snap with end of grace period. */
  392. rdp->dynticks_snap = snap;
  393. rdp->dynticks_nmi_snap = snap_nmi;
  394. ret = ((snap & 0x1) == 0) && ((snap_nmi & 0x1) == 0);
  395. if (ret)
  396. rdp->dynticks_fqs++;
  397. return ret;
  398. }
  399. /*
  400. * Return true if the specified CPU has passed through a quiescent
  401. * state by virtue of being in or having passed through an dynticks
  402. * idle state since the last call to dyntick_save_progress_counter()
  403. * for this same CPU.
  404. */
  405. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  406. {
  407. long curr;
  408. long curr_nmi;
  409. long snap;
  410. long snap_nmi;
  411. curr = rdp->dynticks->dynticks;
  412. snap = rdp->dynticks_snap;
  413. curr_nmi = rdp->dynticks->dynticks_nmi;
  414. snap_nmi = rdp->dynticks_nmi_snap;
  415. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  416. /*
  417. * If the CPU passed through or entered a dynticks idle phase with
  418. * no active irq/NMI handlers, then we can safely pretend that the CPU
  419. * already acknowledged the request to pass through a quiescent
  420. * state. Either way, that CPU cannot possibly be in an RCU
  421. * read-side critical section that started before the beginning
  422. * of the current RCU grace period.
  423. */
  424. if ((curr != snap || (curr & 0x1) == 0) &&
  425. (curr_nmi != snap_nmi || (curr_nmi & 0x1) == 0)) {
  426. rdp->dynticks_fqs++;
  427. return 1;
  428. }
  429. /* Go check for the CPU being offline. */
  430. return rcu_implicit_offline_qs(rdp);
  431. }
  432. #endif /* #ifdef CONFIG_SMP */
  433. #else /* #ifdef CONFIG_NO_HZ */
  434. #ifdef CONFIG_SMP
  435. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  436. {
  437. return 0;
  438. }
  439. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  440. {
  441. return rcu_implicit_offline_qs(rdp);
  442. }
  443. #endif /* #ifdef CONFIG_SMP */
  444. #endif /* #else #ifdef CONFIG_NO_HZ */
  445. int rcu_cpu_stall_suppress __read_mostly;
  446. static void record_gp_stall_check_time(struct rcu_state *rsp)
  447. {
  448. rsp->gp_start = jiffies;
  449. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  450. }
  451. static void print_other_cpu_stall(struct rcu_state *rsp)
  452. {
  453. int cpu;
  454. long delta;
  455. unsigned long flags;
  456. struct rcu_node *rnp = rcu_get_root(rsp);
  457. /* Only let one CPU complain about others per time interval. */
  458. raw_spin_lock_irqsave(&rnp->lock, flags);
  459. delta = jiffies - rsp->jiffies_stall;
  460. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  461. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  462. return;
  463. }
  464. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  465. /*
  466. * Now rat on any tasks that got kicked up to the root rcu_node
  467. * due to CPU offlining.
  468. */
  469. rcu_print_task_stall(rnp);
  470. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  471. /*
  472. * OK, time to rat on our buddy...
  473. * See Documentation/RCU/stallwarn.txt for info on how to debug
  474. * RCU CPU stall warnings.
  475. */
  476. printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks: {",
  477. rsp->name);
  478. rcu_for_each_leaf_node(rsp, rnp) {
  479. raw_spin_lock_irqsave(&rnp->lock, flags);
  480. rcu_print_task_stall(rnp);
  481. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  482. if (rnp->qsmask == 0)
  483. continue;
  484. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  485. if (rnp->qsmask & (1UL << cpu))
  486. printk(" %d", rnp->grplo + cpu);
  487. }
  488. printk("} (detected by %d, t=%ld jiffies)\n",
  489. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  490. trigger_all_cpu_backtrace();
  491. /* If so configured, complain about tasks blocking the grace period. */
  492. rcu_print_detail_task_stall(rsp);
  493. force_quiescent_state(rsp, 0); /* Kick them all. */
  494. }
  495. static void print_cpu_stall(struct rcu_state *rsp)
  496. {
  497. unsigned long flags;
  498. struct rcu_node *rnp = rcu_get_root(rsp);
  499. /*
  500. * OK, time to rat on ourselves...
  501. * See Documentation/RCU/stallwarn.txt for info on how to debug
  502. * RCU CPU stall warnings.
  503. */
  504. printk(KERN_ERR "INFO: %s detected stall on CPU %d (t=%lu jiffies)\n",
  505. rsp->name, smp_processor_id(), jiffies - rsp->gp_start);
  506. trigger_all_cpu_backtrace();
  507. raw_spin_lock_irqsave(&rnp->lock, flags);
  508. if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
  509. rsp->jiffies_stall =
  510. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  511. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  512. set_need_resched(); /* kick ourselves to get things going. */
  513. }
  514. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  515. {
  516. unsigned long j;
  517. unsigned long js;
  518. struct rcu_node *rnp;
  519. if (rcu_cpu_stall_suppress)
  520. return;
  521. j = ACCESS_ONCE(jiffies);
  522. js = ACCESS_ONCE(rsp->jiffies_stall);
  523. rnp = rdp->mynode;
  524. if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
  525. /* We haven't checked in, so go dump stack. */
  526. print_cpu_stall(rsp);
  527. } else if (rcu_gp_in_progress(rsp) &&
  528. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  529. /* They had a few time units to dump stack, so complain. */
  530. print_other_cpu_stall(rsp);
  531. }
  532. }
  533. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  534. {
  535. rcu_cpu_stall_suppress = 1;
  536. return NOTIFY_DONE;
  537. }
  538. /**
  539. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  540. *
  541. * Set the stall-warning timeout way off into the future, thus preventing
  542. * any RCU CPU stall-warning messages from appearing in the current set of
  543. * RCU grace periods.
  544. *
  545. * The caller must disable hard irqs.
  546. */
  547. void rcu_cpu_stall_reset(void)
  548. {
  549. rcu_sched_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  550. rcu_bh_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  551. rcu_preempt_stall_reset();
  552. }
  553. static struct notifier_block rcu_panic_block = {
  554. .notifier_call = rcu_panic,
  555. };
  556. static void __init check_cpu_stall_init(void)
  557. {
  558. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  559. }
  560. /*
  561. * Update CPU-local rcu_data state to record the newly noticed grace period.
  562. * This is used both when we started the grace period and when we notice
  563. * that someone else started the grace period. The caller must hold the
  564. * ->lock of the leaf rcu_node structure corresponding to the current CPU,
  565. * and must have irqs disabled.
  566. */
  567. static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  568. {
  569. if (rdp->gpnum != rnp->gpnum) {
  570. /*
  571. * If the current grace period is waiting for this CPU,
  572. * set up to detect a quiescent state, otherwise don't
  573. * go looking for one.
  574. */
  575. rdp->gpnum = rnp->gpnum;
  576. if (rnp->qsmask & rdp->grpmask) {
  577. rdp->qs_pending = 1;
  578. rdp->passed_quiesc = 0;
  579. } else
  580. rdp->qs_pending = 0;
  581. }
  582. }
  583. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  584. {
  585. unsigned long flags;
  586. struct rcu_node *rnp;
  587. local_irq_save(flags);
  588. rnp = rdp->mynode;
  589. if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
  590. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  591. local_irq_restore(flags);
  592. return;
  593. }
  594. __note_new_gpnum(rsp, rnp, rdp);
  595. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  596. }
  597. /*
  598. * Did someone else start a new RCU grace period start since we last
  599. * checked? Update local state appropriately if so. Must be called
  600. * on the CPU corresponding to rdp.
  601. */
  602. static int
  603. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  604. {
  605. unsigned long flags;
  606. int ret = 0;
  607. local_irq_save(flags);
  608. if (rdp->gpnum != rsp->gpnum) {
  609. note_new_gpnum(rsp, rdp);
  610. ret = 1;
  611. }
  612. local_irq_restore(flags);
  613. return ret;
  614. }
  615. /*
  616. * Advance this CPU's callbacks, but only if the current grace period
  617. * has ended. This may be called only from the CPU to whom the rdp
  618. * belongs. In addition, the corresponding leaf rcu_node structure's
  619. * ->lock must be held by the caller, with irqs disabled.
  620. */
  621. static void
  622. __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  623. {
  624. /* Did another grace period end? */
  625. if (rdp->completed != rnp->completed) {
  626. /* Advance callbacks. No harm if list empty. */
  627. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  628. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  629. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  630. /* Remember that we saw this grace-period completion. */
  631. rdp->completed = rnp->completed;
  632. /*
  633. * If we were in an extended quiescent state, we may have
  634. * missed some grace periods that others CPUs handled on
  635. * our behalf. Catch up with this state to avoid noting
  636. * spurious new grace periods. If another grace period
  637. * has started, then rnp->gpnum will have advanced, so
  638. * we will detect this later on.
  639. */
  640. if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
  641. rdp->gpnum = rdp->completed;
  642. /*
  643. * If RCU does not need a quiescent state from this CPU,
  644. * then make sure that this CPU doesn't go looking for one.
  645. */
  646. if ((rnp->qsmask & rdp->grpmask) == 0)
  647. rdp->qs_pending = 0;
  648. }
  649. }
  650. /*
  651. * Advance this CPU's callbacks, but only if the current grace period
  652. * has ended. This may be called only from the CPU to whom the rdp
  653. * belongs.
  654. */
  655. static void
  656. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  657. {
  658. unsigned long flags;
  659. struct rcu_node *rnp;
  660. local_irq_save(flags);
  661. rnp = rdp->mynode;
  662. if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
  663. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  664. local_irq_restore(flags);
  665. return;
  666. }
  667. __rcu_process_gp_end(rsp, rnp, rdp);
  668. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  669. }
  670. /*
  671. * Do per-CPU grace-period initialization for running CPU. The caller
  672. * must hold the lock of the leaf rcu_node structure corresponding to
  673. * this CPU.
  674. */
  675. static void
  676. rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  677. {
  678. /* Prior grace period ended, so advance callbacks for current CPU. */
  679. __rcu_process_gp_end(rsp, rnp, rdp);
  680. /*
  681. * Because this CPU just now started the new grace period, we know
  682. * that all of its callbacks will be covered by this upcoming grace
  683. * period, even the ones that were registered arbitrarily recently.
  684. * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
  685. *
  686. * Other CPUs cannot be sure exactly when the grace period started.
  687. * Therefore, their recently registered callbacks must pass through
  688. * an additional RCU_NEXT_READY stage, so that they will be handled
  689. * by the next RCU grace period.
  690. */
  691. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  692. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  693. /* Set state so that this CPU will detect the next quiescent state. */
  694. __note_new_gpnum(rsp, rnp, rdp);
  695. }
  696. /*
  697. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  698. * in preparation for detecting the next grace period. The caller must hold
  699. * the root node's ->lock, which is released before return. Hard irqs must
  700. * be disabled.
  701. */
  702. static void
  703. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  704. __releases(rcu_get_root(rsp)->lock)
  705. {
  706. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  707. struct rcu_node *rnp = rcu_get_root(rsp);
  708. if (!cpu_needs_another_gp(rsp, rdp) || rsp->fqs_active) {
  709. if (cpu_needs_another_gp(rsp, rdp))
  710. rsp->fqs_need_gp = 1;
  711. if (rnp->completed == rsp->completed) {
  712. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  713. return;
  714. }
  715. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  716. /*
  717. * Propagate new ->completed value to rcu_node structures
  718. * so that other CPUs don't have to wait until the start
  719. * of the next grace period to process their callbacks.
  720. */
  721. rcu_for_each_node_breadth_first(rsp, rnp) {
  722. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  723. rnp->completed = rsp->completed;
  724. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  725. }
  726. local_irq_restore(flags);
  727. return;
  728. }
  729. /* Advance to a new grace period and initialize state. */
  730. rsp->gpnum++;
  731. WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
  732. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  733. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  734. record_gp_stall_check_time(rsp);
  735. /* Special-case the common single-level case. */
  736. if (NUM_RCU_NODES == 1) {
  737. rcu_preempt_check_blocked_tasks(rnp);
  738. rnp->qsmask = rnp->qsmaskinit;
  739. rnp->gpnum = rsp->gpnum;
  740. rnp->completed = rsp->completed;
  741. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  742. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  743. rcu_preempt_boost_start_gp(rnp);
  744. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  745. return;
  746. }
  747. raw_spin_unlock(&rnp->lock); /* leave irqs disabled. */
  748. /* Exclude any concurrent CPU-hotplug operations. */
  749. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  750. /*
  751. * Set the quiescent-state-needed bits in all the rcu_node
  752. * structures for all currently online CPUs in breadth-first
  753. * order, starting from the root rcu_node structure. This
  754. * operation relies on the layout of the hierarchy within the
  755. * rsp->node[] array. Note that other CPUs will access only
  756. * the leaves of the hierarchy, which still indicate that no
  757. * grace period is in progress, at least until the corresponding
  758. * leaf node has been initialized. In addition, we have excluded
  759. * CPU-hotplug operations.
  760. *
  761. * Note that the grace period cannot complete until we finish
  762. * the initialization process, as there will be at least one
  763. * qsmask bit set in the root node until that time, namely the
  764. * one corresponding to this CPU, due to the fact that we have
  765. * irqs disabled.
  766. */
  767. rcu_for_each_node_breadth_first(rsp, rnp) {
  768. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  769. rcu_preempt_check_blocked_tasks(rnp);
  770. rnp->qsmask = rnp->qsmaskinit;
  771. rnp->gpnum = rsp->gpnum;
  772. rnp->completed = rsp->completed;
  773. if (rnp == rdp->mynode)
  774. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  775. rcu_preempt_boost_start_gp(rnp);
  776. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  777. }
  778. rnp = rcu_get_root(rsp);
  779. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  780. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  781. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  782. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  783. }
  784. /*
  785. * Report a full set of quiescent states to the specified rcu_state
  786. * data structure. This involves cleaning up after the prior grace
  787. * period and letting rcu_start_gp() start up the next grace period
  788. * if one is needed. Note that the caller must hold rnp->lock, as
  789. * required by rcu_start_gp(), which will release it.
  790. */
  791. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  792. __releases(rcu_get_root(rsp)->lock)
  793. {
  794. unsigned long gp_duration;
  795. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  796. gp_duration = jiffies - rsp->gp_start;
  797. if (gp_duration > rsp->gp_max)
  798. rsp->gp_max = gp_duration;
  799. rsp->completed = rsp->gpnum;
  800. rsp->signaled = RCU_GP_IDLE;
  801. rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
  802. }
  803. /*
  804. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  805. * Allows quiescent states for a group of CPUs to be reported at one go
  806. * to the specified rcu_node structure, though all the CPUs in the group
  807. * must be represented by the same rcu_node structure (which need not be
  808. * a leaf rcu_node structure, though it often will be). That structure's
  809. * lock must be held upon entry, and it is released before return.
  810. */
  811. static void
  812. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  813. struct rcu_node *rnp, unsigned long flags)
  814. __releases(rnp->lock)
  815. {
  816. struct rcu_node *rnp_c;
  817. /* Walk up the rcu_node hierarchy. */
  818. for (;;) {
  819. if (!(rnp->qsmask & mask)) {
  820. /* Our bit has already been cleared, so done. */
  821. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  822. return;
  823. }
  824. rnp->qsmask &= ~mask;
  825. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  826. /* Other bits still set at this level, so done. */
  827. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  828. return;
  829. }
  830. mask = rnp->grpmask;
  831. if (rnp->parent == NULL) {
  832. /* No more levels. Exit loop holding root lock. */
  833. break;
  834. }
  835. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  836. rnp_c = rnp;
  837. rnp = rnp->parent;
  838. raw_spin_lock_irqsave(&rnp->lock, flags);
  839. WARN_ON_ONCE(rnp_c->qsmask);
  840. }
  841. /*
  842. * Get here if we are the last CPU to pass through a quiescent
  843. * state for this grace period. Invoke rcu_report_qs_rsp()
  844. * to clean up and start the next grace period if one is needed.
  845. */
  846. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  847. }
  848. /*
  849. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  850. * structure. This must be either called from the specified CPU, or
  851. * called when the specified CPU is known to be offline (and when it is
  852. * also known that no other CPU is concurrently trying to help the offline
  853. * CPU). The lastcomp argument is used to make sure we are still in the
  854. * grace period of interest. We don't want to end the current grace period
  855. * based on quiescent states detected in an earlier grace period!
  856. */
  857. static void
  858. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  859. {
  860. unsigned long flags;
  861. unsigned long mask;
  862. struct rcu_node *rnp;
  863. rnp = rdp->mynode;
  864. raw_spin_lock_irqsave(&rnp->lock, flags);
  865. if (lastcomp != rnp->completed) {
  866. /*
  867. * Someone beat us to it for this grace period, so leave.
  868. * The race with GP start is resolved by the fact that we
  869. * hold the leaf rcu_node lock, so that the per-CPU bits
  870. * cannot yet be initialized -- so we would simply find our
  871. * CPU's bit already cleared in rcu_report_qs_rnp() if this
  872. * race occurred.
  873. */
  874. rdp->passed_quiesc = 0; /* try again later! */
  875. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  876. return;
  877. }
  878. mask = rdp->grpmask;
  879. if ((rnp->qsmask & mask) == 0) {
  880. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  881. } else {
  882. rdp->qs_pending = 0;
  883. /*
  884. * This GP can't end until cpu checks in, so all of our
  885. * callbacks can be processed during the next GP.
  886. */
  887. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  888. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  889. }
  890. }
  891. /*
  892. * Check to see if there is a new grace period of which this CPU
  893. * is not yet aware, and if so, set up local rcu_data state for it.
  894. * Otherwise, see if this CPU has just passed through its first
  895. * quiescent state for this grace period, and record that fact if so.
  896. */
  897. static void
  898. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  899. {
  900. /* If there is now a new grace period, record and return. */
  901. if (check_for_new_grace_period(rsp, rdp))
  902. return;
  903. /*
  904. * Does this CPU still need to do its part for current grace period?
  905. * If no, return and let the other CPUs do their part as well.
  906. */
  907. if (!rdp->qs_pending)
  908. return;
  909. /*
  910. * Was there a quiescent state since the beginning of the grace
  911. * period? If no, then exit and wait for the next call.
  912. */
  913. if (!rdp->passed_quiesc)
  914. return;
  915. /*
  916. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  917. * judge of that).
  918. */
  919. rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  920. }
  921. #ifdef CONFIG_HOTPLUG_CPU
  922. /*
  923. * Move a dying CPU's RCU callbacks to online CPU's callback list.
  924. * Synchronization is not required because this function executes
  925. * in stop_machine() context.
  926. */
  927. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  928. {
  929. int i;
  930. /* current DYING CPU is cleared in the cpu_online_mask */
  931. int receive_cpu = cpumask_any(cpu_online_mask);
  932. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  933. struct rcu_data *receive_rdp = per_cpu_ptr(rsp->rda, receive_cpu);
  934. if (rdp->nxtlist == NULL)
  935. return; /* irqs disabled, so comparison is stable. */
  936. *receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
  937. receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  938. receive_rdp->qlen += rdp->qlen;
  939. receive_rdp->n_cbs_adopted += rdp->qlen;
  940. rdp->n_cbs_orphaned += rdp->qlen;
  941. rdp->nxtlist = NULL;
  942. for (i = 0; i < RCU_NEXT_SIZE; i++)
  943. rdp->nxttail[i] = &rdp->nxtlist;
  944. rdp->qlen = 0;
  945. }
  946. /*
  947. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  948. * and move all callbacks from the outgoing CPU to the current one.
  949. * There can only be one CPU hotplug operation at a time, so no other
  950. * CPU can be attempting to update rcu_cpu_kthread_task.
  951. */
  952. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  953. {
  954. unsigned long flags;
  955. unsigned long mask;
  956. int need_report = 0;
  957. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  958. struct rcu_node *rnp;
  959. struct task_struct *t;
  960. /* Stop the CPU's kthread. */
  961. t = per_cpu(rcu_cpu_kthread_task, cpu);
  962. if (t != NULL) {
  963. per_cpu(rcu_cpu_kthread_task, cpu) = NULL;
  964. kthread_stop(t);
  965. }
  966. /* Exclude any attempts to start a new grace period. */
  967. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  968. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  969. rnp = rdp->mynode; /* this is the outgoing CPU's rnp. */
  970. mask = rdp->grpmask; /* rnp->grplo is constant. */
  971. do {
  972. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  973. rnp->qsmaskinit &= ~mask;
  974. if (rnp->qsmaskinit != 0) {
  975. if (rnp != rdp->mynode)
  976. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  977. break;
  978. }
  979. if (rnp == rdp->mynode)
  980. need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  981. else
  982. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  983. mask = rnp->grpmask;
  984. rnp = rnp->parent;
  985. } while (rnp != NULL);
  986. /*
  987. * We still hold the leaf rcu_node structure lock here, and
  988. * irqs are still disabled. The reason for this subterfuge is
  989. * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
  990. * held leads to deadlock.
  991. */
  992. raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  993. rnp = rdp->mynode;
  994. if (need_report & RCU_OFL_TASKS_NORM_GP)
  995. rcu_report_unblock_qs_rnp(rnp, flags);
  996. else
  997. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  998. if (need_report & RCU_OFL_TASKS_EXP_GP)
  999. rcu_report_exp_rnp(rsp, rnp);
  1000. rcu_node_kthread_setaffinity(rnp, -1);
  1001. }
  1002. /*
  1003. * Remove the specified CPU from the RCU hierarchy and move any pending
  1004. * callbacks that it might have to the current CPU. This code assumes
  1005. * that at least one CPU in the system will remain running at all times.
  1006. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  1007. */
  1008. static void rcu_offline_cpu(int cpu)
  1009. {
  1010. __rcu_offline_cpu(cpu, &rcu_sched_state);
  1011. __rcu_offline_cpu(cpu, &rcu_bh_state);
  1012. rcu_preempt_offline_cpu(cpu);
  1013. }
  1014. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1015. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  1016. {
  1017. }
  1018. static void rcu_offline_cpu(int cpu)
  1019. {
  1020. }
  1021. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1022. /*
  1023. * Invoke any RCU callbacks that have made it to the end of their grace
  1024. * period. Thottle as specified by rdp->blimit.
  1025. */
  1026. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  1027. {
  1028. unsigned long flags;
  1029. struct rcu_head *next, *list, **tail;
  1030. int count;
  1031. /* If no callbacks are ready, just return.*/
  1032. if (!cpu_has_callbacks_ready_to_invoke(rdp))
  1033. return;
  1034. /*
  1035. * Extract the list of ready callbacks, disabling to prevent
  1036. * races with call_rcu() from interrupt handlers.
  1037. */
  1038. local_irq_save(flags);
  1039. list = rdp->nxtlist;
  1040. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  1041. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1042. tail = rdp->nxttail[RCU_DONE_TAIL];
  1043. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  1044. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  1045. rdp->nxttail[count] = &rdp->nxtlist;
  1046. local_irq_restore(flags);
  1047. /* Invoke callbacks. */
  1048. count = 0;
  1049. while (list) {
  1050. next = list->next;
  1051. prefetch(next);
  1052. debug_rcu_head_unqueue(list);
  1053. __rcu_reclaim(list);
  1054. list = next;
  1055. if (++count >= rdp->blimit)
  1056. break;
  1057. }
  1058. local_irq_save(flags);
  1059. /* Update count, and requeue any remaining callbacks. */
  1060. rdp->qlen -= count;
  1061. rdp->n_cbs_invoked += count;
  1062. if (list != NULL) {
  1063. *tail = rdp->nxtlist;
  1064. rdp->nxtlist = list;
  1065. for (count = 0; count < RCU_NEXT_SIZE; count++)
  1066. if (&rdp->nxtlist == rdp->nxttail[count])
  1067. rdp->nxttail[count] = tail;
  1068. else
  1069. break;
  1070. }
  1071. /* Reinstate batch limit if we have worked down the excess. */
  1072. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  1073. rdp->blimit = blimit;
  1074. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  1075. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  1076. rdp->qlen_last_fqs_check = 0;
  1077. rdp->n_force_qs_snap = rsp->n_force_qs;
  1078. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  1079. rdp->qlen_last_fqs_check = rdp->qlen;
  1080. local_irq_restore(flags);
  1081. /* Re-raise the RCU softirq if there are callbacks remaining. */
  1082. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1083. invoke_rcu_cpu_kthread();
  1084. }
  1085. /*
  1086. * Check to see if this CPU is in a non-context-switch quiescent state
  1087. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  1088. * Also schedule the RCU softirq handler.
  1089. *
  1090. * This function must be called with hardirqs disabled. It is normally
  1091. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  1092. * false, there is no point in invoking rcu_check_callbacks().
  1093. */
  1094. void rcu_check_callbacks(int cpu, int user)
  1095. {
  1096. if (user ||
  1097. (idle_cpu(cpu) && rcu_scheduler_active &&
  1098. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  1099. /*
  1100. * Get here if this CPU took its interrupt from user
  1101. * mode or from the idle loop, and if this is not a
  1102. * nested interrupt. In this case, the CPU is in
  1103. * a quiescent state, so note it.
  1104. *
  1105. * No memory barrier is required here because both
  1106. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  1107. * variables that other CPUs neither access nor modify,
  1108. * at least not while the corresponding CPU is online.
  1109. */
  1110. rcu_sched_qs(cpu);
  1111. rcu_bh_qs(cpu);
  1112. } else if (!in_softirq()) {
  1113. /*
  1114. * Get here if this CPU did not take its interrupt from
  1115. * softirq, in other words, if it is not interrupting
  1116. * a rcu_bh read-side critical section. This is an _bh
  1117. * critical section, so note it.
  1118. */
  1119. rcu_bh_qs(cpu);
  1120. }
  1121. rcu_preempt_check_callbacks(cpu);
  1122. if (rcu_pending(cpu))
  1123. invoke_rcu_cpu_kthread();
  1124. }
  1125. #ifdef CONFIG_SMP
  1126. /*
  1127. * Scan the leaf rcu_node structures, processing dyntick state for any that
  1128. * have not yet encountered a quiescent state, using the function specified.
  1129. * Also initiate boosting for any threads blocked on the root rcu_node.
  1130. *
  1131. * The caller must have suppressed start of new grace periods.
  1132. */
  1133. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
  1134. {
  1135. unsigned long bit;
  1136. int cpu;
  1137. unsigned long flags;
  1138. unsigned long mask;
  1139. struct rcu_node *rnp;
  1140. rcu_for_each_leaf_node(rsp, rnp) {
  1141. mask = 0;
  1142. raw_spin_lock_irqsave(&rnp->lock, flags);
  1143. if (!rcu_gp_in_progress(rsp)) {
  1144. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1145. return;
  1146. }
  1147. if (rnp->qsmask == 0) {
  1148. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  1149. continue;
  1150. }
  1151. cpu = rnp->grplo;
  1152. bit = 1;
  1153. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1154. if ((rnp->qsmask & bit) != 0 &&
  1155. f(per_cpu_ptr(rsp->rda, cpu)))
  1156. mask |= bit;
  1157. }
  1158. if (mask != 0) {
  1159. /* rcu_report_qs_rnp() releases rnp->lock. */
  1160. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1161. continue;
  1162. }
  1163. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1164. }
  1165. rnp = rcu_get_root(rsp);
  1166. if (rnp->qsmask == 0) {
  1167. raw_spin_lock_irqsave(&rnp->lock, flags);
  1168. rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
  1169. }
  1170. }
  1171. /*
  1172. * Force quiescent states on reluctant CPUs, and also detect which
  1173. * CPUs are in dyntick-idle mode.
  1174. */
  1175. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1176. {
  1177. unsigned long flags;
  1178. struct rcu_node *rnp = rcu_get_root(rsp);
  1179. if (!rcu_gp_in_progress(rsp))
  1180. return; /* No grace period in progress, nothing to force. */
  1181. if (!raw_spin_trylock_irqsave(&rsp->fqslock, flags)) {
  1182. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  1183. return; /* Someone else is already on the job. */
  1184. }
  1185. if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
  1186. goto unlock_fqs_ret; /* no emergency and done recently. */
  1187. rsp->n_force_qs++;
  1188. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1189. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  1190. if(!rcu_gp_in_progress(rsp)) {
  1191. rsp->n_force_qs_ngp++;
  1192. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1193. goto unlock_fqs_ret; /* no GP in progress, time updated. */
  1194. }
  1195. rsp->fqs_active = 1;
  1196. switch (rsp->signaled) {
  1197. case RCU_GP_IDLE:
  1198. case RCU_GP_INIT:
  1199. break; /* grace period idle or initializing, ignore. */
  1200. case RCU_SAVE_DYNTICK:
  1201. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  1202. break; /* So gcc recognizes the dead code. */
  1203. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1204. /* Record dyntick-idle state. */
  1205. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1206. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1207. if (rcu_gp_in_progress(rsp))
  1208. rsp->signaled = RCU_FORCE_QS;
  1209. break;
  1210. case RCU_FORCE_QS:
  1211. /* Check dyntick-idle state, send IPI to laggarts. */
  1212. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1213. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1214. /* Leave state in case more forcing is required. */
  1215. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1216. break;
  1217. }
  1218. rsp->fqs_active = 0;
  1219. if (rsp->fqs_need_gp) {
  1220. raw_spin_unlock(&rsp->fqslock); /* irqs remain disabled */
  1221. rsp->fqs_need_gp = 0;
  1222. rcu_start_gp(rsp, flags); /* releases rnp->lock */
  1223. return;
  1224. }
  1225. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1226. unlock_fqs_ret:
  1227. raw_spin_unlock_irqrestore(&rsp->fqslock, flags);
  1228. }
  1229. #else /* #ifdef CONFIG_SMP */
  1230. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1231. {
  1232. set_need_resched();
  1233. }
  1234. #endif /* #else #ifdef CONFIG_SMP */
  1235. /*
  1236. * This does the RCU processing work from softirq context for the
  1237. * specified rcu_state and rcu_data structures. This may be called
  1238. * only from the CPU to whom the rdp belongs.
  1239. */
  1240. static void
  1241. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1242. {
  1243. unsigned long flags;
  1244. WARN_ON_ONCE(rdp->beenonline == 0);
  1245. /*
  1246. * If an RCU GP has gone long enough, go check for dyntick
  1247. * idle CPUs and, if needed, send resched IPIs.
  1248. */
  1249. if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1250. force_quiescent_state(rsp, 1);
  1251. /*
  1252. * Advance callbacks in response to end of earlier grace
  1253. * period that some other CPU ended.
  1254. */
  1255. rcu_process_gp_end(rsp, rdp);
  1256. /* Update RCU state based on any recent quiescent states. */
  1257. rcu_check_quiescent_state(rsp, rdp);
  1258. /* Does this CPU require a not-yet-started grace period? */
  1259. if (cpu_needs_another_gp(rsp, rdp)) {
  1260. raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1261. rcu_start_gp(rsp, flags); /* releases above lock */
  1262. }
  1263. /* If there are callbacks ready, invoke them. */
  1264. rcu_do_batch(rsp, rdp);
  1265. }
  1266. /*
  1267. * Do softirq processing for the current CPU.
  1268. */
  1269. static void rcu_process_callbacks(void)
  1270. {
  1271. /*
  1272. * Memory references from any prior RCU read-side critical sections
  1273. * executed by the interrupted code must be seen before any RCU
  1274. * grace-period manipulations below.
  1275. */
  1276. smp_mb(); /* See above block comment. */
  1277. __rcu_process_callbacks(&rcu_sched_state,
  1278. &__get_cpu_var(rcu_sched_data));
  1279. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1280. rcu_preempt_process_callbacks();
  1281. /*
  1282. * Memory references from any later RCU read-side critical sections
  1283. * executed by the interrupted code must be seen after any RCU
  1284. * grace-period manipulations above.
  1285. */
  1286. smp_mb(); /* See above block comment. */
  1287. /* If we are last CPU on way to dyntick-idle mode, accelerate it. */
  1288. rcu_needs_cpu_flush();
  1289. }
  1290. /*
  1291. * Wake up the current CPU's kthread. This replaces raise_softirq()
  1292. * in earlier versions of RCU. Note that because we are running on
  1293. * the current CPU with interrupts disabled, the rcu_cpu_kthread_task
  1294. * cannot disappear out from under us.
  1295. */
  1296. static void invoke_rcu_cpu_kthread(void)
  1297. {
  1298. unsigned long flags;
  1299. local_irq_save(flags);
  1300. __this_cpu_write(rcu_cpu_has_work, 1);
  1301. if (__this_cpu_read(rcu_cpu_kthread_task) == NULL) {
  1302. local_irq_restore(flags);
  1303. return;
  1304. }
  1305. wake_up(&__get_cpu_var(rcu_cpu_wq));
  1306. local_irq_restore(flags);
  1307. }
  1308. /*
  1309. * Wake up the specified per-rcu_node-structure kthread.
  1310. * Because the per-rcu_node kthreads are immortal, we don't need
  1311. * to do anything to keep them alive.
  1312. */
  1313. static void invoke_rcu_node_kthread(struct rcu_node *rnp)
  1314. {
  1315. struct task_struct *t;
  1316. t = rnp->node_kthread_task;
  1317. if (t != NULL)
  1318. wake_up_process(t);
  1319. }
  1320. /*
  1321. * Set the specified CPU's kthread to run RT or not, as specified by
  1322. * the to_rt argument. The CPU-hotplug locks are held, so the task
  1323. * is not going away.
  1324. */
  1325. static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
  1326. {
  1327. int policy;
  1328. struct sched_param sp;
  1329. struct task_struct *t;
  1330. t = per_cpu(rcu_cpu_kthread_task, cpu);
  1331. if (t == NULL)
  1332. return;
  1333. if (to_rt) {
  1334. policy = SCHED_FIFO;
  1335. sp.sched_priority = RCU_KTHREAD_PRIO;
  1336. } else {
  1337. policy = SCHED_NORMAL;
  1338. sp.sched_priority = 0;
  1339. }
  1340. sched_setscheduler_nocheck(t, policy, &sp);
  1341. }
  1342. /*
  1343. * Timer handler to initiate the waking up of per-CPU kthreads that
  1344. * have yielded the CPU due to excess numbers of RCU callbacks.
  1345. * We wake up the per-rcu_node kthread, which in turn will wake up
  1346. * the booster kthread.
  1347. */
  1348. static void rcu_cpu_kthread_timer(unsigned long arg)
  1349. {
  1350. unsigned long flags;
  1351. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, arg);
  1352. struct rcu_node *rnp = rdp->mynode;
  1353. raw_spin_lock_irqsave(&rnp->lock, flags);
  1354. rnp->wakemask |= rdp->grpmask;
  1355. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1356. invoke_rcu_node_kthread(rnp);
  1357. }
  1358. /*
  1359. * Drop to non-real-time priority and yield, but only after posting a
  1360. * timer that will cause us to regain our real-time priority if we
  1361. * remain preempted. Either way, we restore our real-time priority
  1362. * before returning.
  1363. */
  1364. static void rcu_yield(void (*f)(unsigned long), unsigned long arg)
  1365. {
  1366. struct sched_param sp;
  1367. struct timer_list yield_timer;
  1368. setup_timer_on_stack(&yield_timer, f, arg);
  1369. mod_timer(&yield_timer, jiffies + 2);
  1370. sp.sched_priority = 0;
  1371. sched_setscheduler_nocheck(current, SCHED_NORMAL, &sp);
  1372. set_user_nice(current, 19);
  1373. schedule();
  1374. sp.sched_priority = RCU_KTHREAD_PRIO;
  1375. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1376. del_timer(&yield_timer);
  1377. }
  1378. /*
  1379. * Handle cases where the rcu_cpu_kthread() ends up on the wrong CPU.
  1380. * This can happen while the corresponding CPU is either coming online
  1381. * or going offline. We cannot wait until the CPU is fully online
  1382. * before starting the kthread, because the various notifier functions
  1383. * can wait for RCU grace periods. So we park rcu_cpu_kthread() until
  1384. * the corresponding CPU is online.
  1385. *
  1386. * Return 1 if the kthread needs to stop, 0 otherwise.
  1387. *
  1388. * Caller must disable bh. This function can momentarily enable it.
  1389. */
  1390. static int rcu_cpu_kthread_should_stop(int cpu)
  1391. {
  1392. while (cpu_is_offline(cpu) ||
  1393. !cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)) ||
  1394. smp_processor_id() != cpu) {
  1395. if (kthread_should_stop())
  1396. return 1;
  1397. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1398. per_cpu(rcu_cpu_kthread_cpu, cpu) = raw_smp_processor_id();
  1399. local_bh_enable();
  1400. schedule_timeout_uninterruptible(1);
  1401. if (!cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)))
  1402. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  1403. local_bh_disable();
  1404. }
  1405. per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
  1406. return 0;
  1407. }
  1408. /*
  1409. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1410. * earlier RCU softirq.
  1411. */
  1412. static int rcu_cpu_kthread(void *arg)
  1413. {
  1414. int cpu = (int)(long)arg;
  1415. unsigned long flags;
  1416. int spincnt = 0;
  1417. unsigned int *statusp = &per_cpu(rcu_cpu_kthread_status, cpu);
  1418. wait_queue_head_t *wqp = &per_cpu(rcu_cpu_wq, cpu);
  1419. char work;
  1420. char *workp = &per_cpu(rcu_cpu_has_work, cpu);
  1421. for (;;) {
  1422. *statusp = RCU_KTHREAD_WAITING;
  1423. wait_event_interruptible(*wqp,
  1424. *workp != 0 || kthread_should_stop());
  1425. local_bh_disable();
  1426. if (rcu_cpu_kthread_should_stop(cpu)) {
  1427. local_bh_enable();
  1428. break;
  1429. }
  1430. *statusp = RCU_KTHREAD_RUNNING;
  1431. per_cpu(rcu_cpu_kthread_loops, cpu)++;
  1432. local_irq_save(flags);
  1433. work = *workp;
  1434. *workp = 0;
  1435. local_irq_restore(flags);
  1436. if (work)
  1437. rcu_process_callbacks();
  1438. local_bh_enable();
  1439. if (*workp != 0)
  1440. spincnt++;
  1441. else
  1442. spincnt = 0;
  1443. if (spincnt > 10) {
  1444. *statusp = RCU_KTHREAD_YIELDING;
  1445. rcu_yield(rcu_cpu_kthread_timer, (unsigned long)cpu);
  1446. spincnt = 0;
  1447. }
  1448. }
  1449. *statusp = RCU_KTHREAD_STOPPED;
  1450. return 0;
  1451. }
  1452. /*
  1453. * Spawn a per-CPU kthread, setting up affinity and priority.
  1454. * Because the CPU hotplug lock is held, no other CPU will be attempting
  1455. * to manipulate rcu_cpu_kthread_task. There might be another CPU
  1456. * attempting to access it during boot, but the locking in kthread_bind()
  1457. * will enforce sufficient ordering.
  1458. */
  1459. static int __cpuinit rcu_spawn_one_cpu_kthread(int cpu)
  1460. {
  1461. struct sched_param sp;
  1462. struct task_struct *t;
  1463. if (!rcu_kthreads_spawnable ||
  1464. per_cpu(rcu_cpu_kthread_task, cpu) != NULL)
  1465. return 0;
  1466. t = kthread_create(rcu_cpu_kthread, (void *)(long)cpu, "rcuc%d", cpu);
  1467. if (IS_ERR(t))
  1468. return PTR_ERR(t);
  1469. kthread_bind(t, cpu);
  1470. per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
  1471. WARN_ON_ONCE(per_cpu(rcu_cpu_kthread_task, cpu) != NULL);
  1472. per_cpu(rcu_cpu_kthread_task, cpu) = t;
  1473. wake_up_process(t);
  1474. sp.sched_priority = RCU_KTHREAD_PRIO;
  1475. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1476. return 0;
  1477. }
  1478. /*
  1479. * Per-rcu_node kthread, which is in charge of waking up the per-CPU
  1480. * kthreads when needed. We ignore requests to wake up kthreads
  1481. * for offline CPUs, which is OK because force_quiescent_state()
  1482. * takes care of this case.
  1483. */
  1484. static int rcu_node_kthread(void *arg)
  1485. {
  1486. int cpu;
  1487. unsigned long flags;
  1488. unsigned long mask;
  1489. struct rcu_node *rnp = (struct rcu_node *)arg;
  1490. struct sched_param sp;
  1491. struct task_struct *t;
  1492. for (;;) {
  1493. rnp->node_kthread_status = RCU_KTHREAD_WAITING;
  1494. wait_event_interruptible(rnp->node_wq, rnp->wakemask != 0);
  1495. rnp->node_kthread_status = RCU_KTHREAD_RUNNING;
  1496. raw_spin_lock_irqsave(&rnp->lock, flags);
  1497. mask = rnp->wakemask;
  1498. rnp->wakemask = 0;
  1499. rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
  1500. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1) {
  1501. if ((mask & 0x1) == 0)
  1502. continue;
  1503. preempt_disable();
  1504. t = per_cpu(rcu_cpu_kthread_task, cpu);
  1505. if (!cpu_online(cpu) || t == NULL) {
  1506. preempt_enable();
  1507. continue;
  1508. }
  1509. per_cpu(rcu_cpu_has_work, cpu) = 1;
  1510. sp.sched_priority = RCU_KTHREAD_PRIO;
  1511. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1512. preempt_enable();
  1513. }
  1514. }
  1515. /* NOTREACHED */
  1516. rnp->node_kthread_status = RCU_KTHREAD_STOPPED;
  1517. return 0;
  1518. }
  1519. /*
  1520. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1521. * served by the rcu_node in question. The CPU hotplug lock is still
  1522. * held, so the value of rnp->qsmaskinit will be stable.
  1523. *
  1524. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1525. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1526. * this function allows the kthread to execute on any CPU.
  1527. */
  1528. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1529. {
  1530. cpumask_var_t cm;
  1531. int cpu;
  1532. unsigned long mask = rnp->qsmaskinit;
  1533. if (rnp->node_kthread_task == NULL)
  1534. return;
  1535. if (!alloc_cpumask_var(&cm, GFP_KERNEL))
  1536. return;
  1537. cpumask_clear(cm);
  1538. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
  1539. if ((mask & 0x1) && cpu != outgoingcpu)
  1540. cpumask_set_cpu(cpu, cm);
  1541. if (cpumask_weight(cm) == 0) {
  1542. cpumask_setall(cm);
  1543. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++)
  1544. cpumask_clear_cpu(cpu, cm);
  1545. WARN_ON_ONCE(cpumask_weight(cm) == 0);
  1546. }
  1547. set_cpus_allowed_ptr(rnp->node_kthread_task, cm);
  1548. rcu_boost_kthread_setaffinity(rnp, cm);
  1549. free_cpumask_var(cm);
  1550. }
  1551. /*
  1552. * Spawn a per-rcu_node kthread, setting priority and affinity.
  1553. * Called during boot before online/offline can happen, or, if
  1554. * during runtime, with the main CPU-hotplug locks held. So only
  1555. * one of these can be executing at a time.
  1556. */
  1557. static int __cpuinit rcu_spawn_one_node_kthread(struct rcu_state *rsp,
  1558. struct rcu_node *rnp)
  1559. {
  1560. unsigned long flags;
  1561. int rnp_index = rnp - &rsp->node[0];
  1562. struct sched_param sp;
  1563. struct task_struct *t;
  1564. if (!rcu_kthreads_spawnable ||
  1565. rnp->qsmaskinit == 0)
  1566. return 0;
  1567. if (rnp->node_kthread_task == NULL) {
  1568. t = kthread_create(rcu_node_kthread, (void *)rnp,
  1569. "rcun%d", rnp_index);
  1570. if (IS_ERR(t))
  1571. return PTR_ERR(t);
  1572. raw_spin_lock_irqsave(&rnp->lock, flags);
  1573. rnp->node_kthread_task = t;
  1574. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1575. wake_up_process(t);
  1576. sp.sched_priority = 99;
  1577. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1578. }
  1579. return rcu_spawn_one_boost_kthread(rsp, rnp, rnp_index);
  1580. }
  1581. /*
  1582. * Spawn all kthreads -- called as soon as the scheduler is running.
  1583. */
  1584. static int __init rcu_spawn_kthreads(void)
  1585. {
  1586. int cpu;
  1587. struct rcu_node *rnp;
  1588. rcu_kthreads_spawnable = 1;
  1589. for_each_possible_cpu(cpu) {
  1590. init_waitqueue_head(&per_cpu(rcu_cpu_wq, cpu));
  1591. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1592. if (cpu_online(cpu))
  1593. (void)rcu_spawn_one_cpu_kthread(cpu);
  1594. }
  1595. rnp = rcu_get_root(rcu_state);
  1596. init_waitqueue_head(&rnp->node_wq);
  1597. rcu_init_boost_waitqueue(rnp);
  1598. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1599. if (NUM_RCU_NODES > 1)
  1600. rcu_for_each_leaf_node(rcu_state, rnp) {
  1601. init_waitqueue_head(&rnp->node_wq);
  1602. rcu_init_boost_waitqueue(rnp);
  1603. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1604. }
  1605. return 0;
  1606. }
  1607. early_initcall(rcu_spawn_kthreads);
  1608. static void
  1609. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1610. struct rcu_state *rsp)
  1611. {
  1612. unsigned long flags;
  1613. struct rcu_data *rdp;
  1614. debug_rcu_head_queue(head);
  1615. head->func = func;
  1616. head->next = NULL;
  1617. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1618. /*
  1619. * Opportunistically note grace-period endings and beginnings.
  1620. * Note that we might see a beginning right after we see an
  1621. * end, but never vice versa, since this CPU has to pass through
  1622. * a quiescent state betweentimes.
  1623. */
  1624. local_irq_save(flags);
  1625. rdp = this_cpu_ptr(rsp->rda);
  1626. /* Add the callback to our list. */
  1627. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1628. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1629. rdp->qlen++;
  1630. /* If interrupts were disabled, don't dive into RCU core. */
  1631. if (irqs_disabled_flags(flags)) {
  1632. local_irq_restore(flags);
  1633. return;
  1634. }
  1635. /*
  1636. * Force the grace period if too many callbacks or too long waiting.
  1637. * Enforce hysteresis, and don't invoke force_quiescent_state()
  1638. * if some other CPU has recently done so. Also, don't bother
  1639. * invoking force_quiescent_state() if the newly enqueued callback
  1640. * is the only one waiting for a grace period to complete.
  1641. */
  1642. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  1643. /* Are we ignoring a completed grace period? */
  1644. rcu_process_gp_end(rsp, rdp);
  1645. check_for_new_grace_period(rsp, rdp);
  1646. /* Start a new grace period if one not already started. */
  1647. if (!rcu_gp_in_progress(rsp)) {
  1648. unsigned long nestflag;
  1649. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1650. raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
  1651. rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
  1652. } else {
  1653. /* Give the grace period a kick. */
  1654. rdp->blimit = LONG_MAX;
  1655. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  1656. *rdp->nxttail[RCU_DONE_TAIL] != head)
  1657. force_quiescent_state(rsp, 0);
  1658. rdp->n_force_qs_snap = rsp->n_force_qs;
  1659. rdp->qlen_last_fqs_check = rdp->qlen;
  1660. }
  1661. } else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1662. force_quiescent_state(rsp, 1);
  1663. local_irq_restore(flags);
  1664. }
  1665. /*
  1666. * Queue an RCU-sched callback for invocation after a grace period.
  1667. */
  1668. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1669. {
  1670. __call_rcu(head, func, &rcu_sched_state);
  1671. }
  1672. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1673. /*
  1674. * Queue an RCU for invocation after a quicker grace period.
  1675. */
  1676. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1677. {
  1678. __call_rcu(head, func, &rcu_bh_state);
  1679. }
  1680. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1681. /**
  1682. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  1683. *
  1684. * Control will return to the caller some time after a full rcu-sched
  1685. * grace period has elapsed, in other words after all currently executing
  1686. * rcu-sched read-side critical sections have completed. These read-side
  1687. * critical sections are delimited by rcu_read_lock_sched() and
  1688. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  1689. * local_irq_disable(), and so on may be used in place of
  1690. * rcu_read_lock_sched().
  1691. *
  1692. * This means that all preempt_disable code sequences, including NMI and
  1693. * hardware-interrupt handlers, in progress on entry will have completed
  1694. * before this primitive returns. However, this does not guarantee that
  1695. * softirq handlers will have completed, since in some kernels, these
  1696. * handlers can run in process context, and can block.
  1697. *
  1698. * This primitive provides the guarantees made by the (now removed)
  1699. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  1700. * guarantees that rcu_read_lock() sections will have completed.
  1701. * In "classic RCU", these two guarantees happen to be one and
  1702. * the same, but can differ in realtime RCU implementations.
  1703. */
  1704. void synchronize_sched(void)
  1705. {
  1706. struct rcu_synchronize rcu;
  1707. if (rcu_blocking_is_gp())
  1708. return;
  1709. init_rcu_head_on_stack(&rcu.head);
  1710. init_completion(&rcu.completion);
  1711. /* Will wake me after RCU finished. */
  1712. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  1713. /* Wait for it. */
  1714. wait_for_completion(&rcu.completion);
  1715. destroy_rcu_head_on_stack(&rcu.head);
  1716. }
  1717. EXPORT_SYMBOL_GPL(synchronize_sched);
  1718. /**
  1719. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  1720. *
  1721. * Control will return to the caller some time after a full rcu_bh grace
  1722. * period has elapsed, in other words after all currently executing rcu_bh
  1723. * read-side critical sections have completed. RCU read-side critical
  1724. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  1725. * and may be nested.
  1726. */
  1727. void synchronize_rcu_bh(void)
  1728. {
  1729. struct rcu_synchronize rcu;
  1730. if (rcu_blocking_is_gp())
  1731. return;
  1732. init_rcu_head_on_stack(&rcu.head);
  1733. init_completion(&rcu.completion);
  1734. /* Will wake me after RCU finished. */
  1735. call_rcu_bh(&rcu.head, wakeme_after_rcu);
  1736. /* Wait for it. */
  1737. wait_for_completion(&rcu.completion);
  1738. destroy_rcu_head_on_stack(&rcu.head);
  1739. }
  1740. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  1741. /*
  1742. * Check to see if there is any immediate RCU-related work to be done
  1743. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1744. * The checks are in order of increasing expense: checks that can be
  1745. * carried out against CPU-local state are performed first. However,
  1746. * we must check for CPU stalls first, else we might not get a chance.
  1747. */
  1748. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1749. {
  1750. struct rcu_node *rnp = rdp->mynode;
  1751. rdp->n_rcu_pending++;
  1752. /* Check for CPU stalls, if enabled. */
  1753. check_cpu_stall(rsp, rdp);
  1754. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1755. if (rdp->qs_pending && !rdp->passed_quiesc) {
  1756. /*
  1757. * If force_quiescent_state() coming soon and this CPU
  1758. * needs a quiescent state, and this is either RCU-sched
  1759. * or RCU-bh, force a local reschedule.
  1760. */
  1761. rdp->n_rp_qs_pending++;
  1762. if (!rdp->preemptible &&
  1763. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs) - 1,
  1764. jiffies))
  1765. set_need_resched();
  1766. } else if (rdp->qs_pending && rdp->passed_quiesc) {
  1767. rdp->n_rp_report_qs++;
  1768. return 1;
  1769. }
  1770. /* Does this CPU have callbacks ready to invoke? */
  1771. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  1772. rdp->n_rp_cb_ready++;
  1773. return 1;
  1774. }
  1775. /* Has RCU gone idle with this CPU needing another grace period? */
  1776. if (cpu_needs_another_gp(rsp, rdp)) {
  1777. rdp->n_rp_cpu_needs_gp++;
  1778. return 1;
  1779. }
  1780. /* Has another RCU grace period completed? */
  1781. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  1782. rdp->n_rp_gp_completed++;
  1783. return 1;
  1784. }
  1785. /* Has a new RCU grace period started? */
  1786. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  1787. rdp->n_rp_gp_started++;
  1788. return 1;
  1789. }
  1790. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1791. if (rcu_gp_in_progress(rsp) &&
  1792. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
  1793. rdp->n_rp_need_fqs++;
  1794. return 1;
  1795. }
  1796. /* nothing to do */
  1797. rdp->n_rp_need_nothing++;
  1798. return 0;
  1799. }
  1800. /*
  1801. * Check to see if there is any immediate RCU-related work to be done
  1802. * by the current CPU, returning 1 if so. This function is part of the
  1803. * RCU implementation; it is -not- an exported member of the RCU API.
  1804. */
  1805. static int rcu_pending(int cpu)
  1806. {
  1807. return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
  1808. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
  1809. rcu_preempt_pending(cpu);
  1810. }
  1811. /*
  1812. * Check to see if any future RCU-related work will need to be done
  1813. * by the current CPU, even if none need be done immediately, returning
  1814. * 1 if so.
  1815. */
  1816. static int rcu_needs_cpu_quick_check(int cpu)
  1817. {
  1818. /* RCU callbacks either ready or pending? */
  1819. return per_cpu(rcu_sched_data, cpu).nxtlist ||
  1820. per_cpu(rcu_bh_data, cpu).nxtlist ||
  1821. rcu_preempt_needs_cpu(cpu);
  1822. }
  1823. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  1824. static atomic_t rcu_barrier_cpu_count;
  1825. static DEFINE_MUTEX(rcu_barrier_mutex);
  1826. static struct completion rcu_barrier_completion;
  1827. static void rcu_barrier_callback(struct rcu_head *notused)
  1828. {
  1829. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1830. complete(&rcu_barrier_completion);
  1831. }
  1832. /*
  1833. * Called with preemption disabled, and from cross-cpu IRQ context.
  1834. */
  1835. static void rcu_barrier_func(void *type)
  1836. {
  1837. int cpu = smp_processor_id();
  1838. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  1839. void (*call_rcu_func)(struct rcu_head *head,
  1840. void (*func)(struct rcu_head *head));
  1841. atomic_inc(&rcu_barrier_cpu_count);
  1842. call_rcu_func = type;
  1843. call_rcu_func(head, rcu_barrier_callback);
  1844. }
  1845. /*
  1846. * Orchestrate the specified type of RCU barrier, waiting for all
  1847. * RCU callbacks of the specified type to complete.
  1848. */
  1849. static void _rcu_barrier(struct rcu_state *rsp,
  1850. void (*call_rcu_func)(struct rcu_head *head,
  1851. void (*func)(struct rcu_head *head)))
  1852. {
  1853. BUG_ON(in_interrupt());
  1854. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  1855. mutex_lock(&rcu_barrier_mutex);
  1856. init_completion(&rcu_barrier_completion);
  1857. /*
  1858. * Initialize rcu_barrier_cpu_count to 1, then invoke
  1859. * rcu_barrier_func() on each CPU, so that each CPU also has
  1860. * incremented rcu_barrier_cpu_count. Only then is it safe to
  1861. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  1862. * might complete its grace period before all of the other CPUs
  1863. * did their increment, causing this function to return too
  1864. * early. Note that on_each_cpu() disables irqs, which prevents
  1865. * any CPUs from coming online or going offline until each online
  1866. * CPU has queued its RCU-barrier callback.
  1867. */
  1868. atomic_set(&rcu_barrier_cpu_count, 1);
  1869. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  1870. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1871. complete(&rcu_barrier_completion);
  1872. wait_for_completion(&rcu_barrier_completion);
  1873. mutex_unlock(&rcu_barrier_mutex);
  1874. }
  1875. /**
  1876. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  1877. */
  1878. void rcu_barrier_bh(void)
  1879. {
  1880. _rcu_barrier(&rcu_bh_state, call_rcu_bh);
  1881. }
  1882. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  1883. /**
  1884. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  1885. */
  1886. void rcu_barrier_sched(void)
  1887. {
  1888. _rcu_barrier(&rcu_sched_state, call_rcu_sched);
  1889. }
  1890. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  1891. /*
  1892. * Do boot-time initialization of a CPU's per-CPU RCU data.
  1893. */
  1894. static void __init
  1895. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  1896. {
  1897. unsigned long flags;
  1898. int i;
  1899. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1900. struct rcu_node *rnp = rcu_get_root(rsp);
  1901. /* Set up local state, ensuring consistent view of global state. */
  1902. raw_spin_lock_irqsave(&rnp->lock, flags);
  1903. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1904. rdp->nxtlist = NULL;
  1905. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1906. rdp->nxttail[i] = &rdp->nxtlist;
  1907. rdp->qlen = 0;
  1908. #ifdef CONFIG_NO_HZ
  1909. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1910. #endif /* #ifdef CONFIG_NO_HZ */
  1911. rdp->cpu = cpu;
  1912. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1913. }
  1914. /*
  1915. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  1916. * offline event can be happening at a given time. Note also that we
  1917. * can accept some slop in the rsp->completed access due to the fact
  1918. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  1919. */
  1920. static void __cpuinit
  1921. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
  1922. {
  1923. unsigned long flags;
  1924. unsigned long mask;
  1925. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1926. struct rcu_node *rnp = rcu_get_root(rsp);
  1927. /* Set up local state, ensuring consistent view of global state. */
  1928. raw_spin_lock_irqsave(&rnp->lock, flags);
  1929. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1930. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1931. rdp->beenonline = 1; /* We have now been online. */
  1932. rdp->preemptible = preemptible;
  1933. rdp->qlen_last_fqs_check = 0;
  1934. rdp->n_force_qs_snap = rsp->n_force_qs;
  1935. rdp->blimit = blimit;
  1936. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1937. /*
  1938. * A new grace period might start here. If so, we won't be part
  1939. * of it, but that is OK, as we are currently in a quiescent state.
  1940. */
  1941. /* Exclude any attempts to start a new GP on large systems. */
  1942. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1943. /* Add CPU to rcu_node bitmasks. */
  1944. rnp = rdp->mynode;
  1945. mask = rdp->grpmask;
  1946. do {
  1947. /* Exclude any attempts to start a new GP on small systems. */
  1948. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1949. rnp->qsmaskinit |= mask;
  1950. mask = rnp->grpmask;
  1951. if (rnp == rdp->mynode) {
  1952. rdp->gpnum = rnp->completed; /* if GP in progress... */
  1953. rdp->completed = rnp->completed;
  1954. rdp->passed_quiesc_completed = rnp->completed - 1;
  1955. }
  1956. raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
  1957. rnp = rnp->parent;
  1958. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1959. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  1960. }
  1961. static void __cpuinit rcu_online_cpu(int cpu)
  1962. {
  1963. rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
  1964. rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
  1965. rcu_preempt_init_percpu_data(cpu);
  1966. }
  1967. static void __cpuinit rcu_online_kthreads(int cpu)
  1968. {
  1969. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1970. struct rcu_node *rnp = rdp->mynode;
  1971. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1972. if (rcu_kthreads_spawnable) {
  1973. (void)rcu_spawn_one_cpu_kthread(cpu);
  1974. if (rnp->node_kthread_task == NULL)
  1975. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1976. }
  1977. }
  1978. /*
  1979. * Handle CPU online/offline notification events.
  1980. */
  1981. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1982. unsigned long action, void *hcpu)
  1983. {
  1984. long cpu = (long)hcpu;
  1985. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1986. struct rcu_node *rnp = rdp->mynode;
  1987. switch (action) {
  1988. case CPU_UP_PREPARE:
  1989. case CPU_UP_PREPARE_FROZEN:
  1990. rcu_online_cpu(cpu);
  1991. rcu_online_kthreads(cpu);
  1992. break;
  1993. case CPU_ONLINE:
  1994. case CPU_DOWN_FAILED:
  1995. rcu_node_kthread_setaffinity(rnp, -1);
  1996. rcu_cpu_kthread_setrt(cpu, 1);
  1997. break;
  1998. case CPU_DOWN_PREPARE:
  1999. rcu_node_kthread_setaffinity(rnp, cpu);
  2000. rcu_cpu_kthread_setrt(cpu, 0);
  2001. break;
  2002. case CPU_DYING:
  2003. case CPU_DYING_FROZEN:
  2004. /*
  2005. * The whole machine is "stopped" except this CPU, so we can
  2006. * touch any data without introducing corruption. We send the
  2007. * dying CPU's callbacks to an arbitrarily chosen online CPU.
  2008. */
  2009. rcu_send_cbs_to_online(&rcu_bh_state);
  2010. rcu_send_cbs_to_online(&rcu_sched_state);
  2011. rcu_preempt_send_cbs_to_online();
  2012. break;
  2013. case CPU_DEAD:
  2014. case CPU_DEAD_FROZEN:
  2015. case CPU_UP_CANCELED:
  2016. case CPU_UP_CANCELED_FROZEN:
  2017. rcu_offline_cpu(cpu);
  2018. break;
  2019. default:
  2020. break;
  2021. }
  2022. return NOTIFY_OK;
  2023. }
  2024. /*
  2025. * This function is invoked towards the end of the scheduler's initialization
  2026. * process. Before this is called, the idle task might contain
  2027. * RCU read-side critical sections (during which time, this idle
  2028. * task is booting the system). After this function is called, the
  2029. * idle tasks are prohibited from containing RCU read-side critical
  2030. * sections. This function also enables RCU lockdep checking.
  2031. */
  2032. void rcu_scheduler_starting(void)
  2033. {
  2034. WARN_ON(num_online_cpus() != 1);
  2035. WARN_ON(nr_context_switches() > 0);
  2036. rcu_scheduler_active = 1;
  2037. }
  2038. /*
  2039. * Compute the per-level fanout, either using the exact fanout specified
  2040. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  2041. */
  2042. #ifdef CONFIG_RCU_FANOUT_EXACT
  2043. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  2044. {
  2045. int i;
  2046. for (i = NUM_RCU_LVLS - 1; i > 0; i--)
  2047. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  2048. rsp->levelspread[0] = RCU_FANOUT_LEAF;
  2049. }
  2050. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  2051. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  2052. {
  2053. int ccur;
  2054. int cprv;
  2055. int i;
  2056. cprv = NR_CPUS;
  2057. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  2058. ccur = rsp->levelcnt[i];
  2059. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  2060. cprv = ccur;
  2061. }
  2062. }
  2063. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  2064. /*
  2065. * Helper function for rcu_init() that initializes one rcu_state structure.
  2066. */
  2067. static void __init rcu_init_one(struct rcu_state *rsp,
  2068. struct rcu_data __percpu *rda)
  2069. {
  2070. static char *buf[] = { "rcu_node_level_0",
  2071. "rcu_node_level_1",
  2072. "rcu_node_level_2",
  2073. "rcu_node_level_3" }; /* Match MAX_RCU_LVLS */
  2074. int cpustride = 1;
  2075. int i;
  2076. int j;
  2077. struct rcu_node *rnp;
  2078. BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  2079. /* Initialize the level-tracking arrays. */
  2080. for (i = 1; i < NUM_RCU_LVLS; i++)
  2081. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  2082. rcu_init_levelspread(rsp);
  2083. /* Initialize the elements themselves, starting from the leaves. */
  2084. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  2085. cpustride *= rsp->levelspread[i];
  2086. rnp = rsp->level[i];
  2087. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  2088. raw_spin_lock_init(&rnp->lock);
  2089. lockdep_set_class_and_name(&rnp->lock,
  2090. &rcu_node_class[i], buf[i]);
  2091. rnp->gpnum = 0;
  2092. rnp->qsmask = 0;
  2093. rnp->qsmaskinit = 0;
  2094. rnp->grplo = j * cpustride;
  2095. rnp->grphi = (j + 1) * cpustride - 1;
  2096. if (rnp->grphi >= NR_CPUS)
  2097. rnp->grphi = NR_CPUS - 1;
  2098. if (i == 0) {
  2099. rnp->grpnum = 0;
  2100. rnp->grpmask = 0;
  2101. rnp->parent = NULL;
  2102. } else {
  2103. rnp->grpnum = j % rsp->levelspread[i - 1];
  2104. rnp->grpmask = 1UL << rnp->grpnum;
  2105. rnp->parent = rsp->level[i - 1] +
  2106. j / rsp->levelspread[i - 1];
  2107. }
  2108. rnp->level = i;
  2109. INIT_LIST_HEAD(&rnp->blkd_tasks);
  2110. }
  2111. }
  2112. rsp->rda = rda;
  2113. rnp = rsp->level[NUM_RCU_LVLS - 1];
  2114. for_each_possible_cpu(i) {
  2115. while (i > rnp->grphi)
  2116. rnp++;
  2117. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  2118. rcu_boot_init_percpu_data(i, rsp);
  2119. }
  2120. }
  2121. void __init rcu_init(void)
  2122. {
  2123. int cpu;
  2124. rcu_bootup_announce();
  2125. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  2126. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  2127. __rcu_init_preempt();
  2128. /*
  2129. * We don't need protection against CPU-hotplug here because
  2130. * this is called early in boot, before either interrupts
  2131. * or the scheduler are operational.
  2132. */
  2133. cpu_notifier(rcu_cpu_notify, 0);
  2134. for_each_online_cpu(cpu)
  2135. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  2136. check_cpu_stall_init();
  2137. }
  2138. #include "rcutree_plugin.h"