tree_plugin.h 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094
  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, you can access it online at
  18. * http://www.gnu.org/licenses/gpl-2.0.html.
  19. *
  20. * Copyright Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/gfp.h>
  28. #include <linux/oom.h>
  29. #include <linux/smpboot.h>
  30. #include "../time/tick-internal.h"
  31. #ifdef CONFIG_RCU_BOOST
  32. #include "../locking/rtmutex_common.h"
  33. /*
  34. * Control variables for per-CPU and per-rcu_node kthreads. These
  35. * handle all flavors of RCU.
  36. */
  37. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  38. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  39. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  40. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  41. #endif /* #ifdef CONFIG_RCU_BOOST */
  42. #ifdef CONFIG_RCU_NOCB_CPU
  43. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  44. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  45. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  46. static char __initdata nocb_buf[NR_CPUS * 5];
  47. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  48. /*
  49. * Check the RCU kernel configuration parameters and print informative
  50. * messages about anything out of the ordinary. If you like #ifdef, you
  51. * will love this function.
  52. */
  53. static void __init rcu_bootup_announce_oddness(void)
  54. {
  55. #ifdef CONFIG_RCU_TRACE
  56. pr_info("\tRCU debugfs-based tracing is enabled.\n");
  57. #endif
  58. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  59. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  60. CONFIG_RCU_FANOUT);
  61. #endif
  62. #ifdef CONFIG_RCU_FANOUT_EXACT
  63. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  64. #endif
  65. #ifdef CONFIG_RCU_FAST_NO_HZ
  66. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  67. #endif
  68. #ifdef CONFIG_PROVE_RCU
  69. pr_info("\tRCU lockdep checking is enabled.\n");
  70. #endif
  71. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  72. pr_info("\tRCU torture testing starts during boot.\n");
  73. #endif
  74. #if defined(CONFIG_RCU_CPU_STALL_INFO)
  75. pr_info("\tAdditional per-CPU info printed with stalls.\n");
  76. #endif
  77. #if NUM_RCU_LVL_4 != 0
  78. pr_info("\tFour-level hierarchy is enabled.\n");
  79. #endif
  80. if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
  81. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
  82. if (nr_cpu_ids != NR_CPUS)
  83. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
  84. #ifdef CONFIG_RCU_BOOST
  85. pr_info("\tRCU kthread priority: %d.\n", kthread_prio);
  86. #endif
  87. }
  88. #ifdef CONFIG_PREEMPT_RCU
  89. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  90. static struct rcu_state *rcu_state_p = &rcu_preempt_state;
  91. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  92. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  93. bool wake);
  94. /*
  95. * Tell them what RCU they are running.
  96. */
  97. static void __init rcu_bootup_announce(void)
  98. {
  99. pr_info("Preemptible hierarchical RCU implementation.\n");
  100. rcu_bootup_announce_oddness();
  101. }
  102. /*
  103. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  104. * that this just means that the task currently running on the CPU is
  105. * not in a quiescent state. There might be any number of tasks blocked
  106. * while in an RCU read-side critical section.
  107. *
  108. * As with the other rcu_*_qs() functions, callers to this function
  109. * must disable preemption.
  110. */
  111. static void rcu_preempt_qs(void)
  112. {
  113. if (!__this_cpu_read(rcu_preempt_data.passed_quiesce)) {
  114. trace_rcu_grace_period(TPS("rcu_preempt"),
  115. __this_cpu_read(rcu_preempt_data.gpnum),
  116. TPS("cpuqs"));
  117. __this_cpu_write(rcu_preempt_data.passed_quiesce, 1);
  118. barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
  119. current->rcu_read_unlock_special.b.need_qs = false;
  120. }
  121. }
  122. /*
  123. * We have entered the scheduler, and the current task might soon be
  124. * context-switched away from. If this task is in an RCU read-side
  125. * critical section, we will no longer be able to rely on the CPU to
  126. * record that fact, so we enqueue the task on the blkd_tasks list.
  127. * The task will dequeue itself when it exits the outermost enclosing
  128. * RCU read-side critical section. Therefore, the current grace period
  129. * cannot be permitted to complete until the blkd_tasks list entries
  130. * predating the current grace period drain, in other words, until
  131. * rnp->gp_tasks becomes NULL.
  132. *
  133. * Caller must disable preemption.
  134. */
  135. static void rcu_preempt_note_context_switch(void)
  136. {
  137. struct task_struct *t = current;
  138. unsigned long flags;
  139. struct rcu_data *rdp;
  140. struct rcu_node *rnp;
  141. if (t->rcu_read_lock_nesting > 0 &&
  142. !t->rcu_read_unlock_special.b.blocked) {
  143. /* Possibly blocking in an RCU read-side critical section. */
  144. rdp = this_cpu_ptr(rcu_preempt_state.rda);
  145. rnp = rdp->mynode;
  146. raw_spin_lock_irqsave(&rnp->lock, flags);
  147. smp_mb__after_unlock_lock();
  148. t->rcu_read_unlock_special.b.blocked = true;
  149. t->rcu_blocked_node = rnp;
  150. /*
  151. * If this CPU has already checked in, then this task
  152. * will hold up the next grace period rather than the
  153. * current grace period. Queue the task accordingly.
  154. * If the task is queued for the current grace period
  155. * (i.e., this CPU has not yet passed through a quiescent
  156. * state for the current grace period), then as long
  157. * as that task remains queued, the current grace period
  158. * cannot end. Note that there is some uncertainty as
  159. * to exactly when the current grace period started.
  160. * We take a conservative approach, which can result
  161. * in unnecessarily waiting on tasks that started very
  162. * slightly after the current grace period began. C'est
  163. * la vie!!!
  164. *
  165. * But first, note that the current CPU must still be
  166. * on line!
  167. */
  168. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  169. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  170. if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
  171. list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
  172. rnp->gp_tasks = &t->rcu_node_entry;
  173. #ifdef CONFIG_RCU_BOOST
  174. if (rnp->boost_tasks != NULL)
  175. rnp->boost_tasks = rnp->gp_tasks;
  176. #endif /* #ifdef CONFIG_RCU_BOOST */
  177. } else {
  178. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  179. if (rnp->qsmask & rdp->grpmask)
  180. rnp->gp_tasks = &t->rcu_node_entry;
  181. }
  182. trace_rcu_preempt_task(rdp->rsp->name,
  183. t->pid,
  184. (rnp->qsmask & rdp->grpmask)
  185. ? rnp->gpnum
  186. : rnp->gpnum + 1);
  187. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  188. } else if (t->rcu_read_lock_nesting < 0 &&
  189. t->rcu_read_unlock_special.s) {
  190. /*
  191. * Complete exit from RCU read-side critical section on
  192. * behalf of preempted instance of __rcu_read_unlock().
  193. */
  194. rcu_read_unlock_special(t);
  195. }
  196. /*
  197. * Either we were not in an RCU read-side critical section to
  198. * begin with, or we have now recorded that critical section
  199. * globally. Either way, we can now note a quiescent state
  200. * for this CPU. Again, if we were in an RCU read-side critical
  201. * section, and if that critical section was blocking the current
  202. * grace period, then the fact that the task has been enqueued
  203. * means that we continue to block the current grace period.
  204. */
  205. rcu_preempt_qs();
  206. }
  207. /*
  208. * Check for preempted RCU readers blocking the current grace period
  209. * for the specified rcu_node structure. If the caller needs a reliable
  210. * answer, it must hold the rcu_node's ->lock.
  211. */
  212. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  213. {
  214. return rnp->gp_tasks != NULL;
  215. }
  216. /*
  217. * Record a quiescent state for all tasks that were previously queued
  218. * on the specified rcu_node structure and that were blocking the current
  219. * RCU grace period. The caller must hold the specified rnp->lock with
  220. * irqs disabled, and this lock is released upon return, but irqs remain
  221. * disabled.
  222. */
  223. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  224. __releases(rnp->lock)
  225. {
  226. unsigned long mask;
  227. struct rcu_node *rnp_p;
  228. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  229. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  230. return; /* Still need more quiescent states! */
  231. }
  232. rnp_p = rnp->parent;
  233. if (rnp_p == NULL) {
  234. /*
  235. * Either there is only one rcu_node in the tree,
  236. * or tasks were kicked up to root rcu_node due to
  237. * CPUs going offline.
  238. */
  239. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  240. return;
  241. }
  242. /* Report up the rest of the hierarchy. */
  243. mask = rnp->grpmask;
  244. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  245. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  246. smp_mb__after_unlock_lock();
  247. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  248. }
  249. /*
  250. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  251. * returning NULL if at the end of the list.
  252. */
  253. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  254. struct rcu_node *rnp)
  255. {
  256. struct list_head *np;
  257. np = t->rcu_node_entry.next;
  258. if (np == &rnp->blkd_tasks)
  259. np = NULL;
  260. return np;
  261. }
  262. /*
  263. * Return true if the specified rcu_node structure has tasks that were
  264. * preempted within an RCU read-side critical section.
  265. */
  266. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  267. {
  268. return !list_empty(&rnp->blkd_tasks);
  269. }
  270. /*
  271. * Handle special cases during rcu_read_unlock(), such as needing to
  272. * notify RCU core processing or task having blocked during the RCU
  273. * read-side critical section.
  274. */
  275. void rcu_read_unlock_special(struct task_struct *t)
  276. {
  277. bool empty;
  278. bool empty_exp;
  279. bool empty_norm;
  280. bool empty_exp_now;
  281. unsigned long flags;
  282. struct list_head *np;
  283. #ifdef CONFIG_RCU_BOOST
  284. bool drop_boost_mutex = false;
  285. #endif /* #ifdef CONFIG_RCU_BOOST */
  286. struct rcu_node *rnp;
  287. union rcu_special special;
  288. /* NMI handlers cannot block and cannot safely manipulate state. */
  289. if (in_nmi())
  290. return;
  291. local_irq_save(flags);
  292. /*
  293. * If RCU core is waiting for this CPU to exit critical section,
  294. * let it know that we have done so. Because irqs are disabled,
  295. * t->rcu_read_unlock_special cannot change.
  296. */
  297. special = t->rcu_read_unlock_special;
  298. if (special.b.need_qs) {
  299. rcu_preempt_qs();
  300. t->rcu_read_unlock_special.b.need_qs = false;
  301. if (!t->rcu_read_unlock_special.s) {
  302. local_irq_restore(flags);
  303. return;
  304. }
  305. }
  306. /* Hardware IRQ handlers cannot block, complain if they get here. */
  307. if (WARN_ON_ONCE(in_irq() || in_serving_softirq())) {
  308. local_irq_restore(flags);
  309. return;
  310. }
  311. /* Clean up if blocked during RCU read-side critical section. */
  312. if (special.b.blocked) {
  313. t->rcu_read_unlock_special.b.blocked = false;
  314. /*
  315. * Remove this task from the list it blocked on. The
  316. * task can migrate while we acquire the lock, but at
  317. * most one time. So at most two passes through loop.
  318. */
  319. for (;;) {
  320. rnp = t->rcu_blocked_node;
  321. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  322. smp_mb__after_unlock_lock();
  323. if (rnp == t->rcu_blocked_node)
  324. break;
  325. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  326. }
  327. empty = !rcu_preempt_has_tasks(rnp);
  328. empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
  329. empty_exp = !rcu_preempted_readers_exp(rnp);
  330. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  331. np = rcu_next_node_entry(t, rnp);
  332. list_del_init(&t->rcu_node_entry);
  333. t->rcu_blocked_node = NULL;
  334. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  335. rnp->gpnum, t->pid);
  336. if (&t->rcu_node_entry == rnp->gp_tasks)
  337. rnp->gp_tasks = np;
  338. if (&t->rcu_node_entry == rnp->exp_tasks)
  339. rnp->exp_tasks = np;
  340. #ifdef CONFIG_RCU_BOOST
  341. if (&t->rcu_node_entry == rnp->boost_tasks)
  342. rnp->boost_tasks = np;
  343. /* Snapshot ->boost_mtx ownership with rcu_node lock held. */
  344. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
  345. #endif /* #ifdef CONFIG_RCU_BOOST */
  346. /*
  347. * If this was the last task on the list, go see if we
  348. * need to propagate ->qsmaskinit bit clearing up the
  349. * rcu_node tree.
  350. */
  351. if (!empty && !rcu_preempt_has_tasks(rnp))
  352. rcu_cleanup_dead_rnp(rnp);
  353. /*
  354. * If this was the last task on the current list, and if
  355. * we aren't waiting on any CPUs, report the quiescent state.
  356. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  357. * so we must take a snapshot of the expedited state.
  358. */
  359. empty_exp_now = !rcu_preempted_readers_exp(rnp);
  360. if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
  361. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  362. rnp->gpnum,
  363. 0, rnp->qsmask,
  364. rnp->level,
  365. rnp->grplo,
  366. rnp->grphi,
  367. !!rnp->gp_tasks);
  368. rcu_report_unblock_qs_rnp(rnp, flags);
  369. } else {
  370. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  371. }
  372. #ifdef CONFIG_RCU_BOOST
  373. /* Unboost if we were boosted. */
  374. if (drop_boost_mutex)
  375. rt_mutex_unlock(&rnp->boost_mtx);
  376. #endif /* #ifdef CONFIG_RCU_BOOST */
  377. /*
  378. * If this was the last task on the expedited lists,
  379. * then we need to report up the rcu_node hierarchy.
  380. */
  381. if (!empty_exp && empty_exp_now)
  382. rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
  383. } else {
  384. local_irq_restore(flags);
  385. }
  386. }
  387. /*
  388. * Dump detailed information for all tasks blocking the current RCU
  389. * grace period on the specified rcu_node structure.
  390. */
  391. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  392. {
  393. unsigned long flags;
  394. struct task_struct *t;
  395. raw_spin_lock_irqsave(&rnp->lock, flags);
  396. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  397. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  398. return;
  399. }
  400. t = list_entry(rnp->gp_tasks,
  401. struct task_struct, rcu_node_entry);
  402. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  403. sched_show_task(t);
  404. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  405. }
  406. /*
  407. * Dump detailed information for all tasks blocking the current RCU
  408. * grace period.
  409. */
  410. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  411. {
  412. struct rcu_node *rnp = rcu_get_root(rsp);
  413. rcu_print_detail_task_stall_rnp(rnp);
  414. rcu_for_each_leaf_node(rsp, rnp)
  415. rcu_print_detail_task_stall_rnp(rnp);
  416. }
  417. #ifdef CONFIG_RCU_CPU_STALL_INFO
  418. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  419. {
  420. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  421. rnp->level, rnp->grplo, rnp->grphi);
  422. }
  423. static void rcu_print_task_stall_end(void)
  424. {
  425. pr_cont("\n");
  426. }
  427. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  428. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  429. {
  430. }
  431. static void rcu_print_task_stall_end(void)
  432. {
  433. }
  434. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  435. /*
  436. * Scan the current list of tasks blocked within RCU read-side critical
  437. * sections, printing out the tid of each.
  438. */
  439. static int rcu_print_task_stall(struct rcu_node *rnp)
  440. {
  441. struct task_struct *t;
  442. int ndetected = 0;
  443. if (!rcu_preempt_blocked_readers_cgp(rnp))
  444. return 0;
  445. rcu_print_task_stall_begin(rnp);
  446. t = list_entry(rnp->gp_tasks,
  447. struct task_struct, rcu_node_entry);
  448. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  449. pr_cont(" P%d", t->pid);
  450. ndetected++;
  451. }
  452. rcu_print_task_stall_end();
  453. return ndetected;
  454. }
  455. /*
  456. * Check that the list of blocked tasks for the newly completed grace
  457. * period is in fact empty. It is a serious bug to complete a grace
  458. * period that still has RCU readers blocked! This function must be
  459. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  460. * must be held by the caller.
  461. *
  462. * Also, if there are blocked tasks on the list, they automatically
  463. * block the newly created grace period, so set up ->gp_tasks accordingly.
  464. */
  465. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  466. {
  467. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  468. if (rcu_preempt_has_tasks(rnp))
  469. rnp->gp_tasks = rnp->blkd_tasks.next;
  470. WARN_ON_ONCE(rnp->qsmask);
  471. }
  472. #ifdef CONFIG_HOTPLUG_CPU
  473. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  474. /*
  475. * Check for a quiescent state from the current CPU. When a task blocks,
  476. * the task is recorded in the corresponding CPU's rcu_node structure,
  477. * which is checked elsewhere.
  478. *
  479. * Caller must disable hard irqs.
  480. */
  481. static void rcu_preempt_check_callbacks(void)
  482. {
  483. struct task_struct *t = current;
  484. if (t->rcu_read_lock_nesting == 0) {
  485. rcu_preempt_qs();
  486. return;
  487. }
  488. if (t->rcu_read_lock_nesting > 0 &&
  489. __this_cpu_read(rcu_preempt_data.qs_pending) &&
  490. !__this_cpu_read(rcu_preempt_data.passed_quiesce))
  491. t->rcu_read_unlock_special.b.need_qs = true;
  492. }
  493. #ifdef CONFIG_RCU_BOOST
  494. static void rcu_preempt_do_callbacks(void)
  495. {
  496. rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  497. }
  498. #endif /* #ifdef CONFIG_RCU_BOOST */
  499. /*
  500. * Queue a preemptible-RCU callback for invocation after a grace period.
  501. */
  502. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  503. {
  504. __call_rcu(head, func, &rcu_preempt_state, -1, 0);
  505. }
  506. EXPORT_SYMBOL_GPL(call_rcu);
  507. /**
  508. * synchronize_rcu - wait until a grace period has elapsed.
  509. *
  510. * Control will return to the caller some time after a full grace
  511. * period has elapsed, in other words after all currently executing RCU
  512. * read-side critical sections have completed. Note, however, that
  513. * upon return from synchronize_rcu(), the caller might well be executing
  514. * concurrently with new RCU read-side critical sections that began while
  515. * synchronize_rcu() was waiting. RCU read-side critical sections are
  516. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  517. *
  518. * See the description of synchronize_sched() for more detailed information
  519. * on memory ordering guarantees.
  520. */
  521. void synchronize_rcu(void)
  522. {
  523. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  524. !lock_is_held(&rcu_lock_map) &&
  525. !lock_is_held(&rcu_sched_lock_map),
  526. "Illegal synchronize_rcu() in RCU read-side critical section");
  527. if (!rcu_scheduler_active)
  528. return;
  529. if (rcu_expedited)
  530. synchronize_rcu_expedited();
  531. else
  532. wait_rcu_gp(call_rcu);
  533. }
  534. EXPORT_SYMBOL_GPL(synchronize_rcu);
  535. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  536. static unsigned long sync_rcu_preempt_exp_count;
  537. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  538. /*
  539. * Return non-zero if there are any tasks in RCU read-side critical
  540. * sections blocking the current preemptible-RCU expedited grace period.
  541. * If there is no preemptible-RCU expedited grace period currently in
  542. * progress, returns zero unconditionally.
  543. */
  544. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  545. {
  546. return rnp->exp_tasks != NULL;
  547. }
  548. /*
  549. * return non-zero if there is no RCU expedited grace period in progress
  550. * for the specified rcu_node structure, in other words, if all CPUs and
  551. * tasks covered by the specified rcu_node structure have done their bit
  552. * for the current expedited grace period. Works only for preemptible
  553. * RCU -- other RCU implementation use other means.
  554. *
  555. * Caller must hold sync_rcu_preempt_exp_mutex.
  556. */
  557. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  558. {
  559. return !rcu_preempted_readers_exp(rnp) &&
  560. ACCESS_ONCE(rnp->expmask) == 0;
  561. }
  562. /*
  563. * Report the exit from RCU read-side critical section for the last task
  564. * that queued itself during or before the current expedited preemptible-RCU
  565. * grace period. This event is reported either to the rcu_node structure on
  566. * which the task was queued or to one of that rcu_node structure's ancestors,
  567. * recursively up the tree. (Calm down, calm down, we do the recursion
  568. * iteratively!)
  569. *
  570. * Most callers will set the "wake" flag, but the task initiating the
  571. * expedited grace period need not wake itself.
  572. *
  573. * Caller must hold sync_rcu_preempt_exp_mutex.
  574. */
  575. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  576. bool wake)
  577. {
  578. unsigned long flags;
  579. unsigned long mask;
  580. raw_spin_lock_irqsave(&rnp->lock, flags);
  581. smp_mb__after_unlock_lock();
  582. for (;;) {
  583. if (!sync_rcu_preempt_exp_done(rnp)) {
  584. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  585. break;
  586. }
  587. if (rnp->parent == NULL) {
  588. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  589. if (wake) {
  590. smp_mb(); /* EGP done before wake_up(). */
  591. wake_up(&sync_rcu_preempt_exp_wq);
  592. }
  593. break;
  594. }
  595. mask = rnp->grpmask;
  596. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  597. rnp = rnp->parent;
  598. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  599. smp_mb__after_unlock_lock();
  600. rnp->expmask &= ~mask;
  601. }
  602. }
  603. /*
  604. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  605. * grace period for the specified rcu_node structure. If there are no such
  606. * tasks, report it up the rcu_node hierarchy.
  607. *
  608. * Caller must hold sync_rcu_preempt_exp_mutex and must exclude
  609. * CPU hotplug operations.
  610. */
  611. static void
  612. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  613. {
  614. unsigned long flags;
  615. int must_wait = 0;
  616. raw_spin_lock_irqsave(&rnp->lock, flags);
  617. smp_mb__after_unlock_lock();
  618. if (!rcu_preempt_has_tasks(rnp)) {
  619. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  620. } else {
  621. rnp->exp_tasks = rnp->blkd_tasks.next;
  622. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  623. must_wait = 1;
  624. }
  625. if (!must_wait)
  626. rcu_report_exp_rnp(rsp, rnp, false); /* Don't wake self. */
  627. }
  628. /**
  629. * synchronize_rcu_expedited - Brute-force RCU grace period
  630. *
  631. * Wait for an RCU-preempt grace period, but expedite it. The basic
  632. * idea is to invoke synchronize_sched_expedited() to push all the tasks to
  633. * the ->blkd_tasks lists and wait for this list to drain. This consumes
  634. * significant time on all CPUs and is unfriendly to real-time workloads,
  635. * so is thus not recommended for any sort of common-case code.
  636. * In fact, if you are using synchronize_rcu_expedited() in a loop,
  637. * please restructure your code to batch your updates, and then Use a
  638. * single synchronize_rcu() instead.
  639. */
  640. void synchronize_rcu_expedited(void)
  641. {
  642. unsigned long flags;
  643. struct rcu_node *rnp;
  644. struct rcu_state *rsp = &rcu_preempt_state;
  645. unsigned long snap;
  646. int trycount = 0;
  647. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  648. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  649. smp_mb(); /* Above access cannot bleed into critical section. */
  650. /*
  651. * Block CPU-hotplug operations. This means that any CPU-hotplug
  652. * operation that finds an rcu_node structure with tasks in the
  653. * process of being boosted will know that all tasks blocking
  654. * this expedited grace period will already be in the process of
  655. * being boosted. This simplifies the process of moving tasks
  656. * from leaf to root rcu_node structures.
  657. */
  658. if (!try_get_online_cpus()) {
  659. /* CPU-hotplug operation in flight, fall back to normal GP. */
  660. wait_rcu_gp(call_rcu);
  661. return;
  662. }
  663. /*
  664. * Acquire lock, falling back to synchronize_rcu() if too many
  665. * lock-acquisition failures. Of course, if someone does the
  666. * expedited grace period for us, just leave.
  667. */
  668. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  669. if (ULONG_CMP_LT(snap,
  670. ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  671. put_online_cpus();
  672. goto mb_ret; /* Others did our work for us. */
  673. }
  674. if (trycount++ < 10) {
  675. udelay(trycount * num_online_cpus());
  676. } else {
  677. put_online_cpus();
  678. wait_rcu_gp(call_rcu);
  679. return;
  680. }
  681. }
  682. if (ULONG_CMP_LT(snap, ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  683. put_online_cpus();
  684. goto unlock_mb_ret; /* Others did our work for us. */
  685. }
  686. /* force all RCU readers onto ->blkd_tasks lists. */
  687. synchronize_sched_expedited();
  688. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  689. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  690. raw_spin_lock_irqsave(&rnp->lock, flags);
  691. smp_mb__after_unlock_lock();
  692. rnp->expmask = rnp->qsmaskinit;
  693. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  694. }
  695. /* Snapshot current state of ->blkd_tasks lists. */
  696. rcu_for_each_leaf_node(rsp, rnp)
  697. sync_rcu_preempt_exp_init(rsp, rnp);
  698. if (NUM_RCU_NODES > 1)
  699. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  700. put_online_cpus();
  701. /* Wait for snapshotted ->blkd_tasks lists to drain. */
  702. rnp = rcu_get_root(rsp);
  703. wait_event(sync_rcu_preempt_exp_wq,
  704. sync_rcu_preempt_exp_done(rnp));
  705. /* Clean up and exit. */
  706. smp_mb(); /* ensure expedited GP seen before counter increment. */
  707. ACCESS_ONCE(sync_rcu_preempt_exp_count) =
  708. sync_rcu_preempt_exp_count + 1;
  709. unlock_mb_ret:
  710. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  711. mb_ret:
  712. smp_mb(); /* ensure subsequent action seen after grace period. */
  713. }
  714. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  715. /**
  716. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  717. *
  718. * Note that this primitive does not necessarily wait for an RCU grace period
  719. * to complete. For example, if there are no RCU callbacks queued anywhere
  720. * in the system, then rcu_barrier() is within its rights to return
  721. * immediately, without waiting for anything, much less an RCU grace period.
  722. */
  723. void rcu_barrier(void)
  724. {
  725. _rcu_barrier(&rcu_preempt_state);
  726. }
  727. EXPORT_SYMBOL_GPL(rcu_barrier);
  728. /*
  729. * Initialize preemptible RCU's state structures.
  730. */
  731. static void __init __rcu_init_preempt(void)
  732. {
  733. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  734. }
  735. /*
  736. * Check for a task exiting while in a preemptible-RCU read-side
  737. * critical section, clean up if so. No need to issue warnings,
  738. * as debug_check_no_locks_held() already does this if lockdep
  739. * is enabled.
  740. */
  741. void exit_rcu(void)
  742. {
  743. struct task_struct *t = current;
  744. if (likely(list_empty(&current->rcu_node_entry)))
  745. return;
  746. t->rcu_read_lock_nesting = 1;
  747. barrier();
  748. t->rcu_read_unlock_special.b.blocked = true;
  749. __rcu_read_unlock();
  750. }
  751. #else /* #ifdef CONFIG_PREEMPT_RCU */
  752. static struct rcu_state *rcu_state_p = &rcu_sched_state;
  753. /*
  754. * Tell them what RCU they are running.
  755. */
  756. static void __init rcu_bootup_announce(void)
  757. {
  758. pr_info("Hierarchical RCU implementation.\n");
  759. rcu_bootup_announce_oddness();
  760. }
  761. /*
  762. * Because preemptible RCU does not exist, we never have to check for
  763. * CPUs being in quiescent states.
  764. */
  765. static void rcu_preempt_note_context_switch(void)
  766. {
  767. }
  768. /*
  769. * Because preemptible RCU does not exist, there are never any preempted
  770. * RCU readers.
  771. */
  772. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  773. {
  774. return 0;
  775. }
  776. #ifdef CONFIG_HOTPLUG_CPU
  777. /*
  778. * Because there is no preemptible RCU, there can be no readers blocked.
  779. */
  780. static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
  781. {
  782. return false;
  783. }
  784. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  785. /*
  786. * Because preemptible RCU does not exist, we never have to check for
  787. * tasks blocked within RCU read-side critical sections.
  788. */
  789. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  790. {
  791. }
  792. /*
  793. * Because preemptible RCU does not exist, we never have to check for
  794. * tasks blocked within RCU read-side critical sections.
  795. */
  796. static int rcu_print_task_stall(struct rcu_node *rnp)
  797. {
  798. return 0;
  799. }
  800. /*
  801. * Because there is no preemptible RCU, there can be no readers blocked,
  802. * so there is no need to check for blocked tasks. So check only for
  803. * bogus qsmask values.
  804. */
  805. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  806. {
  807. WARN_ON_ONCE(rnp->qsmask);
  808. }
  809. /*
  810. * Because preemptible RCU does not exist, it never has any callbacks
  811. * to check.
  812. */
  813. static void rcu_preempt_check_callbacks(void)
  814. {
  815. }
  816. /*
  817. * Wait for an rcu-preempt grace period, but make it happen quickly.
  818. * But because preemptible RCU does not exist, map to rcu-sched.
  819. */
  820. void synchronize_rcu_expedited(void)
  821. {
  822. synchronize_sched_expedited();
  823. }
  824. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  825. /*
  826. * Because preemptible RCU does not exist, rcu_barrier() is just
  827. * another name for rcu_barrier_sched().
  828. */
  829. void rcu_barrier(void)
  830. {
  831. rcu_barrier_sched();
  832. }
  833. EXPORT_SYMBOL_GPL(rcu_barrier);
  834. /*
  835. * Because preemptible RCU does not exist, it need not be initialized.
  836. */
  837. static void __init __rcu_init_preempt(void)
  838. {
  839. }
  840. /*
  841. * Because preemptible RCU does not exist, tasks cannot possibly exit
  842. * while in preemptible RCU read-side critical sections.
  843. */
  844. void exit_rcu(void)
  845. {
  846. }
  847. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  848. #ifdef CONFIG_RCU_BOOST
  849. #include "../locking/rtmutex_common.h"
  850. #ifdef CONFIG_RCU_TRACE
  851. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  852. {
  853. if (!rcu_preempt_has_tasks(rnp))
  854. rnp->n_balk_blkd_tasks++;
  855. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  856. rnp->n_balk_exp_gp_tasks++;
  857. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  858. rnp->n_balk_boost_tasks++;
  859. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  860. rnp->n_balk_notblocked++;
  861. else if (rnp->gp_tasks != NULL &&
  862. ULONG_CMP_LT(jiffies, rnp->boost_time))
  863. rnp->n_balk_notyet++;
  864. else
  865. rnp->n_balk_nos++;
  866. }
  867. #else /* #ifdef CONFIG_RCU_TRACE */
  868. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  869. {
  870. }
  871. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  872. static void rcu_wake_cond(struct task_struct *t, int status)
  873. {
  874. /*
  875. * If the thread is yielding, only wake it when this
  876. * is invoked from idle
  877. */
  878. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  879. wake_up_process(t);
  880. }
  881. /*
  882. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  883. * or ->boost_tasks, advancing the pointer to the next task in the
  884. * ->blkd_tasks list.
  885. *
  886. * Note that irqs must be enabled: boosting the task can block.
  887. * Returns 1 if there are more tasks needing to be boosted.
  888. */
  889. static int rcu_boost(struct rcu_node *rnp)
  890. {
  891. unsigned long flags;
  892. struct task_struct *t;
  893. struct list_head *tb;
  894. if (ACCESS_ONCE(rnp->exp_tasks) == NULL &&
  895. ACCESS_ONCE(rnp->boost_tasks) == NULL)
  896. return 0; /* Nothing left to boost. */
  897. raw_spin_lock_irqsave(&rnp->lock, flags);
  898. smp_mb__after_unlock_lock();
  899. /*
  900. * Recheck under the lock: all tasks in need of boosting
  901. * might exit their RCU read-side critical sections on their own.
  902. */
  903. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  904. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  905. return 0;
  906. }
  907. /*
  908. * Preferentially boost tasks blocking expedited grace periods.
  909. * This cannot starve the normal grace periods because a second
  910. * expedited grace period must boost all blocked tasks, including
  911. * those blocking the pre-existing normal grace period.
  912. */
  913. if (rnp->exp_tasks != NULL) {
  914. tb = rnp->exp_tasks;
  915. rnp->n_exp_boosts++;
  916. } else {
  917. tb = rnp->boost_tasks;
  918. rnp->n_normal_boosts++;
  919. }
  920. rnp->n_tasks_boosted++;
  921. /*
  922. * We boost task t by manufacturing an rt_mutex that appears to
  923. * be held by task t. We leave a pointer to that rt_mutex where
  924. * task t can find it, and task t will release the mutex when it
  925. * exits its outermost RCU read-side critical section. Then
  926. * simply acquiring this artificial rt_mutex will boost task
  927. * t's priority. (Thanks to tglx for suggesting this approach!)
  928. *
  929. * Note that task t must acquire rnp->lock to remove itself from
  930. * the ->blkd_tasks list, which it will do from exit() if from
  931. * nowhere else. We therefore are guaranteed that task t will
  932. * stay around at least until we drop rnp->lock. Note that
  933. * rnp->lock also resolves races between our priority boosting
  934. * and task t's exiting its outermost RCU read-side critical
  935. * section.
  936. */
  937. t = container_of(tb, struct task_struct, rcu_node_entry);
  938. rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
  939. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  940. /* Lock only for side effect: boosts task t's priority. */
  941. rt_mutex_lock(&rnp->boost_mtx);
  942. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  943. return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
  944. ACCESS_ONCE(rnp->boost_tasks) != NULL;
  945. }
  946. /*
  947. * Priority-boosting kthread. One per leaf rcu_node and one for the
  948. * root rcu_node.
  949. */
  950. static int rcu_boost_kthread(void *arg)
  951. {
  952. struct rcu_node *rnp = (struct rcu_node *)arg;
  953. int spincnt = 0;
  954. int more2boost;
  955. trace_rcu_utilization(TPS("Start boost kthread@init"));
  956. for (;;) {
  957. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  958. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  959. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  960. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  961. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  962. more2boost = rcu_boost(rnp);
  963. if (more2boost)
  964. spincnt++;
  965. else
  966. spincnt = 0;
  967. if (spincnt > 10) {
  968. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  969. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  970. schedule_timeout_interruptible(2);
  971. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  972. spincnt = 0;
  973. }
  974. }
  975. /* NOTREACHED */
  976. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  977. return 0;
  978. }
  979. /*
  980. * Check to see if it is time to start boosting RCU readers that are
  981. * blocking the current grace period, and, if so, tell the per-rcu_node
  982. * kthread to start boosting them. If there is an expedited grace
  983. * period in progress, it is always time to boost.
  984. *
  985. * The caller must hold rnp->lock, which this function releases.
  986. * The ->boost_kthread_task is immortal, so we don't need to worry
  987. * about it going away.
  988. */
  989. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  990. __releases(rnp->lock)
  991. {
  992. struct task_struct *t;
  993. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  994. rnp->n_balk_exp_gp_tasks++;
  995. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  996. return;
  997. }
  998. if (rnp->exp_tasks != NULL ||
  999. (rnp->gp_tasks != NULL &&
  1000. rnp->boost_tasks == NULL &&
  1001. rnp->qsmask == 0 &&
  1002. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1003. if (rnp->exp_tasks == NULL)
  1004. rnp->boost_tasks = rnp->gp_tasks;
  1005. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1006. t = rnp->boost_kthread_task;
  1007. if (t)
  1008. rcu_wake_cond(t, rnp->boost_kthread_status);
  1009. } else {
  1010. rcu_initiate_boost_trace(rnp);
  1011. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1012. }
  1013. }
  1014. /*
  1015. * Wake up the per-CPU kthread to invoke RCU callbacks.
  1016. */
  1017. static void invoke_rcu_callbacks_kthread(void)
  1018. {
  1019. unsigned long flags;
  1020. local_irq_save(flags);
  1021. __this_cpu_write(rcu_cpu_has_work, 1);
  1022. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  1023. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  1024. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  1025. __this_cpu_read(rcu_cpu_kthread_status));
  1026. }
  1027. local_irq_restore(flags);
  1028. }
  1029. /*
  1030. * Is the current CPU running the RCU-callbacks kthread?
  1031. * Caller must have preemption disabled.
  1032. */
  1033. static bool rcu_is_callbacks_kthread(void)
  1034. {
  1035. return __this_cpu_read(rcu_cpu_kthread_task) == current;
  1036. }
  1037. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1038. /*
  1039. * Do priority-boost accounting for the start of a new grace period.
  1040. */
  1041. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1042. {
  1043. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1044. }
  1045. /*
  1046. * Create an RCU-boost kthread for the specified node if one does not
  1047. * already exist. We only create this kthread for preemptible RCU.
  1048. * Returns zero if all is well, a negated errno otherwise.
  1049. */
  1050. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1051. struct rcu_node *rnp)
  1052. {
  1053. int rnp_index = rnp - &rsp->node[0];
  1054. unsigned long flags;
  1055. struct sched_param sp;
  1056. struct task_struct *t;
  1057. if (&rcu_preempt_state != rsp)
  1058. return 0;
  1059. if (!rcu_scheduler_fully_active || rnp->qsmaskinit == 0)
  1060. return 0;
  1061. rsp->boost = 1;
  1062. if (rnp->boost_kthread_task != NULL)
  1063. return 0;
  1064. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1065. "rcub/%d", rnp_index);
  1066. if (IS_ERR(t))
  1067. return PTR_ERR(t);
  1068. raw_spin_lock_irqsave(&rnp->lock, flags);
  1069. smp_mb__after_unlock_lock();
  1070. rnp->boost_kthread_task = t;
  1071. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1072. sp.sched_priority = kthread_prio;
  1073. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1074. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1075. return 0;
  1076. }
  1077. static void rcu_kthread_do_work(void)
  1078. {
  1079. rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  1080. rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  1081. rcu_preempt_do_callbacks();
  1082. }
  1083. static void rcu_cpu_kthread_setup(unsigned int cpu)
  1084. {
  1085. struct sched_param sp;
  1086. sp.sched_priority = kthread_prio;
  1087. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1088. }
  1089. static void rcu_cpu_kthread_park(unsigned int cpu)
  1090. {
  1091. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1092. }
  1093. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  1094. {
  1095. return __this_cpu_read(rcu_cpu_has_work);
  1096. }
  1097. /*
  1098. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1099. * RCU softirq used in flavors and configurations of RCU that do not
  1100. * support RCU priority boosting.
  1101. */
  1102. static void rcu_cpu_kthread(unsigned int cpu)
  1103. {
  1104. unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  1105. char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  1106. int spincnt;
  1107. for (spincnt = 0; spincnt < 10; spincnt++) {
  1108. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1109. local_bh_disable();
  1110. *statusp = RCU_KTHREAD_RUNNING;
  1111. this_cpu_inc(rcu_cpu_kthread_loops);
  1112. local_irq_disable();
  1113. work = *workp;
  1114. *workp = 0;
  1115. local_irq_enable();
  1116. if (work)
  1117. rcu_kthread_do_work();
  1118. local_bh_enable();
  1119. if (*workp == 0) {
  1120. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1121. *statusp = RCU_KTHREAD_WAITING;
  1122. return;
  1123. }
  1124. }
  1125. *statusp = RCU_KTHREAD_YIELDING;
  1126. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1127. schedule_timeout_interruptible(2);
  1128. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1129. *statusp = RCU_KTHREAD_WAITING;
  1130. }
  1131. /*
  1132. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1133. * served by the rcu_node in question. The CPU hotplug lock is still
  1134. * held, so the value of rnp->qsmaskinit will be stable.
  1135. *
  1136. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1137. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1138. * this function allows the kthread to execute on any CPU.
  1139. */
  1140. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1141. {
  1142. struct task_struct *t = rnp->boost_kthread_task;
  1143. unsigned long mask = rnp->qsmaskinit;
  1144. cpumask_var_t cm;
  1145. int cpu;
  1146. if (!t)
  1147. return;
  1148. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1149. return;
  1150. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
  1151. if ((mask & 0x1) && cpu != outgoingcpu)
  1152. cpumask_set_cpu(cpu, cm);
  1153. if (cpumask_weight(cm) == 0)
  1154. cpumask_setall(cm);
  1155. set_cpus_allowed_ptr(t, cm);
  1156. free_cpumask_var(cm);
  1157. }
  1158. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1159. .store = &rcu_cpu_kthread_task,
  1160. .thread_should_run = rcu_cpu_kthread_should_run,
  1161. .thread_fn = rcu_cpu_kthread,
  1162. .thread_comm = "rcuc/%u",
  1163. .setup = rcu_cpu_kthread_setup,
  1164. .park = rcu_cpu_kthread_park,
  1165. };
  1166. /*
  1167. * Spawn boost kthreads -- called as soon as the scheduler is running.
  1168. */
  1169. static void __init rcu_spawn_boost_kthreads(void)
  1170. {
  1171. struct rcu_node *rnp;
  1172. int cpu;
  1173. for_each_possible_cpu(cpu)
  1174. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1175. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1176. rcu_for_each_leaf_node(rcu_state_p, rnp)
  1177. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1178. }
  1179. static void rcu_prepare_kthreads(int cpu)
  1180. {
  1181. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  1182. struct rcu_node *rnp = rdp->mynode;
  1183. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1184. if (rcu_scheduler_fully_active)
  1185. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1186. }
  1187. #else /* #ifdef CONFIG_RCU_BOOST */
  1188. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1189. __releases(rnp->lock)
  1190. {
  1191. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1192. }
  1193. static void invoke_rcu_callbacks_kthread(void)
  1194. {
  1195. WARN_ON_ONCE(1);
  1196. }
  1197. static bool rcu_is_callbacks_kthread(void)
  1198. {
  1199. return false;
  1200. }
  1201. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1202. {
  1203. }
  1204. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1205. {
  1206. }
  1207. static void __init rcu_spawn_boost_kthreads(void)
  1208. {
  1209. }
  1210. static void rcu_prepare_kthreads(int cpu)
  1211. {
  1212. }
  1213. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1214. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1215. /*
  1216. * Check to see if any future RCU-related work will need to be done
  1217. * by the current CPU, even if none need be done immediately, returning
  1218. * 1 if so. This function is part of the RCU implementation; it is -not-
  1219. * an exported member of the RCU API.
  1220. *
  1221. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1222. * any flavor of RCU.
  1223. */
  1224. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1225. int rcu_needs_cpu(unsigned long *delta_jiffies)
  1226. {
  1227. *delta_jiffies = ULONG_MAX;
  1228. return rcu_cpu_has_callbacks(NULL);
  1229. }
  1230. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1231. /*
  1232. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1233. * after it.
  1234. */
  1235. static void rcu_cleanup_after_idle(void)
  1236. {
  1237. }
  1238. /*
  1239. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1240. * is nothing.
  1241. */
  1242. static void rcu_prepare_for_idle(void)
  1243. {
  1244. }
  1245. /*
  1246. * Don't bother keeping a running count of the number of RCU callbacks
  1247. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1248. */
  1249. static void rcu_idle_count_callbacks_posted(void)
  1250. {
  1251. }
  1252. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1253. /*
  1254. * This code is invoked when a CPU goes idle, at which point we want
  1255. * to have the CPU do everything required for RCU so that it can enter
  1256. * the energy-efficient dyntick-idle mode. This is handled by a
  1257. * state machine implemented by rcu_prepare_for_idle() below.
  1258. *
  1259. * The following three proprocessor symbols control this state machine:
  1260. *
  1261. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1262. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1263. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1264. * benchmarkers who might otherwise be tempted to set this to a large
  1265. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1266. * system. And if you are -that- concerned about energy efficiency,
  1267. * just power the system down and be done with it!
  1268. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1269. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1270. * callbacks pending. Setting this too high can OOM your system.
  1271. *
  1272. * The values below work well in practice. If future workloads require
  1273. * adjustment, they can be converted into kernel config parameters, though
  1274. * making the state machine smarter might be a better option.
  1275. */
  1276. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1277. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1278. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1279. module_param(rcu_idle_gp_delay, int, 0644);
  1280. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1281. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1282. extern int tick_nohz_active;
  1283. /*
  1284. * Try to advance callbacks for all flavors of RCU on the current CPU, but
  1285. * only if it has been awhile since the last time we did so. Afterwards,
  1286. * if there are any callbacks ready for immediate invocation, return true.
  1287. */
  1288. static bool __maybe_unused rcu_try_advance_all_cbs(void)
  1289. {
  1290. bool cbs_ready = false;
  1291. struct rcu_data *rdp;
  1292. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1293. struct rcu_node *rnp;
  1294. struct rcu_state *rsp;
  1295. /* Exit early if we advanced recently. */
  1296. if (jiffies == rdtp->last_advance_all)
  1297. return false;
  1298. rdtp->last_advance_all = jiffies;
  1299. for_each_rcu_flavor(rsp) {
  1300. rdp = this_cpu_ptr(rsp->rda);
  1301. rnp = rdp->mynode;
  1302. /*
  1303. * Don't bother checking unless a grace period has
  1304. * completed since we last checked and there are
  1305. * callbacks not yet ready to invoke.
  1306. */
  1307. if ((rdp->completed != rnp->completed ||
  1308. unlikely(ACCESS_ONCE(rdp->gpwrap))) &&
  1309. rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
  1310. note_gp_changes(rsp, rdp);
  1311. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1312. cbs_ready = true;
  1313. }
  1314. return cbs_ready;
  1315. }
  1316. /*
  1317. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1318. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1319. * caller to set the timeout based on whether or not there are non-lazy
  1320. * callbacks.
  1321. *
  1322. * The caller must have disabled interrupts.
  1323. */
  1324. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1325. int rcu_needs_cpu(unsigned long *dj)
  1326. {
  1327. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1328. /* Snapshot to detect later posting of non-lazy callback. */
  1329. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1330. /* If no callbacks, RCU doesn't need the CPU. */
  1331. if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
  1332. *dj = ULONG_MAX;
  1333. return 0;
  1334. }
  1335. /* Attempt to advance callbacks. */
  1336. if (rcu_try_advance_all_cbs()) {
  1337. /* Some ready to invoke, so initiate later invocation. */
  1338. invoke_rcu_core();
  1339. return 1;
  1340. }
  1341. rdtp->last_accelerate = jiffies;
  1342. /* Request timer delay depending on laziness, and round. */
  1343. if (!rdtp->all_lazy) {
  1344. *dj = round_up(rcu_idle_gp_delay + jiffies,
  1345. rcu_idle_gp_delay) - jiffies;
  1346. } else {
  1347. *dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1348. }
  1349. return 0;
  1350. }
  1351. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1352. /*
  1353. * Prepare a CPU for idle from an RCU perspective. The first major task
  1354. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1355. * The second major task is to check to see if a non-lazy callback has
  1356. * arrived at a CPU that previously had only lazy callbacks. The third
  1357. * major task is to accelerate (that is, assign grace-period numbers to)
  1358. * any recently arrived callbacks.
  1359. *
  1360. * The caller must have disabled interrupts.
  1361. */
  1362. static void rcu_prepare_for_idle(void)
  1363. {
  1364. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1365. bool needwake;
  1366. struct rcu_data *rdp;
  1367. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1368. struct rcu_node *rnp;
  1369. struct rcu_state *rsp;
  1370. int tne;
  1371. /* Handle nohz enablement switches conservatively. */
  1372. tne = ACCESS_ONCE(tick_nohz_active);
  1373. if (tne != rdtp->tick_nohz_enabled_snap) {
  1374. if (rcu_cpu_has_callbacks(NULL))
  1375. invoke_rcu_core(); /* force nohz to see update. */
  1376. rdtp->tick_nohz_enabled_snap = tne;
  1377. return;
  1378. }
  1379. if (!tne)
  1380. return;
  1381. /* If this is a no-CBs CPU, no callbacks, just return. */
  1382. if (rcu_is_nocb_cpu(smp_processor_id()))
  1383. return;
  1384. /*
  1385. * If a non-lazy callback arrived at a CPU having only lazy
  1386. * callbacks, invoke RCU core for the side-effect of recalculating
  1387. * idle duration on re-entry to idle.
  1388. */
  1389. if (rdtp->all_lazy &&
  1390. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1391. rdtp->all_lazy = false;
  1392. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1393. invoke_rcu_core();
  1394. return;
  1395. }
  1396. /*
  1397. * If we have not yet accelerated this jiffy, accelerate all
  1398. * callbacks on this CPU.
  1399. */
  1400. if (rdtp->last_accelerate == jiffies)
  1401. return;
  1402. rdtp->last_accelerate = jiffies;
  1403. for_each_rcu_flavor(rsp) {
  1404. rdp = this_cpu_ptr(rsp->rda);
  1405. if (!*rdp->nxttail[RCU_DONE_TAIL])
  1406. continue;
  1407. rnp = rdp->mynode;
  1408. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1409. smp_mb__after_unlock_lock();
  1410. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  1411. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1412. if (needwake)
  1413. rcu_gp_kthread_wake(rsp);
  1414. }
  1415. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1416. }
  1417. /*
  1418. * Clean up for exit from idle. Attempt to advance callbacks based on
  1419. * any grace periods that elapsed while the CPU was idle, and if any
  1420. * callbacks are now ready to invoke, initiate invocation.
  1421. */
  1422. static void rcu_cleanup_after_idle(void)
  1423. {
  1424. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1425. if (rcu_is_nocb_cpu(smp_processor_id()))
  1426. return;
  1427. if (rcu_try_advance_all_cbs())
  1428. invoke_rcu_core();
  1429. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1430. }
  1431. /*
  1432. * Keep a running count of the number of non-lazy callbacks posted
  1433. * on this CPU. This running counter (which is never decremented) allows
  1434. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1435. * posts a callback, even if an equal number of callbacks are invoked.
  1436. * Of course, callbacks should only be posted from within a trace event
  1437. * designed to be called from idle or from within RCU_NONIDLE().
  1438. */
  1439. static void rcu_idle_count_callbacks_posted(void)
  1440. {
  1441. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1442. }
  1443. /*
  1444. * Data for flushing lazy RCU callbacks at OOM time.
  1445. */
  1446. static atomic_t oom_callback_count;
  1447. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1448. /*
  1449. * RCU OOM callback -- decrement the outstanding count and deliver the
  1450. * wake-up if we are the last one.
  1451. */
  1452. static void rcu_oom_callback(struct rcu_head *rhp)
  1453. {
  1454. if (atomic_dec_and_test(&oom_callback_count))
  1455. wake_up(&oom_callback_wq);
  1456. }
  1457. /*
  1458. * Post an rcu_oom_notify callback on the current CPU if it has at
  1459. * least one lazy callback. This will unnecessarily post callbacks
  1460. * to CPUs that already have a non-lazy callback at the end of their
  1461. * callback list, but this is an infrequent operation, so accept some
  1462. * extra overhead to keep things simple.
  1463. */
  1464. static void rcu_oom_notify_cpu(void *unused)
  1465. {
  1466. struct rcu_state *rsp;
  1467. struct rcu_data *rdp;
  1468. for_each_rcu_flavor(rsp) {
  1469. rdp = raw_cpu_ptr(rsp->rda);
  1470. if (rdp->qlen_lazy != 0) {
  1471. atomic_inc(&oom_callback_count);
  1472. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1473. }
  1474. }
  1475. }
  1476. /*
  1477. * If low on memory, ensure that each CPU has a non-lazy callback.
  1478. * This will wake up CPUs that have only lazy callbacks, in turn
  1479. * ensuring that they free up the corresponding memory in a timely manner.
  1480. * Because an uncertain amount of memory will be freed in some uncertain
  1481. * timeframe, we do not claim to have freed anything.
  1482. */
  1483. static int rcu_oom_notify(struct notifier_block *self,
  1484. unsigned long notused, void *nfreed)
  1485. {
  1486. int cpu;
  1487. /* Wait for callbacks from earlier instance to complete. */
  1488. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1489. smp_mb(); /* Ensure callback reuse happens after callback invocation. */
  1490. /*
  1491. * Prevent premature wakeup: ensure that all increments happen
  1492. * before there is a chance of the counter reaching zero.
  1493. */
  1494. atomic_set(&oom_callback_count, 1);
  1495. get_online_cpus();
  1496. for_each_online_cpu(cpu) {
  1497. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1498. cond_resched_rcu_qs();
  1499. }
  1500. put_online_cpus();
  1501. /* Unconditionally decrement: no need to wake ourselves up. */
  1502. atomic_dec(&oom_callback_count);
  1503. return NOTIFY_OK;
  1504. }
  1505. static struct notifier_block rcu_oom_nb = {
  1506. .notifier_call = rcu_oom_notify
  1507. };
  1508. static int __init rcu_register_oom_notifier(void)
  1509. {
  1510. register_oom_notifier(&rcu_oom_nb);
  1511. return 0;
  1512. }
  1513. early_initcall(rcu_register_oom_notifier);
  1514. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1515. #ifdef CONFIG_RCU_CPU_STALL_INFO
  1516. #ifdef CONFIG_RCU_FAST_NO_HZ
  1517. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1518. {
  1519. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1520. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1521. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1522. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1523. ulong2long(nlpd),
  1524. rdtp->all_lazy ? 'L' : '.',
  1525. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1526. }
  1527. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1528. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1529. {
  1530. *cp = '\0';
  1531. }
  1532. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1533. /* Initiate the stall-info list. */
  1534. static void print_cpu_stall_info_begin(void)
  1535. {
  1536. pr_cont("\n");
  1537. }
  1538. /*
  1539. * Print out diagnostic information for the specified stalled CPU.
  1540. *
  1541. * If the specified CPU is aware of the current RCU grace period
  1542. * (flavor specified by rsp), then print the number of scheduling
  1543. * clock interrupts the CPU has taken during the time that it has
  1544. * been aware. Otherwise, print the number of RCU grace periods
  1545. * that this CPU is ignorant of, for example, "1" if the CPU was
  1546. * aware of the previous grace period.
  1547. *
  1548. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1549. */
  1550. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1551. {
  1552. char fast_no_hz[72];
  1553. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1554. struct rcu_dynticks *rdtp = rdp->dynticks;
  1555. char *ticks_title;
  1556. unsigned long ticks_value;
  1557. if (rsp->gpnum == rdp->gpnum) {
  1558. ticks_title = "ticks this GP";
  1559. ticks_value = rdp->ticks_this_gp;
  1560. } else {
  1561. ticks_title = "GPs behind";
  1562. ticks_value = rsp->gpnum - rdp->gpnum;
  1563. }
  1564. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1565. pr_err("\t%d: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u fqs=%ld %s\n",
  1566. cpu, ticks_value, ticks_title,
  1567. atomic_read(&rdtp->dynticks) & 0xfff,
  1568. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1569. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1570. ACCESS_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
  1571. fast_no_hz);
  1572. }
  1573. /* Terminate the stall-info list. */
  1574. static void print_cpu_stall_info_end(void)
  1575. {
  1576. pr_err("\t");
  1577. }
  1578. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1579. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1580. {
  1581. rdp->ticks_this_gp = 0;
  1582. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1583. }
  1584. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1585. static void increment_cpu_stall_ticks(void)
  1586. {
  1587. struct rcu_state *rsp;
  1588. for_each_rcu_flavor(rsp)
  1589. raw_cpu_inc(rsp->rda->ticks_this_gp);
  1590. }
  1591. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1592. static void print_cpu_stall_info_begin(void)
  1593. {
  1594. pr_cont(" {");
  1595. }
  1596. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1597. {
  1598. pr_cont(" %d", cpu);
  1599. }
  1600. static void print_cpu_stall_info_end(void)
  1601. {
  1602. pr_cont("} ");
  1603. }
  1604. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1605. {
  1606. }
  1607. static void increment_cpu_stall_ticks(void)
  1608. {
  1609. }
  1610. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1611. #ifdef CONFIG_RCU_NOCB_CPU
  1612. /*
  1613. * Offload callback processing from the boot-time-specified set of CPUs
  1614. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1615. * kthread created that pulls the callbacks from the corresponding CPU,
  1616. * waits for a grace period to elapse, and invokes the callbacks.
  1617. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1618. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1619. * has been specified, in which case each kthread actively polls its
  1620. * CPU. (Which isn't so great for energy efficiency, but which does
  1621. * reduce RCU's overhead on that CPU.)
  1622. *
  1623. * This is intended to be used in conjunction with Frederic Weisbecker's
  1624. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1625. * running CPU-bound user-mode computations.
  1626. *
  1627. * Offloading of callback processing could also in theory be used as
  1628. * an energy-efficiency measure because CPUs with no RCU callbacks
  1629. * queued are more aggressive about entering dyntick-idle mode.
  1630. */
  1631. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1632. static int __init rcu_nocb_setup(char *str)
  1633. {
  1634. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1635. have_rcu_nocb_mask = true;
  1636. cpulist_parse(str, rcu_nocb_mask);
  1637. return 1;
  1638. }
  1639. __setup("rcu_nocbs=", rcu_nocb_setup);
  1640. static int __init parse_rcu_nocb_poll(char *arg)
  1641. {
  1642. rcu_nocb_poll = 1;
  1643. return 0;
  1644. }
  1645. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1646. /*
  1647. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1648. * grace period.
  1649. */
  1650. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1651. {
  1652. wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  1653. }
  1654. /*
  1655. * Set the root rcu_node structure's ->need_future_gp field
  1656. * based on the sum of those of all rcu_node structures. This does
  1657. * double-count the root rcu_node structure's requests, but this
  1658. * is necessary to handle the possibility of a rcu_nocb_kthread()
  1659. * having awakened during the time that the rcu_node structures
  1660. * were being updated for the end of the previous grace period.
  1661. */
  1662. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  1663. {
  1664. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  1665. }
  1666. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1667. {
  1668. init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  1669. init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  1670. }
  1671. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1672. /* Is the specified CPU a no-CBs CPU? */
  1673. bool rcu_is_nocb_cpu(int cpu)
  1674. {
  1675. if (have_rcu_nocb_mask)
  1676. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1677. return false;
  1678. }
  1679. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1680. /*
  1681. * Kick the leader kthread for this NOCB group.
  1682. */
  1683. static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  1684. {
  1685. struct rcu_data *rdp_leader = rdp->nocb_leader;
  1686. if (!ACCESS_ONCE(rdp_leader->nocb_kthread))
  1687. return;
  1688. if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  1689. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  1690. ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
  1691. wake_up(&rdp_leader->nocb_wq);
  1692. }
  1693. }
  1694. /*
  1695. * Does the specified CPU need an RCU callback for the specified flavor
  1696. * of rcu_barrier()?
  1697. */
  1698. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  1699. {
  1700. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1701. unsigned long ret;
  1702. #ifdef CONFIG_PROVE_RCU
  1703. struct rcu_head *rhp;
  1704. #endif /* #ifdef CONFIG_PROVE_RCU */
  1705. /*
  1706. * Check count of all no-CBs callbacks awaiting invocation.
  1707. * There needs to be a barrier before this function is called,
  1708. * but associated with a prior determination that no more
  1709. * callbacks would be posted. In the worst case, the first
  1710. * barrier in _rcu_barrier() suffices (but the caller cannot
  1711. * necessarily rely on this, not a substitute for the caller
  1712. * getting the concurrency design right!). There must also be
  1713. * a barrier between the following load an posting of a callback
  1714. * (if a callback is in fact needed). This is associated with an
  1715. * atomic_inc() in the caller.
  1716. */
  1717. ret = atomic_long_read(&rdp->nocb_q_count);
  1718. #ifdef CONFIG_PROVE_RCU
  1719. rhp = ACCESS_ONCE(rdp->nocb_head);
  1720. if (!rhp)
  1721. rhp = ACCESS_ONCE(rdp->nocb_gp_head);
  1722. if (!rhp)
  1723. rhp = ACCESS_ONCE(rdp->nocb_follower_head);
  1724. /* Having no rcuo kthread but CBs after scheduler starts is bad! */
  1725. if (!ACCESS_ONCE(rdp->nocb_kthread) && rhp) {
  1726. /* RCU callback enqueued before CPU first came online??? */
  1727. pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
  1728. cpu, rhp->func);
  1729. WARN_ON_ONCE(1);
  1730. }
  1731. #endif /* #ifdef CONFIG_PROVE_RCU */
  1732. return !!ret;
  1733. }
  1734. /*
  1735. * Enqueue the specified string of rcu_head structures onto the specified
  1736. * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
  1737. * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
  1738. * counts are supplied by rhcount and rhcount_lazy.
  1739. *
  1740. * If warranted, also wake up the kthread servicing this CPUs queues.
  1741. */
  1742. static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
  1743. struct rcu_head *rhp,
  1744. struct rcu_head **rhtp,
  1745. int rhcount, int rhcount_lazy,
  1746. unsigned long flags)
  1747. {
  1748. int len;
  1749. struct rcu_head **old_rhpp;
  1750. struct task_struct *t;
  1751. /* Enqueue the callback on the nocb list and update counts. */
  1752. atomic_long_add(rhcount, &rdp->nocb_q_count);
  1753. /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
  1754. old_rhpp = xchg(&rdp->nocb_tail, rhtp);
  1755. ACCESS_ONCE(*old_rhpp) = rhp;
  1756. atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
  1757. smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
  1758. /* If we are not being polled and there is a kthread, awaken it ... */
  1759. t = ACCESS_ONCE(rdp->nocb_kthread);
  1760. if (rcu_nocb_poll || !t) {
  1761. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1762. TPS("WakeNotPoll"));
  1763. return;
  1764. }
  1765. len = atomic_long_read(&rdp->nocb_q_count);
  1766. if (old_rhpp == &rdp->nocb_head) {
  1767. if (!irqs_disabled_flags(flags)) {
  1768. /* ... if queue was empty ... */
  1769. wake_nocb_leader(rdp, false);
  1770. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1771. TPS("WakeEmpty"));
  1772. } else {
  1773. rdp->nocb_defer_wakeup = RCU_NOGP_WAKE;
  1774. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1775. TPS("WakeEmptyIsDeferred"));
  1776. }
  1777. rdp->qlen_last_fqs_check = 0;
  1778. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  1779. /* ... or if many callbacks queued. */
  1780. if (!irqs_disabled_flags(flags)) {
  1781. wake_nocb_leader(rdp, true);
  1782. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1783. TPS("WakeOvf"));
  1784. } else {
  1785. rdp->nocb_defer_wakeup = RCU_NOGP_WAKE_FORCE;
  1786. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1787. TPS("WakeOvfIsDeferred"));
  1788. }
  1789. rdp->qlen_last_fqs_check = LONG_MAX / 2;
  1790. } else {
  1791. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
  1792. }
  1793. return;
  1794. }
  1795. /*
  1796. * This is a helper for __call_rcu(), which invokes this when the normal
  1797. * callback queue is inoperable. If this is not a no-CBs CPU, this
  1798. * function returns failure back to __call_rcu(), which can complain
  1799. * appropriately.
  1800. *
  1801. * Otherwise, this function queues the callback where the corresponding
  1802. * "rcuo" kthread can find it.
  1803. */
  1804. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  1805. bool lazy, unsigned long flags)
  1806. {
  1807. if (!rcu_is_nocb_cpu(rdp->cpu))
  1808. return false;
  1809. __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
  1810. if (__is_kfree_rcu_offset((unsigned long)rhp->func))
  1811. trace_rcu_kfree_callback(rdp->rsp->name, rhp,
  1812. (unsigned long)rhp->func,
  1813. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1814. -atomic_long_read(&rdp->nocb_q_count));
  1815. else
  1816. trace_rcu_callback(rdp->rsp->name, rhp,
  1817. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1818. -atomic_long_read(&rdp->nocb_q_count));
  1819. /*
  1820. * If called from an extended quiescent state with interrupts
  1821. * disabled, invoke the RCU core in order to allow the idle-entry
  1822. * deferred-wakeup check to function.
  1823. */
  1824. if (irqs_disabled_flags(flags) &&
  1825. !rcu_is_watching() &&
  1826. cpu_online(smp_processor_id()))
  1827. invoke_rcu_core();
  1828. return true;
  1829. }
  1830. /*
  1831. * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
  1832. * not a no-CBs CPU.
  1833. */
  1834. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  1835. struct rcu_data *rdp,
  1836. unsigned long flags)
  1837. {
  1838. long ql = rsp->qlen;
  1839. long qll = rsp->qlen_lazy;
  1840. /* If this is not a no-CBs CPU, tell the caller to do it the old way. */
  1841. if (!rcu_is_nocb_cpu(smp_processor_id()))
  1842. return false;
  1843. rsp->qlen = 0;
  1844. rsp->qlen_lazy = 0;
  1845. /* First, enqueue the donelist, if any. This preserves CB ordering. */
  1846. if (rsp->orphan_donelist != NULL) {
  1847. __call_rcu_nocb_enqueue(rdp, rsp->orphan_donelist,
  1848. rsp->orphan_donetail, ql, qll, flags);
  1849. ql = qll = 0;
  1850. rsp->orphan_donelist = NULL;
  1851. rsp->orphan_donetail = &rsp->orphan_donelist;
  1852. }
  1853. if (rsp->orphan_nxtlist != NULL) {
  1854. __call_rcu_nocb_enqueue(rdp, rsp->orphan_nxtlist,
  1855. rsp->orphan_nxttail, ql, qll, flags);
  1856. ql = qll = 0;
  1857. rsp->orphan_nxtlist = NULL;
  1858. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  1859. }
  1860. return true;
  1861. }
  1862. /*
  1863. * If necessary, kick off a new grace period, and either way wait
  1864. * for a subsequent grace period to complete.
  1865. */
  1866. static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  1867. {
  1868. unsigned long c;
  1869. bool d;
  1870. unsigned long flags;
  1871. bool needwake;
  1872. struct rcu_node *rnp = rdp->mynode;
  1873. raw_spin_lock_irqsave(&rnp->lock, flags);
  1874. smp_mb__after_unlock_lock();
  1875. needwake = rcu_start_future_gp(rnp, rdp, &c);
  1876. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1877. if (needwake)
  1878. rcu_gp_kthread_wake(rdp->rsp);
  1879. /*
  1880. * Wait for the grace period. Do so interruptibly to avoid messing
  1881. * up the load average.
  1882. */
  1883. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  1884. for (;;) {
  1885. wait_event_interruptible(
  1886. rnp->nocb_gp_wq[c & 0x1],
  1887. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  1888. if (likely(d))
  1889. break;
  1890. WARN_ON(signal_pending(current));
  1891. trace_rcu_future_gp(rnp, rdp, c, TPS("ResumeWait"));
  1892. }
  1893. trace_rcu_future_gp(rnp, rdp, c, TPS("EndWait"));
  1894. smp_mb(); /* Ensure that CB invocation happens after GP end. */
  1895. }
  1896. /*
  1897. * Leaders come here to wait for additional callbacks to show up.
  1898. * This function does not return until callbacks appear.
  1899. */
  1900. static void nocb_leader_wait(struct rcu_data *my_rdp)
  1901. {
  1902. bool firsttime = true;
  1903. bool gotcbs;
  1904. struct rcu_data *rdp;
  1905. struct rcu_head **tail;
  1906. wait_again:
  1907. /* Wait for callbacks to appear. */
  1908. if (!rcu_nocb_poll) {
  1909. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  1910. wait_event_interruptible(my_rdp->nocb_wq,
  1911. !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
  1912. /* Memory barrier handled by smp_mb() calls below and repoll. */
  1913. } else if (firsttime) {
  1914. firsttime = false; /* Don't drown trace log with "Poll"! */
  1915. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Poll");
  1916. }
  1917. /*
  1918. * Each pass through the following loop checks a follower for CBs.
  1919. * We are our own first follower. Any CBs found are moved to
  1920. * nocb_gp_head, where they await a grace period.
  1921. */
  1922. gotcbs = false;
  1923. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1924. rdp->nocb_gp_head = ACCESS_ONCE(rdp->nocb_head);
  1925. if (!rdp->nocb_gp_head)
  1926. continue; /* No CBs here, try next follower. */
  1927. /* Move callbacks to wait-for-GP list, which is empty. */
  1928. ACCESS_ONCE(rdp->nocb_head) = NULL;
  1929. rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  1930. gotcbs = true;
  1931. }
  1932. /*
  1933. * If there were no callbacks, sleep a bit, rescan after a
  1934. * memory barrier, and go retry.
  1935. */
  1936. if (unlikely(!gotcbs)) {
  1937. if (!rcu_nocb_poll)
  1938. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
  1939. "WokeEmpty");
  1940. WARN_ON(signal_pending(current));
  1941. schedule_timeout_interruptible(1);
  1942. /* Rescan in case we were a victim of memory ordering. */
  1943. my_rdp->nocb_leader_sleep = true;
  1944. smp_mb(); /* Ensure _sleep true before scan. */
  1945. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower)
  1946. if (ACCESS_ONCE(rdp->nocb_head)) {
  1947. /* Found CB, so short-circuit next wait. */
  1948. my_rdp->nocb_leader_sleep = false;
  1949. break;
  1950. }
  1951. goto wait_again;
  1952. }
  1953. /* Wait for one grace period. */
  1954. rcu_nocb_wait_gp(my_rdp);
  1955. /*
  1956. * We left ->nocb_leader_sleep unset to reduce cache thrashing.
  1957. * We set it now, but recheck for new callbacks while
  1958. * traversing our follower list.
  1959. */
  1960. my_rdp->nocb_leader_sleep = true;
  1961. smp_mb(); /* Ensure _sleep true before scan of ->nocb_head. */
  1962. /* Each pass through the following loop wakes a follower, if needed. */
  1963. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1964. if (ACCESS_ONCE(rdp->nocb_head))
  1965. my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
  1966. if (!rdp->nocb_gp_head)
  1967. continue; /* No CBs, so no need to wake follower. */
  1968. /* Append callbacks to follower's "done" list. */
  1969. tail = xchg(&rdp->nocb_follower_tail, rdp->nocb_gp_tail);
  1970. *tail = rdp->nocb_gp_head;
  1971. smp_mb__after_atomic(); /* Store *tail before wakeup. */
  1972. if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
  1973. /*
  1974. * List was empty, wake up the follower.
  1975. * Memory barriers supplied by atomic_long_add().
  1976. */
  1977. wake_up(&rdp->nocb_wq);
  1978. }
  1979. }
  1980. /* If we (the leader) don't have CBs, go wait some more. */
  1981. if (!my_rdp->nocb_follower_head)
  1982. goto wait_again;
  1983. }
  1984. /*
  1985. * Followers come here to wait for additional callbacks to show up.
  1986. * This function does not return until callbacks appear.
  1987. */
  1988. static void nocb_follower_wait(struct rcu_data *rdp)
  1989. {
  1990. bool firsttime = true;
  1991. for (;;) {
  1992. if (!rcu_nocb_poll) {
  1993. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1994. "FollowerSleep");
  1995. wait_event_interruptible(rdp->nocb_wq,
  1996. ACCESS_ONCE(rdp->nocb_follower_head));
  1997. } else if (firsttime) {
  1998. /* Don't drown trace log with "Poll"! */
  1999. firsttime = false;
  2000. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "Poll");
  2001. }
  2002. if (smp_load_acquire(&rdp->nocb_follower_head)) {
  2003. /* ^^^ Ensure CB invocation follows _head test. */
  2004. return;
  2005. }
  2006. if (!rcu_nocb_poll)
  2007. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2008. "WokeEmpty");
  2009. WARN_ON(signal_pending(current));
  2010. schedule_timeout_interruptible(1);
  2011. }
  2012. }
  2013. /*
  2014. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  2015. * callbacks queued by the corresponding no-CBs CPU, however, there is
  2016. * an optional leader-follower relationship so that the grace-period
  2017. * kthreads don't have to do quite so many wakeups.
  2018. */
  2019. static int rcu_nocb_kthread(void *arg)
  2020. {
  2021. int c, cl;
  2022. struct rcu_head *list;
  2023. struct rcu_head *next;
  2024. struct rcu_head **tail;
  2025. struct rcu_data *rdp = arg;
  2026. /* Each pass through this loop invokes one batch of callbacks */
  2027. for (;;) {
  2028. /* Wait for callbacks. */
  2029. if (rdp->nocb_leader == rdp)
  2030. nocb_leader_wait(rdp);
  2031. else
  2032. nocb_follower_wait(rdp);
  2033. /* Pull the ready-to-invoke callbacks onto local list. */
  2034. list = ACCESS_ONCE(rdp->nocb_follower_head);
  2035. BUG_ON(!list);
  2036. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "WokeNonEmpty");
  2037. ACCESS_ONCE(rdp->nocb_follower_head) = NULL;
  2038. tail = xchg(&rdp->nocb_follower_tail, &rdp->nocb_follower_head);
  2039. /* Each pass through the following loop invokes a callback. */
  2040. trace_rcu_batch_start(rdp->rsp->name,
  2041. atomic_long_read(&rdp->nocb_q_count_lazy),
  2042. atomic_long_read(&rdp->nocb_q_count), -1);
  2043. c = cl = 0;
  2044. while (list) {
  2045. next = list->next;
  2046. /* Wait for enqueuing to complete, if needed. */
  2047. while (next == NULL && &list->next != tail) {
  2048. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2049. TPS("WaitQueue"));
  2050. schedule_timeout_interruptible(1);
  2051. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2052. TPS("WokeQueue"));
  2053. next = list->next;
  2054. }
  2055. debug_rcu_head_unqueue(list);
  2056. local_bh_disable();
  2057. if (__rcu_reclaim(rdp->rsp->name, list))
  2058. cl++;
  2059. c++;
  2060. local_bh_enable();
  2061. list = next;
  2062. }
  2063. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  2064. smp_mb__before_atomic(); /* _add after CB invocation. */
  2065. atomic_long_add(-c, &rdp->nocb_q_count);
  2066. atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
  2067. rdp->n_nocbs_invoked += c;
  2068. }
  2069. return 0;
  2070. }
  2071. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  2072. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2073. {
  2074. return ACCESS_ONCE(rdp->nocb_defer_wakeup);
  2075. }
  2076. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  2077. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2078. {
  2079. int ndw;
  2080. if (!rcu_nocb_need_deferred_wakeup(rdp))
  2081. return;
  2082. ndw = ACCESS_ONCE(rdp->nocb_defer_wakeup);
  2083. ACCESS_ONCE(rdp->nocb_defer_wakeup) = RCU_NOGP_WAKE_NOT;
  2084. wake_nocb_leader(rdp, ndw == RCU_NOGP_WAKE_FORCE);
  2085. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
  2086. }
  2087. void __init rcu_init_nohz(void)
  2088. {
  2089. int cpu;
  2090. bool need_rcu_nocb_mask = true;
  2091. struct rcu_state *rsp;
  2092. #ifdef CONFIG_RCU_NOCB_CPU_NONE
  2093. need_rcu_nocb_mask = false;
  2094. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
  2095. #if defined(CONFIG_NO_HZ_FULL)
  2096. if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
  2097. need_rcu_nocb_mask = true;
  2098. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2099. if (!have_rcu_nocb_mask && need_rcu_nocb_mask) {
  2100. if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
  2101. pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
  2102. return;
  2103. }
  2104. have_rcu_nocb_mask = true;
  2105. }
  2106. if (!have_rcu_nocb_mask)
  2107. return;
  2108. #ifdef CONFIG_RCU_NOCB_CPU_ZERO
  2109. pr_info("\tOffload RCU callbacks from CPU 0\n");
  2110. cpumask_set_cpu(0, rcu_nocb_mask);
  2111. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
  2112. #ifdef CONFIG_RCU_NOCB_CPU_ALL
  2113. pr_info("\tOffload RCU callbacks from all CPUs\n");
  2114. cpumask_copy(rcu_nocb_mask, cpu_possible_mask);
  2115. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
  2116. #if defined(CONFIG_NO_HZ_FULL)
  2117. if (tick_nohz_full_running)
  2118. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
  2119. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2120. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  2121. pr_info("\tNote: kernel parameter 'rcu_nocbs=' contains nonexistent CPUs.\n");
  2122. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  2123. rcu_nocb_mask);
  2124. }
  2125. cpulist_scnprintf(nocb_buf, sizeof(nocb_buf), rcu_nocb_mask);
  2126. pr_info("\tOffload RCU callbacks from CPUs: %s.\n", nocb_buf);
  2127. if (rcu_nocb_poll)
  2128. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  2129. for_each_rcu_flavor(rsp) {
  2130. for_each_cpu(cpu, rcu_nocb_mask) {
  2131. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2132. /*
  2133. * If there are early callbacks, they will need
  2134. * to be moved to the nocb lists.
  2135. */
  2136. WARN_ON_ONCE(rdp->nxttail[RCU_NEXT_TAIL] !=
  2137. &rdp->nxtlist &&
  2138. rdp->nxttail[RCU_NEXT_TAIL] != NULL);
  2139. init_nocb_callback_list(rdp);
  2140. }
  2141. rcu_organize_nocb_kthreads(rsp);
  2142. }
  2143. }
  2144. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2145. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2146. {
  2147. rdp->nocb_tail = &rdp->nocb_head;
  2148. init_waitqueue_head(&rdp->nocb_wq);
  2149. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2150. }
  2151. /*
  2152. * If the specified CPU is a no-CBs CPU that does not already have its
  2153. * rcuo kthread for the specified RCU flavor, spawn it. If the CPUs are
  2154. * brought online out of order, this can require re-organizing the
  2155. * leader-follower relationships.
  2156. */
  2157. static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
  2158. {
  2159. struct rcu_data *rdp;
  2160. struct rcu_data *rdp_last;
  2161. struct rcu_data *rdp_old_leader;
  2162. struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
  2163. struct task_struct *t;
  2164. /*
  2165. * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
  2166. * then nothing to do.
  2167. */
  2168. if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
  2169. return;
  2170. /* If we didn't spawn the leader first, reorganize! */
  2171. rdp_old_leader = rdp_spawn->nocb_leader;
  2172. if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
  2173. rdp_last = NULL;
  2174. rdp = rdp_old_leader;
  2175. do {
  2176. rdp->nocb_leader = rdp_spawn;
  2177. if (rdp_last && rdp != rdp_spawn)
  2178. rdp_last->nocb_next_follower = rdp;
  2179. if (rdp == rdp_spawn) {
  2180. rdp = rdp->nocb_next_follower;
  2181. } else {
  2182. rdp_last = rdp;
  2183. rdp = rdp->nocb_next_follower;
  2184. rdp_last->nocb_next_follower = NULL;
  2185. }
  2186. } while (rdp);
  2187. rdp_spawn->nocb_next_follower = rdp_old_leader;
  2188. }
  2189. /* Spawn the kthread for this CPU and RCU flavor. */
  2190. t = kthread_run(rcu_nocb_kthread, rdp_spawn,
  2191. "rcuo%c/%d", rsp->abbr, cpu);
  2192. BUG_ON(IS_ERR(t));
  2193. ACCESS_ONCE(rdp_spawn->nocb_kthread) = t;
  2194. }
  2195. /*
  2196. * If the specified CPU is a no-CBs CPU that does not already have its
  2197. * rcuo kthreads, spawn them.
  2198. */
  2199. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2200. {
  2201. struct rcu_state *rsp;
  2202. if (rcu_scheduler_fully_active)
  2203. for_each_rcu_flavor(rsp)
  2204. rcu_spawn_one_nocb_kthread(rsp, cpu);
  2205. }
  2206. /*
  2207. * Once the scheduler is running, spawn rcuo kthreads for all online
  2208. * no-CBs CPUs. This assumes that the early_initcall()s happen before
  2209. * non-boot CPUs come online -- if this changes, we will need to add
  2210. * some mutual exclusion.
  2211. */
  2212. static void __init rcu_spawn_nocb_kthreads(void)
  2213. {
  2214. int cpu;
  2215. for_each_online_cpu(cpu)
  2216. rcu_spawn_all_nocb_kthreads(cpu);
  2217. }
  2218. /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
  2219. static int rcu_nocb_leader_stride = -1;
  2220. module_param(rcu_nocb_leader_stride, int, 0444);
  2221. /*
  2222. * Initialize leader-follower relationships for all no-CBs CPU.
  2223. */
  2224. static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
  2225. {
  2226. int cpu;
  2227. int ls = rcu_nocb_leader_stride;
  2228. int nl = 0; /* Next leader. */
  2229. struct rcu_data *rdp;
  2230. struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
  2231. struct rcu_data *rdp_prev = NULL;
  2232. if (!have_rcu_nocb_mask)
  2233. return;
  2234. if (ls == -1) {
  2235. ls = int_sqrt(nr_cpu_ids);
  2236. rcu_nocb_leader_stride = ls;
  2237. }
  2238. /*
  2239. * Each pass through this loop sets up one rcu_data structure and
  2240. * spawns one rcu_nocb_kthread().
  2241. */
  2242. for_each_cpu(cpu, rcu_nocb_mask) {
  2243. rdp = per_cpu_ptr(rsp->rda, cpu);
  2244. if (rdp->cpu >= nl) {
  2245. /* New leader, set up for followers & next leader. */
  2246. nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
  2247. rdp->nocb_leader = rdp;
  2248. rdp_leader = rdp;
  2249. } else {
  2250. /* Another follower, link to previous leader. */
  2251. rdp->nocb_leader = rdp_leader;
  2252. rdp_prev->nocb_next_follower = rdp;
  2253. }
  2254. rdp_prev = rdp;
  2255. }
  2256. }
  2257. /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
  2258. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2259. {
  2260. if (!rcu_is_nocb_cpu(rdp->cpu))
  2261. return false;
  2262. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  2263. return true;
  2264. }
  2265. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  2266. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  2267. {
  2268. WARN_ON_ONCE(1); /* Should be dead code. */
  2269. return false;
  2270. }
  2271. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  2272. {
  2273. }
  2274. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  2275. {
  2276. }
  2277. static void rcu_init_one_nocb(struct rcu_node *rnp)
  2278. {
  2279. }
  2280. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  2281. bool lazy, unsigned long flags)
  2282. {
  2283. return false;
  2284. }
  2285. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  2286. struct rcu_data *rdp,
  2287. unsigned long flags)
  2288. {
  2289. return false;
  2290. }
  2291. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2292. {
  2293. }
  2294. static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2295. {
  2296. return false;
  2297. }
  2298. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2299. {
  2300. }
  2301. static void rcu_spawn_all_nocb_kthreads(int cpu)
  2302. {
  2303. }
  2304. static void __init rcu_spawn_nocb_kthreads(void)
  2305. {
  2306. }
  2307. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2308. {
  2309. return false;
  2310. }
  2311. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  2312. /*
  2313. * An adaptive-ticks CPU can potentially execute in kernel mode for an
  2314. * arbitrarily long period of time with the scheduling-clock tick turned
  2315. * off. RCU will be paying attention to this CPU because it is in the
  2316. * kernel, but the CPU cannot be guaranteed to be executing the RCU state
  2317. * machine because the scheduling-clock tick has been disabled. Therefore,
  2318. * if an adaptive-ticks CPU is failing to respond to the current grace
  2319. * period and has not be idle from an RCU perspective, kick it.
  2320. */
  2321. static void __maybe_unused rcu_kick_nohz_cpu(int cpu)
  2322. {
  2323. #ifdef CONFIG_NO_HZ_FULL
  2324. if (tick_nohz_full_cpu(cpu))
  2325. smp_send_reschedule(cpu);
  2326. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2327. }
  2328. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  2329. static int full_sysidle_state; /* Current system-idle state. */
  2330. #define RCU_SYSIDLE_NOT 0 /* Some CPU is not idle. */
  2331. #define RCU_SYSIDLE_SHORT 1 /* All CPUs idle for brief period. */
  2332. #define RCU_SYSIDLE_LONG 2 /* All CPUs idle for long enough. */
  2333. #define RCU_SYSIDLE_FULL 3 /* All CPUs idle, ready for sysidle. */
  2334. #define RCU_SYSIDLE_FULL_NOTED 4 /* Actually entered sysidle state. */
  2335. /*
  2336. * Invoked to note exit from irq or task transition to idle. Note that
  2337. * usermode execution does -not- count as idle here! After all, we want
  2338. * to detect full-system idle states, not RCU quiescent states and grace
  2339. * periods. The caller must have disabled interrupts.
  2340. */
  2341. static void rcu_sysidle_enter(int irq)
  2342. {
  2343. unsigned long j;
  2344. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  2345. /* If there are no nohz_full= CPUs, no need to track this. */
  2346. if (!tick_nohz_full_enabled())
  2347. return;
  2348. /* Adjust nesting, check for fully idle. */
  2349. if (irq) {
  2350. rdtp->dynticks_idle_nesting--;
  2351. WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
  2352. if (rdtp->dynticks_idle_nesting != 0)
  2353. return; /* Still not fully idle. */
  2354. } else {
  2355. if ((rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) ==
  2356. DYNTICK_TASK_NEST_VALUE) {
  2357. rdtp->dynticks_idle_nesting = 0;
  2358. } else {
  2359. rdtp->dynticks_idle_nesting -= DYNTICK_TASK_NEST_VALUE;
  2360. WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
  2361. return; /* Still not fully idle. */
  2362. }
  2363. }
  2364. /* Record start of fully idle period. */
  2365. j = jiffies;
  2366. ACCESS_ONCE(rdtp->dynticks_idle_jiffies) = j;
  2367. smp_mb__before_atomic();
  2368. atomic_inc(&rdtp->dynticks_idle);
  2369. smp_mb__after_atomic();
  2370. WARN_ON_ONCE(atomic_read(&rdtp->dynticks_idle) & 0x1);
  2371. }
  2372. /*
  2373. * Unconditionally force exit from full system-idle state. This is
  2374. * invoked when a normal CPU exits idle, but must be called separately
  2375. * for the timekeeping CPU (tick_do_timer_cpu). The reason for this
  2376. * is that the timekeeping CPU is permitted to take scheduling-clock
  2377. * interrupts while the system is in system-idle state, and of course
  2378. * rcu_sysidle_exit() has no way of distinguishing a scheduling-clock
  2379. * interrupt from any other type of interrupt.
  2380. */
  2381. void rcu_sysidle_force_exit(void)
  2382. {
  2383. int oldstate = ACCESS_ONCE(full_sysidle_state);
  2384. int newoldstate;
  2385. /*
  2386. * Each pass through the following loop attempts to exit full
  2387. * system-idle state. If contention proves to be a problem,
  2388. * a trylock-based contention tree could be used here.
  2389. */
  2390. while (oldstate > RCU_SYSIDLE_SHORT) {
  2391. newoldstate = cmpxchg(&full_sysidle_state,
  2392. oldstate, RCU_SYSIDLE_NOT);
  2393. if (oldstate == newoldstate &&
  2394. oldstate == RCU_SYSIDLE_FULL_NOTED) {
  2395. rcu_kick_nohz_cpu(tick_do_timer_cpu);
  2396. return; /* We cleared it, done! */
  2397. }
  2398. oldstate = newoldstate;
  2399. }
  2400. smp_mb(); /* Order initial oldstate fetch vs. later non-idle work. */
  2401. }
  2402. /*
  2403. * Invoked to note entry to irq or task transition from idle. Note that
  2404. * usermode execution does -not- count as idle here! The caller must
  2405. * have disabled interrupts.
  2406. */
  2407. static void rcu_sysidle_exit(int irq)
  2408. {
  2409. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  2410. /* If there are no nohz_full= CPUs, no need to track this. */
  2411. if (!tick_nohz_full_enabled())
  2412. return;
  2413. /* Adjust nesting, check for already non-idle. */
  2414. if (irq) {
  2415. rdtp->dynticks_idle_nesting++;
  2416. WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
  2417. if (rdtp->dynticks_idle_nesting != 1)
  2418. return; /* Already non-idle. */
  2419. } else {
  2420. /*
  2421. * Allow for irq misnesting. Yes, it really is possible
  2422. * to enter an irq handler then never leave it, and maybe
  2423. * also vice versa. Handle both possibilities.
  2424. */
  2425. if (rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) {
  2426. rdtp->dynticks_idle_nesting += DYNTICK_TASK_NEST_VALUE;
  2427. WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
  2428. return; /* Already non-idle. */
  2429. } else {
  2430. rdtp->dynticks_idle_nesting = DYNTICK_TASK_EXIT_IDLE;
  2431. }
  2432. }
  2433. /* Record end of idle period. */
  2434. smp_mb__before_atomic();
  2435. atomic_inc(&rdtp->dynticks_idle);
  2436. smp_mb__after_atomic();
  2437. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks_idle) & 0x1));
  2438. /*
  2439. * If we are the timekeeping CPU, we are permitted to be non-idle
  2440. * during a system-idle state. This must be the case, because
  2441. * the timekeeping CPU has to take scheduling-clock interrupts
  2442. * during the time that the system is transitioning to full
  2443. * system-idle state. This means that the timekeeping CPU must
  2444. * invoke rcu_sysidle_force_exit() directly if it does anything
  2445. * more than take a scheduling-clock interrupt.
  2446. */
  2447. if (smp_processor_id() == tick_do_timer_cpu)
  2448. return;
  2449. /* Update system-idle state: We are clearly no longer fully idle! */
  2450. rcu_sysidle_force_exit();
  2451. }
  2452. /*
  2453. * Check to see if the current CPU is idle. Note that usermode execution
  2454. * does not count as idle. The caller must have disabled interrupts.
  2455. */
  2456. static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
  2457. unsigned long *maxj)
  2458. {
  2459. int cur;
  2460. unsigned long j;
  2461. struct rcu_dynticks *rdtp = rdp->dynticks;
  2462. /* If there are no nohz_full= CPUs, don't check system-wide idleness. */
  2463. if (!tick_nohz_full_enabled())
  2464. return;
  2465. /*
  2466. * If some other CPU has already reported non-idle, if this is
  2467. * not the flavor of RCU that tracks sysidle state, or if this
  2468. * is an offline or the timekeeping CPU, nothing to do.
  2469. */
  2470. if (!*isidle || rdp->rsp != rcu_state_p ||
  2471. cpu_is_offline(rdp->cpu) || rdp->cpu == tick_do_timer_cpu)
  2472. return;
  2473. if (rcu_gp_in_progress(rdp->rsp))
  2474. WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu);
  2475. /* Pick up current idle and NMI-nesting counter and check. */
  2476. cur = atomic_read(&rdtp->dynticks_idle);
  2477. if (cur & 0x1) {
  2478. *isidle = false; /* We are not idle! */
  2479. return;
  2480. }
  2481. smp_mb(); /* Read counters before timestamps. */
  2482. /* Pick up timestamps. */
  2483. j = ACCESS_ONCE(rdtp->dynticks_idle_jiffies);
  2484. /* If this CPU entered idle more recently, update maxj timestamp. */
  2485. if (ULONG_CMP_LT(*maxj, j))
  2486. *maxj = j;
  2487. }
  2488. /*
  2489. * Is this the flavor of RCU that is handling full-system idle?
  2490. */
  2491. static bool is_sysidle_rcu_state(struct rcu_state *rsp)
  2492. {
  2493. return rsp == rcu_state_p;
  2494. }
  2495. /*
  2496. * Return a delay in jiffies based on the number of CPUs, rcu_node
  2497. * leaf fanout, and jiffies tick rate. The idea is to allow larger
  2498. * systems more time to transition to full-idle state in order to
  2499. * avoid the cache thrashing that otherwise occur on the state variable.
  2500. * Really small systems (less than a couple of tens of CPUs) should
  2501. * instead use a single global atomically incremented counter, and later
  2502. * versions of this will automatically reconfigure themselves accordingly.
  2503. */
  2504. static unsigned long rcu_sysidle_delay(void)
  2505. {
  2506. if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
  2507. return 0;
  2508. return DIV_ROUND_UP(nr_cpu_ids * HZ, rcu_fanout_leaf * 1000);
  2509. }
  2510. /*
  2511. * Advance the full-system-idle state. This is invoked when all of
  2512. * the non-timekeeping CPUs are idle.
  2513. */
  2514. static void rcu_sysidle(unsigned long j)
  2515. {
  2516. /* Check the current state. */
  2517. switch (ACCESS_ONCE(full_sysidle_state)) {
  2518. case RCU_SYSIDLE_NOT:
  2519. /* First time all are idle, so note a short idle period. */
  2520. ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_SHORT;
  2521. break;
  2522. case RCU_SYSIDLE_SHORT:
  2523. /*
  2524. * Idle for a bit, time to advance to next state?
  2525. * cmpxchg failure means race with non-idle, let them win.
  2526. */
  2527. if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
  2528. (void)cmpxchg(&full_sysidle_state,
  2529. RCU_SYSIDLE_SHORT, RCU_SYSIDLE_LONG);
  2530. break;
  2531. case RCU_SYSIDLE_LONG:
  2532. /*
  2533. * Do an additional check pass before advancing to full.
  2534. * cmpxchg failure means race with non-idle, let them win.
  2535. */
  2536. if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
  2537. (void)cmpxchg(&full_sysidle_state,
  2538. RCU_SYSIDLE_LONG, RCU_SYSIDLE_FULL);
  2539. break;
  2540. default:
  2541. break;
  2542. }
  2543. }
  2544. /*
  2545. * Found a non-idle non-timekeeping CPU, so kick the system-idle state
  2546. * back to the beginning.
  2547. */
  2548. static void rcu_sysidle_cancel(void)
  2549. {
  2550. smp_mb();
  2551. if (full_sysidle_state > RCU_SYSIDLE_SHORT)
  2552. ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_NOT;
  2553. }
  2554. /*
  2555. * Update the sysidle state based on the results of a force-quiescent-state
  2556. * scan of the CPUs' dyntick-idle state.
  2557. */
  2558. static void rcu_sysidle_report(struct rcu_state *rsp, int isidle,
  2559. unsigned long maxj, bool gpkt)
  2560. {
  2561. if (rsp != rcu_state_p)
  2562. return; /* Wrong flavor, ignore. */
  2563. if (gpkt && nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
  2564. return; /* Running state machine from timekeeping CPU. */
  2565. if (isidle)
  2566. rcu_sysidle(maxj); /* More idle! */
  2567. else
  2568. rcu_sysidle_cancel(); /* Idle is over. */
  2569. }
  2570. /*
  2571. * Wrapper for rcu_sysidle_report() when called from the grace-period
  2572. * kthread's context.
  2573. */
  2574. static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
  2575. unsigned long maxj)
  2576. {
  2577. /* If there are no nohz_full= CPUs, no need to track this. */
  2578. if (!tick_nohz_full_enabled())
  2579. return;
  2580. rcu_sysidle_report(rsp, isidle, maxj, true);
  2581. }
  2582. /* Callback and function for forcing an RCU grace period. */
  2583. struct rcu_sysidle_head {
  2584. struct rcu_head rh;
  2585. int inuse;
  2586. };
  2587. static void rcu_sysidle_cb(struct rcu_head *rhp)
  2588. {
  2589. struct rcu_sysidle_head *rshp;
  2590. /*
  2591. * The following memory barrier is needed to replace the
  2592. * memory barriers that would normally be in the memory
  2593. * allocator.
  2594. */
  2595. smp_mb(); /* grace period precedes setting inuse. */
  2596. rshp = container_of(rhp, struct rcu_sysidle_head, rh);
  2597. ACCESS_ONCE(rshp->inuse) = 0;
  2598. }
  2599. /*
  2600. * Check to see if the system is fully idle, other than the timekeeping CPU.
  2601. * The caller must have disabled interrupts. This is not intended to be
  2602. * called unless tick_nohz_full_enabled().
  2603. */
  2604. bool rcu_sys_is_idle(void)
  2605. {
  2606. static struct rcu_sysidle_head rsh;
  2607. int rss = ACCESS_ONCE(full_sysidle_state);
  2608. if (WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu))
  2609. return false;
  2610. /* Handle small-system case by doing a full scan of CPUs. */
  2611. if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL) {
  2612. int oldrss = rss - 1;
  2613. /*
  2614. * One pass to advance to each state up to _FULL.
  2615. * Give up if any pass fails to advance the state.
  2616. */
  2617. while (rss < RCU_SYSIDLE_FULL && oldrss < rss) {
  2618. int cpu;
  2619. bool isidle = true;
  2620. unsigned long maxj = jiffies - ULONG_MAX / 4;
  2621. struct rcu_data *rdp;
  2622. /* Scan all the CPUs looking for nonidle CPUs. */
  2623. for_each_possible_cpu(cpu) {
  2624. rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  2625. rcu_sysidle_check_cpu(rdp, &isidle, &maxj);
  2626. if (!isidle)
  2627. break;
  2628. }
  2629. rcu_sysidle_report(rcu_state_p, isidle, maxj, false);
  2630. oldrss = rss;
  2631. rss = ACCESS_ONCE(full_sysidle_state);
  2632. }
  2633. }
  2634. /* If this is the first observation of an idle period, record it. */
  2635. if (rss == RCU_SYSIDLE_FULL) {
  2636. rss = cmpxchg(&full_sysidle_state,
  2637. RCU_SYSIDLE_FULL, RCU_SYSIDLE_FULL_NOTED);
  2638. return rss == RCU_SYSIDLE_FULL;
  2639. }
  2640. smp_mb(); /* ensure rss load happens before later caller actions. */
  2641. /* If already fully idle, tell the caller (in case of races). */
  2642. if (rss == RCU_SYSIDLE_FULL_NOTED)
  2643. return true;
  2644. /*
  2645. * If we aren't there yet, and a grace period is not in flight,
  2646. * initiate a grace period. Either way, tell the caller that
  2647. * we are not there yet. We use an xchg() rather than an assignment
  2648. * to make up for the memory barriers that would otherwise be
  2649. * provided by the memory allocator.
  2650. */
  2651. if (nr_cpu_ids > CONFIG_NO_HZ_FULL_SYSIDLE_SMALL &&
  2652. !rcu_gp_in_progress(rcu_state_p) &&
  2653. !rsh.inuse && xchg(&rsh.inuse, 1) == 0)
  2654. call_rcu(&rsh.rh, rcu_sysidle_cb);
  2655. return false;
  2656. }
  2657. /*
  2658. * Initialize dynticks sysidle state for CPUs coming online.
  2659. */
  2660. static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
  2661. {
  2662. rdtp->dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE;
  2663. }
  2664. #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2665. static void rcu_sysidle_enter(int irq)
  2666. {
  2667. }
  2668. static void rcu_sysidle_exit(int irq)
  2669. {
  2670. }
  2671. static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
  2672. unsigned long *maxj)
  2673. {
  2674. }
  2675. static bool is_sysidle_rcu_state(struct rcu_state *rsp)
  2676. {
  2677. return false;
  2678. }
  2679. static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
  2680. unsigned long maxj)
  2681. {
  2682. }
  2683. static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
  2684. {
  2685. }
  2686. #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2687. /*
  2688. * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
  2689. * grace-period kthread will do force_quiescent_state() processing?
  2690. * The idea is to avoid waking up RCU core processing on such a
  2691. * CPU unless the grace period has extended for too long.
  2692. *
  2693. * This code relies on the fact that all NO_HZ_FULL CPUs are also
  2694. * CONFIG_RCU_NOCB_CPU CPUs.
  2695. */
  2696. static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
  2697. {
  2698. #ifdef CONFIG_NO_HZ_FULL
  2699. if (tick_nohz_full_cpu(smp_processor_id()) &&
  2700. (!rcu_gp_in_progress(rsp) ||
  2701. ULONG_CMP_LT(jiffies, ACCESS_ONCE(rsp->gp_start) + HZ)))
  2702. return 1;
  2703. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2704. return 0;
  2705. }
  2706. /*
  2707. * Bind the grace-period kthread for the sysidle flavor of RCU to the
  2708. * timekeeping CPU.
  2709. */
  2710. static void rcu_bind_gp_kthread(void)
  2711. {
  2712. int __maybe_unused cpu;
  2713. if (!tick_nohz_full_enabled())
  2714. return;
  2715. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  2716. cpu = tick_do_timer_cpu;
  2717. if (cpu >= 0 && cpu < nr_cpu_ids && raw_smp_processor_id() != cpu)
  2718. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  2719. #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2720. if (!is_housekeeping_cpu(raw_smp_processor_id()))
  2721. housekeeping_affine(current);
  2722. #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2723. }
  2724. /* Record the current task on dyntick-idle entry. */
  2725. static void rcu_dynticks_task_enter(void)
  2726. {
  2727. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2728. ACCESS_ONCE(current->rcu_tasks_idle_cpu) = smp_processor_id();
  2729. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2730. }
  2731. /* Record no current task on dyntick-idle exit. */
  2732. static void rcu_dynticks_task_exit(void)
  2733. {
  2734. #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
  2735. ACCESS_ONCE(current->rcu_tasks_idle_cpu) = -1;
  2736. #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
  2737. }