rcutree_plugin.h 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  3. * Internal non-public definitions that provide either classic
  4. * or preemptible semantics.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * Copyright Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/gfp.h>
  28. #include <linux/oom.h>
  29. #include <linux/smpboot.h>
  30. #include <linux/tick.h>
  31. #define RCU_KTHREAD_PRIO 1
  32. #ifdef CONFIG_RCU_BOOST
  33. #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
  34. #else
  35. #define RCU_BOOST_PRIO RCU_KTHREAD_PRIO
  36. #endif
  37. #ifdef CONFIG_RCU_NOCB_CPU
  38. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  39. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  40. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  41. static char __initdata nocb_buf[NR_CPUS * 5];
  42. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  43. /*
  44. * Check the RCU kernel configuration parameters and print informative
  45. * messages about anything out of the ordinary. If you like #ifdef, you
  46. * will love this function.
  47. */
  48. static void __init rcu_bootup_announce_oddness(void)
  49. {
  50. #ifdef CONFIG_RCU_TRACE
  51. pr_info("\tRCU debugfs-based tracing is enabled.\n");
  52. #endif
  53. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  54. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  55. CONFIG_RCU_FANOUT);
  56. #endif
  57. #ifdef CONFIG_RCU_FANOUT_EXACT
  58. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  59. #endif
  60. #ifdef CONFIG_RCU_FAST_NO_HZ
  61. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  62. #endif
  63. #ifdef CONFIG_PROVE_RCU
  64. pr_info("\tRCU lockdep checking is enabled.\n");
  65. #endif
  66. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  67. pr_info("\tRCU torture testing starts during boot.\n");
  68. #endif
  69. #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
  70. pr_info("\tDump stacks of tasks blocking RCU-preempt GP.\n");
  71. #endif
  72. #if defined(CONFIG_RCU_CPU_STALL_INFO)
  73. pr_info("\tAdditional per-CPU info printed with stalls.\n");
  74. #endif
  75. #if NUM_RCU_LVL_4 != 0
  76. pr_info("\tFour-level hierarchy is enabled.\n");
  77. #endif
  78. if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
  79. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
  80. if (nr_cpu_ids != NR_CPUS)
  81. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
  82. #ifdef CONFIG_RCU_NOCB_CPU
  83. #ifndef CONFIG_RCU_NOCB_CPU_NONE
  84. if (!have_rcu_nocb_mask) {
  85. zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL);
  86. have_rcu_nocb_mask = true;
  87. }
  88. #ifdef CONFIG_RCU_NOCB_CPU_ZERO
  89. pr_info("\tOffload RCU callbacks from CPU 0\n");
  90. cpumask_set_cpu(0, rcu_nocb_mask);
  91. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
  92. #ifdef CONFIG_RCU_NOCB_CPU_ALL
  93. pr_info("\tOffload RCU callbacks from all CPUs\n");
  94. cpumask_setall(rcu_nocb_mask);
  95. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
  96. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
  97. if (have_rcu_nocb_mask) {
  98. cpulist_scnprintf(nocb_buf, sizeof(nocb_buf), rcu_nocb_mask);
  99. pr_info("\tOffload RCU callbacks from CPUs: %s.\n", nocb_buf);
  100. if (rcu_nocb_poll)
  101. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  102. }
  103. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  104. }
  105. #ifdef CONFIG_TREE_PREEMPT_RCU
  106. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  107. static struct rcu_state *rcu_state = &rcu_preempt_state;
  108. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  109. /*
  110. * Tell them what RCU they are running.
  111. */
  112. static void __init rcu_bootup_announce(void)
  113. {
  114. pr_info("Preemptible hierarchical RCU implementation.\n");
  115. rcu_bootup_announce_oddness();
  116. }
  117. /*
  118. * Return the number of RCU-preempt batches processed thus far
  119. * for debug and statistics.
  120. */
  121. long rcu_batches_completed_preempt(void)
  122. {
  123. return rcu_preempt_state.completed;
  124. }
  125. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  126. /*
  127. * Return the number of RCU batches processed thus far for debug & stats.
  128. */
  129. long rcu_batches_completed(void)
  130. {
  131. return rcu_batches_completed_preempt();
  132. }
  133. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  134. /*
  135. * Force a quiescent state for preemptible RCU.
  136. */
  137. void rcu_force_quiescent_state(void)
  138. {
  139. force_quiescent_state(&rcu_preempt_state);
  140. }
  141. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  142. /*
  143. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  144. * that this just means that the task currently running on the CPU is
  145. * not in a quiescent state. There might be any number of tasks blocked
  146. * while in an RCU read-side critical section.
  147. *
  148. * Unlike the other rcu_*_qs() functions, callers to this function
  149. * must disable irqs in order to protect the assignment to
  150. * ->rcu_read_unlock_special.
  151. */
  152. static void rcu_preempt_qs(int cpu)
  153. {
  154. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  155. if (rdp->passed_quiesce == 0)
  156. trace_rcu_grace_period(TPS("rcu_preempt"), rdp->gpnum, TPS("cpuqs"));
  157. rdp->passed_quiesce = 1;
  158. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  159. }
  160. /*
  161. * We have entered the scheduler, and the current task might soon be
  162. * context-switched away from. If this task is in an RCU read-side
  163. * critical section, we will no longer be able to rely on the CPU to
  164. * record that fact, so we enqueue the task on the blkd_tasks list.
  165. * The task will dequeue itself when it exits the outermost enclosing
  166. * RCU read-side critical section. Therefore, the current grace period
  167. * cannot be permitted to complete until the blkd_tasks list entries
  168. * predating the current grace period drain, in other words, until
  169. * rnp->gp_tasks becomes NULL.
  170. *
  171. * Caller must disable preemption.
  172. */
  173. static void rcu_preempt_note_context_switch(int cpu)
  174. {
  175. struct task_struct *t = current;
  176. unsigned long flags;
  177. struct rcu_data *rdp;
  178. struct rcu_node *rnp;
  179. if (t->rcu_read_lock_nesting > 0 &&
  180. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  181. /* Possibly blocking in an RCU read-side critical section. */
  182. rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
  183. rnp = rdp->mynode;
  184. raw_spin_lock_irqsave(&rnp->lock, flags);
  185. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  186. t->rcu_blocked_node = rnp;
  187. /*
  188. * If this CPU has already checked in, then this task
  189. * will hold up the next grace period rather than the
  190. * current grace period. Queue the task accordingly.
  191. * If the task is queued for the current grace period
  192. * (i.e., this CPU has not yet passed through a quiescent
  193. * state for the current grace period), then as long
  194. * as that task remains queued, the current grace period
  195. * cannot end. Note that there is some uncertainty as
  196. * to exactly when the current grace period started.
  197. * We take a conservative approach, which can result
  198. * in unnecessarily waiting on tasks that started very
  199. * slightly after the current grace period began. C'est
  200. * la vie!!!
  201. *
  202. * But first, note that the current CPU must still be
  203. * on line!
  204. */
  205. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  206. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  207. if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
  208. list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
  209. rnp->gp_tasks = &t->rcu_node_entry;
  210. #ifdef CONFIG_RCU_BOOST
  211. if (rnp->boost_tasks != NULL)
  212. rnp->boost_tasks = rnp->gp_tasks;
  213. #endif /* #ifdef CONFIG_RCU_BOOST */
  214. } else {
  215. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  216. if (rnp->qsmask & rdp->grpmask)
  217. rnp->gp_tasks = &t->rcu_node_entry;
  218. }
  219. trace_rcu_preempt_task(rdp->rsp->name,
  220. t->pid,
  221. (rnp->qsmask & rdp->grpmask)
  222. ? rnp->gpnum
  223. : rnp->gpnum + 1);
  224. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  225. } else if (t->rcu_read_lock_nesting < 0 &&
  226. t->rcu_read_unlock_special) {
  227. /*
  228. * Complete exit from RCU read-side critical section on
  229. * behalf of preempted instance of __rcu_read_unlock().
  230. */
  231. rcu_read_unlock_special(t);
  232. }
  233. /*
  234. * Either we were not in an RCU read-side critical section to
  235. * begin with, or we have now recorded that critical section
  236. * globally. Either way, we can now note a quiescent state
  237. * for this CPU. Again, if we were in an RCU read-side critical
  238. * section, and if that critical section was blocking the current
  239. * grace period, then the fact that the task has been enqueued
  240. * means that we continue to block the current grace period.
  241. */
  242. local_irq_save(flags);
  243. rcu_preempt_qs(cpu);
  244. local_irq_restore(flags);
  245. }
  246. /*
  247. * Check for preempted RCU readers blocking the current grace period
  248. * for the specified rcu_node structure. If the caller needs a reliable
  249. * answer, it must hold the rcu_node's ->lock.
  250. */
  251. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  252. {
  253. return rnp->gp_tasks != NULL;
  254. }
  255. /*
  256. * Record a quiescent state for all tasks that were previously queued
  257. * on the specified rcu_node structure and that were blocking the current
  258. * RCU grace period. The caller must hold the specified rnp->lock with
  259. * irqs disabled, and this lock is released upon return, but irqs remain
  260. * disabled.
  261. */
  262. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  263. __releases(rnp->lock)
  264. {
  265. unsigned long mask;
  266. struct rcu_node *rnp_p;
  267. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  268. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  269. return; /* Still need more quiescent states! */
  270. }
  271. rnp_p = rnp->parent;
  272. if (rnp_p == NULL) {
  273. /*
  274. * Either there is only one rcu_node in the tree,
  275. * or tasks were kicked up to root rcu_node due to
  276. * CPUs going offline.
  277. */
  278. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  279. return;
  280. }
  281. /* Report up the rest of the hierarchy. */
  282. mask = rnp->grpmask;
  283. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  284. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  285. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  286. }
  287. /*
  288. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  289. * returning NULL if at the end of the list.
  290. */
  291. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  292. struct rcu_node *rnp)
  293. {
  294. struct list_head *np;
  295. np = t->rcu_node_entry.next;
  296. if (np == &rnp->blkd_tasks)
  297. np = NULL;
  298. return np;
  299. }
  300. /*
  301. * Handle special cases during rcu_read_unlock(), such as needing to
  302. * notify RCU core processing or task having blocked during the RCU
  303. * read-side critical section.
  304. */
  305. void rcu_read_unlock_special(struct task_struct *t)
  306. {
  307. int empty;
  308. int empty_exp;
  309. int empty_exp_now;
  310. unsigned long flags;
  311. struct list_head *np;
  312. #ifdef CONFIG_RCU_BOOST
  313. struct rt_mutex *rbmp = NULL;
  314. #endif /* #ifdef CONFIG_RCU_BOOST */
  315. struct rcu_node *rnp;
  316. int special;
  317. /* NMI handlers cannot block and cannot safely manipulate state. */
  318. if (in_nmi())
  319. return;
  320. local_irq_save(flags);
  321. /*
  322. * If RCU core is waiting for this CPU to exit critical section,
  323. * let it know that we have done so.
  324. */
  325. special = t->rcu_read_unlock_special;
  326. if (special & RCU_READ_UNLOCK_NEED_QS) {
  327. rcu_preempt_qs(smp_processor_id());
  328. }
  329. /* Hardware IRQ handlers cannot block. */
  330. if (in_irq() || in_serving_softirq()) {
  331. local_irq_restore(flags);
  332. return;
  333. }
  334. /* Clean up if blocked during RCU read-side critical section. */
  335. if (special & RCU_READ_UNLOCK_BLOCKED) {
  336. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  337. /*
  338. * Remove this task from the list it blocked on. The
  339. * task can migrate while we acquire the lock, but at
  340. * most one time. So at most two passes through loop.
  341. */
  342. for (;;) {
  343. rnp = t->rcu_blocked_node;
  344. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  345. if (rnp == t->rcu_blocked_node)
  346. break;
  347. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  348. }
  349. empty = !rcu_preempt_blocked_readers_cgp(rnp);
  350. empty_exp = !rcu_preempted_readers_exp(rnp);
  351. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  352. np = rcu_next_node_entry(t, rnp);
  353. list_del_init(&t->rcu_node_entry);
  354. t->rcu_blocked_node = NULL;
  355. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  356. rnp->gpnum, t->pid);
  357. if (&t->rcu_node_entry == rnp->gp_tasks)
  358. rnp->gp_tasks = np;
  359. if (&t->rcu_node_entry == rnp->exp_tasks)
  360. rnp->exp_tasks = np;
  361. #ifdef CONFIG_RCU_BOOST
  362. if (&t->rcu_node_entry == rnp->boost_tasks)
  363. rnp->boost_tasks = np;
  364. /* Snapshot/clear ->rcu_boost_mutex with rcu_node lock held. */
  365. if (t->rcu_boost_mutex) {
  366. rbmp = t->rcu_boost_mutex;
  367. t->rcu_boost_mutex = NULL;
  368. }
  369. #endif /* #ifdef CONFIG_RCU_BOOST */
  370. /*
  371. * If this was the last task on the current list, and if
  372. * we aren't waiting on any CPUs, report the quiescent state.
  373. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  374. * so we must take a snapshot of the expedited state.
  375. */
  376. empty_exp_now = !rcu_preempted_readers_exp(rnp);
  377. if (!empty && !rcu_preempt_blocked_readers_cgp(rnp)) {
  378. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  379. rnp->gpnum,
  380. 0, rnp->qsmask,
  381. rnp->level,
  382. rnp->grplo,
  383. rnp->grphi,
  384. !!rnp->gp_tasks);
  385. rcu_report_unblock_qs_rnp(rnp, flags);
  386. } else {
  387. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  388. }
  389. #ifdef CONFIG_RCU_BOOST
  390. /* Unboost if we were boosted. */
  391. if (rbmp)
  392. rt_mutex_unlock(rbmp);
  393. #endif /* #ifdef CONFIG_RCU_BOOST */
  394. /*
  395. * If this was the last task on the expedited lists,
  396. * then we need to report up the rcu_node hierarchy.
  397. */
  398. if (!empty_exp && empty_exp_now)
  399. rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
  400. } else {
  401. local_irq_restore(flags);
  402. }
  403. }
  404. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  405. /*
  406. * Dump detailed information for all tasks blocking the current RCU
  407. * grace period on the specified rcu_node structure.
  408. */
  409. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  410. {
  411. unsigned long flags;
  412. struct task_struct *t;
  413. raw_spin_lock_irqsave(&rnp->lock, flags);
  414. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  415. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  416. return;
  417. }
  418. t = list_entry(rnp->gp_tasks,
  419. struct task_struct, rcu_node_entry);
  420. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  421. sched_show_task(t);
  422. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  423. }
  424. /*
  425. * Dump detailed information for all tasks blocking the current RCU
  426. * grace period.
  427. */
  428. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  429. {
  430. struct rcu_node *rnp = rcu_get_root(rsp);
  431. rcu_print_detail_task_stall_rnp(rnp);
  432. rcu_for_each_leaf_node(rsp, rnp)
  433. rcu_print_detail_task_stall_rnp(rnp);
  434. }
  435. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  436. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  437. {
  438. }
  439. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  440. #ifdef CONFIG_RCU_CPU_STALL_INFO
  441. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  442. {
  443. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  444. rnp->level, rnp->grplo, rnp->grphi);
  445. }
  446. static void rcu_print_task_stall_end(void)
  447. {
  448. pr_cont("\n");
  449. }
  450. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  451. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  452. {
  453. }
  454. static void rcu_print_task_stall_end(void)
  455. {
  456. }
  457. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  458. /*
  459. * Scan the current list of tasks blocked within RCU read-side critical
  460. * sections, printing out the tid of each.
  461. */
  462. static int rcu_print_task_stall(struct rcu_node *rnp)
  463. {
  464. struct task_struct *t;
  465. int ndetected = 0;
  466. if (!rcu_preempt_blocked_readers_cgp(rnp))
  467. return 0;
  468. rcu_print_task_stall_begin(rnp);
  469. t = list_entry(rnp->gp_tasks,
  470. struct task_struct, rcu_node_entry);
  471. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  472. pr_cont(" P%d", t->pid);
  473. ndetected++;
  474. }
  475. rcu_print_task_stall_end();
  476. return ndetected;
  477. }
  478. /*
  479. * Check that the list of blocked tasks for the newly completed grace
  480. * period is in fact empty. It is a serious bug to complete a grace
  481. * period that still has RCU readers blocked! This function must be
  482. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  483. * must be held by the caller.
  484. *
  485. * Also, if there are blocked tasks on the list, they automatically
  486. * block the newly created grace period, so set up ->gp_tasks accordingly.
  487. */
  488. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  489. {
  490. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  491. if (!list_empty(&rnp->blkd_tasks))
  492. rnp->gp_tasks = rnp->blkd_tasks.next;
  493. WARN_ON_ONCE(rnp->qsmask);
  494. }
  495. #ifdef CONFIG_HOTPLUG_CPU
  496. /*
  497. * Handle tasklist migration for case in which all CPUs covered by the
  498. * specified rcu_node have gone offline. Move them up to the root
  499. * rcu_node. The reason for not just moving them to the immediate
  500. * parent is to remove the need for rcu_read_unlock_special() to
  501. * make more than two attempts to acquire the target rcu_node's lock.
  502. * Returns true if there were tasks blocking the current RCU grace
  503. * period.
  504. *
  505. * Returns 1 if there was previously a task blocking the current grace
  506. * period on the specified rcu_node structure.
  507. *
  508. * The caller must hold rnp->lock with irqs disabled.
  509. */
  510. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  511. struct rcu_node *rnp,
  512. struct rcu_data *rdp)
  513. {
  514. struct list_head *lp;
  515. struct list_head *lp_root;
  516. int retval = 0;
  517. struct rcu_node *rnp_root = rcu_get_root(rsp);
  518. struct task_struct *t;
  519. if (rnp == rnp_root) {
  520. WARN_ONCE(1, "Last CPU thought to be offlined?");
  521. return 0; /* Shouldn't happen: at least one CPU online. */
  522. }
  523. /* If we are on an internal node, complain bitterly. */
  524. WARN_ON_ONCE(rnp != rdp->mynode);
  525. /*
  526. * Move tasks up to root rcu_node. Don't try to get fancy for
  527. * this corner-case operation -- just put this node's tasks
  528. * at the head of the root node's list, and update the root node's
  529. * ->gp_tasks and ->exp_tasks pointers to those of this node's,
  530. * if non-NULL. This might result in waiting for more tasks than
  531. * absolutely necessary, but this is a good performance/complexity
  532. * tradeoff.
  533. */
  534. if (rcu_preempt_blocked_readers_cgp(rnp) && rnp->qsmask == 0)
  535. retval |= RCU_OFL_TASKS_NORM_GP;
  536. if (rcu_preempted_readers_exp(rnp))
  537. retval |= RCU_OFL_TASKS_EXP_GP;
  538. lp = &rnp->blkd_tasks;
  539. lp_root = &rnp_root->blkd_tasks;
  540. while (!list_empty(lp)) {
  541. t = list_entry(lp->next, typeof(*t), rcu_node_entry);
  542. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  543. list_del(&t->rcu_node_entry);
  544. t->rcu_blocked_node = rnp_root;
  545. list_add(&t->rcu_node_entry, lp_root);
  546. if (&t->rcu_node_entry == rnp->gp_tasks)
  547. rnp_root->gp_tasks = rnp->gp_tasks;
  548. if (&t->rcu_node_entry == rnp->exp_tasks)
  549. rnp_root->exp_tasks = rnp->exp_tasks;
  550. #ifdef CONFIG_RCU_BOOST
  551. if (&t->rcu_node_entry == rnp->boost_tasks)
  552. rnp_root->boost_tasks = rnp->boost_tasks;
  553. #endif /* #ifdef CONFIG_RCU_BOOST */
  554. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  555. }
  556. rnp->gp_tasks = NULL;
  557. rnp->exp_tasks = NULL;
  558. #ifdef CONFIG_RCU_BOOST
  559. rnp->boost_tasks = NULL;
  560. /*
  561. * In case root is being boosted and leaf was not. Make sure
  562. * that we boost the tasks blocking the current grace period
  563. * in this case.
  564. */
  565. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  566. if (rnp_root->boost_tasks != NULL &&
  567. rnp_root->boost_tasks != rnp_root->gp_tasks &&
  568. rnp_root->boost_tasks != rnp_root->exp_tasks)
  569. rnp_root->boost_tasks = rnp_root->gp_tasks;
  570. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  571. #endif /* #ifdef CONFIG_RCU_BOOST */
  572. return retval;
  573. }
  574. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  575. /*
  576. * Check for a quiescent state from the current CPU. When a task blocks,
  577. * the task is recorded in the corresponding CPU's rcu_node structure,
  578. * which is checked elsewhere.
  579. *
  580. * Caller must disable hard irqs.
  581. */
  582. static void rcu_preempt_check_callbacks(int cpu)
  583. {
  584. struct task_struct *t = current;
  585. if (t->rcu_read_lock_nesting == 0) {
  586. rcu_preempt_qs(cpu);
  587. return;
  588. }
  589. if (t->rcu_read_lock_nesting > 0 &&
  590. per_cpu(rcu_preempt_data, cpu).qs_pending)
  591. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  592. }
  593. #ifdef CONFIG_RCU_BOOST
  594. static void rcu_preempt_do_callbacks(void)
  595. {
  596. rcu_do_batch(&rcu_preempt_state, &__get_cpu_var(rcu_preempt_data));
  597. }
  598. #endif /* #ifdef CONFIG_RCU_BOOST */
  599. /*
  600. * Queue a preemptible-RCU callback for invocation after a grace period.
  601. */
  602. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  603. {
  604. __call_rcu(head, func, &rcu_preempt_state, -1, 0);
  605. }
  606. EXPORT_SYMBOL_GPL(call_rcu);
  607. /*
  608. * Queue an RCU callback for lazy invocation after a grace period.
  609. * This will likely be later named something like "call_rcu_lazy()",
  610. * but this change will require some way of tagging the lazy RCU
  611. * callbacks in the list of pending callbacks. Until then, this
  612. * function may only be called from __kfree_rcu().
  613. */
  614. void kfree_call_rcu(struct rcu_head *head,
  615. void (*func)(struct rcu_head *rcu))
  616. {
  617. __call_rcu(head, func, &rcu_preempt_state, -1, 1);
  618. }
  619. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  620. /**
  621. * synchronize_rcu - wait until a grace period has elapsed.
  622. *
  623. * Control will return to the caller some time after a full grace
  624. * period has elapsed, in other words after all currently executing RCU
  625. * read-side critical sections have completed. Note, however, that
  626. * upon return from synchronize_rcu(), the caller might well be executing
  627. * concurrently with new RCU read-side critical sections that began while
  628. * synchronize_rcu() was waiting. RCU read-side critical sections are
  629. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  630. *
  631. * See the description of synchronize_sched() for more detailed information
  632. * on memory ordering guarantees.
  633. */
  634. void synchronize_rcu(void)
  635. {
  636. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  637. !lock_is_held(&rcu_lock_map) &&
  638. !lock_is_held(&rcu_sched_lock_map),
  639. "Illegal synchronize_rcu() in RCU read-side critical section");
  640. if (!rcu_scheduler_active)
  641. return;
  642. if (rcu_expedited)
  643. synchronize_rcu_expedited();
  644. else
  645. wait_rcu_gp(call_rcu);
  646. }
  647. EXPORT_SYMBOL_GPL(synchronize_rcu);
  648. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  649. static unsigned long sync_rcu_preempt_exp_count;
  650. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  651. /*
  652. * Return non-zero if there are any tasks in RCU read-side critical
  653. * sections blocking the current preemptible-RCU expedited grace period.
  654. * If there is no preemptible-RCU expedited grace period currently in
  655. * progress, returns zero unconditionally.
  656. */
  657. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  658. {
  659. return rnp->exp_tasks != NULL;
  660. }
  661. /*
  662. * return non-zero if there is no RCU expedited grace period in progress
  663. * for the specified rcu_node structure, in other words, if all CPUs and
  664. * tasks covered by the specified rcu_node structure have done their bit
  665. * for the current expedited grace period. Works only for preemptible
  666. * RCU -- other RCU implementation use other means.
  667. *
  668. * Caller must hold sync_rcu_preempt_exp_mutex.
  669. */
  670. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  671. {
  672. return !rcu_preempted_readers_exp(rnp) &&
  673. ACCESS_ONCE(rnp->expmask) == 0;
  674. }
  675. /*
  676. * Report the exit from RCU read-side critical section for the last task
  677. * that queued itself during or before the current expedited preemptible-RCU
  678. * grace period. This event is reported either to the rcu_node structure on
  679. * which the task was queued or to one of that rcu_node structure's ancestors,
  680. * recursively up the tree. (Calm down, calm down, we do the recursion
  681. * iteratively!)
  682. *
  683. * Most callers will set the "wake" flag, but the task initiating the
  684. * expedited grace period need not wake itself.
  685. *
  686. * Caller must hold sync_rcu_preempt_exp_mutex.
  687. */
  688. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  689. bool wake)
  690. {
  691. unsigned long flags;
  692. unsigned long mask;
  693. raw_spin_lock_irqsave(&rnp->lock, flags);
  694. for (;;) {
  695. if (!sync_rcu_preempt_exp_done(rnp)) {
  696. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  697. break;
  698. }
  699. if (rnp->parent == NULL) {
  700. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  701. if (wake)
  702. wake_up(&sync_rcu_preempt_exp_wq);
  703. break;
  704. }
  705. mask = rnp->grpmask;
  706. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  707. rnp = rnp->parent;
  708. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  709. rnp->expmask &= ~mask;
  710. }
  711. }
  712. /*
  713. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  714. * grace period for the specified rcu_node structure. If there are no such
  715. * tasks, report it up the rcu_node hierarchy.
  716. *
  717. * Caller must hold sync_rcu_preempt_exp_mutex and must exclude
  718. * CPU hotplug operations.
  719. */
  720. static void
  721. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  722. {
  723. unsigned long flags;
  724. int must_wait = 0;
  725. raw_spin_lock_irqsave(&rnp->lock, flags);
  726. if (list_empty(&rnp->blkd_tasks)) {
  727. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  728. } else {
  729. rnp->exp_tasks = rnp->blkd_tasks.next;
  730. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  731. must_wait = 1;
  732. }
  733. if (!must_wait)
  734. rcu_report_exp_rnp(rsp, rnp, false); /* Don't wake self. */
  735. }
  736. /**
  737. * synchronize_rcu_expedited - Brute-force RCU grace period
  738. *
  739. * Wait for an RCU-preempt grace period, but expedite it. The basic
  740. * idea is to invoke synchronize_sched_expedited() to push all the tasks to
  741. * the ->blkd_tasks lists and wait for this list to drain. This consumes
  742. * significant time on all CPUs and is unfriendly to real-time workloads,
  743. * so is thus not recommended for any sort of common-case code.
  744. * In fact, if you are using synchronize_rcu_expedited() in a loop,
  745. * please restructure your code to batch your updates, and then Use a
  746. * single synchronize_rcu() instead.
  747. *
  748. * Note that it is illegal to call this function while holding any lock
  749. * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
  750. * to call this function from a CPU-hotplug notifier. Failing to observe
  751. * these restriction will result in deadlock.
  752. */
  753. void synchronize_rcu_expedited(void)
  754. {
  755. unsigned long flags;
  756. struct rcu_node *rnp;
  757. struct rcu_state *rsp = &rcu_preempt_state;
  758. unsigned long snap;
  759. int trycount = 0;
  760. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  761. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  762. smp_mb(); /* Above access cannot bleed into critical section. */
  763. /*
  764. * Block CPU-hotplug operations. This means that any CPU-hotplug
  765. * operation that finds an rcu_node structure with tasks in the
  766. * process of being boosted will know that all tasks blocking
  767. * this expedited grace period will already be in the process of
  768. * being boosted. This simplifies the process of moving tasks
  769. * from leaf to root rcu_node structures.
  770. */
  771. get_online_cpus();
  772. /*
  773. * Acquire lock, falling back to synchronize_rcu() if too many
  774. * lock-acquisition failures. Of course, if someone does the
  775. * expedited grace period for us, just leave.
  776. */
  777. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  778. if (ULONG_CMP_LT(snap,
  779. ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  780. put_online_cpus();
  781. goto mb_ret; /* Others did our work for us. */
  782. }
  783. if (trycount++ < 10) {
  784. udelay(trycount * num_online_cpus());
  785. } else {
  786. put_online_cpus();
  787. wait_rcu_gp(call_rcu);
  788. return;
  789. }
  790. }
  791. if (ULONG_CMP_LT(snap, ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  792. put_online_cpus();
  793. goto unlock_mb_ret; /* Others did our work for us. */
  794. }
  795. /* force all RCU readers onto ->blkd_tasks lists. */
  796. synchronize_sched_expedited();
  797. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  798. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  799. raw_spin_lock_irqsave(&rnp->lock, flags);
  800. rnp->expmask = rnp->qsmaskinit;
  801. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  802. }
  803. /* Snapshot current state of ->blkd_tasks lists. */
  804. rcu_for_each_leaf_node(rsp, rnp)
  805. sync_rcu_preempt_exp_init(rsp, rnp);
  806. if (NUM_RCU_NODES > 1)
  807. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  808. put_online_cpus();
  809. /* Wait for snapshotted ->blkd_tasks lists to drain. */
  810. rnp = rcu_get_root(rsp);
  811. wait_event(sync_rcu_preempt_exp_wq,
  812. sync_rcu_preempt_exp_done(rnp));
  813. /* Clean up and exit. */
  814. smp_mb(); /* ensure expedited GP seen before counter increment. */
  815. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  816. unlock_mb_ret:
  817. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  818. mb_ret:
  819. smp_mb(); /* ensure subsequent action seen after grace period. */
  820. }
  821. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  822. /**
  823. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  824. *
  825. * Note that this primitive does not necessarily wait for an RCU grace period
  826. * to complete. For example, if there are no RCU callbacks queued anywhere
  827. * in the system, then rcu_barrier() is within its rights to return
  828. * immediately, without waiting for anything, much less an RCU grace period.
  829. */
  830. void rcu_barrier(void)
  831. {
  832. _rcu_barrier(&rcu_preempt_state);
  833. }
  834. EXPORT_SYMBOL_GPL(rcu_barrier);
  835. /*
  836. * Initialize preemptible RCU's state structures.
  837. */
  838. static void __init __rcu_init_preempt(void)
  839. {
  840. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  841. }
  842. /*
  843. * Check for a task exiting while in a preemptible-RCU read-side
  844. * critical section, clean up if so. No need to issue warnings,
  845. * as debug_check_no_locks_held() already does this if lockdep
  846. * is enabled.
  847. */
  848. void exit_rcu(void)
  849. {
  850. struct task_struct *t = current;
  851. if (likely(list_empty(&current->rcu_node_entry)))
  852. return;
  853. t->rcu_read_lock_nesting = 1;
  854. barrier();
  855. t->rcu_read_unlock_special = RCU_READ_UNLOCK_BLOCKED;
  856. __rcu_read_unlock();
  857. }
  858. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  859. static struct rcu_state *rcu_state = &rcu_sched_state;
  860. /*
  861. * Tell them what RCU they are running.
  862. */
  863. static void __init rcu_bootup_announce(void)
  864. {
  865. pr_info("Hierarchical RCU implementation.\n");
  866. rcu_bootup_announce_oddness();
  867. }
  868. /*
  869. * Return the number of RCU batches processed thus far for debug & stats.
  870. */
  871. long rcu_batches_completed(void)
  872. {
  873. return rcu_batches_completed_sched();
  874. }
  875. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  876. /*
  877. * Force a quiescent state for RCU, which, because there is no preemptible
  878. * RCU, becomes the same as rcu-sched.
  879. */
  880. void rcu_force_quiescent_state(void)
  881. {
  882. rcu_sched_force_quiescent_state();
  883. }
  884. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  885. /*
  886. * Because preemptible RCU does not exist, we never have to check for
  887. * CPUs being in quiescent states.
  888. */
  889. static void rcu_preempt_note_context_switch(int cpu)
  890. {
  891. }
  892. /*
  893. * Because preemptible RCU does not exist, there are never any preempted
  894. * RCU readers.
  895. */
  896. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  897. {
  898. return 0;
  899. }
  900. #ifdef CONFIG_HOTPLUG_CPU
  901. /* Because preemptible RCU does not exist, no quieting of tasks. */
  902. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  903. {
  904. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  905. }
  906. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  907. /*
  908. * Because preemptible RCU does not exist, we never have to check for
  909. * tasks blocked within RCU read-side critical sections.
  910. */
  911. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  912. {
  913. }
  914. /*
  915. * Because preemptible RCU does not exist, we never have to check for
  916. * tasks blocked within RCU read-side critical sections.
  917. */
  918. static int rcu_print_task_stall(struct rcu_node *rnp)
  919. {
  920. return 0;
  921. }
  922. /*
  923. * Because there is no preemptible RCU, there can be no readers blocked,
  924. * so there is no need to check for blocked tasks. So check only for
  925. * bogus qsmask values.
  926. */
  927. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  928. {
  929. WARN_ON_ONCE(rnp->qsmask);
  930. }
  931. #ifdef CONFIG_HOTPLUG_CPU
  932. /*
  933. * Because preemptible RCU does not exist, it never needs to migrate
  934. * tasks that were blocked within RCU read-side critical sections, and
  935. * such non-existent tasks cannot possibly have been blocking the current
  936. * grace period.
  937. */
  938. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  939. struct rcu_node *rnp,
  940. struct rcu_data *rdp)
  941. {
  942. return 0;
  943. }
  944. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  945. /*
  946. * Because preemptible RCU does not exist, it never has any callbacks
  947. * to check.
  948. */
  949. static void rcu_preempt_check_callbacks(int cpu)
  950. {
  951. }
  952. /*
  953. * Queue an RCU callback for lazy invocation after a grace period.
  954. * This will likely be later named something like "call_rcu_lazy()",
  955. * but this change will require some way of tagging the lazy RCU
  956. * callbacks in the list of pending callbacks. Until then, this
  957. * function may only be called from __kfree_rcu().
  958. *
  959. * Because there is no preemptible RCU, we use RCU-sched instead.
  960. */
  961. void kfree_call_rcu(struct rcu_head *head,
  962. void (*func)(struct rcu_head *rcu))
  963. {
  964. __call_rcu(head, func, &rcu_sched_state, -1, 1);
  965. }
  966. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  967. /*
  968. * Wait for an rcu-preempt grace period, but make it happen quickly.
  969. * But because preemptible RCU does not exist, map to rcu-sched.
  970. */
  971. void synchronize_rcu_expedited(void)
  972. {
  973. synchronize_sched_expedited();
  974. }
  975. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  976. #ifdef CONFIG_HOTPLUG_CPU
  977. /*
  978. * Because preemptible RCU does not exist, there is never any need to
  979. * report on tasks preempted in RCU read-side critical sections during
  980. * expedited RCU grace periods.
  981. */
  982. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  983. bool wake)
  984. {
  985. }
  986. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  987. /*
  988. * Because preemptible RCU does not exist, rcu_barrier() is just
  989. * another name for rcu_barrier_sched().
  990. */
  991. void rcu_barrier(void)
  992. {
  993. rcu_barrier_sched();
  994. }
  995. EXPORT_SYMBOL_GPL(rcu_barrier);
  996. /*
  997. * Because preemptible RCU does not exist, it need not be initialized.
  998. */
  999. static void __init __rcu_init_preempt(void)
  1000. {
  1001. }
  1002. /*
  1003. * Because preemptible RCU does not exist, tasks cannot possibly exit
  1004. * while in preemptible RCU read-side critical sections.
  1005. */
  1006. void exit_rcu(void)
  1007. {
  1008. }
  1009. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  1010. #ifdef CONFIG_RCU_BOOST
  1011. #include "rtmutex_common.h"
  1012. #ifdef CONFIG_RCU_TRACE
  1013. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  1014. {
  1015. if (list_empty(&rnp->blkd_tasks))
  1016. rnp->n_balk_blkd_tasks++;
  1017. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  1018. rnp->n_balk_exp_gp_tasks++;
  1019. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  1020. rnp->n_balk_boost_tasks++;
  1021. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  1022. rnp->n_balk_notblocked++;
  1023. else if (rnp->gp_tasks != NULL &&
  1024. ULONG_CMP_LT(jiffies, rnp->boost_time))
  1025. rnp->n_balk_notyet++;
  1026. else
  1027. rnp->n_balk_nos++;
  1028. }
  1029. #else /* #ifdef CONFIG_RCU_TRACE */
  1030. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  1031. {
  1032. }
  1033. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  1034. static void rcu_wake_cond(struct task_struct *t, int status)
  1035. {
  1036. /*
  1037. * If the thread is yielding, only wake it when this
  1038. * is invoked from idle
  1039. */
  1040. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  1041. wake_up_process(t);
  1042. }
  1043. /*
  1044. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  1045. * or ->boost_tasks, advancing the pointer to the next task in the
  1046. * ->blkd_tasks list.
  1047. *
  1048. * Note that irqs must be enabled: boosting the task can block.
  1049. * Returns 1 if there are more tasks needing to be boosted.
  1050. */
  1051. static int rcu_boost(struct rcu_node *rnp)
  1052. {
  1053. unsigned long flags;
  1054. struct rt_mutex mtx;
  1055. struct task_struct *t;
  1056. struct list_head *tb;
  1057. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
  1058. return 0; /* Nothing left to boost. */
  1059. raw_spin_lock_irqsave(&rnp->lock, flags);
  1060. /*
  1061. * Recheck under the lock: all tasks in need of boosting
  1062. * might exit their RCU read-side critical sections on their own.
  1063. */
  1064. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  1065. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1066. return 0;
  1067. }
  1068. /*
  1069. * Preferentially boost tasks blocking expedited grace periods.
  1070. * This cannot starve the normal grace periods because a second
  1071. * expedited grace period must boost all blocked tasks, including
  1072. * those blocking the pre-existing normal grace period.
  1073. */
  1074. if (rnp->exp_tasks != NULL) {
  1075. tb = rnp->exp_tasks;
  1076. rnp->n_exp_boosts++;
  1077. } else {
  1078. tb = rnp->boost_tasks;
  1079. rnp->n_normal_boosts++;
  1080. }
  1081. rnp->n_tasks_boosted++;
  1082. /*
  1083. * We boost task t by manufacturing an rt_mutex that appears to
  1084. * be held by task t. We leave a pointer to that rt_mutex where
  1085. * task t can find it, and task t will release the mutex when it
  1086. * exits its outermost RCU read-side critical section. Then
  1087. * simply acquiring this artificial rt_mutex will boost task
  1088. * t's priority. (Thanks to tglx for suggesting this approach!)
  1089. *
  1090. * Note that task t must acquire rnp->lock to remove itself from
  1091. * the ->blkd_tasks list, which it will do from exit() if from
  1092. * nowhere else. We therefore are guaranteed that task t will
  1093. * stay around at least until we drop rnp->lock. Note that
  1094. * rnp->lock also resolves races between our priority boosting
  1095. * and task t's exiting its outermost RCU read-side critical
  1096. * section.
  1097. */
  1098. t = container_of(tb, struct task_struct, rcu_node_entry);
  1099. rt_mutex_init_proxy_locked(&mtx, t);
  1100. t->rcu_boost_mutex = &mtx;
  1101. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1102. rt_mutex_lock(&mtx); /* Side effect: boosts task t's priority. */
  1103. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  1104. return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
  1105. ACCESS_ONCE(rnp->boost_tasks) != NULL;
  1106. }
  1107. /*
  1108. * Priority-boosting kthread. One per leaf rcu_node and one for the
  1109. * root rcu_node.
  1110. */
  1111. static int rcu_boost_kthread(void *arg)
  1112. {
  1113. struct rcu_node *rnp = (struct rcu_node *)arg;
  1114. int spincnt = 0;
  1115. int more2boost;
  1116. trace_rcu_utilization(TPS("Start boost kthread@init"));
  1117. for (;;) {
  1118. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1119. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  1120. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  1121. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  1122. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1123. more2boost = rcu_boost(rnp);
  1124. if (more2boost)
  1125. spincnt++;
  1126. else
  1127. spincnt = 0;
  1128. if (spincnt > 10) {
  1129. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  1130. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  1131. schedule_timeout_interruptible(2);
  1132. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  1133. spincnt = 0;
  1134. }
  1135. }
  1136. /* NOTREACHED */
  1137. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  1138. return 0;
  1139. }
  1140. /*
  1141. * Check to see if it is time to start boosting RCU readers that are
  1142. * blocking the current grace period, and, if so, tell the per-rcu_node
  1143. * kthread to start boosting them. If there is an expedited grace
  1144. * period in progress, it is always time to boost.
  1145. *
  1146. * The caller must hold rnp->lock, which this function releases.
  1147. * The ->boost_kthread_task is immortal, so we don't need to worry
  1148. * about it going away.
  1149. */
  1150. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1151. {
  1152. struct task_struct *t;
  1153. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1154. rnp->n_balk_exp_gp_tasks++;
  1155. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1156. return;
  1157. }
  1158. if (rnp->exp_tasks != NULL ||
  1159. (rnp->gp_tasks != NULL &&
  1160. rnp->boost_tasks == NULL &&
  1161. rnp->qsmask == 0 &&
  1162. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1163. if (rnp->exp_tasks == NULL)
  1164. rnp->boost_tasks = rnp->gp_tasks;
  1165. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1166. t = rnp->boost_kthread_task;
  1167. if (t)
  1168. rcu_wake_cond(t, rnp->boost_kthread_status);
  1169. } else {
  1170. rcu_initiate_boost_trace(rnp);
  1171. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1172. }
  1173. }
  1174. /*
  1175. * Wake up the per-CPU kthread to invoke RCU callbacks.
  1176. */
  1177. static void invoke_rcu_callbacks_kthread(void)
  1178. {
  1179. unsigned long flags;
  1180. local_irq_save(flags);
  1181. __this_cpu_write(rcu_cpu_has_work, 1);
  1182. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  1183. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  1184. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  1185. __this_cpu_read(rcu_cpu_kthread_status));
  1186. }
  1187. local_irq_restore(flags);
  1188. }
  1189. /*
  1190. * Is the current CPU running the RCU-callbacks kthread?
  1191. * Caller must have preemption disabled.
  1192. */
  1193. static bool rcu_is_callbacks_kthread(void)
  1194. {
  1195. return __get_cpu_var(rcu_cpu_kthread_task) == current;
  1196. }
  1197. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1198. /*
  1199. * Do priority-boost accounting for the start of a new grace period.
  1200. */
  1201. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1202. {
  1203. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1204. }
  1205. /*
  1206. * Create an RCU-boost kthread for the specified node if one does not
  1207. * already exist. We only create this kthread for preemptible RCU.
  1208. * Returns zero if all is well, a negated errno otherwise.
  1209. */
  1210. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1211. struct rcu_node *rnp)
  1212. {
  1213. int rnp_index = rnp - &rsp->node[0];
  1214. unsigned long flags;
  1215. struct sched_param sp;
  1216. struct task_struct *t;
  1217. if (&rcu_preempt_state != rsp)
  1218. return 0;
  1219. if (!rcu_scheduler_fully_active || rnp->qsmaskinit == 0)
  1220. return 0;
  1221. rsp->boost = 1;
  1222. if (rnp->boost_kthread_task != NULL)
  1223. return 0;
  1224. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1225. "rcub/%d", rnp_index);
  1226. if (IS_ERR(t))
  1227. return PTR_ERR(t);
  1228. raw_spin_lock_irqsave(&rnp->lock, flags);
  1229. rnp->boost_kthread_task = t;
  1230. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1231. sp.sched_priority = RCU_BOOST_PRIO;
  1232. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1233. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1234. return 0;
  1235. }
  1236. static void rcu_kthread_do_work(void)
  1237. {
  1238. rcu_do_batch(&rcu_sched_state, &__get_cpu_var(rcu_sched_data));
  1239. rcu_do_batch(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1240. rcu_preempt_do_callbacks();
  1241. }
  1242. static void rcu_cpu_kthread_setup(unsigned int cpu)
  1243. {
  1244. struct sched_param sp;
  1245. sp.sched_priority = RCU_KTHREAD_PRIO;
  1246. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1247. }
  1248. static void rcu_cpu_kthread_park(unsigned int cpu)
  1249. {
  1250. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1251. }
  1252. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  1253. {
  1254. return __get_cpu_var(rcu_cpu_has_work);
  1255. }
  1256. /*
  1257. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1258. * RCU softirq used in flavors and configurations of RCU that do not
  1259. * support RCU priority boosting.
  1260. */
  1261. static void rcu_cpu_kthread(unsigned int cpu)
  1262. {
  1263. unsigned int *statusp = &__get_cpu_var(rcu_cpu_kthread_status);
  1264. char work, *workp = &__get_cpu_var(rcu_cpu_has_work);
  1265. int spincnt;
  1266. for (spincnt = 0; spincnt < 10; spincnt++) {
  1267. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1268. local_bh_disable();
  1269. *statusp = RCU_KTHREAD_RUNNING;
  1270. this_cpu_inc(rcu_cpu_kthread_loops);
  1271. local_irq_disable();
  1272. work = *workp;
  1273. *workp = 0;
  1274. local_irq_enable();
  1275. if (work)
  1276. rcu_kthread_do_work();
  1277. local_bh_enable();
  1278. if (*workp == 0) {
  1279. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1280. *statusp = RCU_KTHREAD_WAITING;
  1281. return;
  1282. }
  1283. }
  1284. *statusp = RCU_KTHREAD_YIELDING;
  1285. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1286. schedule_timeout_interruptible(2);
  1287. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1288. *statusp = RCU_KTHREAD_WAITING;
  1289. }
  1290. /*
  1291. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1292. * served by the rcu_node in question. The CPU hotplug lock is still
  1293. * held, so the value of rnp->qsmaskinit will be stable.
  1294. *
  1295. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1296. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1297. * this function allows the kthread to execute on any CPU.
  1298. */
  1299. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1300. {
  1301. struct task_struct *t = rnp->boost_kthread_task;
  1302. unsigned long mask = rnp->qsmaskinit;
  1303. cpumask_var_t cm;
  1304. int cpu;
  1305. if (!t)
  1306. return;
  1307. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1308. return;
  1309. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
  1310. if ((mask & 0x1) && cpu != outgoingcpu)
  1311. cpumask_set_cpu(cpu, cm);
  1312. if (cpumask_weight(cm) == 0) {
  1313. cpumask_setall(cm);
  1314. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++)
  1315. cpumask_clear_cpu(cpu, cm);
  1316. WARN_ON_ONCE(cpumask_weight(cm) == 0);
  1317. }
  1318. set_cpus_allowed_ptr(t, cm);
  1319. free_cpumask_var(cm);
  1320. }
  1321. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1322. .store = &rcu_cpu_kthread_task,
  1323. .thread_should_run = rcu_cpu_kthread_should_run,
  1324. .thread_fn = rcu_cpu_kthread,
  1325. .thread_comm = "rcuc/%u",
  1326. .setup = rcu_cpu_kthread_setup,
  1327. .park = rcu_cpu_kthread_park,
  1328. };
  1329. /*
  1330. * Spawn all kthreads -- called as soon as the scheduler is running.
  1331. */
  1332. static int __init rcu_spawn_kthreads(void)
  1333. {
  1334. struct rcu_node *rnp;
  1335. int cpu;
  1336. rcu_scheduler_fully_active = 1;
  1337. for_each_possible_cpu(cpu)
  1338. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1339. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1340. rnp = rcu_get_root(rcu_state);
  1341. (void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
  1342. if (NUM_RCU_NODES > 1) {
  1343. rcu_for_each_leaf_node(rcu_state, rnp)
  1344. (void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
  1345. }
  1346. return 0;
  1347. }
  1348. early_initcall(rcu_spawn_kthreads);
  1349. static void rcu_prepare_kthreads(int cpu)
  1350. {
  1351. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1352. struct rcu_node *rnp = rdp->mynode;
  1353. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1354. if (rcu_scheduler_fully_active)
  1355. (void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
  1356. }
  1357. #else /* #ifdef CONFIG_RCU_BOOST */
  1358. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1359. {
  1360. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1361. }
  1362. static void invoke_rcu_callbacks_kthread(void)
  1363. {
  1364. WARN_ON_ONCE(1);
  1365. }
  1366. static bool rcu_is_callbacks_kthread(void)
  1367. {
  1368. return false;
  1369. }
  1370. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1371. {
  1372. }
  1373. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1374. {
  1375. }
  1376. static int __init rcu_scheduler_really_started(void)
  1377. {
  1378. rcu_scheduler_fully_active = 1;
  1379. return 0;
  1380. }
  1381. early_initcall(rcu_scheduler_really_started);
  1382. static void rcu_prepare_kthreads(int cpu)
  1383. {
  1384. }
  1385. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1386. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1387. /*
  1388. * Check to see if any future RCU-related work will need to be done
  1389. * by the current CPU, even if none need be done immediately, returning
  1390. * 1 if so. This function is part of the RCU implementation; it is -not-
  1391. * an exported member of the RCU API.
  1392. *
  1393. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1394. * any flavor of RCU.
  1395. */
  1396. int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
  1397. {
  1398. *delta_jiffies = ULONG_MAX;
  1399. return rcu_cpu_has_callbacks(cpu, NULL);
  1400. }
  1401. /*
  1402. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1403. * after it.
  1404. */
  1405. static void rcu_cleanup_after_idle(int cpu)
  1406. {
  1407. }
  1408. /*
  1409. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1410. * is nothing.
  1411. */
  1412. static void rcu_prepare_for_idle(int cpu)
  1413. {
  1414. }
  1415. /*
  1416. * Don't bother keeping a running count of the number of RCU callbacks
  1417. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1418. */
  1419. static void rcu_idle_count_callbacks_posted(void)
  1420. {
  1421. }
  1422. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1423. /*
  1424. * This code is invoked when a CPU goes idle, at which point we want
  1425. * to have the CPU do everything required for RCU so that it can enter
  1426. * the energy-efficient dyntick-idle mode. This is handled by a
  1427. * state machine implemented by rcu_prepare_for_idle() below.
  1428. *
  1429. * The following three proprocessor symbols control this state machine:
  1430. *
  1431. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1432. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1433. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1434. * benchmarkers who might otherwise be tempted to set this to a large
  1435. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1436. * system. And if you are -that- concerned about energy efficiency,
  1437. * just power the system down and be done with it!
  1438. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1439. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1440. * callbacks pending. Setting this too high can OOM your system.
  1441. *
  1442. * The values below work well in practice. If future workloads require
  1443. * adjustment, they can be converted into kernel config parameters, though
  1444. * making the state machine smarter might be a better option.
  1445. */
  1446. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1447. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1448. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1449. module_param(rcu_idle_gp_delay, int, 0644);
  1450. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1451. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1452. extern int tick_nohz_enabled;
  1453. /*
  1454. * Try to advance callbacks for all flavors of RCU on the current CPU.
  1455. * Afterwards, if there are any callbacks ready for immediate invocation,
  1456. * return true.
  1457. */
  1458. static bool rcu_try_advance_all_cbs(void)
  1459. {
  1460. bool cbs_ready = false;
  1461. struct rcu_data *rdp;
  1462. struct rcu_node *rnp;
  1463. struct rcu_state *rsp;
  1464. for_each_rcu_flavor(rsp) {
  1465. rdp = this_cpu_ptr(rsp->rda);
  1466. rnp = rdp->mynode;
  1467. /*
  1468. * Don't bother checking unless a grace period has
  1469. * completed since we last checked and there are
  1470. * callbacks not yet ready to invoke.
  1471. */
  1472. if (rdp->completed != rnp->completed &&
  1473. rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
  1474. note_gp_changes(rsp, rdp);
  1475. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1476. cbs_ready = true;
  1477. }
  1478. return cbs_ready;
  1479. }
  1480. /*
  1481. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1482. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1483. * caller to set the timeout based on whether or not there are non-lazy
  1484. * callbacks.
  1485. *
  1486. * The caller must have disabled interrupts.
  1487. */
  1488. int rcu_needs_cpu(int cpu, unsigned long *dj)
  1489. {
  1490. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1491. /* Snapshot to detect later posting of non-lazy callback. */
  1492. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1493. /* If no callbacks, RCU doesn't need the CPU. */
  1494. if (!rcu_cpu_has_callbacks(cpu, &rdtp->all_lazy)) {
  1495. *dj = ULONG_MAX;
  1496. return 0;
  1497. }
  1498. /* Attempt to advance callbacks. */
  1499. if (rcu_try_advance_all_cbs()) {
  1500. /* Some ready to invoke, so initiate later invocation. */
  1501. invoke_rcu_core();
  1502. return 1;
  1503. }
  1504. rdtp->last_accelerate = jiffies;
  1505. /* Request timer delay depending on laziness, and round. */
  1506. if (!rdtp->all_lazy) {
  1507. *dj = round_up(rcu_idle_gp_delay + jiffies,
  1508. rcu_idle_gp_delay) - jiffies;
  1509. } else {
  1510. *dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1511. }
  1512. return 0;
  1513. }
  1514. /*
  1515. * Prepare a CPU for idle from an RCU perspective. The first major task
  1516. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1517. * The second major task is to check to see if a non-lazy callback has
  1518. * arrived at a CPU that previously had only lazy callbacks. The third
  1519. * major task is to accelerate (that is, assign grace-period numbers to)
  1520. * any recently arrived callbacks.
  1521. *
  1522. * The caller must have disabled interrupts.
  1523. */
  1524. static void rcu_prepare_for_idle(int cpu)
  1525. {
  1526. struct rcu_data *rdp;
  1527. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1528. struct rcu_node *rnp;
  1529. struct rcu_state *rsp;
  1530. int tne;
  1531. /* Handle nohz enablement switches conservatively. */
  1532. tne = ACCESS_ONCE(tick_nohz_enabled);
  1533. if (tne != rdtp->tick_nohz_enabled_snap) {
  1534. if (rcu_cpu_has_callbacks(cpu, NULL))
  1535. invoke_rcu_core(); /* force nohz to see update. */
  1536. rdtp->tick_nohz_enabled_snap = tne;
  1537. return;
  1538. }
  1539. if (!tne)
  1540. return;
  1541. /* If this is a no-CBs CPU, no callbacks, just return. */
  1542. if (rcu_is_nocb_cpu(cpu))
  1543. return;
  1544. /*
  1545. * If a non-lazy callback arrived at a CPU having only lazy
  1546. * callbacks, invoke RCU core for the side-effect of recalculating
  1547. * idle duration on re-entry to idle.
  1548. */
  1549. if (rdtp->all_lazy &&
  1550. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1551. invoke_rcu_core();
  1552. return;
  1553. }
  1554. /*
  1555. * If we have not yet accelerated this jiffy, accelerate all
  1556. * callbacks on this CPU.
  1557. */
  1558. if (rdtp->last_accelerate == jiffies)
  1559. return;
  1560. rdtp->last_accelerate = jiffies;
  1561. for_each_rcu_flavor(rsp) {
  1562. rdp = per_cpu_ptr(rsp->rda, cpu);
  1563. if (!*rdp->nxttail[RCU_DONE_TAIL])
  1564. continue;
  1565. rnp = rdp->mynode;
  1566. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1567. rcu_accelerate_cbs(rsp, rnp, rdp);
  1568. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1569. }
  1570. }
  1571. /*
  1572. * Clean up for exit from idle. Attempt to advance callbacks based on
  1573. * any grace periods that elapsed while the CPU was idle, and if any
  1574. * callbacks are now ready to invoke, initiate invocation.
  1575. */
  1576. static void rcu_cleanup_after_idle(int cpu)
  1577. {
  1578. struct rcu_data *rdp;
  1579. struct rcu_state *rsp;
  1580. if (rcu_is_nocb_cpu(cpu))
  1581. return;
  1582. rcu_try_advance_all_cbs();
  1583. for_each_rcu_flavor(rsp) {
  1584. rdp = per_cpu_ptr(rsp->rda, cpu);
  1585. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1586. invoke_rcu_core();
  1587. }
  1588. }
  1589. /*
  1590. * Keep a running count of the number of non-lazy callbacks posted
  1591. * on this CPU. This running counter (which is never decremented) allows
  1592. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1593. * posts a callback, even if an equal number of callbacks are invoked.
  1594. * Of course, callbacks should only be posted from within a trace event
  1595. * designed to be called from idle or from within RCU_NONIDLE().
  1596. */
  1597. static void rcu_idle_count_callbacks_posted(void)
  1598. {
  1599. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1600. }
  1601. /*
  1602. * Data for flushing lazy RCU callbacks at OOM time.
  1603. */
  1604. static atomic_t oom_callback_count;
  1605. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1606. /*
  1607. * RCU OOM callback -- decrement the outstanding count and deliver the
  1608. * wake-up if we are the last one.
  1609. */
  1610. static void rcu_oom_callback(struct rcu_head *rhp)
  1611. {
  1612. if (atomic_dec_and_test(&oom_callback_count))
  1613. wake_up(&oom_callback_wq);
  1614. }
  1615. /*
  1616. * Post an rcu_oom_notify callback on the current CPU if it has at
  1617. * least one lazy callback. This will unnecessarily post callbacks
  1618. * to CPUs that already have a non-lazy callback at the end of their
  1619. * callback list, but this is an infrequent operation, so accept some
  1620. * extra overhead to keep things simple.
  1621. */
  1622. static void rcu_oom_notify_cpu(void *unused)
  1623. {
  1624. struct rcu_state *rsp;
  1625. struct rcu_data *rdp;
  1626. for_each_rcu_flavor(rsp) {
  1627. rdp = __this_cpu_ptr(rsp->rda);
  1628. if (rdp->qlen_lazy != 0) {
  1629. atomic_inc(&oom_callback_count);
  1630. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1631. }
  1632. }
  1633. }
  1634. /*
  1635. * If low on memory, ensure that each CPU has a non-lazy callback.
  1636. * This will wake up CPUs that have only lazy callbacks, in turn
  1637. * ensuring that they free up the corresponding memory in a timely manner.
  1638. * Because an uncertain amount of memory will be freed in some uncertain
  1639. * timeframe, we do not claim to have freed anything.
  1640. */
  1641. static int rcu_oom_notify(struct notifier_block *self,
  1642. unsigned long notused, void *nfreed)
  1643. {
  1644. int cpu;
  1645. /* Wait for callbacks from earlier instance to complete. */
  1646. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1647. /*
  1648. * Prevent premature wakeup: ensure that all increments happen
  1649. * before there is a chance of the counter reaching zero.
  1650. */
  1651. atomic_set(&oom_callback_count, 1);
  1652. get_online_cpus();
  1653. for_each_online_cpu(cpu) {
  1654. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1655. cond_resched();
  1656. }
  1657. put_online_cpus();
  1658. /* Unconditionally decrement: no need to wake ourselves up. */
  1659. atomic_dec(&oom_callback_count);
  1660. return NOTIFY_OK;
  1661. }
  1662. static struct notifier_block rcu_oom_nb = {
  1663. .notifier_call = rcu_oom_notify
  1664. };
  1665. static int __init rcu_register_oom_notifier(void)
  1666. {
  1667. register_oom_notifier(&rcu_oom_nb);
  1668. return 0;
  1669. }
  1670. early_initcall(rcu_register_oom_notifier);
  1671. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1672. #ifdef CONFIG_RCU_CPU_STALL_INFO
  1673. #ifdef CONFIG_RCU_FAST_NO_HZ
  1674. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1675. {
  1676. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1677. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1678. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1679. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1680. ulong2long(nlpd),
  1681. rdtp->all_lazy ? 'L' : '.',
  1682. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1683. }
  1684. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1685. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1686. {
  1687. *cp = '\0';
  1688. }
  1689. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1690. /* Initiate the stall-info list. */
  1691. static void print_cpu_stall_info_begin(void)
  1692. {
  1693. pr_cont("\n");
  1694. }
  1695. /*
  1696. * Print out diagnostic information for the specified stalled CPU.
  1697. *
  1698. * If the specified CPU is aware of the current RCU grace period
  1699. * (flavor specified by rsp), then print the number of scheduling
  1700. * clock interrupts the CPU has taken during the time that it has
  1701. * been aware. Otherwise, print the number of RCU grace periods
  1702. * that this CPU is ignorant of, for example, "1" if the CPU was
  1703. * aware of the previous grace period.
  1704. *
  1705. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1706. */
  1707. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1708. {
  1709. char fast_no_hz[72];
  1710. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1711. struct rcu_dynticks *rdtp = rdp->dynticks;
  1712. char *ticks_title;
  1713. unsigned long ticks_value;
  1714. if (rsp->gpnum == rdp->gpnum) {
  1715. ticks_title = "ticks this GP";
  1716. ticks_value = rdp->ticks_this_gp;
  1717. } else {
  1718. ticks_title = "GPs behind";
  1719. ticks_value = rsp->gpnum - rdp->gpnum;
  1720. }
  1721. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1722. pr_err("\t%d: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u %s\n",
  1723. cpu, ticks_value, ticks_title,
  1724. atomic_read(&rdtp->dynticks) & 0xfff,
  1725. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1726. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1727. fast_no_hz);
  1728. }
  1729. /* Terminate the stall-info list. */
  1730. static void print_cpu_stall_info_end(void)
  1731. {
  1732. pr_err("\t");
  1733. }
  1734. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1735. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1736. {
  1737. rdp->ticks_this_gp = 0;
  1738. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1739. }
  1740. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1741. static void increment_cpu_stall_ticks(void)
  1742. {
  1743. struct rcu_state *rsp;
  1744. for_each_rcu_flavor(rsp)
  1745. __this_cpu_ptr(rsp->rda)->ticks_this_gp++;
  1746. }
  1747. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1748. static void print_cpu_stall_info_begin(void)
  1749. {
  1750. pr_cont(" {");
  1751. }
  1752. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1753. {
  1754. pr_cont(" %d", cpu);
  1755. }
  1756. static void print_cpu_stall_info_end(void)
  1757. {
  1758. pr_cont("} ");
  1759. }
  1760. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1761. {
  1762. }
  1763. static void increment_cpu_stall_ticks(void)
  1764. {
  1765. }
  1766. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1767. #ifdef CONFIG_RCU_NOCB_CPU
  1768. /*
  1769. * Offload callback processing from the boot-time-specified set of CPUs
  1770. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1771. * kthread created that pulls the callbacks from the corresponding CPU,
  1772. * waits for a grace period to elapse, and invokes the callbacks.
  1773. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1774. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1775. * has been specified, in which case each kthread actively polls its
  1776. * CPU. (Which isn't so great for energy efficiency, but which does
  1777. * reduce RCU's overhead on that CPU.)
  1778. *
  1779. * This is intended to be used in conjunction with Frederic Weisbecker's
  1780. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1781. * running CPU-bound user-mode computations.
  1782. *
  1783. * Offloading of callback processing could also in theory be used as
  1784. * an energy-efficiency measure because CPUs with no RCU callbacks
  1785. * queued are more aggressive about entering dyntick-idle mode.
  1786. */
  1787. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1788. static int __init rcu_nocb_setup(char *str)
  1789. {
  1790. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1791. have_rcu_nocb_mask = true;
  1792. cpulist_parse(str, rcu_nocb_mask);
  1793. return 1;
  1794. }
  1795. __setup("rcu_nocbs=", rcu_nocb_setup);
  1796. static int __init parse_rcu_nocb_poll(char *arg)
  1797. {
  1798. rcu_nocb_poll = 1;
  1799. return 0;
  1800. }
  1801. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1802. /*
  1803. * Do any no-CBs CPUs need another grace period?
  1804. *
  1805. * Interrupts must be disabled. If the caller does not hold the root
  1806. * rnp_node structure's ->lock, the results are advisory only.
  1807. */
  1808. static int rcu_nocb_needs_gp(struct rcu_state *rsp)
  1809. {
  1810. struct rcu_node *rnp = rcu_get_root(rsp);
  1811. return rnp->need_future_gp[(ACCESS_ONCE(rnp->completed) + 1) & 0x1];
  1812. }
  1813. /*
  1814. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1815. * grace period.
  1816. */
  1817. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1818. {
  1819. wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  1820. }
  1821. /*
  1822. * Set the root rcu_node structure's ->need_future_gp field
  1823. * based on the sum of those of all rcu_node structures. This does
  1824. * double-count the root rcu_node structure's requests, but this
  1825. * is necessary to handle the possibility of a rcu_nocb_kthread()
  1826. * having awakened during the time that the rcu_node structures
  1827. * were being updated for the end of the previous grace period.
  1828. */
  1829. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  1830. {
  1831. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  1832. }
  1833. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1834. {
  1835. init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  1836. init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  1837. }
  1838. /* Is the specified CPU a no-CPUs CPU? */
  1839. bool rcu_is_nocb_cpu(int cpu)
  1840. {
  1841. if (have_rcu_nocb_mask)
  1842. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1843. return false;
  1844. }
  1845. /*
  1846. * Enqueue the specified string of rcu_head structures onto the specified
  1847. * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
  1848. * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
  1849. * counts are supplied by rhcount and rhcount_lazy.
  1850. *
  1851. * If warranted, also wake up the kthread servicing this CPUs queues.
  1852. */
  1853. static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
  1854. struct rcu_head *rhp,
  1855. struct rcu_head **rhtp,
  1856. int rhcount, int rhcount_lazy)
  1857. {
  1858. int len;
  1859. struct rcu_head **old_rhpp;
  1860. struct task_struct *t;
  1861. /* Enqueue the callback on the nocb list and update counts. */
  1862. old_rhpp = xchg(&rdp->nocb_tail, rhtp);
  1863. ACCESS_ONCE(*old_rhpp) = rhp;
  1864. atomic_long_add(rhcount, &rdp->nocb_q_count);
  1865. atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
  1866. /* If we are not being polled and there is a kthread, awaken it ... */
  1867. t = ACCESS_ONCE(rdp->nocb_kthread);
  1868. if (rcu_nocb_poll | !t)
  1869. return;
  1870. len = atomic_long_read(&rdp->nocb_q_count);
  1871. if (old_rhpp == &rdp->nocb_head) {
  1872. wake_up(&rdp->nocb_wq); /* ... only if queue was empty ... */
  1873. rdp->qlen_last_fqs_check = 0;
  1874. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  1875. wake_up_process(t); /* ... or if many callbacks queued. */
  1876. rdp->qlen_last_fqs_check = LONG_MAX / 2;
  1877. }
  1878. return;
  1879. }
  1880. /*
  1881. * This is a helper for __call_rcu(), which invokes this when the normal
  1882. * callback queue is inoperable. If this is not a no-CBs CPU, this
  1883. * function returns failure back to __call_rcu(), which can complain
  1884. * appropriately.
  1885. *
  1886. * Otherwise, this function queues the callback where the corresponding
  1887. * "rcuo" kthread can find it.
  1888. */
  1889. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  1890. bool lazy)
  1891. {
  1892. if (!rcu_is_nocb_cpu(rdp->cpu))
  1893. return 0;
  1894. __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy);
  1895. if (__is_kfree_rcu_offset((unsigned long)rhp->func))
  1896. trace_rcu_kfree_callback(rdp->rsp->name, rhp,
  1897. (unsigned long)rhp->func,
  1898. rdp->qlen_lazy, rdp->qlen);
  1899. else
  1900. trace_rcu_callback(rdp->rsp->name, rhp,
  1901. rdp->qlen_lazy, rdp->qlen);
  1902. return 1;
  1903. }
  1904. /*
  1905. * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
  1906. * not a no-CBs CPU.
  1907. */
  1908. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  1909. struct rcu_data *rdp)
  1910. {
  1911. long ql = rsp->qlen;
  1912. long qll = rsp->qlen_lazy;
  1913. /* If this is not a no-CBs CPU, tell the caller to do it the old way. */
  1914. if (!rcu_is_nocb_cpu(smp_processor_id()))
  1915. return 0;
  1916. rsp->qlen = 0;
  1917. rsp->qlen_lazy = 0;
  1918. /* First, enqueue the donelist, if any. This preserves CB ordering. */
  1919. if (rsp->orphan_donelist != NULL) {
  1920. __call_rcu_nocb_enqueue(rdp, rsp->orphan_donelist,
  1921. rsp->orphan_donetail, ql, qll);
  1922. ql = qll = 0;
  1923. rsp->orphan_donelist = NULL;
  1924. rsp->orphan_donetail = &rsp->orphan_donelist;
  1925. }
  1926. if (rsp->orphan_nxtlist != NULL) {
  1927. __call_rcu_nocb_enqueue(rdp, rsp->orphan_nxtlist,
  1928. rsp->orphan_nxttail, ql, qll);
  1929. ql = qll = 0;
  1930. rsp->orphan_nxtlist = NULL;
  1931. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  1932. }
  1933. return 1;
  1934. }
  1935. /*
  1936. * If necessary, kick off a new grace period, and either way wait
  1937. * for a subsequent grace period to complete.
  1938. */
  1939. static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  1940. {
  1941. unsigned long c;
  1942. bool d;
  1943. unsigned long flags;
  1944. struct rcu_node *rnp = rdp->mynode;
  1945. raw_spin_lock_irqsave(&rnp->lock, flags);
  1946. c = rcu_start_future_gp(rnp, rdp);
  1947. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1948. /*
  1949. * Wait for the grace period. Do so interruptibly to avoid messing
  1950. * up the load average.
  1951. */
  1952. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  1953. for (;;) {
  1954. wait_event_interruptible(
  1955. rnp->nocb_gp_wq[c & 0x1],
  1956. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  1957. if (likely(d))
  1958. break;
  1959. flush_signals(current);
  1960. trace_rcu_future_gp(rnp, rdp, c, TPS("ResumeWait"));
  1961. }
  1962. trace_rcu_future_gp(rnp, rdp, c, TPS("EndWait"));
  1963. smp_mb(); /* Ensure that CB invocation happens after GP end. */
  1964. }
  1965. /*
  1966. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  1967. * callbacks queued by the corresponding no-CBs CPU.
  1968. */
  1969. static int rcu_nocb_kthread(void *arg)
  1970. {
  1971. int c, cl;
  1972. struct rcu_head *list;
  1973. struct rcu_head *next;
  1974. struct rcu_head **tail;
  1975. struct rcu_data *rdp = arg;
  1976. /* Each pass through this loop invokes one batch of callbacks */
  1977. for (;;) {
  1978. /* If not polling, wait for next batch of callbacks. */
  1979. if (!rcu_nocb_poll)
  1980. wait_event_interruptible(rdp->nocb_wq, rdp->nocb_head);
  1981. list = ACCESS_ONCE(rdp->nocb_head);
  1982. if (!list) {
  1983. schedule_timeout_interruptible(1);
  1984. flush_signals(current);
  1985. continue;
  1986. }
  1987. /*
  1988. * Extract queued callbacks, update counts, and wait
  1989. * for a grace period to elapse.
  1990. */
  1991. ACCESS_ONCE(rdp->nocb_head) = NULL;
  1992. tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  1993. c = atomic_long_xchg(&rdp->nocb_q_count, 0);
  1994. cl = atomic_long_xchg(&rdp->nocb_q_count_lazy, 0);
  1995. ACCESS_ONCE(rdp->nocb_p_count) += c;
  1996. ACCESS_ONCE(rdp->nocb_p_count_lazy) += cl;
  1997. rcu_nocb_wait_gp(rdp);
  1998. /* Each pass through the following loop invokes a callback. */
  1999. trace_rcu_batch_start(rdp->rsp->name, cl, c, -1);
  2000. c = cl = 0;
  2001. while (list) {
  2002. next = list->next;
  2003. /* Wait for enqueuing to complete, if needed. */
  2004. while (next == NULL && &list->next != tail) {
  2005. schedule_timeout_interruptible(1);
  2006. next = list->next;
  2007. }
  2008. debug_rcu_head_unqueue(list);
  2009. local_bh_disable();
  2010. if (__rcu_reclaim(rdp->rsp->name, list))
  2011. cl++;
  2012. c++;
  2013. local_bh_enable();
  2014. list = next;
  2015. }
  2016. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  2017. ACCESS_ONCE(rdp->nocb_p_count) -= c;
  2018. ACCESS_ONCE(rdp->nocb_p_count_lazy) -= cl;
  2019. rdp->n_nocbs_invoked += c;
  2020. }
  2021. return 0;
  2022. }
  2023. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2024. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2025. {
  2026. rdp->nocb_tail = &rdp->nocb_head;
  2027. init_waitqueue_head(&rdp->nocb_wq);
  2028. }
  2029. /* Create a kthread for each RCU flavor for each no-CBs CPU. */
  2030. static void __init rcu_spawn_nocb_kthreads(struct rcu_state *rsp)
  2031. {
  2032. int cpu;
  2033. struct rcu_data *rdp;
  2034. struct task_struct *t;
  2035. if (rcu_nocb_mask == NULL)
  2036. return;
  2037. for_each_cpu(cpu, rcu_nocb_mask) {
  2038. rdp = per_cpu_ptr(rsp->rda, cpu);
  2039. t = kthread_run(rcu_nocb_kthread, rdp,
  2040. "rcuo%c/%d", rsp->abbr, cpu);
  2041. BUG_ON(IS_ERR(t));
  2042. ACCESS_ONCE(rdp->nocb_kthread) = t;
  2043. }
  2044. }
  2045. /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
  2046. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2047. {
  2048. if (rcu_nocb_mask == NULL ||
  2049. !cpumask_test_cpu(rdp->cpu, rcu_nocb_mask))
  2050. return false;
  2051. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  2052. return true;
  2053. }
  2054. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  2055. static int rcu_nocb_needs_gp(struct rcu_state *rsp)
  2056. {
  2057. return 0;
  2058. }
  2059. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  2060. {
  2061. }
  2062. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  2063. {
  2064. }
  2065. static void rcu_init_one_nocb(struct rcu_node *rnp)
  2066. {
  2067. }
  2068. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  2069. bool lazy)
  2070. {
  2071. return 0;
  2072. }
  2073. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  2074. struct rcu_data *rdp)
  2075. {
  2076. return 0;
  2077. }
  2078. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2079. {
  2080. }
  2081. static void __init rcu_spawn_nocb_kthreads(struct rcu_state *rsp)
  2082. {
  2083. }
  2084. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2085. {
  2086. return false;
  2087. }
  2088. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  2089. /*
  2090. * An adaptive-ticks CPU can potentially execute in kernel mode for an
  2091. * arbitrarily long period of time with the scheduling-clock tick turned
  2092. * off. RCU will be paying attention to this CPU because it is in the
  2093. * kernel, but the CPU cannot be guaranteed to be executing the RCU state
  2094. * machine because the scheduling-clock tick has been disabled. Therefore,
  2095. * if an adaptive-ticks CPU is failing to respond to the current grace
  2096. * period and has not be idle from an RCU perspective, kick it.
  2097. */
  2098. static void rcu_kick_nohz_cpu(int cpu)
  2099. {
  2100. #ifdef CONFIG_NO_HZ_FULL
  2101. if (tick_nohz_full_cpu(cpu))
  2102. smp_send_reschedule(cpu);
  2103. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2104. }