rcutree_plugin.h 59 KB

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