tree_plugin.h 87 KB

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