rcutree.c 70 KB

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