rcutree_plugin.h 69 KB

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