tree_plugin.h 82 KB

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