tree_plugin.h 90 KB

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