rcutree_plugin.h 62 KB

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