rcutree_plugin.h 69 KB

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