rcutree_plugin.h 57 KB

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