tree_plugin.h 90 KB

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