tree_plugin.h 90 KB

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