tree.c 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  4. *
  5. * Copyright IBM Corporation, 2008
  6. *
  7. * Authors: Dipankar Sarma <[email protected]>
  8. * Manfred Spraul <[email protected]>
  9. * Paul E. McKenney <[email protected]>
  10. *
  11. * Based on the original work by Paul McKenney <[email protected]>
  12. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  13. *
  14. * For detailed explanation of Read-Copy Update mechanism see -
  15. * Documentation/RCU
  16. */
  17. #define pr_fmt(fmt) "rcu: " fmt
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/smp.h>
  23. #include <linux/rcupdate_wait.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/sched.h>
  26. #include <linux/sched/debug.h>
  27. #include <linux/nmi.h>
  28. #include <linux/atomic.h>
  29. #include <linux/bitops.h>
  30. #include <linux/export.h>
  31. #include <linux/completion.h>
  32. #include <linux/kmemleak.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/panic.h>
  35. #include <linux/panic_notifier.h>
  36. #include <linux/percpu.h>
  37. #include <linux/notifier.h>
  38. #include <linux/cpu.h>
  39. #include <linux/mutex.h>
  40. #include <linux/time.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/wait.h>
  43. #include <linux/kthread.h>
  44. #include <uapi/linux/sched/types.h>
  45. #include <linux/prefetch.h>
  46. #include <linux/delay.h>
  47. #include <linux/random.h>
  48. #include <linux/trace_events.h>
  49. #include <linux/suspend.h>
  50. #include <linux/ftrace.h>
  51. #include <linux/tick.h>
  52. #include <linux/sysrq.h>
  53. #include <linux/kprobes.h>
  54. #include <linux/gfp.h>
  55. #include <linux/oom.h>
  56. #include <linux/smpboot.h>
  57. #include <linux/jiffies.h>
  58. #include <linux/slab.h>
  59. #include <linux/sched/isolation.h>
  60. #include <linux/sched/clock.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/mm.h>
  63. #include <linux/kasan.h>
  64. #include <linux/context_tracking.h>
  65. #include "../time/tick-internal.h"
  66. #include "tree.h"
  67. #include "rcu.h"
  68. #ifdef MODULE_PARAM_PREFIX
  69. #undef MODULE_PARAM_PREFIX
  70. #endif
  71. #define MODULE_PARAM_PREFIX "rcutree."
  72. /* Data structures. */
  73. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
  74. .gpwrap = true,
  75. #ifdef CONFIG_RCU_NOCB_CPU
  76. .cblist.flags = SEGCBLIST_RCU_CORE,
  77. #endif
  78. };
  79. static struct rcu_state rcu_state = {
  80. .level = { &rcu_state.node[0] },
  81. .gp_state = RCU_GP_IDLE,
  82. .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
  83. .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
  84. .barrier_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.barrier_lock),
  85. .name = RCU_NAME,
  86. .abbr = RCU_ABBR,
  87. .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
  88. .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
  89. .ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
  90. };
  91. /* Dump rcu_node combining tree at boot to verify correct setup. */
  92. static bool dump_tree;
  93. module_param(dump_tree, bool, 0444);
  94. /* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */
  95. static bool use_softirq = !IS_ENABLED(CONFIG_PREEMPT_RT);
  96. #ifndef CONFIG_PREEMPT_RT
  97. module_param(use_softirq, bool, 0444);
  98. #endif
  99. /* Control rcu_node-tree auto-balancing at boot time. */
  100. static bool rcu_fanout_exact;
  101. module_param(rcu_fanout_exact, bool, 0444);
  102. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  103. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  104. module_param(rcu_fanout_leaf, int, 0444);
  105. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  106. /* Number of rcu_nodes at specified level. */
  107. int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  108. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  109. /*
  110. * The rcu_scheduler_active variable is initialized to the value
  111. * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
  112. * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
  113. * RCU can assume that there is but one task, allowing RCU to (for example)
  114. * optimize synchronize_rcu() to a simple barrier(). When this variable
  115. * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
  116. * to detect real grace periods. This variable is also used to suppress
  117. * boot-time false positives from lockdep-RCU error checking. Finally, it
  118. * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
  119. * is fully initialized, including all of its kthreads having been spawned.
  120. */
  121. int rcu_scheduler_active __read_mostly;
  122. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  123. /*
  124. * The rcu_scheduler_fully_active variable transitions from zero to one
  125. * during the early_initcall() processing, which is after the scheduler
  126. * is capable of creating new tasks. So RCU processing (for example,
  127. * creating tasks for RCU priority boosting) must be delayed until after
  128. * rcu_scheduler_fully_active transitions from zero to one. We also
  129. * currently delay invocation of any RCU callbacks until after this point.
  130. *
  131. * It might later prove better for people registering RCU callbacks during
  132. * early boot to take responsibility for these callbacks, but one step at
  133. * a time.
  134. */
  135. static int rcu_scheduler_fully_active __read_mostly;
  136. static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
  137. unsigned long gps, unsigned long flags);
  138. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  139. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  140. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  141. static void invoke_rcu_core(void);
  142. static void rcu_report_exp_rdp(struct rcu_data *rdp);
  143. static void sync_sched_exp_online_cleanup(int cpu);
  144. static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp);
  145. static bool rcu_rdp_is_offloaded(struct rcu_data *rdp);
  146. /*
  147. * rcuc/rcub/rcuop kthread realtime priority. The "rcuop"
  148. * real-time priority(enabling/disabling) is controlled by
  149. * the extra CONFIG_RCU_NOCB_CPU_CB_BOOST configuration.
  150. */
  151. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  152. module_param(kthread_prio, int, 0444);
  153. /* Delay in jiffies for grace-period initialization delays, debug only. */
  154. static int gp_preinit_delay;
  155. module_param(gp_preinit_delay, int, 0444);
  156. static int gp_init_delay;
  157. module_param(gp_init_delay, int, 0444);
  158. static int gp_cleanup_delay;
  159. module_param(gp_cleanup_delay, int, 0444);
  160. // Add delay to rcu_read_unlock() for strict grace periods.
  161. static int rcu_unlock_delay;
  162. #ifdef CONFIG_RCU_STRICT_GRACE_PERIOD
  163. module_param(rcu_unlock_delay, int, 0444);
  164. #endif
  165. /*
  166. * This rcu parameter is runtime-read-only. It reflects
  167. * a minimum allowed number of objects which can be cached
  168. * per-CPU. Object size is equal to one page. This value
  169. * can be changed at boot time.
  170. */
  171. static int rcu_min_cached_objs = 5;
  172. module_param(rcu_min_cached_objs, int, 0444);
  173. // A page shrinker can ask for pages to be freed to make them
  174. // available for other parts of the system. This usually happens
  175. // under low memory conditions, and in that case we should also
  176. // defer page-cache filling for a short time period.
  177. //
  178. // The default value is 5 seconds, which is long enough to reduce
  179. // interference with the shrinker while it asks other systems to
  180. // drain their caches.
  181. static int rcu_delay_page_cache_fill_msec = 5000;
  182. module_param(rcu_delay_page_cache_fill_msec, int, 0444);
  183. /* Retrieve RCU kthreads priority for rcutorture */
  184. int rcu_get_gp_kthreads_prio(void)
  185. {
  186. return kthread_prio;
  187. }
  188. EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
  189. /*
  190. * Number of grace periods between delays, normalized by the duration of
  191. * the delay. The longer the delay, the more the grace periods between
  192. * each delay. The reason for this normalization is that it means that,
  193. * for non-zero delays, the overall slowdown of grace periods is constant
  194. * regardless of the duration of the delay. This arrangement balances
  195. * the need for long delays to increase some race probabilities with the
  196. * need for fast grace periods to increase other race probabilities.
  197. */
  198. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays for debugging. */
  199. /*
  200. * Compute the mask of online CPUs for the specified rcu_node structure.
  201. * This will not be stable unless the rcu_node structure's ->lock is
  202. * held, but the bit corresponding to the current CPU will be stable
  203. * in most contexts.
  204. */
  205. static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  206. {
  207. return READ_ONCE(rnp->qsmaskinitnext);
  208. }
  209. /*
  210. * Is the CPU corresponding to the specified rcu_data structure online
  211. * from RCU's perspective? This perspective is given by that structure's
  212. * ->qsmaskinitnext field rather than by the global cpu_online_mask.
  213. */
  214. static bool rcu_rdp_cpu_online(struct rcu_data *rdp)
  215. {
  216. return !!(rdp->grpmask & rcu_rnp_online_cpus(rdp->mynode));
  217. }
  218. /*
  219. * Return true if an RCU grace period is in progress. The READ_ONCE()s
  220. * permit this function to be invoked without holding the root rcu_node
  221. * structure's ->lock, but of course results can be subject to change.
  222. */
  223. static int rcu_gp_in_progress(void)
  224. {
  225. return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
  226. }
  227. /*
  228. * Return the number of callbacks queued on the specified CPU.
  229. * Handles both the nocbs and normal cases.
  230. */
  231. static long rcu_get_n_cbs_cpu(int cpu)
  232. {
  233. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  234. if (rcu_segcblist_is_enabled(&rdp->cblist))
  235. return rcu_segcblist_n_cbs(&rdp->cblist);
  236. return 0;
  237. }
  238. void rcu_softirq_qs(void)
  239. {
  240. rcu_qs();
  241. rcu_preempt_deferred_qs(current);
  242. rcu_tasks_qs(current, false);
  243. }
  244. /*
  245. * Reset the current CPU's ->dynticks counter to indicate that the
  246. * newly onlined CPU is no longer in an extended quiescent state.
  247. * This will either leave the counter unchanged, or increment it
  248. * to the next non-quiescent value.
  249. *
  250. * The non-atomic test/increment sequence works because the upper bits
  251. * of the ->dynticks counter are manipulated only by the corresponding CPU,
  252. * or when the corresponding CPU is offline.
  253. */
  254. static void rcu_dynticks_eqs_online(void)
  255. {
  256. if (ct_dynticks() & RCU_DYNTICKS_IDX)
  257. return;
  258. ct_state_inc(RCU_DYNTICKS_IDX);
  259. }
  260. /*
  261. * Snapshot the ->dynticks counter with full ordering so as to allow
  262. * stable comparison of this counter with past and future snapshots.
  263. */
  264. static int rcu_dynticks_snap(int cpu)
  265. {
  266. smp_mb(); // Fundamental RCU ordering guarantee.
  267. return ct_dynticks_cpu_acquire(cpu);
  268. }
  269. /*
  270. * Return true if the snapshot returned from rcu_dynticks_snap()
  271. * indicates that RCU is in an extended quiescent state.
  272. */
  273. static bool rcu_dynticks_in_eqs(int snap)
  274. {
  275. return !(snap & RCU_DYNTICKS_IDX);
  276. }
  277. /* Return true if the specified CPU is currently idle from an RCU viewpoint. */
  278. bool rcu_is_idle_cpu(int cpu)
  279. {
  280. return rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
  281. }
  282. /*
  283. * Return true if the CPU corresponding to the specified rcu_data
  284. * structure has spent some time in an extended quiescent state since
  285. * rcu_dynticks_snap() returned the specified snapshot.
  286. */
  287. static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
  288. {
  289. return snap != rcu_dynticks_snap(rdp->cpu);
  290. }
  291. /*
  292. * Return true if the referenced integer is zero while the specified
  293. * CPU remains within a single extended quiescent state.
  294. */
  295. bool rcu_dynticks_zero_in_eqs(int cpu, int *vp)
  296. {
  297. int snap;
  298. // If not quiescent, force back to earlier extended quiescent state.
  299. snap = ct_dynticks_cpu(cpu) & ~RCU_DYNTICKS_IDX;
  300. smp_rmb(); // Order ->dynticks and *vp reads.
  301. if (READ_ONCE(*vp))
  302. return false; // Non-zero, so report failure;
  303. smp_rmb(); // Order *vp read and ->dynticks re-read.
  304. // If still in the same extended quiescent state, we are good!
  305. return snap == ct_dynticks_cpu(cpu);
  306. }
  307. /*
  308. * Let the RCU core know that this CPU has gone through the scheduler,
  309. * which is a quiescent state. This is called when the need for a
  310. * quiescent state is urgent, so we burn an atomic operation and full
  311. * memory barriers to let the RCU core know about it, regardless of what
  312. * this CPU might (or might not) do in the near future.
  313. *
  314. * We inform the RCU core by emulating a zero-duration dyntick-idle period.
  315. *
  316. * The caller must have disabled interrupts and must not be idle.
  317. */
  318. notrace void rcu_momentary_dyntick_idle(void)
  319. {
  320. int seq;
  321. raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
  322. seq = ct_state_inc(2 * RCU_DYNTICKS_IDX);
  323. /* It is illegal to call this from idle state. */
  324. WARN_ON_ONCE(!(seq & RCU_DYNTICKS_IDX));
  325. rcu_preempt_deferred_qs(current);
  326. }
  327. EXPORT_SYMBOL_GPL(rcu_momentary_dyntick_idle);
  328. /**
  329. * rcu_is_cpu_rrupt_from_idle - see if 'interrupted' from idle
  330. *
  331. * If the current CPU is idle and running at a first-level (not nested)
  332. * interrupt, or directly, from idle, return true.
  333. *
  334. * The caller must have at least disabled IRQs.
  335. */
  336. static int rcu_is_cpu_rrupt_from_idle(void)
  337. {
  338. long nesting;
  339. /*
  340. * Usually called from the tick; but also used from smp_function_call()
  341. * for expedited grace periods. This latter can result in running from
  342. * the idle task, instead of an actual IPI.
  343. */
  344. lockdep_assert_irqs_disabled();
  345. /* Check for counter underflows */
  346. RCU_LOCKDEP_WARN(ct_dynticks_nesting() < 0,
  347. "RCU dynticks_nesting counter underflow!");
  348. RCU_LOCKDEP_WARN(ct_dynticks_nmi_nesting() <= 0,
  349. "RCU dynticks_nmi_nesting counter underflow/zero!");
  350. /* Are we at first interrupt nesting level? */
  351. nesting = ct_dynticks_nmi_nesting();
  352. if (nesting > 1)
  353. return false;
  354. /*
  355. * If we're not in an interrupt, we must be in the idle task!
  356. */
  357. WARN_ON_ONCE(!nesting && !is_idle_task(current));
  358. /* Does CPU appear to be idle from an RCU standpoint? */
  359. return ct_dynticks_nesting() == 0;
  360. }
  361. #define DEFAULT_RCU_BLIMIT (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 1000 : 10)
  362. // Maximum callbacks per rcu_do_batch ...
  363. #define DEFAULT_MAX_RCU_BLIMIT 10000 // ... even during callback flood.
  364. static long blimit = DEFAULT_RCU_BLIMIT;
  365. #define DEFAULT_RCU_QHIMARK 10000 // If this many pending, ignore blimit.
  366. static long qhimark = DEFAULT_RCU_QHIMARK;
  367. #define DEFAULT_RCU_QLOMARK 100 // Once only this many pending, use blimit.
  368. static long qlowmark = DEFAULT_RCU_QLOMARK;
  369. #define DEFAULT_RCU_QOVLD_MULT 2
  370. #define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK)
  371. static long qovld = DEFAULT_RCU_QOVLD; // If this many pending, hammer QS.
  372. static long qovld_calc = -1; // No pre-initialization lock acquisitions!
  373. module_param(blimit, long, 0444);
  374. module_param(qhimark, long, 0444);
  375. module_param(qlowmark, long, 0444);
  376. module_param(qovld, long, 0444);
  377. static ulong jiffies_till_first_fqs = IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 0 : ULONG_MAX;
  378. static ulong jiffies_till_next_fqs = ULONG_MAX;
  379. static bool rcu_kick_kthreads;
  380. static int rcu_divisor = 7;
  381. module_param(rcu_divisor, int, 0644);
  382. /* Force an exit from rcu_do_batch() after 3 milliseconds. */
  383. static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
  384. module_param(rcu_resched_ns, long, 0644);
  385. /*
  386. * How long the grace period must be before we start recruiting
  387. * quiescent-state help from rcu_note_context_switch().
  388. */
  389. static ulong jiffies_till_sched_qs = ULONG_MAX;
  390. module_param(jiffies_till_sched_qs, ulong, 0444);
  391. static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */
  392. module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
  393. /*
  394. * Make sure that we give the grace-period kthread time to detect any
  395. * idle CPUs before taking active measures to force quiescent states.
  396. * However, don't go below 100 milliseconds, adjusted upwards for really
  397. * large systems.
  398. */
  399. static void adjust_jiffies_till_sched_qs(void)
  400. {
  401. unsigned long j;
  402. /* If jiffies_till_sched_qs was specified, respect the request. */
  403. if (jiffies_till_sched_qs != ULONG_MAX) {
  404. WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
  405. return;
  406. }
  407. /* Otherwise, set to third fqs scan, but bound below on large system. */
  408. j = READ_ONCE(jiffies_till_first_fqs) +
  409. 2 * READ_ONCE(jiffies_till_next_fqs);
  410. if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
  411. j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  412. pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
  413. WRITE_ONCE(jiffies_to_sched_qs, j);
  414. }
  415. static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
  416. {
  417. ulong j;
  418. int ret = kstrtoul(val, 0, &j);
  419. if (!ret) {
  420. WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
  421. adjust_jiffies_till_sched_qs();
  422. }
  423. return ret;
  424. }
  425. static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
  426. {
  427. ulong j;
  428. int ret = kstrtoul(val, 0, &j);
  429. if (!ret) {
  430. WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
  431. adjust_jiffies_till_sched_qs();
  432. }
  433. return ret;
  434. }
  435. static const struct kernel_param_ops first_fqs_jiffies_ops = {
  436. .set = param_set_first_fqs_jiffies,
  437. .get = param_get_ulong,
  438. };
  439. static const struct kernel_param_ops next_fqs_jiffies_ops = {
  440. .set = param_set_next_fqs_jiffies,
  441. .get = param_get_ulong,
  442. };
  443. module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
  444. module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
  445. module_param(rcu_kick_kthreads, bool, 0644);
  446. static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
  447. static int rcu_pending(int user);
  448. /*
  449. * Return the number of RCU GPs completed thus far for debug & stats.
  450. */
  451. unsigned long rcu_get_gp_seq(void)
  452. {
  453. return READ_ONCE(rcu_state.gp_seq);
  454. }
  455. EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
  456. /*
  457. * Return the number of RCU expedited batches completed thus far for
  458. * debug & stats. Odd numbers mean that a batch is in progress, even
  459. * numbers mean idle. The value returned will thus be roughly double
  460. * the cumulative batches since boot.
  461. */
  462. unsigned long rcu_exp_batches_completed(void)
  463. {
  464. return rcu_state.expedited_sequence;
  465. }
  466. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
  467. /*
  468. * Return the root node of the rcu_state structure.
  469. */
  470. static struct rcu_node *rcu_get_root(void)
  471. {
  472. return &rcu_state.node[0];
  473. }
  474. /*
  475. * Send along grace-period-related data for rcutorture diagnostics.
  476. */
  477. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  478. unsigned long *gp_seq)
  479. {
  480. switch (test_type) {
  481. case RCU_FLAVOR:
  482. *flags = READ_ONCE(rcu_state.gp_flags);
  483. *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
  484. break;
  485. default:
  486. break;
  487. }
  488. }
  489. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  490. #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK))
  491. /*
  492. * An empty function that will trigger a reschedule on
  493. * IRQ tail once IRQs get re-enabled on userspace/guest resume.
  494. */
  495. static void late_wakeup_func(struct irq_work *work)
  496. {
  497. }
  498. static DEFINE_PER_CPU(struct irq_work, late_wakeup_work) =
  499. IRQ_WORK_INIT(late_wakeup_func);
  500. /*
  501. * If either:
  502. *
  503. * 1) the task is about to enter in guest mode and $ARCH doesn't support KVM generic work
  504. * 2) the task is about to enter in user mode and $ARCH doesn't support generic entry.
  505. *
  506. * In these cases the late RCU wake ups aren't supported in the resched loops and our
  507. * last resort is to fire a local irq_work that will trigger a reschedule once IRQs
  508. * get re-enabled again.
  509. */
  510. noinstr void rcu_irq_work_resched(void)
  511. {
  512. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  513. if (IS_ENABLED(CONFIG_GENERIC_ENTRY) && !(current->flags & PF_VCPU))
  514. return;
  515. if (IS_ENABLED(CONFIG_KVM_XFER_TO_GUEST_WORK) && (current->flags & PF_VCPU))
  516. return;
  517. instrumentation_begin();
  518. if (do_nocb_deferred_wakeup(rdp) && need_resched()) {
  519. irq_work_queue(this_cpu_ptr(&late_wakeup_work));
  520. }
  521. instrumentation_end();
  522. }
  523. #endif /* #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK)) */
  524. #ifdef CONFIG_PROVE_RCU
  525. /**
  526. * rcu_irq_exit_check_preempt - Validate that scheduling is possible
  527. */
  528. void rcu_irq_exit_check_preempt(void)
  529. {
  530. lockdep_assert_irqs_disabled();
  531. RCU_LOCKDEP_WARN(ct_dynticks_nesting() <= 0,
  532. "RCU dynticks_nesting counter underflow/zero!");
  533. RCU_LOCKDEP_WARN(ct_dynticks_nmi_nesting() !=
  534. DYNTICK_IRQ_NONIDLE,
  535. "Bad RCU dynticks_nmi_nesting counter\n");
  536. RCU_LOCKDEP_WARN(rcu_dynticks_curr_cpu_in_eqs(),
  537. "RCU in extended quiescent state!");
  538. }
  539. #endif /* #ifdef CONFIG_PROVE_RCU */
  540. #ifdef CONFIG_NO_HZ_FULL
  541. /**
  542. * __rcu_irq_enter_check_tick - Enable scheduler tick on CPU if RCU needs it.
  543. *
  544. * The scheduler tick is not normally enabled when CPUs enter the kernel
  545. * from nohz_full userspace execution. After all, nohz_full userspace
  546. * execution is an RCU quiescent state and the time executing in the kernel
  547. * is quite short. Except of course when it isn't. And it is not hard to
  548. * cause a large system to spend tens of seconds or even minutes looping
  549. * in the kernel, which can cause a number of problems, include RCU CPU
  550. * stall warnings.
  551. *
  552. * Therefore, if a nohz_full CPU fails to report a quiescent state
  553. * in a timely manner, the RCU grace-period kthread sets that CPU's
  554. * ->rcu_urgent_qs flag with the expectation that the next interrupt or
  555. * exception will invoke this function, which will turn on the scheduler
  556. * tick, which will enable RCU to detect that CPU's quiescent states,
  557. * for example, due to cond_resched() calls in CONFIG_PREEMPT=n kernels.
  558. * The tick will be disabled once a quiescent state is reported for
  559. * this CPU.
  560. *
  561. * Of course, in carefully tuned systems, there might never be an
  562. * interrupt or exception. In that case, the RCU grace-period kthread
  563. * will eventually cause one to happen. However, in less carefully
  564. * controlled environments, this function allows RCU to get what it
  565. * needs without creating otherwise useless interruptions.
  566. */
  567. void __rcu_irq_enter_check_tick(void)
  568. {
  569. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  570. // If we're here from NMI there's nothing to do.
  571. if (in_nmi())
  572. return;
  573. RCU_LOCKDEP_WARN(rcu_dynticks_curr_cpu_in_eqs(),
  574. "Illegal rcu_irq_enter_check_tick() from extended quiescent state");
  575. if (!tick_nohz_full_cpu(rdp->cpu) ||
  576. !READ_ONCE(rdp->rcu_urgent_qs) ||
  577. READ_ONCE(rdp->rcu_forced_tick)) {
  578. // RCU doesn't need nohz_full help from this CPU, or it is
  579. // already getting that help.
  580. return;
  581. }
  582. // We get here only when not in an extended quiescent state and
  583. // from interrupts (as opposed to NMIs). Therefore, (1) RCU is
  584. // already watching and (2) The fact that we are in an interrupt
  585. // handler and that the rcu_node lock is an irq-disabled lock
  586. // prevents self-deadlock. So we can safely recheck under the lock.
  587. // Note that the nohz_full state currently cannot change.
  588. raw_spin_lock_rcu_node(rdp->mynode);
  589. if (rdp->rcu_urgent_qs && !rdp->rcu_forced_tick) {
  590. // A nohz_full CPU is in the kernel and RCU needs a
  591. // quiescent state. Turn on the tick!
  592. WRITE_ONCE(rdp->rcu_forced_tick, true);
  593. tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
  594. }
  595. raw_spin_unlock_rcu_node(rdp->mynode);
  596. }
  597. NOKPROBE_SYMBOL(__rcu_irq_enter_check_tick);
  598. #endif /* CONFIG_NO_HZ_FULL */
  599. /*
  600. * Check to see if any future non-offloaded RCU-related work will need
  601. * to be done by the current CPU, even if none need be done immediately,
  602. * returning 1 if so. This function is part of the RCU implementation;
  603. * it is -not- an exported member of the RCU API. This is used by
  604. * the idle-entry code to figure out whether it is safe to disable the
  605. * scheduler-clock interrupt.
  606. *
  607. * Just check whether or not this CPU has non-offloaded RCU callbacks
  608. * queued.
  609. */
  610. int rcu_needs_cpu(void)
  611. {
  612. return !rcu_segcblist_empty(&this_cpu_ptr(&rcu_data)->cblist) &&
  613. !rcu_rdp_is_offloaded(this_cpu_ptr(&rcu_data));
  614. }
  615. /*
  616. * If any sort of urgency was applied to the current CPU (for example,
  617. * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order
  618. * to get to a quiescent state, disable it.
  619. */
  620. static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
  621. {
  622. raw_lockdep_assert_held_rcu_node(rdp->mynode);
  623. WRITE_ONCE(rdp->rcu_urgent_qs, false);
  624. WRITE_ONCE(rdp->rcu_need_heavy_qs, false);
  625. if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) {
  626. tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
  627. WRITE_ONCE(rdp->rcu_forced_tick, false);
  628. }
  629. }
  630. /**
  631. * rcu_is_watching - see if RCU thinks that the current CPU is not idle
  632. *
  633. * Return true if RCU is watching the running CPU, which means that this
  634. * CPU can safely enter RCU read-side critical sections. In other words,
  635. * if the current CPU is not in its idle loop or is in an interrupt or
  636. * NMI handler, return true.
  637. *
  638. * Make notrace because it can be called by the internal functions of
  639. * ftrace, and making this notrace removes unnecessary recursion calls.
  640. */
  641. notrace bool rcu_is_watching(void)
  642. {
  643. bool ret;
  644. preempt_disable_notrace();
  645. ret = !rcu_dynticks_curr_cpu_in_eqs();
  646. preempt_enable_notrace();
  647. return ret;
  648. }
  649. EXPORT_SYMBOL_GPL(rcu_is_watching);
  650. /*
  651. * If a holdout task is actually running, request an urgent quiescent
  652. * state from its CPU. This is unsynchronized, so migrations can cause
  653. * the request to go to the wrong CPU. Which is OK, all that will happen
  654. * is that the CPU's next context switch will be a bit slower and next
  655. * time around this task will generate another request.
  656. */
  657. void rcu_request_urgent_qs_task(struct task_struct *t)
  658. {
  659. int cpu;
  660. barrier();
  661. cpu = task_cpu(t);
  662. if (!task_curr(t))
  663. return; /* This task is not running on that CPU. */
  664. smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
  665. }
  666. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  667. /*
  668. * Is the current CPU online as far as RCU is concerned?
  669. *
  670. * Disable preemption to avoid false positives that could otherwise
  671. * happen due to the current CPU number being sampled, this task being
  672. * preempted, its old CPU being taken offline, resuming on some other CPU,
  673. * then determining that its old CPU is now offline.
  674. *
  675. * Disable checking if in an NMI handler because we cannot safely
  676. * report errors from NMI handlers anyway. In addition, it is OK to use
  677. * RCU on an offline processor during initial boot, hence the check for
  678. * rcu_scheduler_fully_active.
  679. */
  680. bool rcu_lockdep_current_cpu_online(void)
  681. {
  682. struct rcu_data *rdp;
  683. bool ret = false;
  684. if (in_nmi() || !rcu_scheduler_fully_active)
  685. return true;
  686. preempt_disable_notrace();
  687. rdp = this_cpu_ptr(&rcu_data);
  688. /*
  689. * Strictly, we care here about the case where the current CPU is
  690. * in rcu_cpu_starting() and thus has an excuse for rdp->grpmask
  691. * not being up to date. So arch_spin_is_locked() might have a
  692. * false positive if it's held by some *other* CPU, but that's
  693. * OK because that just means a false *negative* on the warning.
  694. */
  695. if (rcu_rdp_cpu_online(rdp) || arch_spin_is_locked(&rcu_state.ofl_lock))
  696. ret = true;
  697. preempt_enable_notrace();
  698. return ret;
  699. }
  700. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  701. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  702. /*
  703. * When trying to report a quiescent state on behalf of some other CPU,
  704. * it is our responsibility to check for and handle potential overflow
  705. * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
  706. * After all, the CPU might be in deep idle state, and thus executing no
  707. * code whatsoever.
  708. */
  709. static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
  710. {
  711. raw_lockdep_assert_held_rcu_node(rnp);
  712. if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
  713. rnp->gp_seq))
  714. WRITE_ONCE(rdp->gpwrap, true);
  715. if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
  716. rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
  717. }
  718. /*
  719. * Snapshot the specified CPU's dynticks counter so that we can later
  720. * credit them with an implicit quiescent state. Return 1 if this CPU
  721. * is in dynticks idle mode, which is an extended quiescent state.
  722. */
  723. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  724. {
  725. rdp->dynticks_snap = rcu_dynticks_snap(rdp->cpu);
  726. if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
  727. trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
  728. rcu_gpnum_ovf(rdp->mynode, rdp);
  729. return 1;
  730. }
  731. return 0;
  732. }
  733. /*
  734. * Return true if the specified CPU has passed through a quiescent
  735. * state by virtue of being in or having passed through an dynticks
  736. * idle state since the last call to dyntick_save_progress_counter()
  737. * for this same CPU, or by virtue of having been offline.
  738. */
  739. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  740. {
  741. unsigned long jtsq;
  742. struct rcu_node *rnp = rdp->mynode;
  743. /*
  744. * If the CPU passed through or entered a dynticks idle phase with
  745. * no active irq/NMI handlers, then we can safely pretend that the CPU
  746. * already acknowledged the request to pass through a quiescent
  747. * state. Either way, that CPU cannot possibly be in an RCU
  748. * read-side critical section that started before the beginning
  749. * of the current RCU grace period.
  750. */
  751. if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
  752. trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
  753. rcu_gpnum_ovf(rnp, rdp);
  754. return 1;
  755. }
  756. /*
  757. * Complain if a CPU that is considered to be offline from RCU's
  758. * perspective has not yet reported a quiescent state. After all,
  759. * the offline CPU should have reported a quiescent state during
  760. * the CPU-offline process, or, failing that, by rcu_gp_init()
  761. * if it ran concurrently with either the CPU going offline or the
  762. * last task on a leaf rcu_node structure exiting its RCU read-side
  763. * critical section while all CPUs corresponding to that structure
  764. * are offline. This added warning detects bugs in any of these
  765. * code paths.
  766. *
  767. * The rcu_node structure's ->lock is held here, which excludes
  768. * the relevant portions the CPU-hotplug code, the grace-period
  769. * initialization code, and the rcu_read_unlock() code paths.
  770. *
  771. * For more detail, please refer to the "Hotplug CPU" section
  772. * of RCU's Requirements documentation.
  773. */
  774. if (WARN_ON_ONCE(!rcu_rdp_cpu_online(rdp))) {
  775. struct rcu_node *rnp1;
  776. pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
  777. __func__, rnp->grplo, rnp->grphi, rnp->level,
  778. (long)rnp->gp_seq, (long)rnp->completedqs);
  779. for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
  780. pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
  781. __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
  782. pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
  783. __func__, rdp->cpu, ".o"[rcu_rdp_cpu_online(rdp)],
  784. (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
  785. (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
  786. return 1; /* Break things loose after complaining. */
  787. }
  788. /*
  789. * A CPU running for an extended time within the kernel can
  790. * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
  791. * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
  792. * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
  793. * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
  794. * variable are safe because the assignments are repeated if this
  795. * CPU failed to pass through a quiescent state. This code
  796. * also checks .jiffies_resched in case jiffies_to_sched_qs
  797. * is set way high.
  798. */
  799. jtsq = READ_ONCE(jiffies_to_sched_qs);
  800. if (!READ_ONCE(rdp->rcu_need_heavy_qs) &&
  801. (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
  802. time_after(jiffies, rcu_state.jiffies_resched) ||
  803. rcu_state.cbovld)) {
  804. WRITE_ONCE(rdp->rcu_need_heavy_qs, true);
  805. /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
  806. smp_store_release(&rdp->rcu_urgent_qs, true);
  807. } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
  808. WRITE_ONCE(rdp->rcu_urgent_qs, true);
  809. }
  810. /*
  811. * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
  812. * The above code handles this, but only for straight cond_resched().
  813. * And some in-kernel loops check need_resched() before calling
  814. * cond_resched(), which defeats the above code for CPUs that are
  815. * running in-kernel with scheduling-clock interrupts disabled.
  816. * So hit them over the head with the resched_cpu() hammer!
  817. */
  818. if (tick_nohz_full_cpu(rdp->cpu) &&
  819. (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) ||
  820. rcu_state.cbovld)) {
  821. WRITE_ONCE(rdp->rcu_urgent_qs, true);
  822. resched_cpu(rdp->cpu);
  823. WRITE_ONCE(rdp->last_fqs_resched, jiffies);
  824. }
  825. /*
  826. * If more than halfway to RCU CPU stall-warning time, invoke
  827. * resched_cpu() more frequently to try to loosen things up a bit.
  828. * Also check to see if the CPU is getting hammered with interrupts,
  829. * but only once per grace period, just to keep the IPIs down to
  830. * a dull roar.
  831. */
  832. if (time_after(jiffies, rcu_state.jiffies_resched)) {
  833. if (time_after(jiffies,
  834. READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
  835. resched_cpu(rdp->cpu);
  836. WRITE_ONCE(rdp->last_fqs_resched, jiffies);
  837. }
  838. if (IS_ENABLED(CONFIG_IRQ_WORK) &&
  839. !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
  840. (rnp->ffmask & rdp->grpmask)) {
  841. rdp->rcu_iw_pending = true;
  842. rdp->rcu_iw_gp_seq = rnp->gp_seq;
  843. irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
  844. }
  845. }
  846. return 0;
  847. }
  848. /* Trace-event wrapper function for trace_rcu_future_grace_period. */
  849. static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  850. unsigned long gp_seq_req, const char *s)
  851. {
  852. trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
  853. gp_seq_req, rnp->level,
  854. rnp->grplo, rnp->grphi, s);
  855. }
  856. /*
  857. * rcu_start_this_gp - Request the start of a particular grace period
  858. * @rnp_start: The leaf node of the CPU from which to start.
  859. * @rdp: The rcu_data corresponding to the CPU from which to start.
  860. * @gp_seq_req: The gp_seq of the grace period to start.
  861. *
  862. * Start the specified grace period, as needed to handle newly arrived
  863. * callbacks. The required future grace periods are recorded in each
  864. * rcu_node structure's ->gp_seq_needed field. Returns true if there
  865. * is reason to awaken the grace-period kthread.
  866. *
  867. * The caller must hold the specified rcu_node structure's ->lock, which
  868. * is why the caller is responsible for waking the grace-period kthread.
  869. *
  870. * Returns true if the GP thread needs to be awakened else false.
  871. */
  872. static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
  873. unsigned long gp_seq_req)
  874. {
  875. bool ret = false;
  876. struct rcu_node *rnp;
  877. /*
  878. * Use funnel locking to either acquire the root rcu_node
  879. * structure's lock or bail out if the need for this grace period
  880. * has already been recorded -- or if that grace period has in
  881. * fact already started. If there is already a grace period in
  882. * progress in a non-leaf node, no recording is needed because the
  883. * end of the grace period will scan the leaf rcu_node structures.
  884. * Note that rnp_start->lock must not be released.
  885. */
  886. raw_lockdep_assert_held_rcu_node(rnp_start);
  887. trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
  888. for (rnp = rnp_start; 1; rnp = rnp->parent) {
  889. if (rnp != rnp_start)
  890. raw_spin_lock_rcu_node(rnp);
  891. if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
  892. rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
  893. (rnp != rnp_start &&
  894. rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
  895. trace_rcu_this_gp(rnp, rdp, gp_seq_req,
  896. TPS("Prestarted"));
  897. goto unlock_out;
  898. }
  899. WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req);
  900. if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
  901. /*
  902. * We just marked the leaf or internal node, and a
  903. * grace period is in progress, which means that
  904. * rcu_gp_cleanup() will see the marking. Bail to
  905. * reduce contention.
  906. */
  907. trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
  908. TPS("Startedleaf"));
  909. goto unlock_out;
  910. }
  911. if (rnp != rnp_start && rnp->parent != NULL)
  912. raw_spin_unlock_rcu_node(rnp);
  913. if (!rnp->parent)
  914. break; /* At root, and perhaps also leaf. */
  915. }
  916. /* If GP already in progress, just leave, otherwise start one. */
  917. if (rcu_gp_in_progress()) {
  918. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
  919. goto unlock_out;
  920. }
  921. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
  922. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
  923. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  924. if (!READ_ONCE(rcu_state.gp_kthread)) {
  925. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
  926. goto unlock_out;
  927. }
  928. trace_rcu_grace_period(rcu_state.name, data_race(rcu_state.gp_seq), TPS("newreq"));
  929. ret = true; /* Caller must wake GP kthread. */
  930. unlock_out:
  931. /* Push furthest requested GP to leaf node and rcu_data structure. */
  932. if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
  933. WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed);
  934. WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
  935. }
  936. if (rnp != rnp_start)
  937. raw_spin_unlock_rcu_node(rnp);
  938. return ret;
  939. }
  940. /*
  941. * Clean up any old requests for the just-ended grace period. Also return
  942. * whether any additional grace periods have been requested.
  943. */
  944. static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
  945. {
  946. bool needmore;
  947. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  948. needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
  949. if (!needmore)
  950. rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
  951. trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
  952. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  953. return needmore;
  954. }
  955. /*
  956. * Awaken the grace-period kthread. Don't do a self-awaken (unless in an
  957. * interrupt or softirq handler, in which case we just might immediately
  958. * sleep upon return, resulting in a grace-period hang), and don't bother
  959. * awakening when there is nothing for the grace-period kthread to do
  960. * (as in several CPUs raced to awaken, we lost), and finally don't try
  961. * to awaken a kthread that has not yet been created. If all those checks
  962. * are passed, track some debug information and awaken.
  963. *
  964. * So why do the self-wakeup when in an interrupt or softirq handler
  965. * in the grace-period kthread's context? Because the kthread might have
  966. * been interrupted just as it was going to sleep, and just after the final
  967. * pre-sleep check of the awaken condition. In this case, a wakeup really
  968. * is required, and is therefore supplied.
  969. */
  970. static void rcu_gp_kthread_wake(void)
  971. {
  972. struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
  973. if ((current == t && !in_hardirq() && !in_serving_softirq()) ||
  974. !READ_ONCE(rcu_state.gp_flags) || !t)
  975. return;
  976. WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
  977. WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
  978. swake_up_one(&rcu_state.gp_wq);
  979. }
  980. /*
  981. * If there is room, assign a ->gp_seq number to any callbacks on this
  982. * CPU that have not already been assigned. Also accelerate any callbacks
  983. * that were previously assigned a ->gp_seq number that has since proven
  984. * to be too conservative, which can happen if callbacks get assigned a
  985. * ->gp_seq number while RCU is idle, but with reference to a non-root
  986. * rcu_node structure. This function is idempotent, so it does not hurt
  987. * to call it repeatedly. Returns an flag saying that we should awaken
  988. * the RCU grace-period kthread.
  989. *
  990. * The caller must hold rnp->lock with interrupts disabled.
  991. */
  992. static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  993. {
  994. unsigned long gp_seq_req;
  995. bool ret = false;
  996. rcu_lockdep_assert_cblist_protected(rdp);
  997. raw_lockdep_assert_held_rcu_node(rnp);
  998. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  999. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1000. return false;
  1001. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));
  1002. /*
  1003. * Callbacks are often registered with incomplete grace-period
  1004. * information. Something about the fact that getting exact
  1005. * information requires acquiring a global lock... RCU therefore
  1006. * makes a conservative estimate of the grace period number at which
  1007. * a given callback will become ready to invoke. The following
  1008. * code checks this estimate and improves it when possible, thus
  1009. * accelerating callback invocation to an earlier grace-period
  1010. * number.
  1011. */
  1012. gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
  1013. if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
  1014. ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
  1015. /* Trace depending on how much we were able to accelerate. */
  1016. if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
  1017. trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccWaitCB"));
  1018. else
  1019. trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
  1020. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
  1021. return ret;
  1022. }
  1023. /*
  1024. * Similar to rcu_accelerate_cbs(), but does not require that the leaf
  1025. * rcu_node structure's ->lock be held. It consults the cached value
  1026. * of ->gp_seq_needed in the rcu_data structure, and if that indicates
  1027. * that a new grace-period request be made, invokes rcu_accelerate_cbs()
  1028. * while holding the leaf rcu_node structure's ->lock.
  1029. */
  1030. static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
  1031. struct rcu_data *rdp)
  1032. {
  1033. unsigned long c;
  1034. bool needwake;
  1035. rcu_lockdep_assert_cblist_protected(rdp);
  1036. c = rcu_seq_snap(&rcu_state.gp_seq);
  1037. if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
  1038. /* Old request still live, so mark recent callbacks. */
  1039. (void)rcu_segcblist_accelerate(&rdp->cblist, c);
  1040. return;
  1041. }
  1042. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1043. needwake = rcu_accelerate_cbs(rnp, rdp);
  1044. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1045. if (needwake)
  1046. rcu_gp_kthread_wake();
  1047. }
  1048. /*
  1049. * Move any callbacks whose grace period has completed to the
  1050. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1051. * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
  1052. * sublist. This function is idempotent, so it does not hurt to
  1053. * invoke it repeatedly. As long as it is not invoked -too- often...
  1054. * Returns true if the RCU grace-period kthread needs to be awakened.
  1055. *
  1056. * The caller must hold rnp->lock with interrupts disabled.
  1057. */
  1058. static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  1059. {
  1060. rcu_lockdep_assert_cblist_protected(rdp);
  1061. raw_lockdep_assert_held_rcu_node(rnp);
  1062. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1063. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1064. return false;
  1065. /*
  1066. * Find all callbacks whose ->gp_seq numbers indicate that they
  1067. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1068. */
  1069. rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
  1070. /* Classify any remaining callbacks. */
  1071. return rcu_accelerate_cbs(rnp, rdp);
  1072. }
  1073. /*
  1074. * Move and classify callbacks, but only if doing so won't require
  1075. * that the RCU grace-period kthread be awakened.
  1076. */
  1077. static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
  1078. struct rcu_data *rdp)
  1079. {
  1080. rcu_lockdep_assert_cblist_protected(rdp);
  1081. if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) || !raw_spin_trylock_rcu_node(rnp))
  1082. return;
  1083. // The grace period cannot end while we hold the rcu_node lock.
  1084. if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))
  1085. WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
  1086. raw_spin_unlock_rcu_node(rnp);
  1087. }
  1088. /*
  1089. * In CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels, attempt to generate a
  1090. * quiescent state. This is intended to be invoked when the CPU notices
  1091. * a new grace period.
  1092. */
  1093. static void rcu_strict_gp_check_qs(void)
  1094. {
  1095. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) {
  1096. rcu_read_lock();
  1097. rcu_read_unlock();
  1098. }
  1099. }
  1100. /*
  1101. * Update CPU-local rcu_data state to record the beginnings and ends of
  1102. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1103. * structure corresponding to the current CPU, and must have irqs disabled.
  1104. * Returns true if the grace-period kthread needs to be awakened.
  1105. */
  1106. static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
  1107. {
  1108. bool ret = false;
  1109. bool need_qs;
  1110. const bool offloaded = rcu_rdp_is_offloaded(rdp);
  1111. raw_lockdep_assert_held_rcu_node(rnp);
  1112. if (rdp->gp_seq == rnp->gp_seq)
  1113. return false; /* Nothing to do. */
  1114. /* Handle the ends of any preceding grace periods first. */
  1115. if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
  1116. unlikely(READ_ONCE(rdp->gpwrap))) {
  1117. if (!offloaded)
  1118. ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */
  1119. rdp->core_needs_qs = false;
  1120. trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
  1121. } else {
  1122. if (!offloaded)
  1123. ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */
  1124. if (rdp->core_needs_qs)
  1125. rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
  1126. }
  1127. /* Now handle the beginnings of any new-to-this-CPU grace periods. */
  1128. if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
  1129. unlikely(READ_ONCE(rdp->gpwrap))) {
  1130. /*
  1131. * If the current grace period is waiting for this CPU,
  1132. * set up to detect a quiescent state, otherwise don't
  1133. * go looking for one.
  1134. */
  1135. trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
  1136. need_qs = !!(rnp->qsmask & rdp->grpmask);
  1137. rdp->cpu_no_qs.b.norm = need_qs;
  1138. rdp->core_needs_qs = need_qs;
  1139. zero_cpu_stall_ticks(rdp);
  1140. }
  1141. rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
  1142. if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
  1143. WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
  1144. if (IS_ENABLED(CONFIG_PROVE_RCU) && READ_ONCE(rdp->gpwrap))
  1145. WRITE_ONCE(rdp->last_sched_clock, jiffies);
  1146. WRITE_ONCE(rdp->gpwrap, false);
  1147. rcu_gpnum_ovf(rnp, rdp);
  1148. return ret;
  1149. }
  1150. static void note_gp_changes(struct rcu_data *rdp)
  1151. {
  1152. unsigned long flags;
  1153. bool needwake;
  1154. struct rcu_node *rnp;
  1155. local_irq_save(flags);
  1156. rnp = rdp->mynode;
  1157. if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
  1158. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1159. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1160. local_irq_restore(flags);
  1161. return;
  1162. }
  1163. needwake = __note_gp_changes(rnp, rdp);
  1164. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1165. rcu_strict_gp_check_qs();
  1166. if (needwake)
  1167. rcu_gp_kthread_wake();
  1168. }
  1169. static atomic_t *rcu_gp_slow_suppress;
  1170. /* Register a counter to suppress debugging grace-period delays. */
  1171. void rcu_gp_slow_register(atomic_t *rgssp)
  1172. {
  1173. WARN_ON_ONCE(rcu_gp_slow_suppress);
  1174. WRITE_ONCE(rcu_gp_slow_suppress, rgssp);
  1175. }
  1176. EXPORT_SYMBOL_GPL(rcu_gp_slow_register);
  1177. /* Unregister a counter, with NULL for not caring which. */
  1178. void rcu_gp_slow_unregister(atomic_t *rgssp)
  1179. {
  1180. WARN_ON_ONCE(rgssp && rgssp != rcu_gp_slow_suppress);
  1181. WRITE_ONCE(rcu_gp_slow_suppress, NULL);
  1182. }
  1183. EXPORT_SYMBOL_GPL(rcu_gp_slow_unregister);
  1184. static bool rcu_gp_slow_is_suppressed(void)
  1185. {
  1186. atomic_t *rgssp = READ_ONCE(rcu_gp_slow_suppress);
  1187. return rgssp && atomic_read(rgssp);
  1188. }
  1189. static void rcu_gp_slow(int delay)
  1190. {
  1191. if (!rcu_gp_slow_is_suppressed() && delay > 0 &&
  1192. !(rcu_seq_ctr(rcu_state.gp_seq) % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1193. schedule_timeout_idle(delay);
  1194. }
  1195. static unsigned long sleep_duration;
  1196. /* Allow rcutorture to stall the grace-period kthread. */
  1197. void rcu_gp_set_torture_wait(int duration)
  1198. {
  1199. if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST) && duration > 0)
  1200. WRITE_ONCE(sleep_duration, duration);
  1201. }
  1202. EXPORT_SYMBOL_GPL(rcu_gp_set_torture_wait);
  1203. /* Actually implement the aforementioned wait. */
  1204. static void rcu_gp_torture_wait(void)
  1205. {
  1206. unsigned long duration;
  1207. if (!IS_ENABLED(CONFIG_RCU_TORTURE_TEST))
  1208. return;
  1209. duration = xchg(&sleep_duration, 0UL);
  1210. if (duration > 0) {
  1211. pr_alert("%s: Waiting %lu jiffies\n", __func__, duration);
  1212. schedule_timeout_idle(duration);
  1213. pr_alert("%s: Wait complete\n", __func__);
  1214. }
  1215. }
  1216. /*
  1217. * Handler for on_each_cpu() to invoke the target CPU's RCU core
  1218. * processing.
  1219. */
  1220. static void rcu_strict_gp_boundary(void *unused)
  1221. {
  1222. invoke_rcu_core();
  1223. }
  1224. // Has rcu_init() been invoked? This is used (for example) to determine
  1225. // whether spinlocks may be acquired safely.
  1226. static bool rcu_init_invoked(void)
  1227. {
  1228. return !!rcu_state.n_online_cpus;
  1229. }
  1230. // Make the polled API aware of the beginning of a grace period.
  1231. static void rcu_poll_gp_seq_start(unsigned long *snap)
  1232. {
  1233. struct rcu_node *rnp = rcu_get_root();
  1234. if (rcu_init_invoked())
  1235. raw_lockdep_assert_held_rcu_node(rnp);
  1236. // If RCU was idle, note beginning of GP.
  1237. if (!rcu_seq_state(rcu_state.gp_seq_polled))
  1238. rcu_seq_start(&rcu_state.gp_seq_polled);
  1239. // Either way, record current state.
  1240. *snap = rcu_state.gp_seq_polled;
  1241. }
  1242. // Make the polled API aware of the end of a grace period.
  1243. static void rcu_poll_gp_seq_end(unsigned long *snap)
  1244. {
  1245. struct rcu_node *rnp = rcu_get_root();
  1246. if (rcu_init_invoked())
  1247. raw_lockdep_assert_held_rcu_node(rnp);
  1248. // If the previously noted GP is still in effect, record the
  1249. // end of that GP. Either way, zero counter to avoid counter-wrap
  1250. // problems.
  1251. if (*snap && *snap == rcu_state.gp_seq_polled) {
  1252. rcu_seq_end(&rcu_state.gp_seq_polled);
  1253. rcu_state.gp_seq_polled_snap = 0;
  1254. rcu_state.gp_seq_polled_exp_snap = 0;
  1255. } else {
  1256. *snap = 0;
  1257. }
  1258. }
  1259. // Make the polled API aware of the beginning of a grace period, but
  1260. // where caller does not hold the root rcu_node structure's lock.
  1261. static void rcu_poll_gp_seq_start_unlocked(unsigned long *snap)
  1262. {
  1263. unsigned long flags;
  1264. struct rcu_node *rnp = rcu_get_root();
  1265. if (rcu_init_invoked()) {
  1266. lockdep_assert_irqs_enabled();
  1267. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1268. }
  1269. rcu_poll_gp_seq_start(snap);
  1270. if (rcu_init_invoked())
  1271. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1272. }
  1273. // Make the polled API aware of the end of a grace period, but where
  1274. // caller does not hold the root rcu_node structure's lock.
  1275. static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)
  1276. {
  1277. unsigned long flags;
  1278. struct rcu_node *rnp = rcu_get_root();
  1279. if (rcu_init_invoked()) {
  1280. lockdep_assert_irqs_enabled();
  1281. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1282. }
  1283. rcu_poll_gp_seq_end(snap);
  1284. if (rcu_init_invoked())
  1285. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1286. }
  1287. /*
  1288. * Initialize a new grace period. Return false if no grace period required.
  1289. */
  1290. static noinline_for_stack bool rcu_gp_init(void)
  1291. {
  1292. unsigned long flags;
  1293. unsigned long oldmask;
  1294. unsigned long mask;
  1295. struct rcu_data *rdp;
  1296. struct rcu_node *rnp = rcu_get_root();
  1297. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1298. raw_spin_lock_irq_rcu_node(rnp);
  1299. if (!READ_ONCE(rcu_state.gp_flags)) {
  1300. /* Spurious wakeup, tell caller to go back to sleep. */
  1301. raw_spin_unlock_irq_rcu_node(rnp);
  1302. return false;
  1303. }
  1304. WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
  1305. if (WARN_ON_ONCE(rcu_gp_in_progress())) {
  1306. /*
  1307. * Grace period already in progress, don't start another.
  1308. * Not supposed to be able to happen.
  1309. */
  1310. raw_spin_unlock_irq_rcu_node(rnp);
  1311. return false;
  1312. }
  1313. /* Advance to a new grace period and initialize state. */
  1314. record_gp_stall_check_time();
  1315. /* Record GP times before starting GP, hence rcu_seq_start(). */
  1316. rcu_seq_start(&rcu_state.gp_seq);
  1317. ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
  1318. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
  1319. rcu_poll_gp_seq_start(&rcu_state.gp_seq_polled_snap);
  1320. raw_spin_unlock_irq_rcu_node(rnp);
  1321. /*
  1322. * Apply per-leaf buffered online and offline operations to
  1323. * the rcu_node tree. Note that this new grace period need not
  1324. * wait for subsequent online CPUs, and that RCU hooks in the CPU
  1325. * offlining path, when combined with checks in this function,
  1326. * will handle CPUs that are currently going offline or that will
  1327. * go offline later. Please also refer to "Hotplug CPU" section
  1328. * of RCU's Requirements documentation.
  1329. */
  1330. WRITE_ONCE(rcu_state.gp_state, RCU_GP_ONOFF);
  1331. /* Exclude CPU hotplug operations. */
  1332. rcu_for_each_leaf_node(rnp) {
  1333. local_irq_save(flags);
  1334. arch_spin_lock(&rcu_state.ofl_lock);
  1335. raw_spin_lock_rcu_node(rnp);
  1336. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1337. !rnp->wait_blkd_tasks) {
  1338. /* Nothing to do on this leaf rcu_node structure. */
  1339. raw_spin_unlock_rcu_node(rnp);
  1340. arch_spin_unlock(&rcu_state.ofl_lock);
  1341. local_irq_restore(flags);
  1342. continue;
  1343. }
  1344. /* Record old state, apply changes to ->qsmaskinit field. */
  1345. oldmask = rnp->qsmaskinit;
  1346. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1347. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1348. if (!oldmask != !rnp->qsmaskinit) {
  1349. if (!oldmask) { /* First online CPU for rcu_node. */
  1350. if (!rnp->wait_blkd_tasks) /* Ever offline? */
  1351. rcu_init_new_rnp(rnp);
  1352. } else if (rcu_preempt_has_tasks(rnp)) {
  1353. rnp->wait_blkd_tasks = true; /* blocked tasks */
  1354. } else { /* Last offline CPU and can propagate. */
  1355. rcu_cleanup_dead_rnp(rnp);
  1356. }
  1357. }
  1358. /*
  1359. * If all waited-on tasks from prior grace period are
  1360. * done, and if all this rcu_node structure's CPUs are
  1361. * still offline, propagate up the rcu_node tree and
  1362. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1363. * rcu_node structure's CPUs has since come back online,
  1364. * simply clear ->wait_blkd_tasks.
  1365. */
  1366. if (rnp->wait_blkd_tasks &&
  1367. (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
  1368. rnp->wait_blkd_tasks = false;
  1369. if (!rnp->qsmaskinit)
  1370. rcu_cleanup_dead_rnp(rnp);
  1371. }
  1372. raw_spin_unlock_rcu_node(rnp);
  1373. arch_spin_unlock(&rcu_state.ofl_lock);
  1374. local_irq_restore(flags);
  1375. }
  1376. rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
  1377. /*
  1378. * Set the quiescent-state-needed bits in all the rcu_node
  1379. * structures for all currently online CPUs in breadth-first
  1380. * order, starting from the root rcu_node structure, relying on the
  1381. * layout of the tree within the rcu_state.node[] array. Note that
  1382. * other CPUs will access only the leaves of the hierarchy, thus
  1383. * seeing that no grace period is in progress, at least until the
  1384. * corresponding leaf node has been initialized.
  1385. *
  1386. * The grace period cannot complete until the initialization
  1387. * process finishes, because this kthread handles both.
  1388. */
  1389. WRITE_ONCE(rcu_state.gp_state, RCU_GP_INIT);
  1390. rcu_for_each_node_breadth_first(rnp) {
  1391. rcu_gp_slow(gp_init_delay);
  1392. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1393. rdp = this_cpu_ptr(&rcu_data);
  1394. rcu_preempt_check_blocked_tasks(rnp);
  1395. rnp->qsmask = rnp->qsmaskinit;
  1396. WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
  1397. if (rnp == rdp->mynode)
  1398. (void)__note_gp_changes(rnp, rdp);
  1399. rcu_preempt_boost_start_gp(rnp);
  1400. trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
  1401. rnp->level, rnp->grplo,
  1402. rnp->grphi, rnp->qsmask);
  1403. /* Quiescent states for tasks on any now-offline CPUs. */
  1404. mask = rnp->qsmask & ~rnp->qsmaskinitnext;
  1405. rnp->rcu_gp_init_mask = mask;
  1406. if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
  1407. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  1408. else
  1409. raw_spin_unlock_irq_rcu_node(rnp);
  1410. cond_resched_tasks_rcu_qs();
  1411. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1412. }
  1413. // If strict, make all CPUs aware of new grace period.
  1414. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  1415. on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
  1416. return true;
  1417. }
  1418. /*
  1419. * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
  1420. * time.
  1421. */
  1422. static bool rcu_gp_fqs_check_wake(int *gfp)
  1423. {
  1424. struct rcu_node *rnp = rcu_get_root();
  1425. // If under overload conditions, force an immediate FQS scan.
  1426. if (*gfp & RCU_GP_FLAG_OVLD)
  1427. return true;
  1428. // Someone like call_rcu() requested a force-quiescent-state scan.
  1429. *gfp = READ_ONCE(rcu_state.gp_flags);
  1430. if (*gfp & RCU_GP_FLAG_FQS)
  1431. return true;
  1432. // The current grace period has completed.
  1433. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1434. return true;
  1435. return false;
  1436. }
  1437. /*
  1438. * Do one round of quiescent-state forcing.
  1439. */
  1440. static void rcu_gp_fqs(bool first_time)
  1441. {
  1442. int nr_fqs = READ_ONCE(rcu_state.nr_fqs_jiffies_stall);
  1443. struct rcu_node *rnp = rcu_get_root();
  1444. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1445. WRITE_ONCE(rcu_state.n_force_qs, rcu_state.n_force_qs + 1);
  1446. WARN_ON_ONCE(nr_fqs > 3);
  1447. /* Only countdown nr_fqs for stall purposes if jiffies moves. */
  1448. if (nr_fqs) {
  1449. if (nr_fqs == 1) {
  1450. WRITE_ONCE(rcu_state.jiffies_stall,
  1451. jiffies + rcu_jiffies_till_stall_check());
  1452. }
  1453. WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, --nr_fqs);
  1454. }
  1455. if (first_time) {
  1456. /* Collect dyntick-idle snapshots. */
  1457. force_qs_rnp(dyntick_save_progress_counter);
  1458. } else {
  1459. /* Handle dyntick-idle and offline CPUs. */
  1460. force_qs_rnp(rcu_implicit_dynticks_qs);
  1461. }
  1462. /* Clear flag to prevent immediate re-entry. */
  1463. if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
  1464. raw_spin_lock_irq_rcu_node(rnp);
  1465. WRITE_ONCE(rcu_state.gp_flags,
  1466. READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
  1467. raw_spin_unlock_irq_rcu_node(rnp);
  1468. }
  1469. }
  1470. /*
  1471. * Loop doing repeated quiescent-state forcing until the grace period ends.
  1472. */
  1473. static noinline_for_stack void rcu_gp_fqs_loop(void)
  1474. {
  1475. bool first_gp_fqs = true;
  1476. int gf = 0;
  1477. unsigned long j;
  1478. int ret;
  1479. struct rcu_node *rnp = rcu_get_root();
  1480. j = READ_ONCE(jiffies_till_first_fqs);
  1481. if (rcu_state.cbovld)
  1482. gf = RCU_GP_FLAG_OVLD;
  1483. ret = 0;
  1484. for (;;) {
  1485. if (rcu_state.cbovld) {
  1486. j = (j + 2) / 3;
  1487. if (j <= 0)
  1488. j = 1;
  1489. }
  1490. if (!ret || time_before(jiffies + j, rcu_state.jiffies_force_qs)) {
  1491. WRITE_ONCE(rcu_state.jiffies_force_qs, jiffies + j);
  1492. /*
  1493. * jiffies_force_qs before RCU_GP_WAIT_FQS state
  1494. * update; required for stall checks.
  1495. */
  1496. smp_wmb();
  1497. WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
  1498. jiffies + (j ? 3 * j : 2));
  1499. }
  1500. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1501. TPS("fqswait"));
  1502. WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_FQS);
  1503. (void)swait_event_idle_timeout_exclusive(rcu_state.gp_wq,
  1504. rcu_gp_fqs_check_wake(&gf), j);
  1505. rcu_gp_torture_wait();
  1506. WRITE_ONCE(rcu_state.gp_state, RCU_GP_DOING_FQS);
  1507. /* Locking provides needed memory barriers. */
  1508. /*
  1509. * Exit the loop if the root rcu_node structure indicates that the grace period
  1510. * has ended, leave the loop. The rcu_preempt_blocked_readers_cgp(rnp) check
  1511. * is required only for single-node rcu_node trees because readers blocking
  1512. * the current grace period are queued only on leaf rcu_node structures.
  1513. * For multi-node trees, checking the root node's ->qsmask suffices, because a
  1514. * given root node's ->qsmask bit is cleared only when all CPUs and tasks from
  1515. * the corresponding leaf nodes have passed through their quiescent state.
  1516. */
  1517. if (!READ_ONCE(rnp->qsmask) &&
  1518. !rcu_preempt_blocked_readers_cgp(rnp))
  1519. break;
  1520. /* If time for quiescent-state forcing, do it. */
  1521. if (!time_after(rcu_state.jiffies_force_qs, jiffies) ||
  1522. (gf & (RCU_GP_FLAG_FQS | RCU_GP_FLAG_OVLD))) {
  1523. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1524. TPS("fqsstart"));
  1525. rcu_gp_fqs(first_gp_fqs);
  1526. gf = 0;
  1527. if (first_gp_fqs) {
  1528. first_gp_fqs = false;
  1529. gf = rcu_state.cbovld ? RCU_GP_FLAG_OVLD : 0;
  1530. }
  1531. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1532. TPS("fqsend"));
  1533. cond_resched_tasks_rcu_qs();
  1534. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1535. ret = 0; /* Force full wait till next FQS. */
  1536. j = READ_ONCE(jiffies_till_next_fqs);
  1537. } else {
  1538. /* Deal with stray signal. */
  1539. cond_resched_tasks_rcu_qs();
  1540. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1541. WARN_ON(signal_pending(current));
  1542. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1543. TPS("fqswaitsig"));
  1544. ret = 1; /* Keep old FQS timing. */
  1545. j = jiffies;
  1546. if (time_after(jiffies, rcu_state.jiffies_force_qs))
  1547. j = 1;
  1548. else
  1549. j = rcu_state.jiffies_force_qs - j;
  1550. gf = 0;
  1551. }
  1552. }
  1553. }
  1554. /*
  1555. * Clean up after the old grace period.
  1556. */
  1557. static noinline void rcu_gp_cleanup(void)
  1558. {
  1559. int cpu;
  1560. bool needgp = false;
  1561. unsigned long gp_duration;
  1562. unsigned long new_gp_seq;
  1563. bool offloaded;
  1564. struct rcu_data *rdp;
  1565. struct rcu_node *rnp = rcu_get_root();
  1566. struct swait_queue_head *sq;
  1567. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1568. raw_spin_lock_irq_rcu_node(rnp);
  1569. rcu_state.gp_end = jiffies;
  1570. gp_duration = rcu_state.gp_end - rcu_state.gp_start;
  1571. if (gp_duration > rcu_state.gp_max)
  1572. rcu_state.gp_max = gp_duration;
  1573. /*
  1574. * We know the grace period is complete, but to everyone else
  1575. * it appears to still be ongoing. But it is also the case
  1576. * that to everyone else it looks like there is nothing that
  1577. * they can do to advance the grace period. It is therefore
  1578. * safe for us to drop the lock in order to mark the grace
  1579. * period as completed in all of the rcu_node structures.
  1580. */
  1581. rcu_poll_gp_seq_end(&rcu_state.gp_seq_polled_snap);
  1582. raw_spin_unlock_irq_rcu_node(rnp);
  1583. /*
  1584. * Propagate new ->gp_seq value to rcu_node structures so that
  1585. * other CPUs don't have to wait until the start of the next grace
  1586. * period to process their callbacks. This also avoids some nasty
  1587. * RCU grace-period initialization races by forcing the end of
  1588. * the current grace period to be completely recorded in all of
  1589. * the rcu_node structures before the beginning of the next grace
  1590. * period is recorded in any of the rcu_node structures.
  1591. */
  1592. new_gp_seq = rcu_state.gp_seq;
  1593. rcu_seq_end(&new_gp_seq);
  1594. rcu_for_each_node_breadth_first(rnp) {
  1595. raw_spin_lock_irq_rcu_node(rnp);
  1596. if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
  1597. dump_blkd_tasks(rnp, 10);
  1598. WARN_ON_ONCE(rnp->qsmask);
  1599. WRITE_ONCE(rnp->gp_seq, new_gp_seq);
  1600. if (!rnp->parent)
  1601. smp_mb(); // Order against failing poll_state_synchronize_rcu_full().
  1602. rdp = this_cpu_ptr(&rcu_data);
  1603. if (rnp == rdp->mynode)
  1604. needgp = __note_gp_changes(rnp, rdp) || needgp;
  1605. /* smp_mb() provided by prior unlock-lock pair. */
  1606. needgp = rcu_future_gp_cleanup(rnp) || needgp;
  1607. // Reset overload indication for CPUs no longer overloaded
  1608. if (rcu_is_leaf_node(rnp))
  1609. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) {
  1610. rdp = per_cpu_ptr(&rcu_data, cpu);
  1611. check_cb_ovld_locked(rdp, rnp);
  1612. }
  1613. sq = rcu_nocb_gp_get(rnp);
  1614. raw_spin_unlock_irq_rcu_node(rnp);
  1615. rcu_nocb_gp_cleanup(sq);
  1616. cond_resched_tasks_rcu_qs();
  1617. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1618. rcu_gp_slow(gp_cleanup_delay);
  1619. }
  1620. rnp = rcu_get_root();
  1621. raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
  1622. /* Declare grace period done, trace first to use old GP number. */
  1623. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
  1624. rcu_seq_end(&rcu_state.gp_seq);
  1625. ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
  1626. WRITE_ONCE(rcu_state.gp_state, RCU_GP_IDLE);
  1627. /* Check for GP requests since above loop. */
  1628. rdp = this_cpu_ptr(&rcu_data);
  1629. if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
  1630. trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
  1631. TPS("CleanupMore"));
  1632. needgp = true;
  1633. }
  1634. /* Advance CBs to reduce false positives below. */
  1635. offloaded = rcu_rdp_is_offloaded(rdp);
  1636. if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
  1637. // We get here if a grace period was needed (“needgp”)
  1638. // and the above call to rcu_accelerate_cbs() did not set
  1639. // the RCU_GP_FLAG_INIT bit in ->gp_state (which records
  1640. // the need for another grace period).  The purpose
  1641. // of the “offloaded” check is to avoid invoking
  1642. // rcu_accelerate_cbs() on an offloaded CPU because we do not
  1643. // hold the ->nocb_lock needed to safely access an offloaded
  1644. // ->cblist.  We do not want to acquire that lock because
  1645. // it can be heavily contended during callback floods.
  1646. WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
  1647. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  1648. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("newreq"));
  1649. } else {
  1650. // We get here either if there is no need for an
  1651. // additional grace period or if rcu_accelerate_cbs() has
  1652. // already set the RCU_GP_FLAG_INIT bit in ->gp_flags. 
  1653. // So all we need to do is to clear all of the other
  1654. // ->gp_flags bits.
  1655. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags & RCU_GP_FLAG_INIT);
  1656. }
  1657. raw_spin_unlock_irq_rcu_node(rnp);
  1658. // If strict, make all CPUs aware of the end of the old grace period.
  1659. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  1660. on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
  1661. }
  1662. /*
  1663. * Body of kthread that handles grace periods.
  1664. */
  1665. static int __noreturn rcu_gp_kthread(void *unused)
  1666. {
  1667. rcu_bind_gp_kthread();
  1668. for (;;) {
  1669. /* Handle grace-period start. */
  1670. for (;;) {
  1671. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1672. TPS("reqwait"));
  1673. WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_GPS);
  1674. swait_event_idle_exclusive(rcu_state.gp_wq,
  1675. READ_ONCE(rcu_state.gp_flags) &
  1676. RCU_GP_FLAG_INIT);
  1677. rcu_gp_torture_wait();
  1678. WRITE_ONCE(rcu_state.gp_state, RCU_GP_DONE_GPS);
  1679. /* Locking provides needed memory barrier. */
  1680. if (rcu_gp_init())
  1681. break;
  1682. cond_resched_tasks_rcu_qs();
  1683. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1684. WARN_ON(signal_pending(current));
  1685. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1686. TPS("reqwaitsig"));
  1687. }
  1688. /* Handle quiescent-state forcing. */
  1689. rcu_gp_fqs_loop();
  1690. /* Handle grace-period end. */
  1691. WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANUP);
  1692. rcu_gp_cleanup();
  1693. WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANED);
  1694. }
  1695. }
  1696. /*
  1697. * Report a full set of quiescent states to the rcu_state data structure.
  1698. * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
  1699. * another grace period is required. Whether we wake the grace-period
  1700. * kthread or it awakens itself for the next round of quiescent-state
  1701. * forcing, that kthread will clean up after the just-completed grace
  1702. * period. Note that the caller must hold rnp->lock, which is released
  1703. * before return.
  1704. */
  1705. static void rcu_report_qs_rsp(unsigned long flags)
  1706. __releases(rcu_get_root()->lock)
  1707. {
  1708. raw_lockdep_assert_held_rcu_node(rcu_get_root());
  1709. WARN_ON_ONCE(!rcu_gp_in_progress());
  1710. WRITE_ONCE(rcu_state.gp_flags,
  1711. READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
  1712. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
  1713. rcu_gp_kthread_wake();
  1714. }
  1715. /*
  1716. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  1717. * Allows quiescent states for a group of CPUs to be reported at one go
  1718. * to the specified rcu_node structure, though all the CPUs in the group
  1719. * must be represented by the same rcu_node structure (which need not be a
  1720. * leaf rcu_node structure, though it often will be). The gps parameter
  1721. * is the grace-period snapshot, which means that the quiescent states
  1722. * are valid only if rnp->gp_seq is equal to gps. That structure's lock
  1723. * must be held upon entry, and it is released before return.
  1724. *
  1725. * As a special case, if mask is zero, the bit-already-cleared check is
  1726. * disabled. This allows propagating quiescent state due to resumed tasks
  1727. * during grace-period initialization.
  1728. */
  1729. static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
  1730. unsigned long gps, unsigned long flags)
  1731. __releases(rnp->lock)
  1732. {
  1733. unsigned long oldmask = 0;
  1734. struct rcu_node *rnp_c;
  1735. raw_lockdep_assert_held_rcu_node(rnp);
  1736. /* Walk up the rcu_node hierarchy. */
  1737. for (;;) {
  1738. if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
  1739. /*
  1740. * Our bit has already been cleared, or the
  1741. * relevant grace period is already over, so done.
  1742. */
  1743. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1744. return;
  1745. }
  1746. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  1747. WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
  1748. rcu_preempt_blocked_readers_cgp(rnp));
  1749. WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask);
  1750. trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
  1751. mask, rnp->qsmask, rnp->level,
  1752. rnp->grplo, rnp->grphi,
  1753. !!rnp->gp_tasks);
  1754. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  1755. /* Other bits still set at this level, so done. */
  1756. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1757. return;
  1758. }
  1759. rnp->completedqs = rnp->gp_seq;
  1760. mask = rnp->grpmask;
  1761. if (rnp->parent == NULL) {
  1762. /* No more levels. Exit loop holding root lock. */
  1763. break;
  1764. }
  1765. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1766. rnp_c = rnp;
  1767. rnp = rnp->parent;
  1768. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1769. oldmask = READ_ONCE(rnp_c->qsmask);
  1770. }
  1771. /*
  1772. * Get here if we are the last CPU to pass through a quiescent
  1773. * state for this grace period. Invoke rcu_report_qs_rsp()
  1774. * to clean up and start the next grace period if one is needed.
  1775. */
  1776. rcu_report_qs_rsp(flags); /* releases rnp->lock. */
  1777. }
  1778. /*
  1779. * Record a quiescent state for all tasks that were previously queued
  1780. * on the specified rcu_node structure and that were blocking the current
  1781. * RCU grace period. The caller must hold the corresponding rnp->lock with
  1782. * irqs disabled, and this lock is released upon return, but irqs remain
  1783. * disabled.
  1784. */
  1785. static void __maybe_unused
  1786. rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  1787. __releases(rnp->lock)
  1788. {
  1789. unsigned long gps;
  1790. unsigned long mask;
  1791. struct rcu_node *rnp_p;
  1792. raw_lockdep_assert_held_rcu_node(rnp);
  1793. if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) ||
  1794. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
  1795. rnp->qsmask != 0) {
  1796. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1797. return; /* Still need more quiescent states! */
  1798. }
  1799. rnp->completedqs = rnp->gp_seq;
  1800. rnp_p = rnp->parent;
  1801. if (rnp_p == NULL) {
  1802. /*
  1803. * Only one rcu_node structure in the tree, so don't
  1804. * try to report up to its nonexistent parent!
  1805. */
  1806. rcu_report_qs_rsp(flags);
  1807. return;
  1808. }
  1809. /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
  1810. gps = rnp->gp_seq;
  1811. mask = rnp->grpmask;
  1812. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1813. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  1814. rcu_report_qs_rnp(mask, rnp_p, gps, flags);
  1815. }
  1816. /*
  1817. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  1818. * structure. This must be called from the specified CPU.
  1819. */
  1820. static void
  1821. rcu_report_qs_rdp(struct rcu_data *rdp)
  1822. {
  1823. unsigned long flags;
  1824. unsigned long mask;
  1825. bool needwake = false;
  1826. bool needacc = false;
  1827. struct rcu_node *rnp;
  1828. WARN_ON_ONCE(rdp->cpu != smp_processor_id());
  1829. rnp = rdp->mynode;
  1830. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1831. if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
  1832. rdp->gpwrap) {
  1833. /*
  1834. * The grace period in which this quiescent state was
  1835. * recorded has ended, so don't report it upwards.
  1836. * We will instead need a new quiescent state that lies
  1837. * within the current grace period.
  1838. */
  1839. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  1840. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1841. return;
  1842. }
  1843. mask = rdp->grpmask;
  1844. rdp->core_needs_qs = false;
  1845. if ((rnp->qsmask & mask) == 0) {
  1846. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1847. } else {
  1848. /*
  1849. * This GP can't end until cpu checks in, so all of our
  1850. * callbacks can be processed during the next GP.
  1851. *
  1852. * NOCB kthreads have their own way to deal with that...
  1853. */
  1854. if (!rcu_rdp_is_offloaded(rdp)) {
  1855. needwake = rcu_accelerate_cbs(rnp, rdp);
  1856. } else if (!rcu_segcblist_completely_offloaded(&rdp->cblist)) {
  1857. /*
  1858. * ...but NOCB kthreads may miss or delay callbacks acceleration
  1859. * if in the middle of a (de-)offloading process.
  1860. */
  1861. needacc = true;
  1862. }
  1863. rcu_disable_urgency_upon_qs(rdp);
  1864. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  1865. /* ^^^ Released rnp->lock */
  1866. if (needwake)
  1867. rcu_gp_kthread_wake();
  1868. if (needacc) {
  1869. rcu_nocb_lock_irqsave(rdp, flags);
  1870. rcu_accelerate_cbs_unlocked(rnp, rdp);
  1871. rcu_nocb_unlock_irqrestore(rdp, flags);
  1872. }
  1873. }
  1874. }
  1875. /*
  1876. * Check to see if there is a new grace period of which this CPU
  1877. * is not yet aware, and if so, set up local rcu_data state for it.
  1878. * Otherwise, see if this CPU has just passed through its first
  1879. * quiescent state for this grace period, and record that fact if so.
  1880. */
  1881. static void
  1882. rcu_check_quiescent_state(struct rcu_data *rdp)
  1883. {
  1884. /* Check for grace-period ends and beginnings. */
  1885. note_gp_changes(rdp);
  1886. /*
  1887. * Does this CPU still need to do its part for current grace period?
  1888. * If no, return and let the other CPUs do their part as well.
  1889. */
  1890. if (!rdp->core_needs_qs)
  1891. return;
  1892. /*
  1893. * Was there a quiescent state since the beginning of the grace
  1894. * period? If no, then exit and wait for the next call.
  1895. */
  1896. if (rdp->cpu_no_qs.b.norm)
  1897. return;
  1898. /*
  1899. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  1900. * judge of that).
  1901. */
  1902. rcu_report_qs_rdp(rdp);
  1903. }
  1904. /*
  1905. * Near the end of the offline process. Trace the fact that this CPU
  1906. * is going offline.
  1907. */
  1908. int rcutree_dying_cpu(unsigned int cpu)
  1909. {
  1910. bool blkd;
  1911. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1912. struct rcu_node *rnp = rdp->mynode;
  1913. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  1914. return 0;
  1915. blkd = !!(rnp->qsmask & rdp->grpmask);
  1916. trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
  1917. blkd ? TPS("cpuofl-bgp") : TPS("cpuofl"));
  1918. return 0;
  1919. }
  1920. /*
  1921. * All CPUs for the specified rcu_node structure have gone offline,
  1922. * and all tasks that were preempted within an RCU read-side critical
  1923. * section while running on one of those CPUs have since exited their RCU
  1924. * read-side critical section. Some other CPU is reporting this fact with
  1925. * the specified rcu_node structure's ->lock held and interrupts disabled.
  1926. * This function therefore goes up the tree of rcu_node structures,
  1927. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  1928. * the leaf rcu_node structure's ->qsmaskinit field has already been
  1929. * updated.
  1930. *
  1931. * This function does check that the specified rcu_node structure has
  1932. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  1933. * prematurely. That said, invoking it after the fact will cost you
  1934. * a needless lock acquisition. So once it has done its work, don't
  1935. * invoke it again.
  1936. */
  1937. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  1938. {
  1939. long mask;
  1940. struct rcu_node *rnp = rnp_leaf;
  1941. raw_lockdep_assert_held_rcu_node(rnp_leaf);
  1942. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  1943. WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
  1944. WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
  1945. return;
  1946. for (;;) {
  1947. mask = rnp->grpmask;
  1948. rnp = rnp->parent;
  1949. if (!rnp)
  1950. break;
  1951. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1952. rnp->qsmaskinit &= ~mask;
  1953. /* Between grace periods, so better already be zero! */
  1954. WARN_ON_ONCE(rnp->qsmask);
  1955. if (rnp->qsmaskinit) {
  1956. raw_spin_unlock_rcu_node(rnp);
  1957. /* irqs remain disabled. */
  1958. return;
  1959. }
  1960. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1961. }
  1962. }
  1963. /*
  1964. * The CPU has been completely removed, and some other CPU is reporting
  1965. * this fact from process context. Do the remainder of the cleanup.
  1966. * There can only be one CPU hotplug operation at a time, so no need for
  1967. * explicit locking.
  1968. */
  1969. int rcutree_dead_cpu(unsigned int cpu)
  1970. {
  1971. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  1972. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  1973. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  1974. return 0;
  1975. WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus - 1);
  1976. /* Adjust any no-longer-needed kthreads. */
  1977. rcu_boost_kthread_setaffinity(rnp, -1);
  1978. // Stop-machine done, so allow nohz_full to disable tick.
  1979. tick_dep_clear(TICK_DEP_BIT_RCU);
  1980. return 0;
  1981. }
  1982. /*
  1983. * Invoke any RCU callbacks that have made it to the end of their grace
  1984. * period. Throttle as specified by rdp->blimit.
  1985. */
  1986. static void rcu_do_batch(struct rcu_data *rdp)
  1987. {
  1988. int div;
  1989. bool __maybe_unused empty;
  1990. unsigned long flags;
  1991. struct rcu_head *rhp;
  1992. struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
  1993. long bl, count = 0;
  1994. long pending, tlimit = 0;
  1995. /* If no callbacks are ready, just return. */
  1996. if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
  1997. trace_rcu_batch_start(rcu_state.name,
  1998. rcu_segcblist_n_cbs(&rdp->cblist), 0);
  1999. trace_rcu_batch_end(rcu_state.name, 0,
  2000. !rcu_segcblist_empty(&rdp->cblist),
  2001. need_resched(), is_idle_task(current),
  2002. rcu_is_callbacks_kthread(rdp));
  2003. return;
  2004. }
  2005. /*
  2006. * Extract the list of ready callbacks, disabling IRQs to prevent
  2007. * races with call_rcu() from interrupt handlers. Leave the
  2008. * callback counts, as rcu_barrier() needs to be conservative.
  2009. */
  2010. rcu_nocb_lock_irqsave(rdp, flags);
  2011. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2012. pending = rcu_segcblist_n_cbs(&rdp->cblist);
  2013. div = READ_ONCE(rcu_divisor);
  2014. div = div < 0 ? 7 : div > sizeof(long) * 8 - 2 ? sizeof(long) * 8 - 2 : div;
  2015. bl = max(rdp->blimit, pending >> div);
  2016. if (in_serving_softirq() && unlikely(bl > 100)) {
  2017. long rrn = READ_ONCE(rcu_resched_ns);
  2018. rrn = rrn < NSEC_PER_MSEC ? NSEC_PER_MSEC : rrn > NSEC_PER_SEC ? NSEC_PER_SEC : rrn;
  2019. tlimit = local_clock() + rrn;
  2020. }
  2021. trace_rcu_batch_start(rcu_state.name,
  2022. rcu_segcblist_n_cbs(&rdp->cblist), bl);
  2023. rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  2024. if (rcu_rdp_is_offloaded(rdp))
  2025. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2026. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
  2027. rcu_nocb_unlock_irqrestore(rdp, flags);
  2028. /* Invoke callbacks. */
  2029. tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  2030. rhp = rcu_cblist_dequeue(&rcl);
  2031. for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  2032. rcu_callback_t f;
  2033. count++;
  2034. debug_rcu_head_unqueue(rhp);
  2035. rcu_lock_acquire(&rcu_callback_map);
  2036. trace_rcu_invoke_callback(rcu_state.name, rhp);
  2037. f = rhp->func;
  2038. WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
  2039. f(rhp);
  2040. rcu_lock_release(&rcu_callback_map);
  2041. /*
  2042. * Stop only if limit reached and CPU has something to do.
  2043. */
  2044. if (in_serving_softirq()) {
  2045. if (count >= bl && (need_resched() || !is_idle_task(current)))
  2046. break;
  2047. /*
  2048. * Make sure we don't spend too much time here and deprive other
  2049. * softirq vectors of CPU cycles.
  2050. */
  2051. if (unlikely(tlimit)) {
  2052. /* only call local_clock() every 32 callbacks */
  2053. if (likely((count & 31) || local_clock() < tlimit))
  2054. continue;
  2055. /* Exceeded the time limit, so leave. */
  2056. break;
  2057. }
  2058. } else {
  2059. local_bh_enable();
  2060. lockdep_assert_irqs_enabled();
  2061. cond_resched_tasks_rcu_qs();
  2062. lockdep_assert_irqs_enabled();
  2063. local_bh_disable();
  2064. }
  2065. }
  2066. rcu_nocb_lock_irqsave(rdp, flags);
  2067. rdp->n_cbs_invoked += count;
  2068. trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
  2069. is_idle_task(current), rcu_is_callbacks_kthread(rdp));
  2070. /* Update counts and requeue any remaining callbacks. */
  2071. rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
  2072. rcu_segcblist_add_len(&rdp->cblist, -count);
  2073. /* Reinstate batch limit if we have worked down the excess. */
  2074. count = rcu_segcblist_n_cbs(&rdp->cblist);
  2075. if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
  2076. rdp->blimit = blimit;
  2077. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2078. if (count == 0 && rdp->qlen_last_fqs_check != 0) {
  2079. rdp->qlen_last_fqs_check = 0;
  2080. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  2081. } else if (count < rdp->qlen_last_fqs_check - qhimark)
  2082. rdp->qlen_last_fqs_check = count;
  2083. /*
  2084. * The following usually indicates a double call_rcu(). To track
  2085. * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
  2086. */
  2087. empty = rcu_segcblist_empty(&rdp->cblist);
  2088. WARN_ON_ONCE(count == 0 && !empty);
  2089. WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
  2090. count != 0 && empty);
  2091. WARN_ON_ONCE(count == 0 && rcu_segcblist_n_segment_cbs(&rdp->cblist) != 0);
  2092. WARN_ON_ONCE(!empty && rcu_segcblist_n_segment_cbs(&rdp->cblist) == 0);
  2093. rcu_nocb_unlock_irqrestore(rdp, flags);
  2094. tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
  2095. }
  2096. /*
  2097. * This function is invoked from each scheduling-clock interrupt,
  2098. * and checks to see if this CPU is in a non-context-switch quiescent
  2099. * state, for example, user mode or idle loop. It also schedules RCU
  2100. * core processing. If the current grace period has gone on too long,
  2101. * it will ask the scheduler to manufacture a context switch for the sole
  2102. * purpose of providing the needed quiescent state.
  2103. */
  2104. void rcu_sched_clock_irq(int user)
  2105. {
  2106. unsigned long j;
  2107. if (IS_ENABLED(CONFIG_PROVE_RCU)) {
  2108. j = jiffies;
  2109. WARN_ON_ONCE(time_before(j, __this_cpu_read(rcu_data.last_sched_clock)));
  2110. __this_cpu_write(rcu_data.last_sched_clock, j);
  2111. }
  2112. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2113. lockdep_assert_irqs_disabled();
  2114. raw_cpu_inc(rcu_data.ticks_this_gp);
  2115. /* The load-acquire pairs with the store-release setting to true. */
  2116. if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
  2117. /* Idle and userspace execution already are quiescent states. */
  2118. if (!rcu_is_cpu_rrupt_from_idle() && !user) {
  2119. set_tsk_need_resched(current);
  2120. set_preempt_need_resched();
  2121. }
  2122. __this_cpu_write(rcu_data.rcu_urgent_qs, false);
  2123. }
  2124. rcu_flavor_sched_clock_irq(user);
  2125. if (rcu_pending(user))
  2126. invoke_rcu_core();
  2127. if (user || rcu_is_cpu_rrupt_from_idle())
  2128. rcu_note_voluntary_context_switch(current);
  2129. lockdep_assert_irqs_disabled();
  2130. trace_rcu_utilization(TPS("End scheduler-tick"));
  2131. }
  2132. /*
  2133. * Scan the leaf rcu_node structures. For each structure on which all
  2134. * CPUs have reported a quiescent state and on which there are tasks
  2135. * blocking the current grace period, initiate RCU priority boosting.
  2136. * Otherwise, invoke the specified function to check dyntick state for
  2137. * each CPU that has not yet reported a quiescent state.
  2138. */
  2139. static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
  2140. {
  2141. int cpu;
  2142. unsigned long flags;
  2143. unsigned long mask;
  2144. struct rcu_data *rdp;
  2145. struct rcu_node *rnp;
  2146. rcu_state.cbovld = rcu_state.cbovldnext;
  2147. rcu_state.cbovldnext = false;
  2148. rcu_for_each_leaf_node(rnp) {
  2149. cond_resched_tasks_rcu_qs();
  2150. mask = 0;
  2151. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2152. rcu_state.cbovldnext |= !!rnp->cbovldmask;
  2153. if (rnp->qsmask == 0) {
  2154. if (rcu_preempt_blocked_readers_cgp(rnp)) {
  2155. /*
  2156. * No point in scanning bits because they
  2157. * are all zero. But we might need to
  2158. * priority-boost blocked readers.
  2159. */
  2160. rcu_initiate_boost(rnp, flags);
  2161. /* rcu_initiate_boost() releases rnp->lock */
  2162. continue;
  2163. }
  2164. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2165. continue;
  2166. }
  2167. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) {
  2168. rdp = per_cpu_ptr(&rcu_data, cpu);
  2169. if (f(rdp)) {
  2170. mask |= rdp->grpmask;
  2171. rcu_disable_urgency_upon_qs(rdp);
  2172. }
  2173. }
  2174. if (mask != 0) {
  2175. /* Idle/offline CPUs, report (releases rnp->lock). */
  2176. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  2177. } else {
  2178. /* Nothing to do here, so just drop the lock. */
  2179. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2180. }
  2181. }
  2182. }
  2183. /*
  2184. * Force quiescent states on reluctant CPUs, and also detect which
  2185. * CPUs are in dyntick-idle mode.
  2186. */
  2187. void rcu_force_quiescent_state(void)
  2188. {
  2189. unsigned long flags;
  2190. bool ret;
  2191. struct rcu_node *rnp;
  2192. struct rcu_node *rnp_old = NULL;
  2193. /* Funnel through hierarchy to reduce memory contention. */
  2194. rnp = raw_cpu_read(rcu_data.mynode);
  2195. for (; rnp != NULL; rnp = rnp->parent) {
  2196. ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
  2197. !raw_spin_trylock(&rnp->fqslock);
  2198. if (rnp_old != NULL)
  2199. raw_spin_unlock(&rnp_old->fqslock);
  2200. if (ret)
  2201. return;
  2202. rnp_old = rnp;
  2203. }
  2204. /* rnp_old == rcu_get_root(), rnp == NULL. */
  2205. /* Reached the root of the rcu_node tree, acquire lock. */
  2206. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2207. raw_spin_unlock(&rnp_old->fqslock);
  2208. if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
  2209. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2210. return; /* Someone beat us to it. */
  2211. }
  2212. WRITE_ONCE(rcu_state.gp_flags,
  2213. READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
  2214. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2215. rcu_gp_kthread_wake();
  2216. }
  2217. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  2218. // Workqueue handler for an RCU reader for kernels enforcing struct RCU
  2219. // grace periods.
  2220. static void strict_work_handler(struct work_struct *work)
  2221. {
  2222. rcu_read_lock();
  2223. rcu_read_unlock();
  2224. }
  2225. /* Perform RCU core processing work for the current CPU. */
  2226. static __latent_entropy void rcu_core(void)
  2227. {
  2228. unsigned long flags;
  2229. struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
  2230. struct rcu_node *rnp = rdp->mynode;
  2231. /*
  2232. * On RT rcu_core() can be preempted when IRQs aren't disabled.
  2233. * Therefore this function can race with concurrent NOCB (de-)offloading
  2234. * on this CPU and the below condition must be considered volatile.
  2235. * However if we race with:
  2236. *
  2237. * _ Offloading: In the worst case we accelerate or process callbacks
  2238. * concurrently with NOCB kthreads. We are guaranteed to
  2239. * call rcu_nocb_lock() if that happens.
  2240. *
  2241. * _ Deoffloading: In the worst case we miss callbacks acceleration or
  2242. * processing. This is fine because the early stage
  2243. * of deoffloading invokes rcu_core() after setting
  2244. * SEGCBLIST_RCU_CORE. So we guarantee that we'll process
  2245. * what could have been dismissed without the need to wait
  2246. * for the next rcu_pending() check in the next jiffy.
  2247. */
  2248. const bool do_batch = !rcu_segcblist_completely_offloaded(&rdp->cblist);
  2249. if (cpu_is_offline(smp_processor_id()))
  2250. return;
  2251. trace_rcu_utilization(TPS("Start RCU core"));
  2252. WARN_ON_ONCE(!rdp->beenonline);
  2253. /* Report any deferred quiescent states if preemption enabled. */
  2254. if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && (!(preempt_count() & PREEMPT_MASK))) {
  2255. rcu_preempt_deferred_qs(current);
  2256. } else if (rcu_preempt_need_deferred_qs(current)) {
  2257. set_tsk_need_resched(current);
  2258. set_preempt_need_resched();
  2259. }
  2260. /* Update RCU state based on any recent quiescent states. */
  2261. rcu_check_quiescent_state(rdp);
  2262. /* No grace period and unregistered callbacks? */
  2263. if (!rcu_gp_in_progress() &&
  2264. rcu_segcblist_is_enabled(&rdp->cblist) && do_batch) {
  2265. rcu_nocb_lock_irqsave(rdp, flags);
  2266. if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  2267. rcu_accelerate_cbs_unlocked(rnp, rdp);
  2268. rcu_nocb_unlock_irqrestore(rdp, flags);
  2269. }
  2270. rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
  2271. /* If there are callbacks ready, invoke them. */
  2272. if (do_batch && rcu_segcblist_ready_cbs(&rdp->cblist) &&
  2273. likely(READ_ONCE(rcu_scheduler_fully_active))) {
  2274. rcu_do_batch(rdp);
  2275. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2276. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2277. invoke_rcu_core();
  2278. }
  2279. /* Do any needed deferred wakeups of rcuo kthreads. */
  2280. do_nocb_deferred_wakeup(rdp);
  2281. trace_rcu_utilization(TPS("End RCU core"));
  2282. // If strict GPs, schedule an RCU reader in a clean environment.
  2283. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  2284. queue_work_on(rdp->cpu, rcu_gp_wq, &rdp->strict_work);
  2285. }
  2286. static void rcu_core_si(struct softirq_action *h)
  2287. {
  2288. rcu_core();
  2289. }
  2290. static void rcu_wake_cond(struct task_struct *t, int status)
  2291. {
  2292. /*
  2293. * If the thread is yielding, only wake it when this
  2294. * is invoked from idle
  2295. */
  2296. if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  2297. wake_up_process(t);
  2298. }
  2299. static void invoke_rcu_core_kthread(void)
  2300. {
  2301. struct task_struct *t;
  2302. unsigned long flags;
  2303. local_irq_save(flags);
  2304. __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
  2305. t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
  2306. if (t != NULL && t != current)
  2307. rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
  2308. local_irq_restore(flags);
  2309. }
  2310. /*
  2311. * Wake up this CPU's rcuc kthread to do RCU core processing.
  2312. */
  2313. static void invoke_rcu_core(void)
  2314. {
  2315. if (!cpu_online(smp_processor_id()))
  2316. return;
  2317. if (use_softirq)
  2318. raise_softirq(RCU_SOFTIRQ);
  2319. else
  2320. invoke_rcu_core_kthread();
  2321. }
  2322. static void rcu_cpu_kthread_park(unsigned int cpu)
  2323. {
  2324. per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  2325. }
  2326. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  2327. {
  2328. return __this_cpu_read(rcu_data.rcu_cpu_has_work);
  2329. }
  2330. /*
  2331. * Per-CPU kernel thread that invokes RCU callbacks. This replaces
  2332. * the RCU softirq used in configurations of RCU that do not support RCU
  2333. * priority boosting.
  2334. */
  2335. static void rcu_cpu_kthread(unsigned int cpu)
  2336. {
  2337. unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
  2338. char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
  2339. unsigned long *j = this_cpu_ptr(&rcu_data.rcuc_activity);
  2340. int spincnt;
  2341. trace_rcu_utilization(TPS("Start CPU kthread@rcu_run"));
  2342. for (spincnt = 0; spincnt < 10; spincnt++) {
  2343. WRITE_ONCE(*j, jiffies);
  2344. local_bh_disable();
  2345. *statusp = RCU_KTHREAD_RUNNING;
  2346. local_irq_disable();
  2347. work = *workp;
  2348. *workp = 0;
  2349. local_irq_enable();
  2350. if (work)
  2351. rcu_core();
  2352. local_bh_enable();
  2353. if (*workp == 0) {
  2354. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  2355. *statusp = RCU_KTHREAD_WAITING;
  2356. return;
  2357. }
  2358. }
  2359. *statusp = RCU_KTHREAD_YIELDING;
  2360. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  2361. schedule_timeout_idle(2);
  2362. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  2363. *statusp = RCU_KTHREAD_WAITING;
  2364. WRITE_ONCE(*j, jiffies);
  2365. }
  2366. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  2367. .store = &rcu_data.rcu_cpu_kthread_task,
  2368. .thread_should_run = rcu_cpu_kthread_should_run,
  2369. .thread_fn = rcu_cpu_kthread,
  2370. .thread_comm = "rcuc/%u",
  2371. .setup = rcu_cpu_kthread_setup,
  2372. .park = rcu_cpu_kthread_park,
  2373. };
  2374. /*
  2375. * Spawn per-CPU RCU core processing kthreads.
  2376. */
  2377. static int __init rcu_spawn_core_kthreads(void)
  2378. {
  2379. int cpu;
  2380. for_each_possible_cpu(cpu)
  2381. per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
  2382. if (use_softirq)
  2383. return 0;
  2384. WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
  2385. "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
  2386. return 0;
  2387. }
  2388. /*
  2389. * Handle any core-RCU processing required by a call_rcu() invocation.
  2390. */
  2391. static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
  2392. unsigned long flags)
  2393. {
  2394. /*
  2395. * If called from an extended quiescent state, invoke the RCU
  2396. * core in order to force a re-evaluation of RCU's idleness.
  2397. */
  2398. if (!rcu_is_watching())
  2399. invoke_rcu_core();
  2400. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2401. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2402. return;
  2403. /*
  2404. * Force the grace period if too many callbacks or too long waiting.
  2405. * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
  2406. * if some other CPU has recently done so. Also, don't bother
  2407. * invoking rcu_force_quiescent_state() if the newly enqueued callback
  2408. * is the only one waiting for a grace period to complete.
  2409. */
  2410. if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
  2411. rdp->qlen_last_fqs_check + qhimark)) {
  2412. /* Are we ignoring a completed grace period? */
  2413. note_gp_changes(rdp);
  2414. /* Start a new grace period if one not already started. */
  2415. if (!rcu_gp_in_progress()) {
  2416. rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
  2417. } else {
  2418. /* Give the grace period a kick. */
  2419. rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
  2420. if (READ_ONCE(rcu_state.n_force_qs) == rdp->n_force_qs_snap &&
  2421. rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
  2422. rcu_force_quiescent_state();
  2423. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  2424. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2425. }
  2426. }
  2427. }
  2428. /*
  2429. * RCU callback function to leak a callback.
  2430. */
  2431. static void rcu_leak_callback(struct rcu_head *rhp)
  2432. {
  2433. }
  2434. /*
  2435. * Check and if necessary update the leaf rcu_node structure's
  2436. * ->cbovldmask bit corresponding to the current CPU based on that CPU's
  2437. * number of queued RCU callbacks. The caller must hold the leaf rcu_node
  2438. * structure's ->lock.
  2439. */
  2440. static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp)
  2441. {
  2442. raw_lockdep_assert_held_rcu_node(rnp);
  2443. if (qovld_calc <= 0)
  2444. return; // Early boot and wildcard value set.
  2445. if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc)
  2446. WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask);
  2447. else
  2448. WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask);
  2449. }
  2450. /*
  2451. * Check and if necessary update the leaf rcu_node structure's
  2452. * ->cbovldmask bit corresponding to the current CPU based on that CPU's
  2453. * number of queued RCU callbacks. No locks need be held, but the
  2454. * caller must have disabled interrupts.
  2455. *
  2456. * Note that this function ignores the possibility that there are a lot
  2457. * of callbacks all of which have already seen the end of their respective
  2458. * grace periods. This omission is due to the need for no-CBs CPUs to
  2459. * be holding ->nocb_lock to do this check, which is too heavy for a
  2460. * common-case operation.
  2461. */
  2462. static void check_cb_ovld(struct rcu_data *rdp)
  2463. {
  2464. struct rcu_node *const rnp = rdp->mynode;
  2465. if (qovld_calc <= 0 ||
  2466. ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) ==
  2467. !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask)))
  2468. return; // Early boot wildcard value or already set correctly.
  2469. raw_spin_lock_rcu_node(rnp);
  2470. check_cb_ovld_locked(rdp, rnp);
  2471. raw_spin_unlock_rcu_node(rnp);
  2472. }
  2473. static void
  2474. __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy_in)
  2475. {
  2476. static atomic_t doublefrees;
  2477. unsigned long flags;
  2478. bool lazy;
  2479. struct rcu_data *rdp;
  2480. bool was_alldone;
  2481. /* Misaligned rcu_head! */
  2482. WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
  2483. if (debug_rcu_head_queue(head)) {
  2484. /*
  2485. * Probable double call_rcu(), so leak the callback.
  2486. * Use rcu:rcu_callback trace event to find the previous
  2487. * time callback was passed to call_rcu().
  2488. */
  2489. if (atomic_inc_return(&doublefrees) < 4) {
  2490. pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func);
  2491. mem_dump_obj(head);
  2492. }
  2493. WRITE_ONCE(head->func, rcu_leak_callback);
  2494. return;
  2495. }
  2496. head->func = func;
  2497. head->next = NULL;
  2498. kasan_record_aux_stack_noalloc(head);
  2499. local_irq_save(flags);
  2500. rdp = this_cpu_ptr(&rcu_data);
  2501. lazy = lazy_in && !rcu_async_should_hurry();
  2502. /* Add the callback to our list. */
  2503. if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
  2504. // This can trigger due to call_rcu() from offline CPU:
  2505. WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
  2506. WARN_ON_ONCE(!rcu_is_watching());
  2507. // Very early boot, before rcu_init(). Initialize if needed
  2508. // and then drop through to queue the callback.
  2509. if (rcu_segcblist_empty(&rdp->cblist))
  2510. rcu_segcblist_init(&rdp->cblist);
  2511. }
  2512. check_cb_ovld(rdp);
  2513. if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags, lazy))
  2514. return; // Enqueued onto ->nocb_bypass, so just leave.
  2515. // If no-CBs CPU gets here, rcu_nocb_try_bypass() acquired ->nocb_lock.
  2516. rcu_segcblist_enqueue(&rdp->cblist, head);
  2517. if (__is_kvfree_rcu_offset((unsigned long)func))
  2518. trace_rcu_kvfree_callback(rcu_state.name, head,
  2519. (unsigned long)func,
  2520. rcu_segcblist_n_cbs(&rdp->cblist));
  2521. else
  2522. trace_rcu_callback(rcu_state.name, head,
  2523. rcu_segcblist_n_cbs(&rdp->cblist));
  2524. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
  2525. /* Go handle any RCU core processing required. */
  2526. if (unlikely(rcu_rdp_is_offloaded(rdp))) {
  2527. __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
  2528. } else {
  2529. __call_rcu_core(rdp, head, flags);
  2530. local_irq_restore(flags);
  2531. }
  2532. }
  2533. #ifdef CONFIG_RCU_LAZY
  2534. static bool enable_rcu_lazy __read_mostly = !IS_ENABLED(CONFIG_RCU_LAZY_DEFAULT_OFF);
  2535. module_param(enable_rcu_lazy, bool, 0444);
  2536. /**
  2537. * call_rcu_hurry() - Queue RCU callback for invocation after grace period, and
  2538. * flush all lazy callbacks (including the new one) to the main ->cblist while
  2539. * doing so.
  2540. *
  2541. * @head: structure to be used for queueing the RCU updates.
  2542. * @func: actual callback function to be invoked after the grace period
  2543. *
  2544. * The callback function will be invoked some time after a full grace
  2545. * period elapses, in other words after all pre-existing RCU read-side
  2546. * critical sections have completed.
  2547. *
  2548. * Use this API instead of call_rcu() if you don't want the callback to be
  2549. * invoked after very long periods of time, which can happen on systems without
  2550. * memory pressure and on systems which are lightly loaded or mostly idle.
  2551. * This function will cause callbacks to be invoked sooner than later at the
  2552. * expense of extra power. Other than that, this function is identical to, and
  2553. * reuses call_rcu()'s logic. Refer to call_rcu() for more details about memory
  2554. * ordering and other functionality.
  2555. */
  2556. void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
  2557. {
  2558. return __call_rcu_common(head, func, false);
  2559. }
  2560. EXPORT_SYMBOL_GPL(call_rcu_hurry);
  2561. #else
  2562. #define enable_rcu_lazy false
  2563. #endif
  2564. /**
  2565. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  2566. * By default the callbacks are 'lazy' and are kept hidden from the main
  2567. * ->cblist to prevent starting of grace periods too soon.
  2568. * If you desire grace periods to start very soon, use call_rcu_hurry().
  2569. *
  2570. * @head: structure to be used for queueing the RCU updates.
  2571. * @func: actual callback function to be invoked after the grace period
  2572. *
  2573. * The callback function will be invoked some time after a full grace
  2574. * period elapses, in other words after all pre-existing RCU read-side
  2575. * critical sections have completed. However, the callback function
  2576. * might well execute concurrently with RCU read-side critical sections
  2577. * that started after call_rcu() was invoked.
  2578. *
  2579. * RCU read-side critical sections are delimited by rcu_read_lock()
  2580. * and rcu_read_unlock(), and may be nested. In addition, but only in
  2581. * v5.0 and later, regions of code across which interrupts, preemption,
  2582. * or softirqs have been disabled also serve as RCU read-side critical
  2583. * sections. This includes hardware interrupt handlers, softirq handlers,
  2584. * and NMI handlers.
  2585. *
  2586. * Note that all CPUs must agree that the grace period extended beyond
  2587. * all pre-existing RCU read-side critical section. On systems with more
  2588. * than one CPU, this means that when "func()" is invoked, each CPU is
  2589. * guaranteed to have executed a full memory barrier since the end of its
  2590. * last RCU read-side critical section whose beginning preceded the call
  2591. * to call_rcu(). It also means that each CPU executing an RCU read-side
  2592. * critical section that continues beyond the start of "func()" must have
  2593. * executed a memory barrier after the call_rcu() but before the beginning
  2594. * of that RCU read-side critical section. Note that these guarantees
  2595. * include CPUs that are offline, idle, or executing in user mode, as
  2596. * well as CPUs that are executing in the kernel.
  2597. *
  2598. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  2599. * resulting RCU callback function "func()", then both CPU A and CPU B are
  2600. * guaranteed to execute a full memory barrier during the time interval
  2601. * between the call to call_rcu() and the invocation of "func()" -- even
  2602. * if CPU A and CPU B are the same CPU (but again only if the system has
  2603. * more than one CPU).
  2604. *
  2605. * Implementation of these memory-ordering guarantees is described here:
  2606. * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst.
  2607. */
  2608. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  2609. {
  2610. __call_rcu_common(head, func, enable_rcu_lazy);
  2611. }
  2612. EXPORT_SYMBOL_GPL(call_rcu);
  2613. /* Maximum number of jiffies to wait before draining a batch. */
  2614. #define KFREE_DRAIN_JIFFIES (5 * HZ)
  2615. #define KFREE_N_BATCHES 2
  2616. #define FREE_N_CHANNELS 2
  2617. /**
  2618. * struct kvfree_rcu_bulk_data - single block to store kvfree_rcu() pointers
  2619. * @nr_records: Number of active pointers in the array
  2620. * @next: Next bulk object in the block chain
  2621. * @records: Array of the kvfree_rcu() pointers
  2622. */
  2623. struct kvfree_rcu_bulk_data {
  2624. unsigned long nr_records;
  2625. struct kvfree_rcu_bulk_data *next;
  2626. void *records[];
  2627. };
  2628. /*
  2629. * This macro defines how many entries the "records" array
  2630. * will contain. It is based on the fact that the size of
  2631. * kvfree_rcu_bulk_data structure becomes exactly one page.
  2632. */
  2633. #define KVFREE_BULK_MAX_ENTR \
  2634. ((PAGE_SIZE - sizeof(struct kvfree_rcu_bulk_data)) / sizeof(void *))
  2635. /**
  2636. * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests
  2637. * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period
  2638. * @head_free: List of kfree_rcu() objects waiting for a grace period
  2639. * @bkvhead_free: Bulk-List of kvfree_rcu() objects waiting for a grace period
  2640. * @krcp: Pointer to @kfree_rcu_cpu structure
  2641. */
  2642. struct kfree_rcu_cpu_work {
  2643. struct rcu_work rcu_work;
  2644. struct rcu_head *head_free;
  2645. struct kvfree_rcu_bulk_data *bkvhead_free[FREE_N_CHANNELS];
  2646. struct kfree_rcu_cpu *krcp;
  2647. };
  2648. /**
  2649. * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
  2650. * @head: List of kfree_rcu() objects not yet waiting for a grace period
  2651. * @bkvhead: Bulk-List of kvfree_rcu() objects not yet waiting for a grace period
  2652. * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
  2653. * @lock: Synchronize access to this structure
  2654. * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
  2655. * @initialized: The @rcu_work fields have been initialized
  2656. * @count: Number of objects for which GP not started
  2657. * @bkvcache:
  2658. * A simple cache list that contains objects for reuse purpose.
  2659. * In order to save some per-cpu space the list is singular.
  2660. * Even though it is lockless an access has to be protected by the
  2661. * per-cpu lock.
  2662. * @page_cache_work: A work to refill the cache when it is empty
  2663. * @backoff_page_cache_fill: Delay cache refills
  2664. * @work_in_progress: Indicates that page_cache_work is running
  2665. * @hrtimer: A hrtimer for scheduling a page_cache_work
  2666. * @nr_bkv_objs: number of allocated objects at @bkvcache.
  2667. *
  2668. * This is a per-CPU structure. The reason that it is not included in
  2669. * the rcu_data structure is to permit this code to be extracted from
  2670. * the RCU files. Such extraction could allow further optimization of
  2671. * the interactions with the slab allocators.
  2672. */
  2673. struct kfree_rcu_cpu {
  2674. struct rcu_head *head;
  2675. struct kvfree_rcu_bulk_data *bkvhead[FREE_N_CHANNELS];
  2676. struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
  2677. raw_spinlock_t lock;
  2678. struct delayed_work monitor_work;
  2679. bool initialized;
  2680. int count;
  2681. struct delayed_work page_cache_work;
  2682. atomic_t backoff_page_cache_fill;
  2683. atomic_t work_in_progress;
  2684. struct hrtimer hrtimer;
  2685. struct llist_head bkvcache;
  2686. int nr_bkv_objs;
  2687. };
  2688. static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc) = {
  2689. .lock = __RAW_SPIN_LOCK_UNLOCKED(krc.lock),
  2690. };
  2691. static __always_inline void
  2692. debug_rcu_bhead_unqueue(struct kvfree_rcu_bulk_data *bhead)
  2693. {
  2694. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  2695. int i;
  2696. for (i = 0; i < bhead->nr_records; i++)
  2697. debug_rcu_head_unqueue((struct rcu_head *)(bhead->records[i]));
  2698. #endif
  2699. }
  2700. static inline struct kfree_rcu_cpu *
  2701. krc_this_cpu_lock(unsigned long *flags)
  2702. {
  2703. struct kfree_rcu_cpu *krcp;
  2704. local_irq_save(*flags); // For safely calling this_cpu_ptr().
  2705. krcp = this_cpu_ptr(&krc);
  2706. raw_spin_lock(&krcp->lock);
  2707. return krcp;
  2708. }
  2709. static inline void
  2710. krc_this_cpu_unlock(struct kfree_rcu_cpu *krcp, unsigned long flags)
  2711. {
  2712. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2713. }
  2714. static inline struct kvfree_rcu_bulk_data *
  2715. get_cached_bnode(struct kfree_rcu_cpu *krcp)
  2716. {
  2717. if (!krcp->nr_bkv_objs)
  2718. return NULL;
  2719. WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs - 1);
  2720. return (struct kvfree_rcu_bulk_data *)
  2721. llist_del_first(&krcp->bkvcache);
  2722. }
  2723. static inline bool
  2724. put_cached_bnode(struct kfree_rcu_cpu *krcp,
  2725. struct kvfree_rcu_bulk_data *bnode)
  2726. {
  2727. // Check the limit.
  2728. if (krcp->nr_bkv_objs >= rcu_min_cached_objs)
  2729. return false;
  2730. llist_add((struct llist_node *) bnode, &krcp->bkvcache);
  2731. WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs + 1);
  2732. return true;
  2733. }
  2734. static int
  2735. drain_page_cache(struct kfree_rcu_cpu *krcp)
  2736. {
  2737. unsigned long flags;
  2738. struct llist_node *page_list, *pos, *n;
  2739. int freed = 0;
  2740. raw_spin_lock_irqsave(&krcp->lock, flags);
  2741. page_list = llist_del_all(&krcp->bkvcache);
  2742. WRITE_ONCE(krcp->nr_bkv_objs, 0);
  2743. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2744. llist_for_each_safe(pos, n, page_list) {
  2745. free_page((unsigned long)pos);
  2746. freed++;
  2747. }
  2748. return freed;
  2749. }
  2750. /*
  2751. * This function is invoked in workqueue context after a grace period.
  2752. * It frees all the objects queued on ->bkvhead_free or ->head_free.
  2753. */
  2754. static void kfree_rcu_work(struct work_struct *work)
  2755. {
  2756. unsigned long flags;
  2757. struct kvfree_rcu_bulk_data *bkvhead[FREE_N_CHANNELS], *bnext;
  2758. struct rcu_head *head, *next;
  2759. struct kfree_rcu_cpu *krcp;
  2760. struct kfree_rcu_cpu_work *krwp;
  2761. int i, j;
  2762. krwp = container_of(to_rcu_work(work),
  2763. struct kfree_rcu_cpu_work, rcu_work);
  2764. krcp = krwp->krcp;
  2765. raw_spin_lock_irqsave(&krcp->lock, flags);
  2766. // Channels 1 and 2.
  2767. for (i = 0; i < FREE_N_CHANNELS; i++) {
  2768. bkvhead[i] = krwp->bkvhead_free[i];
  2769. krwp->bkvhead_free[i] = NULL;
  2770. }
  2771. // Channel 3.
  2772. head = krwp->head_free;
  2773. krwp->head_free = NULL;
  2774. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2775. // Handle the first two channels.
  2776. for (i = 0; i < FREE_N_CHANNELS; i++) {
  2777. for (; bkvhead[i]; bkvhead[i] = bnext) {
  2778. bnext = bkvhead[i]->next;
  2779. debug_rcu_bhead_unqueue(bkvhead[i]);
  2780. rcu_lock_acquire(&rcu_callback_map);
  2781. if (i == 0) { // kmalloc() / kfree().
  2782. trace_rcu_invoke_kfree_bulk_callback(
  2783. rcu_state.name, bkvhead[i]->nr_records,
  2784. bkvhead[i]->records);
  2785. kfree_bulk(bkvhead[i]->nr_records,
  2786. bkvhead[i]->records);
  2787. } else { // vmalloc() / vfree().
  2788. for (j = 0; j < bkvhead[i]->nr_records; j++) {
  2789. trace_rcu_invoke_kvfree_callback(
  2790. rcu_state.name,
  2791. bkvhead[i]->records[j], 0);
  2792. vfree(bkvhead[i]->records[j]);
  2793. }
  2794. }
  2795. rcu_lock_release(&rcu_callback_map);
  2796. raw_spin_lock_irqsave(&krcp->lock, flags);
  2797. if (put_cached_bnode(krcp, bkvhead[i]))
  2798. bkvhead[i] = NULL;
  2799. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2800. if (bkvhead[i])
  2801. free_page((unsigned long) bkvhead[i]);
  2802. cond_resched_tasks_rcu_qs();
  2803. }
  2804. }
  2805. /*
  2806. * This is used when the "bulk" path can not be used for the
  2807. * double-argument of kvfree_rcu(). This happens when the
  2808. * page-cache is empty, which means that objects are instead
  2809. * queued on a linked list through their rcu_head structures.
  2810. * This list is named "Channel 3".
  2811. */
  2812. for (; head; head = next) {
  2813. unsigned long offset = (unsigned long)head->func;
  2814. void *ptr = (void *)head - offset;
  2815. next = head->next;
  2816. debug_rcu_head_unqueue((struct rcu_head *)ptr);
  2817. rcu_lock_acquire(&rcu_callback_map);
  2818. trace_rcu_invoke_kvfree_callback(rcu_state.name, head, offset);
  2819. if (!WARN_ON_ONCE(!__is_kvfree_rcu_offset(offset)))
  2820. kvfree(ptr);
  2821. rcu_lock_release(&rcu_callback_map);
  2822. cond_resched_tasks_rcu_qs();
  2823. }
  2824. }
  2825. static bool
  2826. need_offload_krc(struct kfree_rcu_cpu *krcp)
  2827. {
  2828. int i;
  2829. for (i = 0; i < FREE_N_CHANNELS; i++)
  2830. if (krcp->bkvhead[i])
  2831. return true;
  2832. return !!krcp->head;
  2833. }
  2834. static bool
  2835. need_wait_for_krwp_work(struct kfree_rcu_cpu_work *krwp)
  2836. {
  2837. int i;
  2838. for (i = 0; i < FREE_N_CHANNELS; i++)
  2839. if (krwp->bkvhead_free[i])
  2840. return true;
  2841. return !!krwp->head_free;
  2842. }
  2843. static void
  2844. schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp)
  2845. {
  2846. long delay, delay_left;
  2847. delay = READ_ONCE(krcp->count) >= KVFREE_BULK_MAX_ENTR ? 1:KFREE_DRAIN_JIFFIES;
  2848. if (delayed_work_pending(&krcp->monitor_work)) {
  2849. delay_left = krcp->monitor_work.timer.expires - jiffies;
  2850. if (delay < delay_left)
  2851. mod_delayed_work(system_wq, &krcp->monitor_work, delay);
  2852. return;
  2853. }
  2854. queue_delayed_work(system_wq, &krcp->monitor_work, delay);
  2855. }
  2856. /*
  2857. * This function is invoked after the KFREE_DRAIN_JIFFIES timeout.
  2858. */
  2859. static void kfree_rcu_monitor(struct work_struct *work)
  2860. {
  2861. struct kfree_rcu_cpu *krcp = container_of(work,
  2862. struct kfree_rcu_cpu, monitor_work.work);
  2863. unsigned long flags;
  2864. int i, j;
  2865. raw_spin_lock_irqsave(&krcp->lock, flags);
  2866. // Attempt to start a new batch.
  2867. for (i = 0; i < KFREE_N_BATCHES; i++) {
  2868. struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]);
  2869. // Try to detach bulk_head or head and attach it, only when
  2870. // all channels are free. Any channel is not free means at krwp
  2871. // there is on-going rcu work to handle krwp's free business.
  2872. if (need_wait_for_krwp_work(krwp))
  2873. continue;
  2874. if (need_offload_krc(krcp)) {
  2875. // Channel 1 corresponds to the SLAB-pointer bulk path.
  2876. // Channel 2 corresponds to vmalloc-pointer bulk path.
  2877. for (j = 0; j < FREE_N_CHANNELS; j++) {
  2878. if (!krwp->bkvhead_free[j]) {
  2879. krwp->bkvhead_free[j] = krcp->bkvhead[j];
  2880. krcp->bkvhead[j] = NULL;
  2881. }
  2882. }
  2883. // Channel 3 corresponds to both SLAB and vmalloc
  2884. // objects queued on the linked list.
  2885. if (!krwp->head_free) {
  2886. krwp->head_free = krcp->head;
  2887. krcp->head = NULL;
  2888. }
  2889. WRITE_ONCE(krcp->count, 0);
  2890. // One work is per one batch, so there are three
  2891. // "free channels", the batch can handle. It can
  2892. // be that the work is in the pending state when
  2893. // channels have been detached following by each
  2894. // other.
  2895. queue_rcu_work(system_wq, &krwp->rcu_work);
  2896. }
  2897. }
  2898. // If there is nothing to detach, it means that our job is
  2899. // successfully done here. In case of having at least one
  2900. // of the channels that is still busy we should rearm the
  2901. // work to repeat an attempt. Because previous batches are
  2902. // still in progress.
  2903. if (need_offload_krc(krcp))
  2904. schedule_delayed_monitor_work(krcp);
  2905. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2906. }
  2907. static enum hrtimer_restart
  2908. schedule_page_work_fn(struct hrtimer *t)
  2909. {
  2910. struct kfree_rcu_cpu *krcp =
  2911. container_of(t, struct kfree_rcu_cpu, hrtimer);
  2912. queue_delayed_work(system_highpri_wq, &krcp->page_cache_work, 0);
  2913. return HRTIMER_NORESTART;
  2914. }
  2915. static void fill_page_cache_func(struct work_struct *work)
  2916. {
  2917. struct kvfree_rcu_bulk_data *bnode;
  2918. struct kfree_rcu_cpu *krcp =
  2919. container_of(work, struct kfree_rcu_cpu,
  2920. page_cache_work.work);
  2921. unsigned long flags;
  2922. int nr_pages;
  2923. bool pushed;
  2924. int i;
  2925. nr_pages = atomic_read(&krcp->backoff_page_cache_fill) ?
  2926. 1 : rcu_min_cached_objs;
  2927. for (i = 0; i < nr_pages; i++) {
  2928. bnode = (struct kvfree_rcu_bulk_data *)
  2929. __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  2930. if (!bnode)
  2931. break;
  2932. raw_spin_lock_irqsave(&krcp->lock, flags);
  2933. pushed = put_cached_bnode(krcp, bnode);
  2934. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  2935. if (!pushed) {
  2936. free_page((unsigned long) bnode);
  2937. break;
  2938. }
  2939. }
  2940. atomic_set(&krcp->work_in_progress, 0);
  2941. atomic_set(&krcp->backoff_page_cache_fill, 0);
  2942. }
  2943. static void
  2944. run_page_cache_worker(struct kfree_rcu_cpu *krcp)
  2945. {
  2946. if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
  2947. !atomic_xchg(&krcp->work_in_progress, 1)) {
  2948. if (atomic_read(&krcp->backoff_page_cache_fill)) {
  2949. queue_delayed_work(system_wq,
  2950. &krcp->page_cache_work,
  2951. msecs_to_jiffies(rcu_delay_page_cache_fill_msec));
  2952. } else {
  2953. hrtimer_init(&krcp->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2954. krcp->hrtimer.function = schedule_page_work_fn;
  2955. hrtimer_start(&krcp->hrtimer, 0, HRTIMER_MODE_REL);
  2956. }
  2957. }
  2958. }
  2959. // Record ptr in a page managed by krcp, with the pre-krc_this_cpu_lock()
  2960. // state specified by flags. If can_alloc is true, the caller must
  2961. // be schedulable and not be holding any locks or mutexes that might be
  2962. // acquired by the memory allocator or anything that it might invoke.
  2963. // Returns true if ptr was successfully recorded, else the caller must
  2964. // use a fallback.
  2965. static inline bool
  2966. add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
  2967. unsigned long *flags, void *ptr, bool can_alloc)
  2968. {
  2969. struct kvfree_rcu_bulk_data *bnode;
  2970. int idx;
  2971. *krcp = krc_this_cpu_lock(flags);
  2972. if (unlikely(!(*krcp)->initialized))
  2973. return false;
  2974. idx = !!is_vmalloc_addr(ptr);
  2975. /* Check if a new block is required. */
  2976. if (!(*krcp)->bkvhead[idx] ||
  2977. (*krcp)->bkvhead[idx]->nr_records == KVFREE_BULK_MAX_ENTR) {
  2978. bnode = get_cached_bnode(*krcp);
  2979. if (!bnode && can_alloc) {
  2980. krc_this_cpu_unlock(*krcp, *flags);
  2981. // __GFP_NORETRY - allows a light-weight direct reclaim
  2982. // what is OK from minimizing of fallback hitting point of
  2983. // view. Apart of that it forbids any OOM invoking what is
  2984. // also beneficial since we are about to release memory soon.
  2985. //
  2986. // __GFP_NOMEMALLOC - prevents from consuming of all the
  2987. // memory reserves. Please note we have a fallback path.
  2988. //
  2989. // __GFP_NOWARN - it is supposed that an allocation can
  2990. // be failed under low memory or high memory pressure
  2991. // scenarios.
  2992. bnode = (struct kvfree_rcu_bulk_data *)
  2993. __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  2994. *krcp = krc_this_cpu_lock(flags);
  2995. }
  2996. if (!bnode)
  2997. return false;
  2998. /* Initialize the new block. */
  2999. bnode->nr_records = 0;
  3000. bnode->next = (*krcp)->bkvhead[idx];
  3001. /* Attach it to the head. */
  3002. (*krcp)->bkvhead[idx] = bnode;
  3003. }
  3004. /* Finally insert. */
  3005. (*krcp)->bkvhead[idx]->records
  3006. [(*krcp)->bkvhead[idx]->nr_records++] = ptr;
  3007. return true;
  3008. }
  3009. /*
  3010. * Queue a request for lazy invocation of the appropriate free routine
  3011. * after a grace period. Please note that three paths are maintained,
  3012. * two for the common case using arrays of pointers and a third one that
  3013. * is used only when the main paths cannot be used, for example, due to
  3014. * memory pressure.
  3015. *
  3016. * Each kvfree_call_rcu() request is added to a batch. The batch will be drained
  3017. * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will
  3018. * be free'd in workqueue context. This allows us to: batch requests together to
  3019. * reduce the number of grace periods during heavy kfree_rcu()/kvfree_rcu() load.
  3020. */
  3021. void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
  3022. {
  3023. unsigned long flags;
  3024. struct kfree_rcu_cpu *krcp;
  3025. bool success;
  3026. void *ptr;
  3027. if (head) {
  3028. ptr = (void *) head - (unsigned long) func;
  3029. } else {
  3030. /*
  3031. * Please note there is a limitation for the head-less
  3032. * variant, that is why there is a clear rule for such
  3033. * objects: it can be used from might_sleep() context
  3034. * only. For other places please embed an rcu_head to
  3035. * your data.
  3036. */
  3037. might_sleep();
  3038. ptr = (unsigned long *) func;
  3039. }
  3040. // Queue the object but don't yet schedule the batch.
  3041. if (debug_rcu_head_queue(ptr)) {
  3042. // Probable double kfree_rcu(), just leak.
  3043. WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n",
  3044. __func__, head);
  3045. // Mark as success and leave.
  3046. return;
  3047. }
  3048. kasan_record_aux_stack_noalloc(ptr);
  3049. success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head);
  3050. if (!success) {
  3051. run_page_cache_worker(krcp);
  3052. if (head == NULL)
  3053. // Inline if kvfree_rcu(one_arg) call.
  3054. goto unlock_return;
  3055. head->func = func;
  3056. head->next = krcp->head;
  3057. krcp->head = head;
  3058. success = true;
  3059. }
  3060. WRITE_ONCE(krcp->count, krcp->count + 1);
  3061. /*
  3062. * The kvfree_rcu() caller considers the pointer freed at this point
  3063. * and likely removes any references to it. Since the actual slab
  3064. * freeing (and kmemleak_free()) is deferred, tell kmemleak to ignore
  3065. * this object (no scanning or false positives reporting).
  3066. */
  3067. kmemleak_ignore(ptr);
  3068. // Set timer to drain after KFREE_DRAIN_JIFFIES.
  3069. if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING)
  3070. schedule_delayed_monitor_work(krcp);
  3071. unlock_return:
  3072. krc_this_cpu_unlock(krcp, flags);
  3073. /*
  3074. * Inline kvfree() after synchronize_rcu(). We can do
  3075. * it from might_sleep() context only, so the current
  3076. * CPU can pass the QS state.
  3077. */
  3078. if (!success) {
  3079. debug_rcu_head_unqueue((struct rcu_head *) ptr);
  3080. synchronize_rcu();
  3081. kvfree(ptr);
  3082. }
  3083. }
  3084. EXPORT_SYMBOL_GPL(kvfree_call_rcu);
  3085. static unsigned long
  3086. kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  3087. {
  3088. int cpu;
  3089. unsigned long count = 0;
  3090. /* Snapshot count of all CPUs */
  3091. for_each_possible_cpu(cpu) {
  3092. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3093. count += READ_ONCE(krcp->count);
  3094. count += READ_ONCE(krcp->nr_bkv_objs);
  3095. atomic_set(&krcp->backoff_page_cache_fill, 1);
  3096. }
  3097. return count == 0 ? SHRINK_EMPTY : count;
  3098. }
  3099. static unsigned long
  3100. kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  3101. {
  3102. int cpu, freed = 0;
  3103. for_each_possible_cpu(cpu) {
  3104. int count;
  3105. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3106. count = krcp->count;
  3107. count += drain_page_cache(krcp);
  3108. kfree_rcu_monitor(&krcp->monitor_work.work);
  3109. sc->nr_to_scan -= count;
  3110. freed += count;
  3111. if (sc->nr_to_scan <= 0)
  3112. break;
  3113. }
  3114. return freed == 0 ? SHRINK_STOP : freed;
  3115. }
  3116. static struct shrinker kfree_rcu_shrinker = {
  3117. .count_objects = kfree_rcu_shrink_count,
  3118. .scan_objects = kfree_rcu_shrink_scan,
  3119. .batch = 0,
  3120. .seeks = DEFAULT_SEEKS,
  3121. };
  3122. void __init kfree_rcu_scheduler_running(void)
  3123. {
  3124. int cpu;
  3125. unsigned long flags;
  3126. for_each_possible_cpu(cpu) {
  3127. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3128. raw_spin_lock_irqsave(&krcp->lock, flags);
  3129. if (need_offload_krc(krcp))
  3130. schedule_delayed_monitor_work(krcp);
  3131. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3132. }
  3133. }
  3134. /*
  3135. * During early boot, any blocking grace-period wait automatically
  3136. * implies a grace period.
  3137. *
  3138. * Later on, this could in theory be the case for kernels built with
  3139. * CONFIG_SMP=y && CONFIG_PREEMPTION=y running on a single CPU, but this
  3140. * is not a common case. Furthermore, this optimization would cause
  3141. * the rcu_gp_oldstate structure to expand by 50%, so this potential
  3142. * grace-period optimization is ignored once the scheduler is running.
  3143. */
  3144. static int rcu_blocking_is_gp(void)
  3145. {
  3146. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE)
  3147. return false;
  3148. might_sleep(); /* Check for RCU read-side critical section. */
  3149. return true;
  3150. }
  3151. /**
  3152. * synchronize_rcu - wait until a grace period has elapsed.
  3153. *
  3154. * Control will return to the caller some time after a full grace
  3155. * period has elapsed, in other words after all currently executing RCU
  3156. * read-side critical sections have completed. Note, however, that
  3157. * upon return from synchronize_rcu(), the caller might well be executing
  3158. * concurrently with new RCU read-side critical sections that began while
  3159. * synchronize_rcu() was waiting.
  3160. *
  3161. * RCU read-side critical sections are delimited by rcu_read_lock()
  3162. * and rcu_read_unlock(), and may be nested. In addition, but only in
  3163. * v5.0 and later, regions of code across which interrupts, preemption,
  3164. * or softirqs have been disabled also serve as RCU read-side critical
  3165. * sections. This includes hardware interrupt handlers, softirq handlers,
  3166. * and NMI handlers.
  3167. *
  3168. * Note that this guarantee implies further memory-ordering guarantees.
  3169. * On systems with more than one CPU, when synchronize_rcu() returns,
  3170. * each CPU is guaranteed to have executed a full memory barrier since
  3171. * the end of its last RCU read-side critical section whose beginning
  3172. * preceded the call to synchronize_rcu(). In addition, each CPU having
  3173. * an RCU read-side critical section that extends beyond the return from
  3174. * synchronize_rcu() is guaranteed to have executed a full memory barrier
  3175. * after the beginning of synchronize_rcu() and before the beginning of
  3176. * that RCU read-side critical section. Note that these guarantees include
  3177. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  3178. * that are executing in the kernel.
  3179. *
  3180. * Furthermore, if CPU A invoked synchronize_rcu(), which returned
  3181. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  3182. * to have executed a full memory barrier during the execution of
  3183. * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
  3184. * again only if the system has more than one CPU).
  3185. *
  3186. * Implementation of these memory-ordering guarantees is described here:
  3187. * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst.
  3188. */
  3189. void synchronize_rcu(void)
  3190. {
  3191. unsigned long flags;
  3192. struct rcu_node *rnp;
  3193. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  3194. lock_is_held(&rcu_lock_map) ||
  3195. lock_is_held(&rcu_sched_lock_map),
  3196. "Illegal synchronize_rcu() in RCU read-side critical section");
  3197. if (!rcu_blocking_is_gp()) {
  3198. if (rcu_gp_is_expedited())
  3199. synchronize_rcu_expedited();
  3200. else
  3201. wait_rcu_gp(call_rcu_hurry);
  3202. return;
  3203. }
  3204. // Context allows vacuous grace periods.
  3205. // Note well that this code runs with !PREEMPT && !SMP.
  3206. // In addition, all code that advances grace periods runs at
  3207. // process level. Therefore, this normal GP overlaps with other
  3208. // normal GPs only by being fully nested within them, which allows
  3209. // reuse of ->gp_seq_polled_snap.
  3210. rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_snap);
  3211. rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_snap);
  3212. // Update the normal grace-period counters to record
  3213. // this grace period, but only those used by the boot CPU.
  3214. // The rcu_scheduler_starting() will take care of the rest of
  3215. // these counters.
  3216. local_irq_save(flags);
  3217. WARN_ON_ONCE(num_online_cpus() > 1);
  3218. rcu_state.gp_seq += (1 << RCU_SEQ_CTR_SHIFT);
  3219. for (rnp = this_cpu_ptr(&rcu_data)->mynode; rnp; rnp = rnp->parent)
  3220. rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq;
  3221. local_irq_restore(flags);
  3222. }
  3223. EXPORT_SYMBOL_GPL(synchronize_rcu);
  3224. /**
  3225. * get_completed_synchronize_rcu_full - Return a full pre-completed polled state cookie
  3226. * @rgosp: Place to put state cookie
  3227. *
  3228. * Stores into @rgosp a value that will always be treated by functions
  3229. * like poll_state_synchronize_rcu_full() as a cookie whose grace period
  3230. * has already completed.
  3231. */
  3232. void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3233. {
  3234. rgosp->rgos_norm = RCU_GET_STATE_COMPLETED;
  3235. rgosp->rgos_exp = RCU_GET_STATE_COMPLETED;
  3236. }
  3237. EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full);
  3238. /**
  3239. * get_state_synchronize_rcu - Snapshot current RCU state
  3240. *
  3241. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  3242. * or poll_state_synchronize_rcu() to determine whether or not a full
  3243. * grace period has elapsed in the meantime.
  3244. */
  3245. unsigned long get_state_synchronize_rcu(void)
  3246. {
  3247. /*
  3248. * Any prior manipulation of RCU-protected data must happen
  3249. * before the load from ->gp_seq.
  3250. */
  3251. smp_mb(); /* ^^^ */
  3252. return rcu_seq_snap(&rcu_state.gp_seq_polled);
  3253. }
  3254. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  3255. /**
  3256. * get_state_synchronize_rcu_full - Snapshot RCU state, both normal and expedited
  3257. * @rgosp: location to place combined normal/expedited grace-period state
  3258. *
  3259. * Places the normal and expedited grace-period states in @rgosp. This
  3260. * state value can be passed to a later call to cond_synchronize_rcu_full()
  3261. * or poll_state_synchronize_rcu_full() to determine whether or not a
  3262. * grace period (whether normal or expedited) has elapsed in the meantime.
  3263. * The rcu_gp_oldstate structure takes up twice the memory of an unsigned
  3264. * long, but is guaranteed to see all grace periods. In contrast, the
  3265. * combined state occupies less memory, but can sometimes fail to take
  3266. * grace periods into account.
  3267. *
  3268. * This does not guarantee that the needed grace period will actually
  3269. * start.
  3270. */
  3271. void get_state_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3272. {
  3273. struct rcu_node *rnp = rcu_get_root();
  3274. /*
  3275. * Any prior manipulation of RCU-protected data must happen
  3276. * before the loads from ->gp_seq and ->expedited_sequence.
  3277. */
  3278. smp_mb(); /* ^^^ */
  3279. rgosp->rgos_norm = rcu_seq_snap(&rnp->gp_seq);
  3280. rgosp->rgos_exp = rcu_seq_snap(&rcu_state.expedited_sequence);
  3281. }
  3282. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu_full);
  3283. /*
  3284. * Helper function for start_poll_synchronize_rcu() and
  3285. * start_poll_synchronize_rcu_full().
  3286. */
  3287. static void start_poll_synchronize_rcu_common(void)
  3288. {
  3289. unsigned long flags;
  3290. bool needwake;
  3291. struct rcu_data *rdp;
  3292. struct rcu_node *rnp;
  3293. lockdep_assert_irqs_enabled();
  3294. local_irq_save(flags);
  3295. rdp = this_cpu_ptr(&rcu_data);
  3296. rnp = rdp->mynode;
  3297. raw_spin_lock_rcu_node(rnp); // irqs already disabled.
  3298. // Note it is possible for a grace period to have elapsed between
  3299. // the above call to get_state_synchronize_rcu() and the below call
  3300. // to rcu_seq_snap. This is OK, the worst that happens is that we
  3301. // get a grace period that no one needed. These accesses are ordered
  3302. // by smp_mb(), and we are accessing them in the opposite order
  3303. // from which they are updated at grace-period start, as required.
  3304. needwake = rcu_start_this_gp(rnp, rdp, rcu_seq_snap(&rcu_state.gp_seq));
  3305. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3306. if (needwake)
  3307. rcu_gp_kthread_wake();
  3308. }
  3309. /**
  3310. * start_poll_synchronize_rcu - Snapshot and start RCU grace period
  3311. *
  3312. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  3313. * or poll_state_synchronize_rcu() to determine whether or not a full
  3314. * grace period has elapsed in the meantime. If the needed grace period
  3315. * is not already slated to start, notifies RCU core of the need for that
  3316. * grace period.
  3317. *
  3318. * Interrupts must be enabled for the case where it is necessary to awaken
  3319. * the grace-period kthread.
  3320. */
  3321. unsigned long start_poll_synchronize_rcu(void)
  3322. {
  3323. unsigned long gp_seq = get_state_synchronize_rcu();
  3324. start_poll_synchronize_rcu_common();
  3325. return gp_seq;
  3326. }
  3327. EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu);
  3328. /**
  3329. * start_poll_synchronize_rcu_full - Take a full snapshot and start RCU grace period
  3330. * @rgosp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full()
  3331. *
  3332. * Places the normal and expedited grace-period states in *@rgos. This
  3333. * state value can be passed to a later call to cond_synchronize_rcu_full()
  3334. * or poll_state_synchronize_rcu_full() to determine whether or not a
  3335. * grace period (whether normal or expedited) has elapsed in the meantime.
  3336. * If the needed grace period is not already slated to start, notifies
  3337. * RCU core of the need for that grace period.
  3338. *
  3339. * Interrupts must be enabled for the case where it is necessary to awaken
  3340. * the grace-period kthread.
  3341. */
  3342. void start_poll_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3343. {
  3344. get_state_synchronize_rcu_full(rgosp);
  3345. start_poll_synchronize_rcu_common();
  3346. }
  3347. EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu_full);
  3348. /**
  3349. * poll_state_synchronize_rcu - Has the specified RCU grace period completed?
  3350. * @oldstate: value from get_state_synchronize_rcu() or start_poll_synchronize_rcu()
  3351. *
  3352. * If a full RCU grace period has elapsed since the earlier call from
  3353. * which @oldstate was obtained, return @true, otherwise return @false.
  3354. * If @false is returned, it is the caller's responsibility to invoke this
  3355. * function later on until it does return @true. Alternatively, the caller
  3356. * can explicitly wait for a grace period, for example, by passing @oldstate
  3357. * to cond_synchronize_rcu() or by directly invoking synchronize_rcu().
  3358. *
  3359. * Yes, this function does not take counter wrap into account.
  3360. * But counter wrap is harmless. If the counter wraps, we have waited for
  3361. * more than a billion grace periods (and way more on a 64-bit system!).
  3362. * Those needing to keep old state values for very long time periods
  3363. * (many hours even on 32-bit systems) should check them occasionally and
  3364. * either refresh them or set a flag indicating that the grace period has
  3365. * completed. Alternatively, they can use get_completed_synchronize_rcu()
  3366. * to get a guaranteed-completed grace-period state.
  3367. *
  3368. * This function provides the same memory-ordering guarantees that
  3369. * would be provided by a synchronize_rcu() that was invoked at the call
  3370. * to the function that provided @oldstate, and that returned at the end
  3371. * of this function.
  3372. */
  3373. bool poll_state_synchronize_rcu(unsigned long oldstate)
  3374. {
  3375. if (oldstate == RCU_GET_STATE_COMPLETED ||
  3376. rcu_seq_done_exact(&rcu_state.gp_seq_polled, oldstate)) {
  3377. smp_mb(); /* Ensure GP ends before subsequent accesses. */
  3378. return true;
  3379. }
  3380. return false;
  3381. }
  3382. EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu);
  3383. /**
  3384. * poll_state_synchronize_rcu_full - Has the specified RCU grace period completed?
  3385. * @rgosp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full()
  3386. *
  3387. * If a full RCU grace period has elapsed since the earlier call from
  3388. * which *rgosp was obtained, return @true, otherwise return @false.
  3389. * If @false is returned, it is the caller's responsibility to invoke this
  3390. * function later on until it does return @true. Alternatively, the caller
  3391. * can explicitly wait for a grace period, for example, by passing @rgosp
  3392. * to cond_synchronize_rcu() or by directly invoking synchronize_rcu().
  3393. *
  3394. * Yes, this function does not take counter wrap into account.
  3395. * But counter wrap is harmless. If the counter wraps, we have waited
  3396. * for more than a billion grace periods (and way more on a 64-bit
  3397. * system!). Those needing to keep rcu_gp_oldstate values for very
  3398. * long time periods (many hours even on 32-bit systems) should check
  3399. * them occasionally and either refresh them or set a flag indicating
  3400. * that the grace period has completed. Alternatively, they can use
  3401. * get_completed_synchronize_rcu_full() to get a guaranteed-completed
  3402. * grace-period state.
  3403. *
  3404. * This function provides the same memory-ordering guarantees that would
  3405. * be provided by a synchronize_rcu() that was invoked at the call to
  3406. * the function that provided @rgosp, and that returned at the end of this
  3407. * function. And this guarantee requires that the root rcu_node structure's
  3408. * ->gp_seq field be checked instead of that of the rcu_state structure.
  3409. * The problem is that the just-ending grace-period's callbacks can be
  3410. * invoked between the time that the root rcu_node structure's ->gp_seq
  3411. * field is updated and the time that the rcu_state structure's ->gp_seq
  3412. * field is updated. Therefore, if a single synchronize_rcu() is to
  3413. * cause a subsequent poll_state_synchronize_rcu_full() to return @true,
  3414. * then the root rcu_node structure is the one that needs to be polled.
  3415. */
  3416. bool poll_state_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3417. {
  3418. struct rcu_node *rnp = rcu_get_root();
  3419. smp_mb(); // Order against root rcu_node structure grace-period cleanup.
  3420. if (rgosp->rgos_norm == RCU_GET_STATE_COMPLETED ||
  3421. rcu_seq_done_exact(&rnp->gp_seq, rgosp->rgos_norm) ||
  3422. rgosp->rgos_exp == RCU_GET_STATE_COMPLETED ||
  3423. rcu_seq_done_exact(&rcu_state.expedited_sequence, rgosp->rgos_exp)) {
  3424. smp_mb(); /* Ensure GP ends before subsequent accesses. */
  3425. return true;
  3426. }
  3427. return false;
  3428. }
  3429. EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu_full);
  3430. /**
  3431. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  3432. * @oldstate: value from get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or start_poll_synchronize_rcu_expedited()
  3433. *
  3434. * If a full RCU grace period has elapsed since the earlier call to
  3435. * get_state_synchronize_rcu() or start_poll_synchronize_rcu(), just return.
  3436. * Otherwise, invoke synchronize_rcu() to wait for a full grace period.
  3437. *
  3438. * Yes, this function does not take counter wrap into account.
  3439. * But counter wrap is harmless. If the counter wraps, we have waited for
  3440. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3441. * so waiting for a couple of additional grace periods should be just fine.
  3442. *
  3443. * This function provides the same memory-ordering guarantees that
  3444. * would be provided by a synchronize_rcu() that was invoked at the call
  3445. * to the function that provided @oldstate and that returned at the end
  3446. * of this function.
  3447. */
  3448. void cond_synchronize_rcu(unsigned long oldstate)
  3449. {
  3450. if (!poll_state_synchronize_rcu(oldstate))
  3451. synchronize_rcu();
  3452. }
  3453. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  3454. /**
  3455. * cond_synchronize_rcu_full - Conditionally wait for an RCU grace period
  3456. * @rgosp: value from get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), or start_poll_synchronize_rcu_expedited_full()
  3457. *
  3458. * If a full RCU grace period has elapsed since the call to
  3459. * get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(),
  3460. * or start_poll_synchronize_rcu_expedited_full() from which @rgosp was
  3461. * obtained, just return. Otherwise, invoke synchronize_rcu() to wait
  3462. * for a full grace period.
  3463. *
  3464. * Yes, this function does not take counter wrap into account.
  3465. * But counter wrap is harmless. If the counter wraps, we have waited for
  3466. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3467. * so waiting for a couple of additional grace periods should be just fine.
  3468. *
  3469. * This function provides the same memory-ordering guarantees that
  3470. * would be provided by a synchronize_rcu() that was invoked at the call
  3471. * to the function that provided @rgosp and that returned at the end of
  3472. * this function.
  3473. */
  3474. void cond_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3475. {
  3476. if (!poll_state_synchronize_rcu_full(rgosp))
  3477. synchronize_rcu();
  3478. }
  3479. EXPORT_SYMBOL_GPL(cond_synchronize_rcu_full);
  3480. /*
  3481. * Check to see if there is any immediate RCU-related work to be done by
  3482. * the current CPU, returning 1 if so and zero otherwise. The checks are
  3483. * in order of increasing expense: checks that can be carried out against
  3484. * CPU-local state are performed first. However, we must check for CPU
  3485. * stalls first, else we might not get a chance.
  3486. */
  3487. static int rcu_pending(int user)
  3488. {
  3489. bool gp_in_progress;
  3490. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  3491. struct rcu_node *rnp = rdp->mynode;
  3492. lockdep_assert_irqs_disabled();
  3493. /* Check for CPU stalls, if enabled. */
  3494. check_cpu_stall(rdp);
  3495. /* Does this CPU need a deferred NOCB wakeup? */
  3496. if (rcu_nocb_need_deferred_wakeup(rdp, RCU_NOCB_WAKE))
  3497. return 1;
  3498. /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */
  3499. if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu())
  3500. return 0;
  3501. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3502. gp_in_progress = rcu_gp_in_progress();
  3503. if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress)
  3504. return 1;
  3505. /* Does this CPU have callbacks ready to invoke? */
  3506. if (!rcu_rdp_is_offloaded(rdp) &&
  3507. rcu_segcblist_ready_cbs(&rdp->cblist))
  3508. return 1;
  3509. /* Has RCU gone idle with this CPU needing another grace period? */
  3510. if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
  3511. !rcu_rdp_is_offloaded(rdp) &&
  3512. !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  3513. return 1;
  3514. /* Have RCU grace period completed or started? */
  3515. if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
  3516. unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
  3517. return 1;
  3518. /* nothing to do */
  3519. return 0;
  3520. }
  3521. /*
  3522. * Helper function for rcu_barrier() tracing. If tracing is disabled,
  3523. * the compiler is expected to optimize this away.
  3524. */
  3525. static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
  3526. {
  3527. trace_rcu_barrier(rcu_state.name, s, cpu,
  3528. atomic_read(&rcu_state.barrier_cpu_count), done);
  3529. }
  3530. /*
  3531. * RCU callback function for rcu_barrier(). If we are last, wake
  3532. * up the task executing rcu_barrier().
  3533. *
  3534. * Note that the value of rcu_state.barrier_sequence must be captured
  3535. * before the atomic_dec_and_test(). Otherwise, if this CPU is not last,
  3536. * other CPUs might count the value down to zero before this CPU gets
  3537. * around to invoking rcu_barrier_trace(), which might result in bogus
  3538. * data from the next instance of rcu_barrier().
  3539. */
  3540. static void rcu_barrier_callback(struct rcu_head *rhp)
  3541. {
  3542. unsigned long __maybe_unused s = rcu_state.barrier_sequence;
  3543. if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
  3544. rcu_barrier_trace(TPS("LastCB"), -1, s);
  3545. complete(&rcu_state.barrier_completion);
  3546. } else {
  3547. rcu_barrier_trace(TPS("CB"), -1, s);
  3548. }
  3549. }
  3550. /*
  3551. * If needed, entrain an rcu_barrier() callback on rdp->cblist.
  3552. */
  3553. static void rcu_barrier_entrain(struct rcu_data *rdp)
  3554. {
  3555. unsigned long gseq = READ_ONCE(rcu_state.barrier_sequence);
  3556. unsigned long lseq = READ_ONCE(rdp->barrier_seq_snap);
  3557. bool wake_nocb = false;
  3558. bool was_alldone = false;
  3559. lockdep_assert_held(&rcu_state.barrier_lock);
  3560. if (rcu_seq_state(lseq) || !rcu_seq_state(gseq) || rcu_seq_ctr(lseq) != rcu_seq_ctr(gseq))
  3561. return;
  3562. rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
  3563. rdp->barrier_head.func = rcu_barrier_callback;
  3564. debug_rcu_head_queue(&rdp->barrier_head);
  3565. rcu_nocb_lock(rdp);
  3566. /*
  3567. * Flush bypass and wakeup rcuog if we add callbacks to an empty regular
  3568. * queue. This way we don't wait for bypass timer that can reach seconds
  3569. * if it's fully lazy.
  3570. */
  3571. was_alldone = rcu_rdp_is_offloaded(rdp) && !rcu_segcblist_pend_cbs(&rdp->cblist);
  3572. WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false));
  3573. wake_nocb = was_alldone && rcu_segcblist_pend_cbs(&rdp->cblist);
  3574. if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) {
  3575. atomic_inc(&rcu_state.barrier_cpu_count);
  3576. } else {
  3577. debug_rcu_head_unqueue(&rdp->barrier_head);
  3578. rcu_barrier_trace(TPS("IRQNQ"), -1, rcu_state.barrier_sequence);
  3579. }
  3580. rcu_nocb_unlock(rdp);
  3581. if (wake_nocb)
  3582. wake_nocb_gp(rdp, false);
  3583. smp_store_release(&rdp->barrier_seq_snap, gseq);
  3584. }
  3585. /*
  3586. * Called with preemption disabled, and from cross-cpu IRQ context.
  3587. */
  3588. static void rcu_barrier_handler(void *cpu_in)
  3589. {
  3590. uintptr_t cpu = (uintptr_t)cpu_in;
  3591. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3592. lockdep_assert_irqs_disabled();
  3593. WARN_ON_ONCE(cpu != rdp->cpu);
  3594. WARN_ON_ONCE(cpu != smp_processor_id());
  3595. raw_spin_lock(&rcu_state.barrier_lock);
  3596. rcu_barrier_entrain(rdp);
  3597. raw_spin_unlock(&rcu_state.barrier_lock);
  3598. }
  3599. /**
  3600. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  3601. *
  3602. * Note that this primitive does not necessarily wait for an RCU grace period
  3603. * to complete. For example, if there are no RCU callbacks queued anywhere
  3604. * in the system, then rcu_barrier() is within its rights to return
  3605. * immediately, without waiting for anything, much less an RCU grace period.
  3606. */
  3607. void rcu_barrier(void)
  3608. {
  3609. uintptr_t cpu;
  3610. unsigned long flags;
  3611. unsigned long gseq;
  3612. struct rcu_data *rdp;
  3613. unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
  3614. rcu_barrier_trace(TPS("Begin"), -1, s);
  3615. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  3616. mutex_lock(&rcu_state.barrier_mutex);
  3617. /* Did someone else do our work for us? */
  3618. if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
  3619. rcu_barrier_trace(TPS("EarlyExit"), -1, rcu_state.barrier_sequence);
  3620. smp_mb(); /* caller's subsequent code after above check. */
  3621. mutex_unlock(&rcu_state.barrier_mutex);
  3622. return;
  3623. }
  3624. /* Mark the start of the barrier operation. */
  3625. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  3626. rcu_seq_start(&rcu_state.barrier_sequence);
  3627. gseq = rcu_state.barrier_sequence;
  3628. rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
  3629. /*
  3630. * Initialize the count to two rather than to zero in order
  3631. * to avoid a too-soon return to zero in case of an immediate
  3632. * invocation of the just-enqueued callback (or preemption of
  3633. * this task). Exclude CPU-hotplug operations to ensure that no
  3634. * offline non-offloaded CPU has callbacks queued.
  3635. */
  3636. init_completion(&rcu_state.barrier_completion);
  3637. atomic_set(&rcu_state.barrier_cpu_count, 2);
  3638. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  3639. /*
  3640. * Force each CPU with callbacks to register a new callback.
  3641. * When that callback is invoked, we will know that all of the
  3642. * corresponding CPU's preceding callbacks have been invoked.
  3643. */
  3644. for_each_possible_cpu(cpu) {
  3645. rdp = per_cpu_ptr(&rcu_data, cpu);
  3646. retry:
  3647. if (smp_load_acquire(&rdp->barrier_seq_snap) == gseq)
  3648. continue;
  3649. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  3650. if (!rcu_segcblist_n_cbs(&rdp->cblist)) {
  3651. WRITE_ONCE(rdp->barrier_seq_snap, gseq);
  3652. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  3653. rcu_barrier_trace(TPS("NQ"), cpu, rcu_state.barrier_sequence);
  3654. continue;
  3655. }
  3656. if (!rcu_rdp_cpu_online(rdp)) {
  3657. rcu_barrier_entrain(rdp);
  3658. WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq);
  3659. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  3660. rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu, rcu_state.barrier_sequence);
  3661. continue;
  3662. }
  3663. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  3664. if (smp_call_function_single(cpu, rcu_barrier_handler, (void *)cpu, 1)) {
  3665. schedule_timeout_uninterruptible(1);
  3666. goto retry;
  3667. }
  3668. WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq);
  3669. rcu_barrier_trace(TPS("OnlineQ"), cpu, rcu_state.barrier_sequence);
  3670. }
  3671. /*
  3672. * Now that we have an rcu_barrier_callback() callback on each
  3673. * CPU, and thus each counted, remove the initial count.
  3674. */
  3675. if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count))
  3676. complete(&rcu_state.barrier_completion);
  3677. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  3678. wait_for_completion(&rcu_state.barrier_completion);
  3679. /* Mark the end of the barrier operation. */
  3680. rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
  3681. rcu_seq_end(&rcu_state.barrier_sequence);
  3682. gseq = rcu_state.barrier_sequence;
  3683. for_each_possible_cpu(cpu) {
  3684. rdp = per_cpu_ptr(&rcu_data, cpu);
  3685. WRITE_ONCE(rdp->barrier_seq_snap, gseq);
  3686. }
  3687. /* Other rcu_barrier() invocations can now safely proceed. */
  3688. mutex_unlock(&rcu_state.barrier_mutex);
  3689. }
  3690. EXPORT_SYMBOL_GPL(rcu_barrier);
  3691. /*
  3692. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  3693. * first CPU in a given leaf rcu_node structure coming online. The caller
  3694. * must hold the corresponding leaf rcu_node ->lock with interrupts
  3695. * disabled.
  3696. */
  3697. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  3698. {
  3699. long mask;
  3700. long oldmask;
  3701. struct rcu_node *rnp = rnp_leaf;
  3702. raw_lockdep_assert_held_rcu_node(rnp_leaf);
  3703. WARN_ON_ONCE(rnp->wait_blkd_tasks);
  3704. for (;;) {
  3705. mask = rnp->grpmask;
  3706. rnp = rnp->parent;
  3707. if (rnp == NULL)
  3708. return;
  3709. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  3710. oldmask = rnp->qsmaskinit;
  3711. rnp->qsmaskinit |= mask;
  3712. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  3713. if (oldmask)
  3714. return;
  3715. }
  3716. }
  3717. /*
  3718. * Do boot-time initialization of a CPU's per-CPU RCU data.
  3719. */
  3720. static void __init
  3721. rcu_boot_init_percpu_data(int cpu)
  3722. {
  3723. struct context_tracking *ct = this_cpu_ptr(&context_tracking);
  3724. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3725. /* Set up local state, ensuring consistent view of global state. */
  3726. rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
  3727. INIT_WORK(&rdp->strict_work, strict_work_handler);
  3728. WARN_ON_ONCE(ct->dynticks_nesting != 1);
  3729. WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu)));
  3730. rdp->barrier_seq_snap = rcu_state.barrier_sequence;
  3731. rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
  3732. rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
  3733. rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
  3734. rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
  3735. rdp->last_sched_clock = jiffies;
  3736. rdp->cpu = cpu;
  3737. rcu_boot_init_nocb_percpu_data(rdp);
  3738. }
  3739. /*
  3740. * Invoked early in the CPU-online process, when pretty much all services
  3741. * are available. The incoming CPU is not present.
  3742. *
  3743. * Initializes a CPU's per-CPU RCU data. Note that only one online or
  3744. * offline event can be happening at a given time. Note also that we can
  3745. * accept some slop in the rsp->gp_seq access due to the fact that this
  3746. * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet.
  3747. * And any offloaded callbacks are being numbered elsewhere.
  3748. */
  3749. int rcutree_prepare_cpu(unsigned int cpu)
  3750. {
  3751. unsigned long flags;
  3752. struct context_tracking *ct = per_cpu_ptr(&context_tracking, cpu);
  3753. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3754. struct rcu_node *rnp = rcu_get_root();
  3755. /* Set up local state, ensuring consistent view of global state. */
  3756. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3757. rdp->qlen_last_fqs_check = 0;
  3758. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  3759. rdp->blimit = blimit;
  3760. ct->dynticks_nesting = 1; /* CPU not up, no tearing. */
  3761. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  3762. /*
  3763. * Only non-NOCB CPUs that didn't have early-boot callbacks need to be
  3764. * (re-)initialized.
  3765. */
  3766. if (!rcu_segcblist_is_enabled(&rdp->cblist))
  3767. rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
  3768. /*
  3769. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  3770. * propagation up the rcu_node tree will happen at the beginning
  3771. * of the next grace period.
  3772. */
  3773. rnp = rdp->mynode;
  3774. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  3775. rdp->gp_seq = READ_ONCE(rnp->gp_seq);
  3776. rdp->gp_seq_needed = rdp->gp_seq;
  3777. rdp->cpu_no_qs.b.norm = true;
  3778. rdp->core_needs_qs = false;
  3779. rdp->rcu_iw_pending = false;
  3780. rdp->rcu_iw = IRQ_WORK_INIT_HARD(rcu_iw_handler);
  3781. rdp->rcu_iw_gp_seq = rdp->gp_seq - 1;
  3782. trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
  3783. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3784. rcu_spawn_one_boost_kthread(rnp);
  3785. rcu_spawn_cpu_nocb_kthread(cpu);
  3786. WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus + 1);
  3787. return 0;
  3788. }
  3789. /*
  3790. * Update RCU priority boot kthread affinity for CPU-hotplug changes.
  3791. */
  3792. static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
  3793. {
  3794. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3795. rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
  3796. }
  3797. /*
  3798. * Has the specified (known valid) CPU ever been fully online?
  3799. */
  3800. bool rcu_cpu_beenfullyonline(int cpu)
  3801. {
  3802. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3803. return smp_load_acquire(&rdp->beenonline);
  3804. }
  3805. /*
  3806. * Near the end of the CPU-online process. Pretty much all services
  3807. * enabled, and the CPU is now very much alive.
  3808. */
  3809. int rcutree_online_cpu(unsigned int cpu)
  3810. {
  3811. unsigned long flags;
  3812. struct rcu_data *rdp;
  3813. struct rcu_node *rnp;
  3814. rdp = per_cpu_ptr(&rcu_data, cpu);
  3815. rnp = rdp->mynode;
  3816. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3817. rnp->ffmask |= rdp->grpmask;
  3818. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3819. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  3820. return 0; /* Too early in boot for scheduler work. */
  3821. sync_sched_exp_online_cleanup(cpu);
  3822. rcutree_affinity_setting(cpu, -1);
  3823. // Stop-machine done, so allow nohz_full to disable tick.
  3824. tick_dep_clear(TICK_DEP_BIT_RCU);
  3825. return 0;
  3826. }
  3827. /*
  3828. * Near the beginning of the process. The CPU is still very much alive
  3829. * with pretty much all services enabled.
  3830. */
  3831. int rcutree_offline_cpu(unsigned int cpu)
  3832. {
  3833. unsigned long flags;
  3834. struct rcu_data *rdp;
  3835. struct rcu_node *rnp;
  3836. rdp = per_cpu_ptr(&rcu_data, cpu);
  3837. rnp = rdp->mynode;
  3838. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3839. rnp->ffmask &= ~rdp->grpmask;
  3840. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3841. rcutree_affinity_setting(cpu, cpu);
  3842. // nohz_full CPUs need the tick for stop-machine to work quickly
  3843. tick_dep_set(TICK_DEP_BIT_RCU);
  3844. return 0;
  3845. }
  3846. /*
  3847. * Mark the specified CPU as being online so that subsequent grace periods
  3848. * (both expedited and normal) will wait on it. Note that this means that
  3849. * incoming CPUs are not allowed to use RCU read-side critical sections
  3850. * until this function is called. Failing to observe this restriction
  3851. * will result in lockdep splats.
  3852. *
  3853. * Note that this function is special in that it is invoked directly
  3854. * from the incoming CPU rather than from the cpuhp_step mechanism.
  3855. * This is because this function must be invoked at a precise location.
  3856. * This incoming CPU must not have enabled interrupts yet.
  3857. */
  3858. void rcu_cpu_starting(unsigned int cpu)
  3859. {
  3860. unsigned long mask;
  3861. struct rcu_data *rdp;
  3862. struct rcu_node *rnp;
  3863. bool newcpu;
  3864. lockdep_assert_irqs_disabled();
  3865. rdp = per_cpu_ptr(&rcu_data, cpu);
  3866. if (rdp->cpu_started)
  3867. return;
  3868. rdp->cpu_started = true;
  3869. rnp = rdp->mynode;
  3870. mask = rdp->grpmask;
  3871. arch_spin_lock(&rcu_state.ofl_lock);
  3872. rcu_dynticks_eqs_online();
  3873. raw_spin_lock(&rcu_state.barrier_lock);
  3874. raw_spin_lock_rcu_node(rnp);
  3875. WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask);
  3876. raw_spin_unlock(&rcu_state.barrier_lock);
  3877. newcpu = !(rnp->expmaskinitnext & mask);
  3878. rnp->expmaskinitnext |= mask;
  3879. /* Allow lockless access for expedited grace periods. */
  3880. smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + newcpu); /* ^^^ */
  3881. ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus);
  3882. rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
  3883. rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
  3884. rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
  3885. /* An incoming CPU should never be blocking a grace period. */
  3886. if (WARN_ON_ONCE(rnp->qsmask & mask)) { /* RCU waiting on incoming CPU? */
  3887. /* rcu_report_qs_rnp() *really* wants some flags to restore */
  3888. unsigned long flags;
  3889. local_irq_save(flags);
  3890. rcu_disable_urgency_upon_qs(rdp);
  3891. /* Report QS -after- changing ->qsmaskinitnext! */
  3892. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  3893. } else {
  3894. raw_spin_unlock_rcu_node(rnp);
  3895. }
  3896. arch_spin_unlock(&rcu_state.ofl_lock);
  3897. smp_store_release(&rdp->beenonline, true);
  3898. smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
  3899. }
  3900. /*
  3901. * The outgoing function has no further need of RCU, so remove it from
  3902. * the rcu_node tree's ->qsmaskinitnext bit masks.
  3903. *
  3904. * Note that this function is special in that it is invoked directly
  3905. * from the outgoing CPU rather than from the cpuhp_step mechanism.
  3906. * This is because this function must be invoked at a precise location.
  3907. */
  3908. void rcu_report_dead(unsigned int cpu)
  3909. {
  3910. unsigned long flags, seq_flags;
  3911. unsigned long mask;
  3912. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3913. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  3914. // Do any dangling deferred wakeups.
  3915. do_nocb_deferred_wakeup(rdp);
  3916. /* QS for any half-done expedited grace period. */
  3917. rcu_report_exp_rdp(rdp);
  3918. rcu_preempt_deferred_qs(current);
  3919. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  3920. mask = rdp->grpmask;
  3921. local_irq_save(seq_flags);
  3922. arch_spin_lock(&rcu_state.ofl_lock);
  3923. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  3924. rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
  3925. rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
  3926. if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
  3927. /* Report quiescent state -before- changing ->qsmaskinitnext! */
  3928. rcu_disable_urgency_upon_qs(rdp);
  3929. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  3930. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3931. }
  3932. WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask);
  3933. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3934. arch_spin_unlock(&rcu_state.ofl_lock);
  3935. local_irq_restore(seq_flags);
  3936. rdp->cpu_started = false;
  3937. }
  3938. #ifdef CONFIG_HOTPLUG_CPU
  3939. /*
  3940. * The outgoing CPU has just passed through the dying-idle state, and we
  3941. * are being invoked from the CPU that was IPIed to continue the offline
  3942. * operation. Migrate the outgoing CPU's callbacks to the current CPU.
  3943. */
  3944. void rcutree_migrate_callbacks(int cpu)
  3945. {
  3946. unsigned long flags;
  3947. struct rcu_data *my_rdp;
  3948. struct rcu_node *my_rnp;
  3949. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  3950. bool needwake;
  3951. if (rcu_rdp_is_offloaded(rdp) ||
  3952. rcu_segcblist_empty(&rdp->cblist))
  3953. return; /* No callbacks to migrate. */
  3954. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  3955. WARN_ON_ONCE(rcu_rdp_cpu_online(rdp));
  3956. rcu_barrier_entrain(rdp);
  3957. my_rdp = this_cpu_ptr(&rcu_data);
  3958. my_rnp = my_rdp->mynode;
  3959. rcu_nocb_lock(my_rdp); /* irqs already disabled. */
  3960. WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies, false));
  3961. raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */
  3962. /* Leverage recent GPs and set GP for new callbacks. */
  3963. needwake = rcu_advance_cbs(my_rnp, rdp) ||
  3964. rcu_advance_cbs(my_rnp, my_rdp);
  3965. rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
  3966. raw_spin_unlock(&rcu_state.barrier_lock); /* irqs remain disabled. */
  3967. needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
  3968. rcu_segcblist_disable(&rdp->cblist);
  3969. WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) != !rcu_segcblist_n_cbs(&my_rdp->cblist));
  3970. check_cb_ovld_locked(my_rdp, my_rnp);
  3971. if (rcu_rdp_is_offloaded(my_rdp)) {
  3972. raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
  3973. __call_rcu_nocb_wake(my_rdp, true, flags);
  3974. } else {
  3975. rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */
  3976. raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags);
  3977. }
  3978. if (needwake)
  3979. rcu_gp_kthread_wake();
  3980. lockdep_assert_irqs_enabled();
  3981. WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
  3982. !rcu_segcblist_empty(&rdp->cblist),
  3983. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
  3984. cpu, rcu_segcblist_n_cbs(&rdp->cblist),
  3985. rcu_segcblist_first_cb(&rdp->cblist));
  3986. }
  3987. #endif
  3988. /*
  3989. * On non-huge systems, use expedited RCU grace periods to make suspend
  3990. * and hibernation run faster.
  3991. */
  3992. static int rcu_pm_notify(struct notifier_block *self,
  3993. unsigned long action, void *hcpu)
  3994. {
  3995. switch (action) {
  3996. case PM_HIBERNATION_PREPARE:
  3997. case PM_SUSPEND_PREPARE:
  3998. rcu_async_hurry();
  3999. rcu_expedite_gp();
  4000. break;
  4001. case PM_POST_HIBERNATION:
  4002. case PM_POST_SUSPEND:
  4003. rcu_unexpedite_gp();
  4004. rcu_async_relax();
  4005. break;
  4006. default:
  4007. break;
  4008. }
  4009. return NOTIFY_OK;
  4010. }
  4011. #ifdef CONFIG_RCU_EXP_KTHREAD
  4012. struct kthread_worker *rcu_exp_gp_kworker;
  4013. struct kthread_worker *rcu_exp_par_gp_kworker;
  4014. static void __init rcu_start_exp_gp_kworkers(void)
  4015. {
  4016. const char *par_gp_kworker_name = "rcu_exp_par_gp_kthread_worker";
  4017. const char *gp_kworker_name = "rcu_exp_gp_kthread_worker";
  4018. struct sched_param param = { .sched_priority = kthread_prio };
  4019. rcu_exp_gp_kworker = kthread_create_worker(0, gp_kworker_name);
  4020. if (IS_ERR_OR_NULL(rcu_exp_gp_kworker)) {
  4021. pr_err("Failed to create %s!\n", gp_kworker_name);
  4022. return;
  4023. }
  4024. rcu_exp_par_gp_kworker = kthread_create_worker(0, par_gp_kworker_name);
  4025. if (IS_ERR_OR_NULL(rcu_exp_par_gp_kworker)) {
  4026. pr_err("Failed to create %s!\n", par_gp_kworker_name);
  4027. kthread_destroy_worker(rcu_exp_gp_kworker);
  4028. return;
  4029. }
  4030. sched_setscheduler_nocheck(rcu_exp_gp_kworker->task, SCHED_FIFO, &param);
  4031. sched_setscheduler_nocheck(rcu_exp_par_gp_kworker->task, SCHED_FIFO,
  4032. &param);
  4033. }
  4034. static inline void rcu_alloc_par_gp_wq(void)
  4035. {
  4036. }
  4037. #else /* !CONFIG_RCU_EXP_KTHREAD */
  4038. struct workqueue_struct *rcu_par_gp_wq;
  4039. static void __init rcu_start_exp_gp_kworkers(void)
  4040. {
  4041. }
  4042. static inline void rcu_alloc_par_gp_wq(void)
  4043. {
  4044. rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
  4045. WARN_ON(!rcu_par_gp_wq);
  4046. }
  4047. #endif /* CONFIG_RCU_EXP_KTHREAD */
  4048. /*
  4049. * Spawn the kthreads that handle RCU's grace periods.
  4050. */
  4051. static int __init rcu_spawn_gp_kthread(void)
  4052. {
  4053. unsigned long flags;
  4054. struct rcu_node *rnp;
  4055. struct sched_param sp;
  4056. struct task_struct *t;
  4057. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  4058. rcu_scheduler_fully_active = 1;
  4059. t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
  4060. if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
  4061. return 0;
  4062. if (kthread_prio) {
  4063. sp.sched_priority = kthread_prio;
  4064. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  4065. }
  4066. rnp = rcu_get_root();
  4067. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4068. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  4069. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  4070. // Reset .gp_activity and .gp_req_activity before setting .gp_kthread.
  4071. smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */
  4072. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4073. wake_up_process(t);
  4074. /* This is a pre-SMP initcall, we expect a single CPU */
  4075. WARN_ON(num_online_cpus() > 1);
  4076. /*
  4077. * Those kthreads couldn't be created on rcu_init() -> rcutree_prepare_cpu()
  4078. * due to rcu_scheduler_fully_active.
  4079. */
  4080. rcu_spawn_cpu_nocb_kthread(smp_processor_id());
  4081. rcu_spawn_one_boost_kthread(rdp->mynode);
  4082. rcu_spawn_core_kthreads();
  4083. /* Create kthread worker for expedited GPs */
  4084. rcu_start_exp_gp_kworkers();
  4085. return 0;
  4086. }
  4087. early_initcall(rcu_spawn_gp_kthread);
  4088. /*
  4089. * This function is invoked towards the end of the scheduler's
  4090. * initialization process. Before this is called, the idle task might
  4091. * contain synchronous grace-period primitives (during which time, this idle
  4092. * task is booting the system, and such primitives are no-ops). After this
  4093. * function is called, any synchronous grace-period primitives are run as
  4094. * expedited, with the requesting task driving the grace period forward.
  4095. * A later core_initcall() rcu_set_runtime_mode() will switch to full
  4096. * runtime RCU functionality.
  4097. */
  4098. void rcu_scheduler_starting(void)
  4099. {
  4100. unsigned long flags;
  4101. struct rcu_node *rnp;
  4102. WARN_ON(num_online_cpus() != 1);
  4103. WARN_ON(nr_context_switches() > 0);
  4104. rcu_test_sync_prims();
  4105. // Fix up the ->gp_seq counters.
  4106. local_irq_save(flags);
  4107. rcu_for_each_node_breadth_first(rnp)
  4108. rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq;
  4109. local_irq_restore(flags);
  4110. // Switch out of early boot mode.
  4111. rcu_scheduler_active = RCU_SCHEDULER_INIT;
  4112. rcu_test_sync_prims();
  4113. }
  4114. /*
  4115. * Helper function for rcu_init() that initializes the rcu_state structure.
  4116. */
  4117. static void __init rcu_init_one(void)
  4118. {
  4119. static const char * const buf[] = RCU_NODE_NAME_INIT;
  4120. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  4121. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  4122. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  4123. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  4124. int cpustride = 1;
  4125. int i;
  4126. int j;
  4127. struct rcu_node *rnp;
  4128. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  4129. /* Silence gcc 4.8 false positive about array index out of range. */
  4130. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  4131. panic("rcu_init_one: rcu_num_lvls out of range");
  4132. /* Initialize the level-tracking arrays. */
  4133. for (i = 1; i < rcu_num_lvls; i++)
  4134. rcu_state.level[i] =
  4135. rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
  4136. rcu_init_levelspread(levelspread, num_rcu_lvl);
  4137. /* Initialize the elements themselves, starting from the leaves. */
  4138. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  4139. cpustride *= levelspread[i];
  4140. rnp = rcu_state.level[i];
  4141. for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
  4142. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  4143. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  4144. &rcu_node_class[i], buf[i]);
  4145. raw_spin_lock_init(&rnp->fqslock);
  4146. lockdep_set_class_and_name(&rnp->fqslock,
  4147. &rcu_fqs_class[i], fqs[i]);
  4148. rnp->gp_seq = rcu_state.gp_seq;
  4149. rnp->gp_seq_needed = rcu_state.gp_seq;
  4150. rnp->completedqs = rcu_state.gp_seq;
  4151. rnp->qsmask = 0;
  4152. rnp->qsmaskinit = 0;
  4153. rnp->grplo = j * cpustride;
  4154. rnp->grphi = (j + 1) * cpustride - 1;
  4155. if (rnp->grphi >= nr_cpu_ids)
  4156. rnp->grphi = nr_cpu_ids - 1;
  4157. if (i == 0) {
  4158. rnp->grpnum = 0;
  4159. rnp->grpmask = 0;
  4160. rnp->parent = NULL;
  4161. } else {
  4162. rnp->grpnum = j % levelspread[i - 1];
  4163. rnp->grpmask = BIT(rnp->grpnum);
  4164. rnp->parent = rcu_state.level[i - 1] +
  4165. j / levelspread[i - 1];
  4166. }
  4167. rnp->level = i;
  4168. INIT_LIST_HEAD(&rnp->blkd_tasks);
  4169. rcu_init_one_nocb(rnp);
  4170. init_waitqueue_head(&rnp->exp_wq[0]);
  4171. init_waitqueue_head(&rnp->exp_wq[1]);
  4172. init_waitqueue_head(&rnp->exp_wq[2]);
  4173. init_waitqueue_head(&rnp->exp_wq[3]);
  4174. spin_lock_init(&rnp->exp_lock);
  4175. mutex_init(&rnp->boost_kthread_mutex);
  4176. raw_spin_lock_init(&rnp->exp_poll_lock);
  4177. rnp->exp_seq_poll_rq = RCU_GET_STATE_COMPLETED;
  4178. INIT_WORK(&rnp->exp_poll_wq, sync_rcu_do_polled_gp);
  4179. }
  4180. }
  4181. init_swait_queue_head(&rcu_state.gp_wq);
  4182. init_swait_queue_head(&rcu_state.expedited_wq);
  4183. rnp = rcu_first_leaf_node();
  4184. for_each_possible_cpu(i) {
  4185. while (i > rnp->grphi)
  4186. rnp++;
  4187. per_cpu_ptr(&rcu_data, i)->mynode = rnp;
  4188. rcu_boot_init_percpu_data(i);
  4189. }
  4190. }
  4191. /*
  4192. * Force priority from the kernel command-line into range.
  4193. */
  4194. static void __init sanitize_kthread_prio(void)
  4195. {
  4196. int kthread_prio_in = kthread_prio;
  4197. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
  4198. && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
  4199. kthread_prio = 2;
  4200. else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  4201. kthread_prio = 1;
  4202. else if (kthread_prio < 0)
  4203. kthread_prio = 0;
  4204. else if (kthread_prio > 99)
  4205. kthread_prio = 99;
  4206. if (kthread_prio != kthread_prio_in)
  4207. pr_alert("%s: Limited prio to %d from %d\n",
  4208. __func__, kthread_prio, kthread_prio_in);
  4209. }
  4210. /*
  4211. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  4212. * replace the definitions in tree.h because those are needed to size
  4213. * the ->node array in the rcu_state structure.
  4214. */
  4215. void rcu_init_geometry(void)
  4216. {
  4217. ulong d;
  4218. int i;
  4219. static unsigned long old_nr_cpu_ids;
  4220. int rcu_capacity[RCU_NUM_LVLS];
  4221. static bool initialized;
  4222. if (initialized) {
  4223. /*
  4224. * Warn if setup_nr_cpu_ids() had not yet been invoked,
  4225. * unless nr_cpus_ids == NR_CPUS, in which case who cares?
  4226. */
  4227. WARN_ON_ONCE(old_nr_cpu_ids != nr_cpu_ids);
  4228. return;
  4229. }
  4230. old_nr_cpu_ids = nr_cpu_ids;
  4231. initialized = true;
  4232. /*
  4233. * Initialize any unspecified boot parameters.
  4234. * The default values of jiffies_till_first_fqs and
  4235. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  4236. * value, which is a function of HZ, then adding one for each
  4237. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  4238. */
  4239. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  4240. if (jiffies_till_first_fqs == ULONG_MAX)
  4241. jiffies_till_first_fqs = d;
  4242. if (jiffies_till_next_fqs == ULONG_MAX)
  4243. jiffies_till_next_fqs = d;
  4244. adjust_jiffies_till_sched_qs();
  4245. /* If the compile-time values are accurate, just leave. */
  4246. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  4247. nr_cpu_ids == NR_CPUS)
  4248. return;
  4249. pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
  4250. rcu_fanout_leaf, nr_cpu_ids);
  4251. /*
  4252. * The boot-time rcu_fanout_leaf parameter must be at least two
  4253. * and cannot exceed the number of bits in the rcu_node masks.
  4254. * Complain and fall back to the compile-time values if this
  4255. * limit is exceeded.
  4256. */
  4257. if (rcu_fanout_leaf < 2 ||
  4258. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  4259. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  4260. WARN_ON(1);
  4261. return;
  4262. }
  4263. /*
  4264. * Compute number of nodes that can be handled an rcu_node tree
  4265. * with the given number of levels.
  4266. */
  4267. rcu_capacity[0] = rcu_fanout_leaf;
  4268. for (i = 1; i < RCU_NUM_LVLS; i++)
  4269. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  4270. /*
  4271. * The tree must be able to accommodate the configured number of CPUs.
  4272. * If this limit is exceeded, fall back to the compile-time values.
  4273. */
  4274. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  4275. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  4276. WARN_ON(1);
  4277. return;
  4278. }
  4279. /* Calculate the number of levels in the tree. */
  4280. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  4281. }
  4282. rcu_num_lvls = i + 1;
  4283. /* Calculate the number of rcu_nodes at each level of the tree. */
  4284. for (i = 0; i < rcu_num_lvls; i++) {
  4285. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  4286. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  4287. }
  4288. /* Calculate the total number of rcu_node structures. */
  4289. rcu_num_nodes = 0;
  4290. for (i = 0; i < rcu_num_lvls; i++)
  4291. rcu_num_nodes += num_rcu_lvl[i];
  4292. }
  4293. /*
  4294. * Dump out the structure of the rcu_node combining tree associated
  4295. * with the rcu_state structure.
  4296. */
  4297. static void __init rcu_dump_rcu_node_tree(void)
  4298. {
  4299. int level = 0;
  4300. struct rcu_node *rnp;
  4301. pr_info("rcu_node tree layout dump\n");
  4302. pr_info(" ");
  4303. rcu_for_each_node_breadth_first(rnp) {
  4304. if (rnp->level != level) {
  4305. pr_cont("\n");
  4306. pr_info(" ");
  4307. level = rnp->level;
  4308. }
  4309. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  4310. }
  4311. pr_cont("\n");
  4312. }
  4313. struct workqueue_struct *rcu_gp_wq;
  4314. static void __init kfree_rcu_batch_init(void)
  4315. {
  4316. int cpu;
  4317. int i;
  4318. /* Clamp it to [0:100] seconds interval. */
  4319. if (rcu_delay_page_cache_fill_msec < 0 ||
  4320. rcu_delay_page_cache_fill_msec > 100 * MSEC_PER_SEC) {
  4321. rcu_delay_page_cache_fill_msec =
  4322. clamp(rcu_delay_page_cache_fill_msec, 0,
  4323. (int) (100 * MSEC_PER_SEC));
  4324. pr_info("Adjusting rcutree.rcu_delay_page_cache_fill_msec to %d ms.\n",
  4325. rcu_delay_page_cache_fill_msec);
  4326. }
  4327. for_each_possible_cpu(cpu) {
  4328. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  4329. for (i = 0; i < KFREE_N_BATCHES; i++) {
  4330. INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
  4331. krcp->krw_arr[i].krcp = krcp;
  4332. }
  4333. INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
  4334. INIT_DELAYED_WORK(&krcp->page_cache_work, fill_page_cache_func);
  4335. krcp->initialized = true;
  4336. }
  4337. if (register_shrinker(&kfree_rcu_shrinker, "rcu-kfree"))
  4338. pr_err("Failed to register kfree_rcu() shrinker!\n");
  4339. }
  4340. void __init rcu_init(void)
  4341. {
  4342. int cpu = smp_processor_id();
  4343. rcu_early_boot_tests();
  4344. kfree_rcu_batch_init();
  4345. rcu_bootup_announce();
  4346. sanitize_kthread_prio();
  4347. rcu_init_geometry();
  4348. rcu_init_one();
  4349. if (dump_tree)
  4350. rcu_dump_rcu_node_tree();
  4351. if (use_softirq)
  4352. open_softirq(RCU_SOFTIRQ, rcu_core_si);
  4353. /*
  4354. * We don't need protection against CPU-hotplug here because
  4355. * this is called early in boot, before either interrupts
  4356. * or the scheduler are operational.
  4357. */
  4358. pm_notifier(rcu_pm_notify, 0);
  4359. WARN_ON(num_online_cpus() > 1); // Only one CPU this early in boot.
  4360. rcutree_prepare_cpu(cpu);
  4361. rcu_cpu_starting(cpu);
  4362. rcutree_online_cpu(cpu);
  4363. /* Create workqueue for Tree SRCU and for expedited GPs. */
  4364. rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
  4365. WARN_ON(!rcu_gp_wq);
  4366. rcu_alloc_par_gp_wq();
  4367. /* Fill in default value for rcutree.qovld boot parameter. */
  4368. /* -After- the rcu_node ->lock fields are initialized! */
  4369. if (qovld < 0)
  4370. qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark;
  4371. else
  4372. qovld_calc = qovld;
  4373. // Kick-start any polled grace periods that started early.
  4374. if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
  4375. (void)start_poll_synchronize_rcu_expedited();
  4376. }
  4377. #include "tree_stall.h"
  4378. #include "tree_exp.h"
  4379. #include "tree_nocb.h"
  4380. #include "tree_plugin.h"