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