tree_plugin.h 90 KB

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