rcutree.c 79 KB

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