rcutree_plugin.h 68 KB

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