rcutree_plugin.h 71 KB

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