sched.h 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Scheduler internal types and methods:
  4. */
  5. #ifndef _KERNEL_SCHED_SCHED_H
  6. #define _KERNEL_SCHED_SCHED_H
  7. #include <linux/sched/affinity.h>
  8. #include <linux/sched/autogroup.h>
  9. #include <linux/sched/cpufreq.h>
  10. #include <linux/sched/deadline.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/loadavg.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/sched/rseq_api.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/sched/smt.h>
  17. #include <linux/sched/stat.h>
  18. #include <linux/sched/sysctl.h>
  19. #include <linux/sched/task_flags.h>
  20. #include <linux/sched/task.h>
  21. #include <linux/sched/topology.h>
  22. #include <linux/atomic.h>
  23. #include <linux/bitmap.h>
  24. #include <linux/bug.h>
  25. #include <linux/capability.h>
  26. #include <linux/cgroup_api.h>
  27. #include <linux/cgroup.h>
  28. #include <linux/context_tracking.h>
  29. #include <linux/cpufreq.h>
  30. #include <linux/cpumask_api.h>
  31. #include <linux/ctype.h>
  32. #include <linux/file.h>
  33. #include <linux/fs_api.h>
  34. #include <linux/hrtimer_api.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/irq_work.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/kref_api.h>
  39. #include <linux/kthread.h>
  40. #include <linux/ktime_api.h>
  41. #include <linux/lockdep_api.h>
  42. #include <linux/lockdep.h>
  43. #include <linux/minmax.h>
  44. #include <linux/mm.h>
  45. #include <linux/module.h>
  46. #include <linux/mutex_api.h>
  47. #include <linux/plist.h>
  48. #include <linux/poll.h>
  49. #include <linux/proc_fs.h>
  50. #include <linux/profile.h>
  51. #include <linux/psi.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/seq_file.h>
  54. #include <linux/seqlock.h>
  55. #include <linux/softirq.h>
  56. #include <linux/spinlock_api.h>
  57. #include <linux/static_key.h>
  58. #include <linux/stop_machine.h>
  59. #include <linux/syscalls_api.h>
  60. #include <linux/syscalls.h>
  61. #include <linux/tick.h>
  62. #include <linux/topology.h>
  63. #include <linux/types.h>
  64. #include <linux/u64_stats_sync_api.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/wait_api.h>
  67. #include <linux/wait_bit.h>
  68. #include <linux/workqueue_api.h>
  69. #include <linux/delayacct.h>
  70. #include <trace/events/power.h>
  71. #include <trace/events/sched.h>
  72. #include "../workqueue_internal.h"
  73. struct rq;
  74. struct cfs_rq;
  75. struct rt_rq;
  76. struct sched_group;
  77. struct cpuidle_state;
  78. #ifdef CONFIG_PARAVIRT
  79. # include <asm/paravirt.h>
  80. # include <asm/paravirt_api_clock.h>
  81. #endif
  82. #include <asm/barrier.h>
  83. #include "cpupri.h"
  84. #include "cpudeadline.h"
  85. #ifdef CONFIG_SCHED_DEBUG
  86. # define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
  87. #else
  88. # define SCHED_WARN_ON(x) ({ (void)(x), 0; })
  89. #endif
  90. /* task_struct::on_rq states: */
  91. #define TASK_ON_RQ_QUEUED 1
  92. #define TASK_ON_RQ_MIGRATING 2
  93. extern __read_mostly int scheduler_running;
  94. extern unsigned long calc_load_update;
  95. extern atomic_long_t calc_load_tasks;
  96. extern void calc_global_load_tick(struct rq *this_rq);
  97. extern long calc_load_fold_active(struct rq *this_rq, long adjust);
  98. extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
  99. extern int sysctl_sched_rt_period;
  100. extern int sysctl_sched_rt_runtime;
  101. extern int sched_rr_timeslice;
  102. /*
  103. * Asymmetric CPU capacity bits
  104. */
  105. struct asym_cap_data {
  106. struct list_head link;
  107. struct rcu_head rcu;
  108. unsigned long capacity;
  109. unsigned long cpus[];
  110. };
  111. extern struct list_head asym_cap_list;
  112. #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
  113. /*
  114. * Helpers for converting nanosecond timing to jiffy resolution
  115. */
  116. #define NS_TO_JIFFIES(time) ((unsigned long)(time) / (NSEC_PER_SEC/HZ))
  117. /*
  118. * Increase resolution of nice-level calculations for 64-bit architectures.
  119. * The extra resolution improves shares distribution and load balancing of
  120. * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group
  121. * hierarchies, especially on larger systems. This is not a user-visible change
  122. * and does not change the user-interface for setting shares/weights.
  123. *
  124. * We increase resolution only if we have enough bits to allow this increased
  125. * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
  126. * are pretty high and the returns do not justify the increased costs.
  127. *
  128. * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
  129. * increase coverage and consistency always enable it on 64-bit platforms.
  130. */
  131. #ifdef CONFIG_64BIT
  132. # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
  133. # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
  134. # define scale_load_down(w) \
  135. ({ \
  136. unsigned long __w = (w); \
  137. \
  138. if (__w) \
  139. __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
  140. __w; \
  141. })
  142. #else
  143. # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
  144. # define scale_load(w) (w)
  145. # define scale_load_down(w) (w)
  146. #endif
  147. /*
  148. * Task weight (visible to users) and its load (invisible to users) have
  149. * independent resolution, but they should be well calibrated. We use
  150. * scale_load() and scale_load_down(w) to convert between them. The
  151. * following must be true:
  152. *
  153. * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD
  154. *
  155. */
  156. #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
  157. /*
  158. * Single value that decides SCHED_DEADLINE internal math precision.
  159. * 10 -> just above 1us
  160. * 9 -> just above 0.5us
  161. */
  162. #define DL_SCALE 10
  163. /*
  164. * Single value that denotes runtime == period, ie unlimited time.
  165. */
  166. #define RUNTIME_INF ((u64)~0ULL)
  167. static inline int idle_policy(int policy)
  168. {
  169. return policy == SCHED_IDLE;
  170. }
  171. static inline int normal_policy(int policy)
  172. {
  173. #ifdef CONFIG_SCHED_CLASS_EXT
  174. if (policy == SCHED_EXT)
  175. return true;
  176. #endif
  177. return policy == SCHED_NORMAL;
  178. }
  179. static inline int fair_policy(int policy)
  180. {
  181. return normal_policy(policy) || policy == SCHED_BATCH;
  182. }
  183. static inline int rt_policy(int policy)
  184. {
  185. return policy == SCHED_FIFO || policy == SCHED_RR;
  186. }
  187. static inline int dl_policy(int policy)
  188. {
  189. return policy == SCHED_DEADLINE;
  190. }
  191. static inline bool valid_policy(int policy)
  192. {
  193. return idle_policy(policy) || fair_policy(policy) ||
  194. rt_policy(policy) || dl_policy(policy);
  195. }
  196. static inline int task_has_idle_policy(struct task_struct *p)
  197. {
  198. return idle_policy(p->policy);
  199. }
  200. static inline int task_has_rt_policy(struct task_struct *p)
  201. {
  202. return rt_policy(p->policy);
  203. }
  204. static inline int task_has_dl_policy(struct task_struct *p)
  205. {
  206. return dl_policy(p->policy);
  207. }
  208. #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
  209. static inline void update_avg(u64 *avg, u64 sample)
  210. {
  211. s64 diff = sample - *avg;
  212. *avg += diff / 8;
  213. }
  214. /*
  215. * Shifting a value by an exponent greater *or equal* to the size of said value
  216. * is UB; cap at size-1.
  217. */
  218. #define shr_bound(val, shift) \
  219. (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
  220. /*
  221. * cgroup weight knobs should use the common MIN, DFL and MAX values which are
  222. * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it
  223. * maps pretty well onto the shares value used by scheduler and the round-trip
  224. * conversions preserve the original value over the entire range.
  225. */
  226. static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight)
  227. {
  228. return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL);
  229. }
  230. static inline unsigned long sched_weight_to_cgroup(unsigned long weight)
  231. {
  232. return clamp_t(unsigned long,
  233. DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024),
  234. CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
  235. }
  236. /*
  237. * !! For sched_setattr_nocheck() (kernel) only !!
  238. *
  239. * This is actually gross. :(
  240. *
  241. * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
  242. * tasks, but still be able to sleep. We need this on platforms that cannot
  243. * atomically change clock frequency. Remove once fast switching will be
  244. * available on such platforms.
  245. *
  246. * SUGOV stands for SchedUtil GOVernor.
  247. */
  248. #define SCHED_FLAG_SUGOV 0x10000000
  249. #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
  250. static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se)
  251. {
  252. #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
  253. return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
  254. #else
  255. return false;
  256. #endif
  257. }
  258. /*
  259. * Tells if entity @a should preempt entity @b.
  260. */
  261. static inline bool dl_entity_preempt(const struct sched_dl_entity *a,
  262. const struct sched_dl_entity *b)
  263. {
  264. return dl_entity_is_special(a) ||
  265. dl_time_before(a->deadline, b->deadline);
  266. }
  267. /*
  268. * This is the priority-queue data structure of the RT scheduling class:
  269. */
  270. struct rt_prio_array {
  271. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  272. struct list_head queue[MAX_RT_PRIO];
  273. };
  274. struct rt_bandwidth {
  275. /* nests inside the rq lock: */
  276. raw_spinlock_t rt_runtime_lock;
  277. ktime_t rt_period;
  278. u64 rt_runtime;
  279. struct hrtimer rt_period_timer;
  280. unsigned int rt_period_active;
  281. };
  282. static inline int dl_bandwidth_enabled(void)
  283. {
  284. return sysctl_sched_rt_runtime >= 0;
  285. }
  286. /*
  287. * To keep the bandwidth of -deadline tasks under control
  288. * we need some place where:
  289. * - store the maximum -deadline bandwidth of each cpu;
  290. * - cache the fraction of bandwidth that is currently allocated in
  291. * each root domain;
  292. *
  293. * This is all done in the data structure below. It is similar to the
  294. * one used for RT-throttling (rt_bandwidth), with the main difference
  295. * that, since here we are only interested in admission control, we
  296. * do not decrease any runtime while the group "executes", neither we
  297. * need a timer to replenish it.
  298. *
  299. * With respect to SMP, bandwidth is given on a per root domain basis,
  300. * meaning that:
  301. * - bw (< 100%) is the deadline bandwidth of each CPU;
  302. * - total_bw is the currently allocated bandwidth in each root domain;
  303. */
  304. struct dl_bw {
  305. raw_spinlock_t lock;
  306. u64 bw;
  307. u64 total_bw;
  308. };
  309. extern void init_dl_bw(struct dl_bw *dl_b);
  310. extern int sched_dl_global_validate(void);
  311. extern void sched_dl_do_global(void);
  312. extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
  313. extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
  314. extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
  315. extern bool __checkparam_dl(const struct sched_attr *attr);
  316. extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
  317. extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
  318. extern int dl_bw_check_overflow(int cpu);
  319. extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec);
  320. /*
  321. * SCHED_DEADLINE supports servers (nested scheduling) with the following
  322. * interface:
  323. *
  324. * dl_se::rq -- runqueue we belong to.
  325. *
  326. * dl_se::server_has_tasks() -- used on bandwidth enforcement; we 'stop' the
  327. * server when it runs out of tasks to run.
  328. *
  329. * dl_se::server_pick() -- nested pick_next_task(); we yield the period if this
  330. * returns NULL.
  331. *
  332. * dl_server_update() -- called from update_curr_common(), propagates runtime
  333. * to the server.
  334. *
  335. * dl_server_start()
  336. * dl_server_stop() -- start/stop the server when it has (no) tasks.
  337. *
  338. * dl_server_init() -- initializes the server.
  339. */
  340. extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec);
  341. extern void dl_server_start(struct sched_dl_entity *dl_se);
  342. extern void dl_server_stop(struct sched_dl_entity *dl_se);
  343. extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
  344. dl_server_has_tasks_f has_tasks,
  345. dl_server_pick_f pick_task);
  346. extern void dl_server_update_idle_time(struct rq *rq,
  347. struct task_struct *p);
  348. extern void fair_server_init(struct rq *rq);
  349. extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
  350. extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
  351. u64 runtime, u64 period, bool init);
  352. static inline bool dl_server_active(struct sched_dl_entity *dl_se)
  353. {
  354. return dl_se->dl_server_active;
  355. }
  356. #ifdef CONFIG_CGROUP_SCHED
  357. extern struct list_head task_groups;
  358. struct cfs_bandwidth {
  359. #ifdef CONFIG_CFS_BANDWIDTH
  360. raw_spinlock_t lock;
  361. ktime_t period;
  362. u64 quota;
  363. u64 runtime;
  364. u64 burst;
  365. u64 runtime_snap;
  366. s64 hierarchical_quota;
  367. u8 idle;
  368. u8 period_active;
  369. u8 slack_started;
  370. struct hrtimer period_timer;
  371. struct hrtimer slack_timer;
  372. struct list_head throttled_cfs_rq;
  373. /* Statistics: */
  374. int nr_periods;
  375. int nr_throttled;
  376. int nr_burst;
  377. u64 throttled_time;
  378. u64 burst_time;
  379. #endif
  380. };
  381. /* Task group related information */
  382. struct task_group {
  383. struct cgroup_subsys_state css;
  384. #ifdef CONFIG_GROUP_SCHED_WEIGHT
  385. /* A positive value indicates that this is a SCHED_IDLE group. */
  386. int idle;
  387. #endif
  388. #ifdef CONFIG_FAIR_GROUP_SCHED
  389. /* schedulable entities of this group on each CPU */
  390. struct sched_entity **se;
  391. /* runqueue "owned" by this group on each CPU */
  392. struct cfs_rq **cfs_rq;
  393. unsigned long shares;
  394. #ifdef CONFIG_SMP
  395. /*
  396. * load_avg can be heavily contended at clock tick time, so put
  397. * it in its own cache-line separated from the fields above which
  398. * will also be accessed at each tick.
  399. */
  400. atomic_long_t load_avg ____cacheline_aligned;
  401. #endif
  402. #endif
  403. #ifdef CONFIG_RT_GROUP_SCHED
  404. struct sched_rt_entity **rt_se;
  405. struct rt_rq **rt_rq;
  406. struct rt_bandwidth rt_bandwidth;
  407. #endif
  408. #ifdef CONFIG_EXT_GROUP_SCHED
  409. u32 scx_flags; /* SCX_TG_* */
  410. u32 scx_weight;
  411. #endif
  412. struct rcu_head rcu;
  413. struct list_head list;
  414. struct task_group *parent;
  415. struct list_head siblings;
  416. struct list_head children;
  417. #ifdef CONFIG_SCHED_AUTOGROUP
  418. struct autogroup *autogroup;
  419. #endif
  420. struct cfs_bandwidth cfs_bandwidth;
  421. #ifdef CONFIG_UCLAMP_TASK_GROUP
  422. /* The two decimal precision [%] value requested from user-space */
  423. unsigned int uclamp_pct[UCLAMP_CNT];
  424. /* Clamp values requested for a task group */
  425. struct uclamp_se uclamp_req[UCLAMP_CNT];
  426. /* Effective clamp values used for a task group */
  427. struct uclamp_se uclamp[UCLAMP_CNT];
  428. #endif
  429. };
  430. #ifdef CONFIG_GROUP_SCHED_WEIGHT
  431. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  432. /*
  433. * A weight of 0 or 1 can cause arithmetics problems.
  434. * A weight of a cfs_rq is the sum of weights of which entities
  435. * are queued on this cfs_rq, so a weight of a entity should not be
  436. * too large, so as the shares value of a task group.
  437. * (The default weight is 1024 - so there's no practical
  438. * limitation from this.)
  439. */
  440. #define MIN_SHARES (1UL << 1)
  441. #define MAX_SHARES (1UL << 18)
  442. #endif
  443. typedef int (*tg_visitor)(struct task_group *, void *);
  444. extern int walk_tg_tree_from(struct task_group *from,
  445. tg_visitor down, tg_visitor up, void *data);
  446. /*
  447. * Iterate the full tree, calling @down when first entering a node and @up when
  448. * leaving it for the final time.
  449. *
  450. * Caller must hold rcu_lock or sufficient equivalent.
  451. */
  452. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  453. {
  454. return walk_tg_tree_from(&root_task_group, down, up, data);
  455. }
  456. static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
  457. {
  458. return css ? container_of(css, struct task_group, css) : NULL;
  459. }
  460. extern int tg_nop(struct task_group *tg, void *data);
  461. #ifdef CONFIG_FAIR_GROUP_SCHED
  462. extern void free_fair_sched_group(struct task_group *tg);
  463. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  464. extern void online_fair_sched_group(struct task_group *tg);
  465. extern void unregister_fair_sched_group(struct task_group *tg);
  466. #else
  467. static inline void free_fair_sched_group(struct task_group *tg) { }
  468. static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  469. {
  470. return 1;
  471. }
  472. static inline void online_fair_sched_group(struct task_group *tg) { }
  473. static inline void unregister_fair_sched_group(struct task_group *tg) { }
  474. #endif
  475. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  476. struct sched_entity *se, int cpu,
  477. struct sched_entity *parent);
  478. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
  479. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  480. extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  481. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  482. extern bool cfs_task_bw_constrained(struct task_struct *p);
  483. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  484. struct sched_rt_entity *rt_se, int cpu,
  485. struct sched_rt_entity *parent);
  486. extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
  487. extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
  488. extern long sched_group_rt_runtime(struct task_group *tg);
  489. extern long sched_group_rt_period(struct task_group *tg);
  490. extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
  491. extern struct task_group *sched_create_group(struct task_group *parent);
  492. extern void sched_online_group(struct task_group *tg,
  493. struct task_group *parent);
  494. extern void sched_destroy_group(struct task_group *tg);
  495. extern void sched_release_group(struct task_group *tg);
  496. extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
  497. #ifdef CONFIG_FAIR_GROUP_SCHED
  498. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  499. extern int sched_group_set_idle(struct task_group *tg, long idle);
  500. #ifdef CONFIG_SMP
  501. extern void set_task_rq_fair(struct sched_entity *se,
  502. struct cfs_rq *prev, struct cfs_rq *next);
  503. #else /* !CONFIG_SMP */
  504. static inline void set_task_rq_fair(struct sched_entity *se,
  505. struct cfs_rq *prev, struct cfs_rq *next) { }
  506. #endif /* CONFIG_SMP */
  507. #else /* !CONFIG_FAIR_GROUP_SCHED */
  508. static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; }
  509. static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; }
  510. #endif /* CONFIG_FAIR_GROUP_SCHED */
  511. #else /* CONFIG_CGROUP_SCHED */
  512. struct cfs_bandwidth { };
  513. static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; }
  514. #endif /* CONFIG_CGROUP_SCHED */
  515. extern void unregister_rt_sched_group(struct task_group *tg);
  516. extern void free_rt_sched_group(struct task_group *tg);
  517. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  518. /*
  519. * u64_u32_load/u64_u32_store
  520. *
  521. * Use a copy of a u64 value to protect against data race. This is only
  522. * applicable for 32-bits architectures.
  523. */
  524. #ifdef CONFIG_64BIT
  525. # define u64_u32_load_copy(var, copy) var
  526. # define u64_u32_store_copy(var, copy, val) (var = val)
  527. #else
  528. # define u64_u32_load_copy(var, copy) \
  529. ({ \
  530. u64 __val, __val_copy; \
  531. do { \
  532. __val_copy = copy; \
  533. /* \
  534. * paired with u64_u32_store_copy(), ordering access \
  535. * to var and copy. \
  536. */ \
  537. smp_rmb(); \
  538. __val = var; \
  539. } while (__val != __val_copy); \
  540. __val; \
  541. })
  542. # define u64_u32_store_copy(var, copy, val) \
  543. do { \
  544. typeof(val) __val = (val); \
  545. var = __val; \
  546. /* \
  547. * paired with u64_u32_load_copy(), ordering access to var and \
  548. * copy. \
  549. */ \
  550. smp_wmb(); \
  551. copy = __val; \
  552. } while (0)
  553. #endif
  554. # define u64_u32_load(var) u64_u32_load_copy(var, var##_copy)
  555. # define u64_u32_store(var, val) u64_u32_store_copy(var, var##_copy, val)
  556. struct balance_callback {
  557. struct balance_callback *next;
  558. void (*func)(struct rq *rq);
  559. };
  560. /* CFS-related fields in a runqueue */
  561. struct cfs_rq {
  562. struct load_weight load;
  563. unsigned int nr_running;
  564. unsigned int h_nr_queued; /* SCHED_{NORMAL,BATCH,IDLE} */
  565. unsigned int h_nr_runnable; /* SCHED_{NORMAL,BATCH,IDLE} */
  566. unsigned int idle_nr_running; /* SCHED_IDLE */
  567. unsigned int idle_h_nr_running; /* SCHED_IDLE */
  568. unsigned int h_nr_delayed;
  569. s64 avg_vruntime;
  570. u64 avg_load;
  571. u64 min_vruntime;
  572. #ifdef CONFIG_SCHED_CORE
  573. unsigned int forceidle_seq;
  574. u64 min_vruntime_fi;
  575. #endif
  576. struct rb_root_cached tasks_timeline;
  577. /*
  578. * 'curr' points to currently running entity on this cfs_rq.
  579. * It is set to NULL otherwise (i.e when none are currently running).
  580. */
  581. struct sched_entity *curr;
  582. struct sched_entity *next;
  583. #ifdef CONFIG_SMP
  584. /*
  585. * CFS load tracking
  586. */
  587. struct sched_avg avg;
  588. #ifndef CONFIG_64BIT
  589. u64 last_update_time_copy;
  590. #endif
  591. struct {
  592. raw_spinlock_t lock ____cacheline_aligned;
  593. int nr;
  594. unsigned long load_avg;
  595. unsigned long util_avg;
  596. unsigned long runnable_avg;
  597. } removed;
  598. #ifdef CONFIG_FAIR_GROUP_SCHED
  599. u64 last_update_tg_load_avg;
  600. unsigned long tg_load_avg_contrib;
  601. long propagate;
  602. long prop_runnable_sum;
  603. /*
  604. * h_load = weight * f(tg)
  605. *
  606. * Where f(tg) is the recursive weight fraction assigned to
  607. * this group.
  608. */
  609. unsigned long h_load;
  610. u64 last_h_load_update;
  611. struct sched_entity *h_load_next;
  612. #endif /* CONFIG_FAIR_GROUP_SCHED */
  613. #endif /* CONFIG_SMP */
  614. #ifdef CONFIG_FAIR_GROUP_SCHED
  615. struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
  616. /*
  617. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  618. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  619. * (like users, containers etc.)
  620. *
  621. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
  622. * This list is used during load balance.
  623. */
  624. int on_list;
  625. struct list_head leaf_cfs_rq_list;
  626. struct task_group *tg; /* group that "owns" this runqueue */
  627. /* Locally cached copy of our task_group's idle value */
  628. int idle;
  629. #ifdef CONFIG_CFS_BANDWIDTH
  630. int runtime_enabled;
  631. s64 runtime_remaining;
  632. u64 throttled_pelt_idle;
  633. #ifndef CONFIG_64BIT
  634. u64 throttled_pelt_idle_copy;
  635. #endif
  636. u64 throttled_clock;
  637. u64 throttled_clock_pelt;
  638. u64 throttled_clock_pelt_time;
  639. u64 throttled_clock_self;
  640. u64 throttled_clock_self_time;
  641. int throttled;
  642. int throttle_count;
  643. struct list_head throttled_list;
  644. struct list_head throttled_csd_list;
  645. #endif /* CONFIG_CFS_BANDWIDTH */
  646. #endif /* CONFIG_FAIR_GROUP_SCHED */
  647. };
  648. #ifdef CONFIG_SCHED_CLASS_EXT
  649. /* scx_rq->flags, protected by the rq lock */
  650. enum scx_rq_flags {
  651. /*
  652. * A hotplugged CPU starts scheduling before rq_online_scx(). Track
  653. * ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called
  654. * only while the BPF scheduler considers the CPU to be online.
  655. */
  656. SCX_RQ_ONLINE = 1 << 0,
  657. SCX_RQ_CAN_STOP_TICK = 1 << 1,
  658. SCX_RQ_BAL_PENDING = 1 << 2, /* balance hasn't run yet */
  659. SCX_RQ_BAL_KEEP = 1 << 3, /* balance decided to keep current */
  660. SCX_RQ_BYPASSING = 1 << 4,
  661. SCX_RQ_IN_WAKEUP = 1 << 16,
  662. SCX_RQ_IN_BALANCE = 1 << 17,
  663. };
  664. struct scx_rq {
  665. struct scx_dispatch_q local_dsq;
  666. struct list_head runnable_list; /* runnable tasks on this rq */
  667. struct list_head ddsp_deferred_locals; /* deferred ddsps from enq */
  668. unsigned long ops_qseq;
  669. u64 extra_enq_flags; /* see move_task_to_local_dsq() */
  670. u32 nr_running;
  671. u32 flags;
  672. u32 cpuperf_target; /* [0, SCHED_CAPACITY_SCALE] */
  673. bool cpu_released;
  674. cpumask_var_t cpus_to_kick;
  675. cpumask_var_t cpus_to_kick_if_idle;
  676. cpumask_var_t cpus_to_preempt;
  677. cpumask_var_t cpus_to_wait;
  678. unsigned long pnt_seq;
  679. struct balance_callback deferred_bal_cb;
  680. struct irq_work deferred_irq_work;
  681. struct irq_work kick_cpus_irq_work;
  682. };
  683. #endif /* CONFIG_SCHED_CLASS_EXT */
  684. static inline int rt_bandwidth_enabled(void)
  685. {
  686. return sysctl_sched_rt_runtime >= 0;
  687. }
  688. /* RT IPI pull logic requires IRQ_WORK */
  689. #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
  690. # define HAVE_RT_PUSH_IPI
  691. #endif
  692. /* Real-Time classes' related field in a runqueue: */
  693. struct rt_rq {
  694. struct rt_prio_array active;
  695. unsigned int rt_nr_running;
  696. unsigned int rr_nr_running;
  697. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  698. struct {
  699. int curr; /* highest queued rt task prio */
  700. #ifdef CONFIG_SMP
  701. int next; /* next highest */
  702. #endif
  703. } highest_prio;
  704. #endif
  705. #ifdef CONFIG_SMP
  706. bool overloaded;
  707. struct plist_head pushable_tasks;
  708. #endif /* CONFIG_SMP */
  709. int rt_queued;
  710. #ifdef CONFIG_RT_GROUP_SCHED
  711. int rt_throttled;
  712. u64 rt_time;
  713. u64 rt_runtime;
  714. /* Nests inside the rq lock: */
  715. raw_spinlock_t rt_runtime_lock;
  716. unsigned int rt_nr_boosted;
  717. struct rq *rq;
  718. struct task_group *tg;
  719. #endif
  720. };
  721. static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
  722. {
  723. return rt_rq->rt_queued && rt_rq->rt_nr_running;
  724. }
  725. /* Deadline class' related fields in a runqueue */
  726. struct dl_rq {
  727. /* runqueue is an rbtree, ordered by deadline */
  728. struct rb_root_cached root;
  729. unsigned int dl_nr_running;
  730. #ifdef CONFIG_SMP
  731. /*
  732. * Deadline values of the currently executing and the
  733. * earliest ready task on this rq. Caching these facilitates
  734. * the decision whether or not a ready but not running task
  735. * should migrate somewhere else.
  736. */
  737. struct {
  738. u64 curr;
  739. u64 next;
  740. } earliest_dl;
  741. bool overloaded;
  742. /*
  743. * Tasks on this rq that can be pushed away. They are kept in
  744. * an rb-tree, ordered by tasks' deadlines, with caching
  745. * of the leftmost (earliest deadline) element.
  746. */
  747. struct rb_root_cached pushable_dl_tasks_root;
  748. #else
  749. struct dl_bw dl_bw;
  750. #endif
  751. /*
  752. * "Active utilization" for this runqueue: increased when a
  753. * task wakes up (becomes TASK_RUNNING) and decreased when a
  754. * task blocks
  755. */
  756. u64 running_bw;
  757. /*
  758. * Utilization of the tasks "assigned" to this runqueue (including
  759. * the tasks that are in runqueue and the tasks that executed on this
  760. * CPU and blocked). Increased when a task moves to this runqueue, and
  761. * decreased when the task moves away (migrates, changes scheduling
  762. * policy, or terminates).
  763. * This is needed to compute the "inactive utilization" for the
  764. * runqueue (inactive utilization = this_bw - running_bw).
  765. */
  766. u64 this_bw;
  767. u64 extra_bw;
  768. /*
  769. * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM
  770. * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB).
  771. */
  772. u64 max_bw;
  773. /*
  774. * Inverse of the fraction of CPU utilization that can be reclaimed
  775. * by the GRUB algorithm.
  776. */
  777. u64 bw_ratio;
  778. };
  779. #ifdef CONFIG_FAIR_GROUP_SCHED
  780. /* An entity is a task if it doesn't "own" a runqueue */
  781. #define entity_is_task(se) (!se->my_q)
  782. static inline void se_update_runnable(struct sched_entity *se)
  783. {
  784. if (!entity_is_task(se)) {
  785. struct cfs_rq *cfs_rq = se->my_q;
  786. se->runnable_weight = cfs_rq->h_nr_queued - cfs_rq->h_nr_delayed;
  787. }
  788. }
  789. static inline long se_runnable(struct sched_entity *se)
  790. {
  791. if (se->sched_delayed)
  792. return false;
  793. if (entity_is_task(se))
  794. return !!se->on_rq;
  795. else
  796. return se->runnable_weight;
  797. }
  798. #else /* !CONFIG_FAIR_GROUP_SCHED: */
  799. #define entity_is_task(se) 1
  800. static inline void se_update_runnable(struct sched_entity *se) { }
  801. static inline long se_runnable(struct sched_entity *se)
  802. {
  803. if (se->sched_delayed)
  804. return false;
  805. return !!se->on_rq;
  806. }
  807. #endif /* !CONFIG_FAIR_GROUP_SCHED */
  808. #ifdef CONFIG_SMP
  809. /*
  810. * XXX we want to get rid of these helpers and use the full load resolution.
  811. */
  812. static inline long se_weight(struct sched_entity *se)
  813. {
  814. return scale_load_down(se->load.weight);
  815. }
  816. static inline bool sched_asym_prefer(int a, int b)
  817. {
  818. return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
  819. }
  820. struct perf_domain {
  821. struct em_perf_domain *em_pd;
  822. struct perf_domain *next;
  823. struct rcu_head rcu;
  824. };
  825. /*
  826. * We add the notion of a root-domain which will be used to define per-domain
  827. * variables. Each exclusive cpuset essentially defines an island domain by
  828. * fully partitioning the member CPUs from any other cpuset. Whenever a new
  829. * exclusive cpuset is created, we also create and attach a new root-domain
  830. * object.
  831. *
  832. */
  833. struct root_domain {
  834. atomic_t refcount;
  835. atomic_t rto_count;
  836. struct rcu_head rcu;
  837. cpumask_var_t span;
  838. cpumask_var_t online;
  839. /*
  840. * Indicate pullable load on at least one CPU, e.g:
  841. * - More than one runnable task
  842. * - Running task is misfit
  843. */
  844. bool overloaded;
  845. /* Indicate one or more CPUs over-utilized (tipping point) */
  846. bool overutilized;
  847. /*
  848. * The bit corresponding to a CPU gets set here if such CPU has more
  849. * than one runnable -deadline task (as it is below for RT tasks).
  850. */
  851. cpumask_var_t dlo_mask;
  852. atomic_t dlo_count;
  853. struct dl_bw dl_bw;
  854. struct cpudl cpudl;
  855. /*
  856. * Indicate whether a root_domain's dl_bw has been checked or
  857. * updated. It's monotonously increasing value.
  858. *
  859. * Also, some corner cases, like 'wrap around' is dangerous, but given
  860. * that u64 is 'big enough'. So that shouldn't be a concern.
  861. */
  862. u64 visit_gen;
  863. #ifdef HAVE_RT_PUSH_IPI
  864. /*
  865. * For IPI pull requests, loop across the rto_mask.
  866. */
  867. struct irq_work rto_push_work;
  868. raw_spinlock_t rto_lock;
  869. /* These are only updated and read within rto_lock */
  870. int rto_loop;
  871. int rto_cpu;
  872. /* These atomics are updated outside of a lock */
  873. atomic_t rto_loop_next;
  874. atomic_t rto_loop_start;
  875. #endif
  876. /*
  877. * The "RT overload" flag: it gets set if a CPU has more than
  878. * one runnable RT task.
  879. */
  880. cpumask_var_t rto_mask;
  881. struct cpupri cpupri;
  882. /*
  883. * NULL-terminated list of performance domains intersecting with the
  884. * CPUs of the rd. Protected by RCU.
  885. */
  886. struct perf_domain __rcu *pd;
  887. };
  888. extern void init_defrootdomain(void);
  889. extern int sched_init_domains(const struct cpumask *cpu_map);
  890. extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
  891. extern void sched_get_rd(struct root_domain *rd);
  892. extern void sched_put_rd(struct root_domain *rd);
  893. static inline int get_rd_overloaded(struct root_domain *rd)
  894. {
  895. return READ_ONCE(rd->overloaded);
  896. }
  897. static inline void set_rd_overloaded(struct root_domain *rd, int status)
  898. {
  899. if (get_rd_overloaded(rd) != status)
  900. WRITE_ONCE(rd->overloaded, status);
  901. }
  902. #ifdef HAVE_RT_PUSH_IPI
  903. extern void rto_push_irq_work_func(struct irq_work *work);
  904. #endif
  905. #endif /* CONFIG_SMP */
  906. #ifdef CONFIG_UCLAMP_TASK
  907. /*
  908. * struct uclamp_bucket - Utilization clamp bucket
  909. * @value: utilization clamp value for tasks on this clamp bucket
  910. * @tasks: number of RUNNABLE tasks on this clamp bucket
  911. *
  912. * Keep track of how many tasks are RUNNABLE for a given utilization
  913. * clamp value.
  914. */
  915. struct uclamp_bucket {
  916. unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
  917. unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
  918. };
  919. /*
  920. * struct uclamp_rq - rq's utilization clamp
  921. * @value: currently active clamp values for a rq
  922. * @bucket: utilization clamp buckets affecting a rq
  923. *
  924. * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
  925. * A clamp value is affecting a rq when there is at least one task RUNNABLE
  926. * (or actually running) with that value.
  927. *
  928. * There are up to UCLAMP_CNT possible different clamp values, currently there
  929. * are only two: minimum utilization and maximum utilization.
  930. *
  931. * All utilization clamping values are MAX aggregated, since:
  932. * - for util_min: we want to run the CPU at least at the max of the minimum
  933. * utilization required by its currently RUNNABLE tasks.
  934. * - for util_max: we want to allow the CPU to run up to the max of the
  935. * maximum utilization allowed by its currently RUNNABLE tasks.
  936. *
  937. * Since on each system we expect only a limited number of different
  938. * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
  939. * the metrics required to compute all the per-rq utilization clamp values.
  940. */
  941. struct uclamp_rq {
  942. unsigned int value;
  943. struct uclamp_bucket bucket[UCLAMP_BUCKETS];
  944. };
  945. DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
  946. #endif /* CONFIG_UCLAMP_TASK */
  947. /*
  948. * This is the main, per-CPU runqueue data structure.
  949. *
  950. * Locking rule: those places that want to lock multiple runqueues
  951. * (such as the load balancing or the thread migration code), lock
  952. * acquire operations must be ordered by ascending &runqueue.
  953. */
  954. struct rq {
  955. /* runqueue lock: */
  956. raw_spinlock_t __lock;
  957. unsigned int nr_running;
  958. #ifdef CONFIG_NUMA_BALANCING
  959. unsigned int nr_numa_running;
  960. unsigned int nr_preferred_running;
  961. unsigned int numa_migrate_on;
  962. #endif
  963. #ifdef CONFIG_NO_HZ_COMMON
  964. #ifdef CONFIG_SMP
  965. unsigned long last_blocked_load_update_tick;
  966. unsigned int has_blocked_load;
  967. call_single_data_t nohz_csd;
  968. #endif /* CONFIG_SMP */
  969. unsigned int nohz_tick_stopped;
  970. atomic_t nohz_flags;
  971. #endif /* CONFIG_NO_HZ_COMMON */
  972. #ifdef CONFIG_SMP
  973. unsigned int ttwu_pending;
  974. #endif
  975. u64 nr_switches;
  976. #ifdef CONFIG_UCLAMP_TASK
  977. /* Utilization clamp values based on CPU's RUNNABLE tasks */
  978. struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
  979. unsigned int uclamp_flags;
  980. #define UCLAMP_FLAG_IDLE 0x01
  981. #endif
  982. struct cfs_rq cfs;
  983. struct rt_rq rt;
  984. struct dl_rq dl;
  985. #ifdef CONFIG_SCHED_CLASS_EXT
  986. struct scx_rq scx;
  987. #endif
  988. struct sched_dl_entity fair_server;
  989. #ifdef CONFIG_FAIR_GROUP_SCHED
  990. /* list of leaf cfs_rq on this CPU: */
  991. struct list_head leaf_cfs_rq_list;
  992. struct list_head *tmp_alone_branch;
  993. #endif /* CONFIG_FAIR_GROUP_SCHED */
  994. /*
  995. * This is part of a global counter where only the total sum
  996. * over all CPUs matters. A task can increase this counter on
  997. * one CPU and if it got migrated afterwards it may decrease
  998. * it on another CPU. Always updated under the runqueue lock:
  999. */
  1000. unsigned long nr_uninterruptible;
  1001. struct task_struct __rcu *curr;
  1002. struct sched_dl_entity *dl_server;
  1003. struct task_struct *idle;
  1004. struct task_struct *stop;
  1005. unsigned long next_balance;
  1006. struct mm_struct *prev_mm;
  1007. unsigned int clock_update_flags;
  1008. u64 clock;
  1009. /* Ensure that all clocks are in the same cache line */
  1010. u64 clock_task ____cacheline_aligned;
  1011. u64 clock_pelt;
  1012. unsigned long lost_idle_time;
  1013. u64 clock_pelt_idle;
  1014. u64 clock_idle;
  1015. #ifndef CONFIG_64BIT
  1016. u64 clock_pelt_idle_copy;
  1017. u64 clock_idle_copy;
  1018. #endif
  1019. atomic_t nr_iowait;
  1020. #ifdef CONFIG_SCHED_DEBUG
  1021. u64 last_seen_need_resched_ns;
  1022. int ticks_without_resched;
  1023. #endif
  1024. #ifdef CONFIG_MEMBARRIER
  1025. int membarrier_state;
  1026. #endif
  1027. #ifdef CONFIG_SMP
  1028. struct root_domain *rd;
  1029. struct sched_domain __rcu *sd;
  1030. unsigned long cpu_capacity;
  1031. struct balance_callback *balance_callback;
  1032. unsigned char nohz_idle_balance;
  1033. unsigned char idle_balance;
  1034. unsigned long misfit_task_load;
  1035. /* For active balancing */
  1036. int active_balance;
  1037. int push_cpu;
  1038. struct cpu_stop_work active_balance_work;
  1039. /* CPU of this runqueue: */
  1040. int cpu;
  1041. int online;
  1042. struct list_head cfs_tasks;
  1043. struct sched_avg avg_rt;
  1044. struct sched_avg avg_dl;
  1045. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  1046. struct sched_avg avg_irq;
  1047. #endif
  1048. #ifdef CONFIG_SCHED_HW_PRESSURE
  1049. struct sched_avg avg_hw;
  1050. #endif
  1051. u64 idle_stamp;
  1052. u64 avg_idle;
  1053. /* This is used to determine avg_idle's max value */
  1054. u64 max_idle_balance_cost;
  1055. #ifdef CONFIG_HOTPLUG_CPU
  1056. struct rcuwait hotplug_wait;
  1057. #endif
  1058. #endif /* CONFIG_SMP */
  1059. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1060. u64 prev_irq_time;
  1061. u64 psi_irq_time;
  1062. #endif
  1063. #ifdef CONFIG_PARAVIRT
  1064. u64 prev_steal_time;
  1065. #endif
  1066. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  1067. u64 prev_steal_time_rq;
  1068. #endif
  1069. /* calc_load related fields */
  1070. unsigned long calc_load_update;
  1071. long calc_load_active;
  1072. #ifdef CONFIG_SCHED_HRTICK
  1073. #ifdef CONFIG_SMP
  1074. call_single_data_t hrtick_csd;
  1075. #endif
  1076. struct hrtimer hrtick_timer;
  1077. ktime_t hrtick_time;
  1078. #endif
  1079. #ifdef CONFIG_SCHEDSTATS
  1080. /* latency stats */
  1081. struct sched_info rq_sched_info;
  1082. unsigned long long rq_cpu_time;
  1083. /* sys_sched_yield() stats */
  1084. unsigned int yld_count;
  1085. /* schedule() stats */
  1086. unsigned int sched_count;
  1087. unsigned int sched_goidle;
  1088. /* try_to_wake_up() stats */
  1089. unsigned int ttwu_count;
  1090. unsigned int ttwu_local;
  1091. #endif
  1092. #ifdef CONFIG_CPU_IDLE
  1093. /* Must be inspected within a RCU lock section */
  1094. struct cpuidle_state *idle_state;
  1095. #endif
  1096. #ifdef CONFIG_SMP
  1097. unsigned int nr_pinned;
  1098. #endif
  1099. unsigned int push_busy;
  1100. struct cpu_stop_work push_work;
  1101. #ifdef CONFIG_SCHED_CORE
  1102. /* per rq */
  1103. struct rq *core;
  1104. struct task_struct *core_pick;
  1105. struct sched_dl_entity *core_dl_server;
  1106. unsigned int core_enabled;
  1107. unsigned int core_sched_seq;
  1108. struct rb_root core_tree;
  1109. /* shared state -- careful with sched_core_cpu_deactivate() */
  1110. unsigned int core_task_seq;
  1111. unsigned int core_pick_seq;
  1112. unsigned long core_cookie;
  1113. unsigned int core_forceidle_count;
  1114. unsigned int core_forceidle_seq;
  1115. unsigned int core_forceidle_occupation;
  1116. u64 core_forceidle_start;
  1117. #endif
  1118. /* Scratch cpumask to be temporarily used under rq_lock */
  1119. cpumask_var_t scratch_mask;
  1120. #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP)
  1121. call_single_data_t cfsb_csd;
  1122. struct list_head cfsb_csd_list;
  1123. #endif
  1124. };
  1125. #ifdef CONFIG_FAIR_GROUP_SCHED
  1126. /* CPU runqueue to which this cfs_rq is attached */
  1127. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  1128. {
  1129. return cfs_rq->rq;
  1130. }
  1131. #else
  1132. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  1133. {
  1134. return container_of(cfs_rq, struct rq, cfs);
  1135. }
  1136. #endif
  1137. static inline int cpu_of(struct rq *rq)
  1138. {
  1139. #ifdef CONFIG_SMP
  1140. return rq->cpu;
  1141. #else
  1142. return 0;
  1143. #endif
  1144. }
  1145. #define MDF_PUSH 0x01
  1146. static inline bool is_migration_disabled(struct task_struct *p)
  1147. {
  1148. #ifdef CONFIG_SMP
  1149. return p->migration_disabled;
  1150. #else
  1151. return false;
  1152. #endif
  1153. }
  1154. DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  1155. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  1156. #define this_rq() this_cpu_ptr(&runqueues)
  1157. #define task_rq(p) cpu_rq(task_cpu(p))
  1158. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  1159. #define raw_rq() raw_cpu_ptr(&runqueues)
  1160. #ifdef CONFIG_SCHED_CORE
  1161. static inline struct cpumask *sched_group_span(struct sched_group *sg);
  1162. DECLARE_STATIC_KEY_FALSE(__sched_core_enabled);
  1163. static inline bool sched_core_enabled(struct rq *rq)
  1164. {
  1165. return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled;
  1166. }
  1167. static inline bool sched_core_disabled(void)
  1168. {
  1169. return !static_branch_unlikely(&__sched_core_enabled);
  1170. }
  1171. /*
  1172. * Be careful with this function; not for general use. The return value isn't
  1173. * stable unless you actually hold a relevant rq->__lock.
  1174. */
  1175. static inline raw_spinlock_t *rq_lockp(struct rq *rq)
  1176. {
  1177. if (sched_core_enabled(rq))
  1178. return &rq->core->__lock;
  1179. return &rq->__lock;
  1180. }
  1181. static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
  1182. {
  1183. if (rq->core_enabled)
  1184. return &rq->core->__lock;
  1185. return &rq->__lock;
  1186. }
  1187. extern bool
  1188. cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);
  1189. extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
  1190. /*
  1191. * Helpers to check if the CPU's core cookie matches with the task's cookie
  1192. * when core scheduling is enabled.
  1193. * A special case is that the task's cookie always matches with CPU's core
  1194. * cookie if the CPU is in an idle core.
  1195. */
  1196. static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
  1197. {
  1198. /* Ignore cookie match if core scheduler is not enabled on the CPU. */
  1199. if (!sched_core_enabled(rq))
  1200. return true;
  1201. return rq->core->core_cookie == p->core_cookie;
  1202. }
  1203. static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
  1204. {
  1205. bool idle_core = true;
  1206. int cpu;
  1207. /* Ignore cookie match if core scheduler is not enabled on the CPU. */
  1208. if (!sched_core_enabled(rq))
  1209. return true;
  1210. for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
  1211. if (!available_idle_cpu(cpu)) {
  1212. idle_core = false;
  1213. break;
  1214. }
  1215. }
  1216. /*
  1217. * A CPU in an idle core is always the best choice for tasks with
  1218. * cookies.
  1219. */
  1220. return idle_core || rq->core->core_cookie == p->core_cookie;
  1221. }
  1222. static inline bool sched_group_cookie_match(struct rq *rq,
  1223. struct task_struct *p,
  1224. struct sched_group *group)
  1225. {
  1226. int cpu;
  1227. /* Ignore cookie match if core scheduler is not enabled on the CPU. */
  1228. if (!sched_core_enabled(rq))
  1229. return true;
  1230. for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
  1231. if (sched_core_cookie_match(cpu_rq(cpu), p))
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. static inline bool sched_core_enqueued(struct task_struct *p)
  1237. {
  1238. return !RB_EMPTY_NODE(&p->core_node);
  1239. }
  1240. extern void sched_core_enqueue(struct rq *rq, struct task_struct *p);
  1241. extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags);
  1242. extern void sched_core_get(void);
  1243. extern void sched_core_put(void);
  1244. #else /* !CONFIG_SCHED_CORE: */
  1245. static inline bool sched_core_enabled(struct rq *rq)
  1246. {
  1247. return false;
  1248. }
  1249. static inline bool sched_core_disabled(void)
  1250. {
  1251. return true;
  1252. }
  1253. static inline raw_spinlock_t *rq_lockp(struct rq *rq)
  1254. {
  1255. return &rq->__lock;
  1256. }
  1257. static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
  1258. {
  1259. return &rq->__lock;
  1260. }
  1261. static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
  1262. {
  1263. return true;
  1264. }
  1265. static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
  1266. {
  1267. return true;
  1268. }
  1269. static inline bool sched_group_cookie_match(struct rq *rq,
  1270. struct task_struct *p,
  1271. struct sched_group *group)
  1272. {
  1273. return true;
  1274. }
  1275. #endif /* !CONFIG_SCHED_CORE */
  1276. static inline void lockdep_assert_rq_held(struct rq *rq)
  1277. {
  1278. lockdep_assert_held(__rq_lockp(rq));
  1279. }
  1280. extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass);
  1281. extern bool raw_spin_rq_trylock(struct rq *rq);
  1282. extern void raw_spin_rq_unlock(struct rq *rq);
  1283. static inline void raw_spin_rq_lock(struct rq *rq)
  1284. {
  1285. raw_spin_rq_lock_nested(rq, 0);
  1286. }
  1287. static inline void raw_spin_rq_lock_irq(struct rq *rq)
  1288. {
  1289. local_irq_disable();
  1290. raw_spin_rq_lock(rq);
  1291. }
  1292. static inline void raw_spin_rq_unlock_irq(struct rq *rq)
  1293. {
  1294. raw_spin_rq_unlock(rq);
  1295. local_irq_enable();
  1296. }
  1297. static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq)
  1298. {
  1299. unsigned long flags;
  1300. local_irq_save(flags);
  1301. raw_spin_rq_lock(rq);
  1302. return flags;
  1303. }
  1304. static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags)
  1305. {
  1306. raw_spin_rq_unlock(rq);
  1307. local_irq_restore(flags);
  1308. }
  1309. #define raw_spin_rq_lock_irqsave(rq, flags) \
  1310. do { \
  1311. flags = _raw_spin_rq_lock_irqsave(rq); \
  1312. } while (0)
  1313. #ifdef CONFIG_SCHED_SMT
  1314. extern void __update_idle_core(struct rq *rq);
  1315. static inline void update_idle_core(struct rq *rq)
  1316. {
  1317. if (static_branch_unlikely(&sched_smt_present))
  1318. __update_idle_core(rq);
  1319. }
  1320. #else
  1321. static inline void update_idle_core(struct rq *rq) { }
  1322. #endif
  1323. #ifdef CONFIG_FAIR_GROUP_SCHED
  1324. static inline struct task_struct *task_of(struct sched_entity *se)
  1325. {
  1326. SCHED_WARN_ON(!entity_is_task(se));
  1327. return container_of(se, struct task_struct, se);
  1328. }
  1329. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  1330. {
  1331. return p->se.cfs_rq;
  1332. }
  1333. /* runqueue on which this entity is (to be) queued */
  1334. static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
  1335. {
  1336. return se->cfs_rq;
  1337. }
  1338. /* runqueue "owned" by this group */
  1339. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  1340. {
  1341. return grp->my_q;
  1342. }
  1343. #else /* !CONFIG_FAIR_GROUP_SCHED: */
  1344. #define task_of(_se) container_of(_se, struct task_struct, se)
  1345. static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p)
  1346. {
  1347. return &task_rq(p)->cfs;
  1348. }
  1349. static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
  1350. {
  1351. const struct task_struct *p = task_of(se);
  1352. struct rq *rq = task_rq(p);
  1353. return &rq->cfs;
  1354. }
  1355. /* runqueue "owned" by this group */
  1356. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  1357. {
  1358. return NULL;
  1359. }
  1360. #endif /* !CONFIG_FAIR_GROUP_SCHED */
  1361. extern void update_rq_clock(struct rq *rq);
  1362. /*
  1363. * rq::clock_update_flags bits
  1364. *
  1365. * %RQCF_REQ_SKIP - will request skipping of clock update on the next
  1366. * call to __schedule(). This is an optimisation to avoid
  1367. * neighbouring rq clock updates.
  1368. *
  1369. * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
  1370. * in effect and calls to update_rq_clock() are being ignored.
  1371. *
  1372. * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
  1373. * made to update_rq_clock() since the last time rq::lock was pinned.
  1374. *
  1375. * If inside of __schedule(), clock_update_flags will have been
  1376. * shifted left (a left shift is a cheap operation for the fast path
  1377. * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
  1378. *
  1379. * if (rq-clock_update_flags >= RQCF_UPDATED)
  1380. *
  1381. * to check if %RQCF_UPDATED is set. It'll never be shifted more than
  1382. * one position though, because the next rq_unpin_lock() will shift it
  1383. * back.
  1384. */
  1385. #define RQCF_REQ_SKIP 0x01
  1386. #define RQCF_ACT_SKIP 0x02
  1387. #define RQCF_UPDATED 0x04
  1388. static inline void assert_clock_updated(struct rq *rq)
  1389. {
  1390. /*
  1391. * The only reason for not seeing a clock update since the
  1392. * last rq_pin_lock() is if we're currently skipping updates.
  1393. */
  1394. SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
  1395. }
  1396. static inline u64 rq_clock(struct rq *rq)
  1397. {
  1398. lockdep_assert_rq_held(rq);
  1399. assert_clock_updated(rq);
  1400. return rq->clock;
  1401. }
  1402. static inline u64 rq_clock_task(struct rq *rq)
  1403. {
  1404. lockdep_assert_rq_held(rq);
  1405. assert_clock_updated(rq);
  1406. return rq->clock_task;
  1407. }
  1408. static inline void rq_clock_skip_update(struct rq *rq)
  1409. {
  1410. lockdep_assert_rq_held(rq);
  1411. rq->clock_update_flags |= RQCF_REQ_SKIP;
  1412. }
  1413. /*
  1414. * See rt task throttling, which is the only time a skip
  1415. * request is canceled.
  1416. */
  1417. static inline void rq_clock_cancel_skipupdate(struct rq *rq)
  1418. {
  1419. lockdep_assert_rq_held(rq);
  1420. rq->clock_update_flags &= ~RQCF_REQ_SKIP;
  1421. }
  1422. /*
  1423. * During cpu offlining and rq wide unthrottling, we can trigger
  1424. * an update_rq_clock() for several cfs and rt runqueues (Typically
  1425. * when using list_for_each_entry_*)
  1426. * rq_clock_start_loop_update() can be called after updating the clock
  1427. * once and before iterating over the list to prevent multiple update.
  1428. * After the iterative traversal, we need to call rq_clock_stop_loop_update()
  1429. * to clear RQCF_ACT_SKIP of rq->clock_update_flags.
  1430. */
  1431. static inline void rq_clock_start_loop_update(struct rq *rq)
  1432. {
  1433. lockdep_assert_rq_held(rq);
  1434. SCHED_WARN_ON(rq->clock_update_flags & RQCF_ACT_SKIP);
  1435. rq->clock_update_flags |= RQCF_ACT_SKIP;
  1436. }
  1437. static inline void rq_clock_stop_loop_update(struct rq *rq)
  1438. {
  1439. lockdep_assert_rq_held(rq);
  1440. rq->clock_update_flags &= ~RQCF_ACT_SKIP;
  1441. }
  1442. struct rq_flags {
  1443. unsigned long flags;
  1444. struct pin_cookie cookie;
  1445. #ifdef CONFIG_SCHED_DEBUG
  1446. /*
  1447. * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
  1448. * current pin context is stashed here in case it needs to be
  1449. * restored in rq_repin_lock().
  1450. */
  1451. unsigned int clock_update_flags;
  1452. #endif
  1453. };
  1454. extern struct balance_callback balance_push_callback;
  1455. /*
  1456. * Lockdep annotation that avoids accidental unlocks; it's like a
  1457. * sticky/continuous lockdep_assert_held().
  1458. *
  1459. * This avoids code that has access to 'struct rq *rq' (basically everything in
  1460. * the scheduler) from accidentally unlocking the rq if they do not also have a
  1461. * copy of the (on-stack) 'struct rq_flags rf'.
  1462. *
  1463. * Also see Documentation/locking/lockdep-design.rst.
  1464. */
  1465. static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
  1466. {
  1467. rf->cookie = lockdep_pin_lock(__rq_lockp(rq));
  1468. #ifdef CONFIG_SCHED_DEBUG
  1469. rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
  1470. rf->clock_update_flags = 0;
  1471. # ifdef CONFIG_SMP
  1472. SCHED_WARN_ON(rq->balance_callback && rq->balance_callback != &balance_push_callback);
  1473. # endif
  1474. #endif
  1475. }
  1476. static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
  1477. {
  1478. #ifdef CONFIG_SCHED_DEBUG
  1479. if (rq->clock_update_flags > RQCF_ACT_SKIP)
  1480. rf->clock_update_flags = RQCF_UPDATED;
  1481. #endif
  1482. lockdep_unpin_lock(__rq_lockp(rq), rf->cookie);
  1483. }
  1484. static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
  1485. {
  1486. lockdep_repin_lock(__rq_lockp(rq), rf->cookie);
  1487. #ifdef CONFIG_SCHED_DEBUG
  1488. /*
  1489. * Restore the value we stashed in @rf for this pin context.
  1490. */
  1491. rq->clock_update_flags |= rf->clock_update_flags;
  1492. #endif
  1493. }
  1494. extern
  1495. struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
  1496. __acquires(rq->lock);
  1497. extern
  1498. struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
  1499. __acquires(p->pi_lock)
  1500. __acquires(rq->lock);
  1501. static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
  1502. __releases(rq->lock)
  1503. {
  1504. rq_unpin_lock(rq, rf);
  1505. raw_spin_rq_unlock(rq);
  1506. }
  1507. static inline void
  1508. task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
  1509. __releases(rq->lock)
  1510. __releases(p->pi_lock)
  1511. {
  1512. rq_unpin_lock(rq, rf);
  1513. raw_spin_rq_unlock(rq);
  1514. raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
  1515. }
  1516. DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct,
  1517. _T->rq = task_rq_lock(_T->lock, &_T->rf),
  1518. task_rq_unlock(_T->rq, _T->lock, &_T->rf),
  1519. struct rq *rq; struct rq_flags rf)
  1520. static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
  1521. __acquires(rq->lock)
  1522. {
  1523. raw_spin_rq_lock_irqsave(rq, rf->flags);
  1524. rq_pin_lock(rq, rf);
  1525. }
  1526. static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf)
  1527. __acquires(rq->lock)
  1528. {
  1529. raw_spin_rq_lock_irq(rq);
  1530. rq_pin_lock(rq, rf);
  1531. }
  1532. static inline void rq_lock(struct rq *rq, struct rq_flags *rf)
  1533. __acquires(rq->lock)
  1534. {
  1535. raw_spin_rq_lock(rq);
  1536. rq_pin_lock(rq, rf);
  1537. }
  1538. static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
  1539. __releases(rq->lock)
  1540. {
  1541. rq_unpin_lock(rq, rf);
  1542. raw_spin_rq_unlock_irqrestore(rq, rf->flags);
  1543. }
  1544. static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
  1545. __releases(rq->lock)
  1546. {
  1547. rq_unpin_lock(rq, rf);
  1548. raw_spin_rq_unlock_irq(rq);
  1549. }
  1550. static inline void rq_unlock(struct rq *rq, struct rq_flags *rf)
  1551. __releases(rq->lock)
  1552. {
  1553. rq_unpin_lock(rq, rf);
  1554. raw_spin_rq_unlock(rq);
  1555. }
  1556. DEFINE_LOCK_GUARD_1(rq_lock, struct rq,
  1557. rq_lock(_T->lock, &_T->rf),
  1558. rq_unlock(_T->lock, &_T->rf),
  1559. struct rq_flags rf)
  1560. DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq,
  1561. rq_lock_irq(_T->lock, &_T->rf),
  1562. rq_unlock_irq(_T->lock, &_T->rf),
  1563. struct rq_flags rf)
  1564. DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
  1565. rq_lock_irqsave(_T->lock, &_T->rf),
  1566. rq_unlock_irqrestore(_T->lock, &_T->rf),
  1567. struct rq_flags rf)
  1568. static inline struct rq *this_rq_lock_irq(struct rq_flags *rf)
  1569. __acquires(rq->lock)
  1570. {
  1571. struct rq *rq;
  1572. local_irq_disable();
  1573. rq = this_rq();
  1574. rq_lock(rq, rf);
  1575. return rq;
  1576. }
  1577. #ifdef CONFIG_NUMA
  1578. enum numa_topology_type {
  1579. NUMA_DIRECT,
  1580. NUMA_GLUELESS_MESH,
  1581. NUMA_BACKPLANE,
  1582. };
  1583. extern enum numa_topology_type sched_numa_topology_type;
  1584. extern int sched_max_numa_distance;
  1585. extern bool find_numa_distance(int distance);
  1586. extern void sched_init_numa(int offline_node);
  1587. extern void sched_update_numa(int cpu, bool online);
  1588. extern void sched_domains_numa_masks_set(unsigned int cpu);
  1589. extern void sched_domains_numa_masks_clear(unsigned int cpu);
  1590. extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
  1591. #else /* !CONFIG_NUMA: */
  1592. static inline void sched_init_numa(int offline_node) { }
  1593. static inline void sched_update_numa(int cpu, bool online) { }
  1594. static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
  1595. static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
  1596. static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
  1597. {
  1598. return nr_cpu_ids;
  1599. }
  1600. #endif /* !CONFIG_NUMA */
  1601. #ifdef CONFIG_NUMA_BALANCING
  1602. /* The regions in numa_faults array from task_struct */
  1603. enum numa_faults_stats {
  1604. NUMA_MEM = 0,
  1605. NUMA_CPU,
  1606. NUMA_MEMBUF,
  1607. NUMA_CPUBUF
  1608. };
  1609. extern void sched_setnuma(struct task_struct *p, int node);
  1610. extern int migrate_task_to(struct task_struct *p, int cpu);
  1611. extern int migrate_swap(struct task_struct *p, struct task_struct *t,
  1612. int cpu, int scpu);
  1613. extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
  1614. #else /* !CONFIG_NUMA_BALANCING: */
  1615. static inline void
  1616. init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
  1617. {
  1618. }
  1619. #endif /* !CONFIG_NUMA_BALANCING */
  1620. #ifdef CONFIG_SMP
  1621. static inline void
  1622. queue_balance_callback(struct rq *rq,
  1623. struct balance_callback *head,
  1624. void (*func)(struct rq *rq))
  1625. {
  1626. lockdep_assert_rq_held(rq);
  1627. /*
  1628. * Don't (re)queue an already queued item; nor queue anything when
  1629. * balance_push() is active, see the comment with
  1630. * balance_push_callback.
  1631. */
  1632. if (unlikely(head->next || rq->balance_callback == &balance_push_callback))
  1633. return;
  1634. head->func = func;
  1635. head->next = rq->balance_callback;
  1636. rq->balance_callback = head;
  1637. }
  1638. #define rcu_dereference_check_sched_domain(p) \
  1639. rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex))
  1640. /*
  1641. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  1642. * See destroy_sched_domains: call_rcu for details.
  1643. *
  1644. * The domain tree of any CPU may only be accessed from within
  1645. * preempt-disabled sections.
  1646. */
  1647. #define for_each_domain(cpu, __sd) \
  1648. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  1649. __sd; __sd = __sd->parent)
  1650. /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */
  1651. #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) |
  1652. static const unsigned int SD_SHARED_CHILD_MASK =
  1653. #include <linux/sched/sd_flags.h>
  1654. 0;
  1655. #undef SD_FLAG
  1656. /**
  1657. * highest_flag_domain - Return highest sched_domain containing flag.
  1658. * @cpu: The CPU whose highest level of sched domain is to
  1659. * be returned.
  1660. * @flag: The flag to check for the highest sched_domain
  1661. * for the given CPU.
  1662. *
  1663. * Returns the highest sched_domain of a CPU which contains @flag. If @flag has
  1664. * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag.
  1665. */
  1666. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  1667. {
  1668. struct sched_domain *sd, *hsd = NULL;
  1669. for_each_domain(cpu, sd) {
  1670. if (sd->flags & flag) {
  1671. hsd = sd;
  1672. continue;
  1673. }
  1674. /*
  1675. * Stop the search if @flag is known to be shared at lower
  1676. * levels. It will not be found further up.
  1677. */
  1678. if (flag & SD_SHARED_CHILD_MASK)
  1679. break;
  1680. }
  1681. return hsd;
  1682. }
  1683. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  1684. {
  1685. struct sched_domain *sd;
  1686. for_each_domain(cpu, sd) {
  1687. if (sd->flags & flag)
  1688. break;
  1689. }
  1690. return sd;
  1691. }
  1692. DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
  1693. DECLARE_PER_CPU(int, sd_llc_size);
  1694. DECLARE_PER_CPU(int, sd_llc_id);
  1695. DECLARE_PER_CPU(int, sd_share_id);
  1696. DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
  1697. DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
  1698. DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
  1699. DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
  1700. extern struct static_key_false sched_asym_cpucapacity;
  1701. extern struct static_key_false sched_cluster_active;
  1702. static __always_inline bool sched_asym_cpucap_active(void)
  1703. {
  1704. return static_branch_unlikely(&sched_asym_cpucapacity);
  1705. }
  1706. struct sched_group_capacity {
  1707. atomic_t ref;
  1708. /*
  1709. * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
  1710. * for a single CPU.
  1711. */
  1712. unsigned long capacity;
  1713. unsigned long min_capacity; /* Min per-CPU capacity in group */
  1714. unsigned long max_capacity; /* Max per-CPU capacity in group */
  1715. unsigned long next_update;
  1716. int imbalance; /* XXX unrelated to capacity but shared group state */
  1717. #ifdef CONFIG_SCHED_DEBUG
  1718. int id;
  1719. #endif
  1720. unsigned long cpumask[]; /* Balance mask */
  1721. };
  1722. struct sched_group {
  1723. struct sched_group *next; /* Must be a circular list */
  1724. atomic_t ref;
  1725. unsigned int group_weight;
  1726. unsigned int cores;
  1727. struct sched_group_capacity *sgc;
  1728. int asym_prefer_cpu; /* CPU of highest priority in group */
  1729. int flags;
  1730. /*
  1731. * The CPUs this group covers.
  1732. *
  1733. * NOTE: this field is variable length. (Allocated dynamically
  1734. * by attaching extra space to the end of the structure,
  1735. * depending on how many CPUs the kernel has booted up with)
  1736. */
  1737. unsigned long cpumask[];
  1738. };
  1739. static inline struct cpumask *sched_group_span(struct sched_group *sg)
  1740. {
  1741. return to_cpumask(sg->cpumask);
  1742. }
  1743. /*
  1744. * See build_balance_mask().
  1745. */
  1746. static inline struct cpumask *group_balance_mask(struct sched_group *sg)
  1747. {
  1748. return to_cpumask(sg->sgc->cpumask);
  1749. }
  1750. extern int group_balance_cpu(struct sched_group *sg);
  1751. #ifdef CONFIG_SCHED_DEBUG
  1752. extern void update_sched_domain_debugfs(void);
  1753. extern void dirty_sched_domain_sysctl(int cpu);
  1754. #else
  1755. static inline void update_sched_domain_debugfs(void) { }
  1756. static inline void dirty_sched_domain_sysctl(int cpu) { }
  1757. #endif
  1758. extern int sched_update_scaling(void);
  1759. static inline const struct cpumask *task_user_cpus(struct task_struct *p)
  1760. {
  1761. if (!p->user_cpus_ptr)
  1762. return cpu_possible_mask; /* &init_task.cpus_mask */
  1763. return p->user_cpus_ptr;
  1764. }
  1765. #endif /* CONFIG_SMP */
  1766. #ifdef CONFIG_CGROUP_SCHED
  1767. /*
  1768. * Return the group to which this tasks belongs.
  1769. *
  1770. * We cannot use task_css() and friends because the cgroup subsystem
  1771. * changes that value before the cgroup_subsys::attach() method is called,
  1772. * therefore we cannot pin it and might observe the wrong value.
  1773. *
  1774. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  1775. * core changes this before calling sched_move_task().
  1776. *
  1777. * Instead we use a 'copy' which is updated from sched_move_task() while
  1778. * holding both task_struct::pi_lock and rq::lock.
  1779. */
  1780. static inline struct task_group *task_group(struct task_struct *p)
  1781. {
  1782. return p->sched_task_group;
  1783. }
  1784. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  1785. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  1786. {
  1787. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  1788. struct task_group *tg = task_group(p);
  1789. #endif
  1790. #ifdef CONFIG_FAIR_GROUP_SCHED
  1791. set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
  1792. p->se.cfs_rq = tg->cfs_rq[cpu];
  1793. p->se.parent = tg->se[cpu];
  1794. p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0;
  1795. #endif
  1796. #ifdef CONFIG_RT_GROUP_SCHED
  1797. p->rt.rt_rq = tg->rt_rq[cpu];
  1798. p->rt.parent = tg->rt_se[cpu];
  1799. #endif
  1800. }
  1801. #else /* !CONFIG_CGROUP_SCHED: */
  1802. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  1803. static inline struct task_group *task_group(struct task_struct *p)
  1804. {
  1805. return NULL;
  1806. }
  1807. #endif /* !CONFIG_CGROUP_SCHED */
  1808. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  1809. {
  1810. set_task_rq(p, cpu);
  1811. #ifdef CONFIG_SMP
  1812. /*
  1813. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  1814. * successfully executed on another CPU. We must ensure that updates of
  1815. * per-task data have been completed by this moment.
  1816. */
  1817. smp_wmb();
  1818. WRITE_ONCE(task_thread_info(p)->cpu, cpu);
  1819. p->wake_cpu = cpu;
  1820. #endif
  1821. }
  1822. /*
  1823. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  1824. */
  1825. #ifdef CONFIG_SCHED_DEBUG
  1826. # define const_debug __read_mostly
  1827. #else
  1828. # define const_debug const
  1829. #endif
  1830. #define SCHED_FEAT(name, enabled) \
  1831. __SCHED_FEAT_##name ,
  1832. enum {
  1833. #include "features.h"
  1834. __SCHED_FEAT_NR,
  1835. };
  1836. #undef SCHED_FEAT
  1837. #ifdef CONFIG_SCHED_DEBUG
  1838. /*
  1839. * To support run-time toggling of sched features, all the translation units
  1840. * (but core.c) reference the sysctl_sched_features defined in core.c.
  1841. */
  1842. extern const_debug unsigned int sysctl_sched_features;
  1843. #ifdef CONFIG_JUMP_LABEL
  1844. #define SCHED_FEAT(name, enabled) \
  1845. static __always_inline bool static_branch_##name(struct static_key *key) \
  1846. { \
  1847. return static_key_##enabled(key); \
  1848. }
  1849. #include "features.h"
  1850. #undef SCHED_FEAT
  1851. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  1852. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  1853. #else /* !CONFIG_JUMP_LABEL: */
  1854. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  1855. #endif /* !CONFIG_JUMP_LABEL */
  1856. #else /* !SCHED_DEBUG: */
  1857. /*
  1858. * Each translation unit has its own copy of sysctl_sched_features to allow
  1859. * constants propagation at compile time and compiler optimization based on
  1860. * features default.
  1861. */
  1862. #define SCHED_FEAT(name, enabled) \
  1863. (1UL << __SCHED_FEAT_##name) * enabled |
  1864. static const_debug __maybe_unused unsigned int sysctl_sched_features =
  1865. #include "features.h"
  1866. 0;
  1867. #undef SCHED_FEAT
  1868. #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  1869. #endif /* !SCHED_DEBUG */
  1870. extern struct static_key_false sched_numa_balancing;
  1871. extern struct static_key_false sched_schedstats;
  1872. static inline u64 global_rt_period(void)
  1873. {
  1874. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  1875. }
  1876. static inline u64 global_rt_runtime(void)
  1877. {
  1878. if (sysctl_sched_rt_runtime < 0)
  1879. return RUNTIME_INF;
  1880. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  1881. }
  1882. static inline int task_current(struct rq *rq, struct task_struct *p)
  1883. {
  1884. return rq->curr == p;
  1885. }
  1886. static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
  1887. {
  1888. #ifdef CONFIG_SMP
  1889. return p->on_cpu;
  1890. #else
  1891. return task_current(rq, p);
  1892. #endif
  1893. }
  1894. static inline int task_on_rq_queued(struct task_struct *p)
  1895. {
  1896. return p->on_rq == TASK_ON_RQ_QUEUED;
  1897. }
  1898. static inline int task_on_rq_migrating(struct task_struct *p)
  1899. {
  1900. return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
  1901. }
  1902. /* Wake flags. The first three directly map to some SD flag value */
  1903. #define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */
  1904. #define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */
  1905. #define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */
  1906. #define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */
  1907. #define WF_MIGRATED 0x20 /* Internal use, task got migrated */
  1908. #define WF_CURRENT_CPU 0x40 /* Prefer to move the wakee to the current CPU. */
  1909. #define WF_RQ_SELECTED 0x80 /* ->select_task_rq() was called */
  1910. #ifdef CONFIG_SMP
  1911. static_assert(WF_EXEC == SD_BALANCE_EXEC);
  1912. static_assert(WF_FORK == SD_BALANCE_FORK);
  1913. static_assert(WF_TTWU == SD_BALANCE_WAKE);
  1914. #endif
  1915. /*
  1916. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  1917. * of tasks with abnormal "nice" values across CPUs the contribution that
  1918. * each task makes to its run queue's load is weighted according to its
  1919. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  1920. * scaled version of the new time slice allocation that they receive on time
  1921. * slice expiry etc.
  1922. */
  1923. #define WEIGHT_IDLEPRIO 3
  1924. #define WMULT_IDLEPRIO 1431655765
  1925. extern const int sched_prio_to_weight[40];
  1926. extern const u32 sched_prio_to_wmult[40];
  1927. /*
  1928. * {de,en}queue flags:
  1929. *
  1930. * DEQUEUE_SLEEP - task is no longer runnable
  1931. * ENQUEUE_WAKEUP - task just became runnable
  1932. *
  1933. * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
  1934. * are in a known state which allows modification. Such pairs
  1935. * should preserve as much state as possible.
  1936. *
  1937. * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
  1938. * in the runqueue.
  1939. *
  1940. * NOCLOCK - skip the update_rq_clock() (avoids double updates)
  1941. *
  1942. * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE)
  1943. *
  1944. * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
  1945. * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
  1946. * ENQUEUE_MIGRATED - the task was migrated during wakeup
  1947. * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called
  1948. *
  1949. */
  1950. #define DEQUEUE_SLEEP 0x01 /* Matches ENQUEUE_WAKEUP */
  1951. #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
  1952. #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
  1953. #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
  1954. #define DEQUEUE_SPECIAL 0x10
  1955. #define DEQUEUE_MIGRATING 0x100 /* Matches ENQUEUE_MIGRATING */
  1956. #define DEQUEUE_DELAYED 0x200 /* Matches ENQUEUE_DELAYED */
  1957. #define ENQUEUE_WAKEUP 0x01
  1958. #define ENQUEUE_RESTORE 0x02
  1959. #define ENQUEUE_MOVE 0x04
  1960. #define ENQUEUE_NOCLOCK 0x08
  1961. #define ENQUEUE_HEAD 0x10
  1962. #define ENQUEUE_REPLENISH 0x20
  1963. #ifdef CONFIG_SMP
  1964. #define ENQUEUE_MIGRATED 0x40
  1965. #else
  1966. #define ENQUEUE_MIGRATED 0x00
  1967. #endif
  1968. #define ENQUEUE_INITIAL 0x80
  1969. #define ENQUEUE_MIGRATING 0x100
  1970. #define ENQUEUE_DELAYED 0x200
  1971. #define ENQUEUE_RQ_SELECTED 0x400
  1972. #define RETRY_TASK ((void *)-1UL)
  1973. struct affinity_context {
  1974. const struct cpumask *new_mask;
  1975. struct cpumask *user_mask;
  1976. unsigned int flags;
  1977. };
  1978. extern s64 update_curr_common(struct rq *rq);
  1979. struct sched_class {
  1980. #ifdef CONFIG_UCLAMP_TASK
  1981. int uclamp_enabled;
  1982. #endif
  1983. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  1984. bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  1985. void (*yield_task) (struct rq *rq);
  1986. bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
  1987. void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
  1988. int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
  1989. struct task_struct *(*pick_task)(struct rq *rq);
  1990. /*
  1991. * Optional! When implemented pick_next_task() should be equivalent to:
  1992. *
  1993. * next = pick_task();
  1994. * if (next) {
  1995. * put_prev_task(prev);
  1996. * set_next_task_first(next);
  1997. * }
  1998. */
  1999. struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev);
  2000. void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
  2001. void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
  2002. #ifdef CONFIG_SMP
  2003. int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags);
  2004. void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
  2005. void (*task_woken)(struct rq *this_rq, struct task_struct *task);
  2006. void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx);
  2007. void (*rq_online)(struct rq *rq);
  2008. void (*rq_offline)(struct rq *rq);
  2009. struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
  2010. #endif
  2011. void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
  2012. void (*task_fork)(struct task_struct *p);
  2013. void (*task_dead)(struct task_struct *p);
  2014. /*
  2015. * The switched_from() call is allowed to drop rq->lock, therefore we
  2016. * cannot assume the switched_from/switched_to pair is serialized by
  2017. * rq->lock. They are however serialized by p->pi_lock.
  2018. */
  2019. void (*switching_to) (struct rq *this_rq, struct task_struct *task);
  2020. void (*switched_from)(struct rq *this_rq, struct task_struct *task);
  2021. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  2022. void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
  2023. const struct load_weight *lw);
  2024. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  2025. int oldprio);
  2026. unsigned int (*get_rr_interval)(struct rq *rq,
  2027. struct task_struct *task);
  2028. void (*update_curr)(struct rq *rq);
  2029. #ifdef CONFIG_FAIR_GROUP_SCHED
  2030. void (*task_change_group)(struct task_struct *p);
  2031. #endif
  2032. #ifdef CONFIG_SCHED_CORE
  2033. int (*task_is_throttled)(struct task_struct *p, int cpu);
  2034. #endif
  2035. };
  2036. static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
  2037. {
  2038. WARN_ON_ONCE(rq->curr != prev);
  2039. prev->sched_class->put_prev_task(rq, prev, NULL);
  2040. }
  2041. static inline void set_next_task(struct rq *rq, struct task_struct *next)
  2042. {
  2043. next->sched_class->set_next_task(rq, next, false);
  2044. }
  2045. static inline void
  2046. __put_prev_set_next_dl_server(struct rq *rq,
  2047. struct task_struct *prev,
  2048. struct task_struct *next)
  2049. {
  2050. prev->dl_server = NULL;
  2051. next->dl_server = rq->dl_server;
  2052. rq->dl_server = NULL;
  2053. }
  2054. static inline void put_prev_set_next_task(struct rq *rq,
  2055. struct task_struct *prev,
  2056. struct task_struct *next)
  2057. {
  2058. WARN_ON_ONCE(rq->curr != prev);
  2059. __put_prev_set_next_dl_server(rq, prev, next);
  2060. if (next == prev)
  2061. return;
  2062. prev->sched_class->put_prev_task(rq, prev, next);
  2063. next->sched_class->set_next_task(rq, next, true);
  2064. }
  2065. /*
  2066. * Helper to define a sched_class instance; each one is placed in a separate
  2067. * section which is ordered by the linker script:
  2068. *
  2069. * include/asm-generic/vmlinux.lds.h
  2070. *
  2071. * *CAREFUL* they are laid out in *REVERSE* order!!!
  2072. *
  2073. * Also enforce alignment on the instance, not the type, to guarantee layout.
  2074. */
  2075. #define DEFINE_SCHED_CLASS(name) \
  2076. const struct sched_class name##_sched_class \
  2077. __aligned(__alignof__(struct sched_class)) \
  2078. __section("__" #name "_sched_class")
  2079. /* Defined in include/asm-generic/vmlinux.lds.h */
  2080. extern struct sched_class __sched_class_highest[];
  2081. extern struct sched_class __sched_class_lowest[];
  2082. extern const struct sched_class stop_sched_class;
  2083. extern const struct sched_class dl_sched_class;
  2084. extern const struct sched_class rt_sched_class;
  2085. extern const struct sched_class fair_sched_class;
  2086. extern const struct sched_class idle_sched_class;
  2087. #ifdef CONFIG_SCHED_CLASS_EXT
  2088. extern const struct sched_class ext_sched_class;
  2089. DECLARE_STATIC_KEY_FALSE(__scx_ops_enabled); /* SCX BPF scheduler loaded */
  2090. DECLARE_STATIC_KEY_FALSE(__scx_switched_all); /* all fair class tasks on SCX */
  2091. #define scx_enabled() static_branch_unlikely(&__scx_ops_enabled)
  2092. #define scx_switched_all() static_branch_unlikely(&__scx_switched_all)
  2093. #else /* !CONFIG_SCHED_CLASS_EXT */
  2094. #define scx_enabled() false
  2095. #define scx_switched_all() false
  2096. #endif /* !CONFIG_SCHED_CLASS_EXT */
  2097. /*
  2098. * Iterate only active classes. SCX can take over all fair tasks or be
  2099. * completely disabled. If the former, skip fair. If the latter, skip SCX.
  2100. */
  2101. static inline const struct sched_class *next_active_class(const struct sched_class *class)
  2102. {
  2103. class++;
  2104. #ifdef CONFIG_SCHED_CLASS_EXT
  2105. if (scx_switched_all() && class == &fair_sched_class)
  2106. class++;
  2107. if (!scx_enabled() && class == &ext_sched_class)
  2108. class++;
  2109. #endif
  2110. return class;
  2111. }
  2112. #define for_class_range(class, _from, _to) \
  2113. for (class = (_from); class < (_to); class++)
  2114. #define for_each_class(class) \
  2115. for_class_range(class, __sched_class_highest, __sched_class_lowest)
  2116. #define for_active_class_range(class, _from, _to) \
  2117. for (class = (_from); class != (_to); class = next_active_class(class))
  2118. #define for_each_active_class(class) \
  2119. for_active_class_range(class, __sched_class_highest, __sched_class_lowest)
  2120. #define sched_class_above(_a, _b) ((_a) < (_b))
  2121. static inline bool sched_stop_runnable(struct rq *rq)
  2122. {
  2123. return rq->stop && task_on_rq_queued(rq->stop);
  2124. }
  2125. static inline bool sched_dl_runnable(struct rq *rq)
  2126. {
  2127. return rq->dl.dl_nr_running > 0;
  2128. }
  2129. static inline bool sched_rt_runnable(struct rq *rq)
  2130. {
  2131. return rq->rt.rt_queued > 0;
  2132. }
  2133. static inline bool sched_fair_runnable(struct rq *rq)
  2134. {
  2135. return rq->cfs.nr_running > 0;
  2136. }
  2137. extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
  2138. extern struct task_struct *pick_task_idle(struct rq *rq);
  2139. #define SCA_CHECK 0x01
  2140. #define SCA_MIGRATE_DISABLE 0x02
  2141. #define SCA_MIGRATE_ENABLE 0x04
  2142. #define SCA_USER 0x08
  2143. #ifdef CONFIG_SMP
  2144. extern void update_group_capacity(struct sched_domain *sd, int cpu);
  2145. extern void sched_balance_trigger(struct rq *rq);
  2146. extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
  2147. extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
  2148. static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
  2149. {
  2150. /* When not in the task's cpumask, no point in looking further. */
  2151. if (!cpumask_test_cpu(cpu, p->cpus_ptr))
  2152. return false;
  2153. /* Can @cpu run a user thread? */
  2154. if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p))
  2155. return false;
  2156. return true;
  2157. }
  2158. static inline cpumask_t *alloc_user_cpus_ptr(int node)
  2159. {
  2160. /*
  2161. * See do_set_cpus_allowed() above for the rcu_head usage.
  2162. */
  2163. int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
  2164. return kmalloc_node(size, GFP_KERNEL, node);
  2165. }
  2166. static inline struct task_struct *get_push_task(struct rq *rq)
  2167. {
  2168. struct task_struct *p = rq->curr;
  2169. lockdep_assert_rq_held(rq);
  2170. if (rq->push_busy)
  2171. return NULL;
  2172. if (p->nr_cpus_allowed == 1)
  2173. return NULL;
  2174. if (p->migration_disabled)
  2175. return NULL;
  2176. rq->push_busy = true;
  2177. return get_task_struct(p);
  2178. }
  2179. extern int push_cpu_stop(void *arg);
  2180. #else /* !CONFIG_SMP: */
  2181. static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
  2182. {
  2183. return true;
  2184. }
  2185. static inline int __set_cpus_allowed_ptr(struct task_struct *p,
  2186. struct affinity_context *ctx)
  2187. {
  2188. return set_cpus_allowed_ptr(p, ctx->new_mask);
  2189. }
  2190. static inline cpumask_t *alloc_user_cpus_ptr(int node)
  2191. {
  2192. return NULL;
  2193. }
  2194. #endif /* !CONFIG_SMP */
  2195. #ifdef CONFIG_CPU_IDLE
  2196. static inline void idle_set_state(struct rq *rq,
  2197. struct cpuidle_state *idle_state)
  2198. {
  2199. rq->idle_state = idle_state;
  2200. }
  2201. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  2202. {
  2203. SCHED_WARN_ON(!rcu_read_lock_held());
  2204. return rq->idle_state;
  2205. }
  2206. #else /* !CONFIG_CPU_IDLE: */
  2207. static inline void idle_set_state(struct rq *rq,
  2208. struct cpuidle_state *idle_state)
  2209. {
  2210. }
  2211. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  2212. {
  2213. return NULL;
  2214. }
  2215. #endif /* !CONFIG_CPU_IDLE */
  2216. extern void schedule_idle(void);
  2217. asmlinkage void schedule_user(void);
  2218. extern void sysrq_sched_debug_show(void);
  2219. extern void sched_init_granularity(void);
  2220. extern void update_max_interval(void);
  2221. extern void init_sched_dl_class(void);
  2222. extern void init_sched_rt_class(void);
  2223. extern void init_sched_fair_class(void);
  2224. extern void resched_curr(struct rq *rq);
  2225. extern void resched_cpu(int cpu);
  2226. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  2227. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  2228. extern void init_dl_entity(struct sched_dl_entity *dl_se);
  2229. #define BW_SHIFT 20
  2230. #define BW_UNIT (1 << BW_SHIFT)
  2231. #define RATIO_SHIFT 8
  2232. #define MAX_BW_BITS (64 - BW_SHIFT)
  2233. #define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
  2234. extern unsigned long to_ratio(u64 period, u64 runtime);
  2235. extern void init_entity_runnable_average(struct sched_entity *se);
  2236. extern void post_init_entity_util_avg(struct task_struct *p);
  2237. #ifdef CONFIG_NO_HZ_FULL
  2238. extern bool sched_can_stop_tick(struct rq *rq);
  2239. extern int __init sched_tick_offload_init(void);
  2240. /*
  2241. * Tick may be needed by tasks in the runqueue depending on their policy and
  2242. * requirements. If tick is needed, lets send the target an IPI to kick it out of
  2243. * nohz mode if necessary.
  2244. */
  2245. static inline void sched_update_tick_dependency(struct rq *rq)
  2246. {
  2247. int cpu = cpu_of(rq);
  2248. if (!tick_nohz_full_cpu(cpu))
  2249. return;
  2250. if (sched_can_stop_tick(rq))
  2251. tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
  2252. else
  2253. tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
  2254. }
  2255. #else /* !CONFIG_NO_HZ_FULL: */
  2256. static inline int sched_tick_offload_init(void) { return 0; }
  2257. static inline void sched_update_tick_dependency(struct rq *rq) { }
  2258. #endif /* !CONFIG_NO_HZ_FULL */
  2259. static inline void add_nr_running(struct rq *rq, unsigned count)
  2260. {
  2261. unsigned prev_nr = rq->nr_running;
  2262. rq->nr_running = prev_nr + count;
  2263. if (trace_sched_update_nr_running_tp_enabled()) {
  2264. call_trace_sched_update_nr_running(rq, count);
  2265. }
  2266. #ifdef CONFIG_SMP
  2267. if (prev_nr < 2 && rq->nr_running >= 2)
  2268. set_rd_overloaded(rq->rd, 1);
  2269. #endif
  2270. sched_update_tick_dependency(rq);
  2271. }
  2272. static inline void sub_nr_running(struct rq *rq, unsigned count)
  2273. {
  2274. rq->nr_running -= count;
  2275. if (trace_sched_update_nr_running_tp_enabled()) {
  2276. call_trace_sched_update_nr_running(rq, -count);
  2277. }
  2278. /* Check if we still need preemption */
  2279. sched_update_tick_dependency(rq);
  2280. }
  2281. static inline void __block_task(struct rq *rq, struct task_struct *p)
  2282. {
  2283. if (p->sched_contributes_to_load)
  2284. rq->nr_uninterruptible++;
  2285. if (p->in_iowait) {
  2286. atomic_inc(&rq->nr_iowait);
  2287. delayacct_blkio_start();
  2288. }
  2289. ASSERT_EXCLUSIVE_WRITER(p->on_rq);
  2290. /*
  2291. * The moment this write goes through, ttwu() can swoop in and migrate
  2292. * this task, rendering our rq->__lock ineffective.
  2293. *
  2294. * __schedule() try_to_wake_up()
  2295. * LOCK rq->__lock LOCK p->pi_lock
  2296. * pick_next_task()
  2297. * pick_next_task_fair()
  2298. * pick_next_entity()
  2299. * dequeue_entities()
  2300. * __block_task()
  2301. * RELEASE p->on_rq = 0 if (p->on_rq && ...)
  2302. * break;
  2303. *
  2304. * ACQUIRE (after ctrl-dep)
  2305. *
  2306. * cpu = select_task_rq();
  2307. * set_task_cpu(p, cpu);
  2308. * ttwu_queue()
  2309. * ttwu_do_activate()
  2310. * LOCK rq->__lock
  2311. * activate_task()
  2312. * STORE p->on_rq = 1
  2313. * UNLOCK rq->__lock
  2314. *
  2315. * Callers must ensure to not reference @p after this -- we no longer
  2316. * own it.
  2317. */
  2318. smp_store_release(&p->on_rq, 0);
  2319. }
  2320. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  2321. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  2322. extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
  2323. #ifdef CONFIG_PREEMPT_RT
  2324. # define SCHED_NR_MIGRATE_BREAK 8
  2325. #else
  2326. # define SCHED_NR_MIGRATE_BREAK 32
  2327. #endif
  2328. extern const_debug unsigned int sysctl_sched_nr_migrate;
  2329. extern const_debug unsigned int sysctl_sched_migration_cost;
  2330. extern unsigned int sysctl_sched_base_slice;
  2331. #ifdef CONFIG_SCHED_DEBUG
  2332. extern int sysctl_resched_latency_warn_ms;
  2333. extern int sysctl_resched_latency_warn_once;
  2334. extern unsigned int sysctl_sched_tunable_scaling;
  2335. extern unsigned int sysctl_numa_balancing_scan_delay;
  2336. extern unsigned int sysctl_numa_balancing_scan_period_min;
  2337. extern unsigned int sysctl_numa_balancing_scan_period_max;
  2338. extern unsigned int sysctl_numa_balancing_scan_size;
  2339. extern unsigned int sysctl_numa_balancing_hot_threshold;
  2340. #endif
  2341. #ifdef CONFIG_SCHED_HRTICK
  2342. /*
  2343. * Use hrtick when:
  2344. * - enabled by features
  2345. * - hrtimer is actually high res
  2346. */
  2347. static inline int hrtick_enabled(struct rq *rq)
  2348. {
  2349. if (!cpu_active(cpu_of(rq)))
  2350. return 0;
  2351. return hrtimer_is_hres_active(&rq->hrtick_timer);
  2352. }
  2353. static inline int hrtick_enabled_fair(struct rq *rq)
  2354. {
  2355. if (!sched_feat(HRTICK))
  2356. return 0;
  2357. return hrtick_enabled(rq);
  2358. }
  2359. static inline int hrtick_enabled_dl(struct rq *rq)
  2360. {
  2361. if (!sched_feat(HRTICK_DL))
  2362. return 0;
  2363. return hrtick_enabled(rq);
  2364. }
  2365. extern void hrtick_start(struct rq *rq, u64 delay);
  2366. #else /* !CONFIG_SCHED_HRTICK: */
  2367. static inline int hrtick_enabled_fair(struct rq *rq)
  2368. {
  2369. return 0;
  2370. }
  2371. static inline int hrtick_enabled_dl(struct rq *rq)
  2372. {
  2373. return 0;
  2374. }
  2375. static inline int hrtick_enabled(struct rq *rq)
  2376. {
  2377. return 0;
  2378. }
  2379. #endif /* !CONFIG_SCHED_HRTICK */
  2380. #ifndef arch_scale_freq_tick
  2381. static __always_inline void arch_scale_freq_tick(void) { }
  2382. #endif
  2383. #ifndef arch_scale_freq_capacity
  2384. /**
  2385. * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
  2386. * @cpu: the CPU in question.
  2387. *
  2388. * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
  2389. *
  2390. * f_curr
  2391. * ------ * SCHED_CAPACITY_SCALE
  2392. * f_max
  2393. */
  2394. static __always_inline
  2395. unsigned long arch_scale_freq_capacity(int cpu)
  2396. {
  2397. return SCHED_CAPACITY_SCALE;
  2398. }
  2399. #endif
  2400. #ifdef CONFIG_SCHED_DEBUG
  2401. /*
  2402. * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to
  2403. * acquire rq lock instead of rq_lock(). So at the end of these two functions
  2404. * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of
  2405. * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning.
  2406. */
  2407. static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
  2408. {
  2409. rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
  2410. /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */
  2411. #ifdef CONFIG_SMP
  2412. rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
  2413. #endif
  2414. }
  2415. #else
  2416. static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) { }
  2417. #endif
  2418. #define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...) \
  2419. __DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \
  2420. static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \
  2421. { class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t; \
  2422. _lock; return _t; }
  2423. #ifdef CONFIG_SMP
  2424. static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
  2425. {
  2426. #ifdef CONFIG_SCHED_CORE
  2427. /*
  2428. * In order to not have {0,2},{1,3} turn into into an AB-BA,
  2429. * order by core-id first and cpu-id second.
  2430. *
  2431. * Notably:
  2432. *
  2433. * double_rq_lock(0,3); will take core-0, core-1 lock
  2434. * double_rq_lock(1,2); will take core-1, core-0 lock
  2435. *
  2436. * when only cpu-id is considered.
  2437. */
  2438. if (rq1->core->cpu < rq2->core->cpu)
  2439. return true;
  2440. if (rq1->core->cpu > rq2->core->cpu)
  2441. return false;
  2442. /*
  2443. * __sched_core_flip() relies on SMT having cpu-id lock order.
  2444. */
  2445. #endif
  2446. return rq1->cpu < rq2->cpu;
  2447. }
  2448. extern void double_rq_lock(struct rq *rq1, struct rq *rq2);
  2449. #ifdef CONFIG_PREEMPTION
  2450. /*
  2451. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  2452. * way at the expense of forcing extra atomic operations in all
  2453. * invocations. This assures that the double_lock is acquired using the
  2454. * same underlying policy as the spinlock_t on this architecture, which
  2455. * reduces latency compared to the unfair variant below. However, it
  2456. * also adds more overhead and therefore may reduce throughput.
  2457. */
  2458. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  2459. __releases(this_rq->lock)
  2460. __acquires(busiest->lock)
  2461. __acquires(this_rq->lock)
  2462. {
  2463. raw_spin_rq_unlock(this_rq);
  2464. double_rq_lock(this_rq, busiest);
  2465. return 1;
  2466. }
  2467. #else /* !CONFIG_PREEMPTION: */
  2468. /*
  2469. * Unfair double_lock_balance: Optimizes throughput at the expense of
  2470. * latency by eliminating extra atomic operations when the locks are
  2471. * already in proper order on entry. This favors lower CPU-ids and will
  2472. * grant the double lock to lower CPUs over higher ids under contention,
  2473. * regardless of entry order into the function.
  2474. */
  2475. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  2476. __releases(this_rq->lock)
  2477. __acquires(busiest->lock)
  2478. __acquires(this_rq->lock)
  2479. {
  2480. if (__rq_lockp(this_rq) == __rq_lockp(busiest) ||
  2481. likely(raw_spin_rq_trylock(busiest))) {
  2482. double_rq_clock_clear_update(this_rq, busiest);
  2483. return 0;
  2484. }
  2485. if (rq_order_less(this_rq, busiest)) {
  2486. raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING);
  2487. double_rq_clock_clear_update(this_rq, busiest);
  2488. return 0;
  2489. }
  2490. raw_spin_rq_unlock(this_rq);
  2491. double_rq_lock(this_rq, busiest);
  2492. return 1;
  2493. }
  2494. #endif /* !CONFIG_PREEMPTION */
  2495. /*
  2496. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  2497. */
  2498. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  2499. {
  2500. lockdep_assert_irqs_disabled();
  2501. return _double_lock_balance(this_rq, busiest);
  2502. }
  2503. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  2504. __releases(busiest->lock)
  2505. {
  2506. if (__rq_lockp(this_rq) != __rq_lockp(busiest))
  2507. raw_spin_rq_unlock(busiest);
  2508. lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_);
  2509. }
  2510. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  2511. {
  2512. if (l1 > l2)
  2513. swap(l1, l2);
  2514. spin_lock(l1);
  2515. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  2516. }
  2517. static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
  2518. {
  2519. if (l1 > l2)
  2520. swap(l1, l2);
  2521. spin_lock_irq(l1);
  2522. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  2523. }
  2524. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  2525. {
  2526. if (l1 > l2)
  2527. swap(l1, l2);
  2528. raw_spin_lock(l1);
  2529. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  2530. }
  2531. static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  2532. {
  2533. raw_spin_unlock(l1);
  2534. raw_spin_unlock(l2);
  2535. }
  2536. DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
  2537. double_raw_lock(_T->lock, _T->lock2),
  2538. double_raw_unlock(_T->lock, _T->lock2))
  2539. /*
  2540. * double_rq_unlock - safely unlock two runqueues
  2541. *
  2542. * Note this does not restore interrupts like task_rq_unlock,
  2543. * you need to do so manually after calling.
  2544. */
  2545. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  2546. __releases(rq1->lock)
  2547. __releases(rq2->lock)
  2548. {
  2549. if (__rq_lockp(rq1) != __rq_lockp(rq2))
  2550. raw_spin_rq_unlock(rq2);
  2551. else
  2552. __release(rq2->lock);
  2553. raw_spin_rq_unlock(rq1);
  2554. }
  2555. extern void set_rq_online (struct rq *rq);
  2556. extern void set_rq_offline(struct rq *rq);
  2557. extern bool sched_smp_initialized;
  2558. #else /* !CONFIG_SMP: */
  2559. /*
  2560. * double_rq_lock - safely lock two runqueues
  2561. *
  2562. * Note this does not disable interrupts like task_rq_lock,
  2563. * you need to do so manually before calling.
  2564. */
  2565. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  2566. __acquires(rq1->lock)
  2567. __acquires(rq2->lock)
  2568. {
  2569. WARN_ON_ONCE(!irqs_disabled());
  2570. WARN_ON_ONCE(rq1 != rq2);
  2571. raw_spin_rq_lock(rq1);
  2572. __acquire(rq2->lock); /* Fake it out ;) */
  2573. double_rq_clock_clear_update(rq1, rq2);
  2574. }
  2575. /*
  2576. * double_rq_unlock - safely unlock two runqueues
  2577. *
  2578. * Note this does not restore interrupts like task_rq_unlock,
  2579. * you need to do so manually after calling.
  2580. */
  2581. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  2582. __releases(rq1->lock)
  2583. __releases(rq2->lock)
  2584. {
  2585. WARN_ON_ONCE(rq1 != rq2);
  2586. raw_spin_rq_unlock(rq1);
  2587. __release(rq2->lock);
  2588. }
  2589. #endif /* !CONFIG_SMP */
  2590. DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
  2591. double_rq_lock(_T->lock, _T->lock2),
  2592. double_rq_unlock(_T->lock, _T->lock2))
  2593. extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq);
  2594. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  2595. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  2596. #ifdef CONFIG_SCHED_DEBUG
  2597. extern bool sched_debug_verbose;
  2598. extern void print_cfs_stats(struct seq_file *m, int cpu);
  2599. extern void print_rt_stats(struct seq_file *m, int cpu);
  2600. extern void print_dl_stats(struct seq_file *m, int cpu);
  2601. extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
  2602. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  2603. extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
  2604. extern void resched_latency_warn(int cpu, u64 latency);
  2605. # ifdef CONFIG_NUMA_BALANCING
  2606. extern void show_numa_stats(struct task_struct *p, struct seq_file *m);
  2607. extern void
  2608. print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
  2609. unsigned long tpf, unsigned long gsf, unsigned long gpf);
  2610. # endif /* CONFIG_NUMA_BALANCING */
  2611. #else /* !CONFIG_SCHED_DEBUG: */
  2612. static inline void resched_latency_warn(int cpu, u64 latency) { }
  2613. #endif /* !CONFIG_SCHED_DEBUG */
  2614. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  2615. extern void init_rt_rq(struct rt_rq *rt_rq);
  2616. extern void init_dl_rq(struct dl_rq *dl_rq);
  2617. extern void cfs_bandwidth_usage_inc(void);
  2618. extern void cfs_bandwidth_usage_dec(void);
  2619. #ifdef CONFIG_NO_HZ_COMMON
  2620. #define NOHZ_BALANCE_KICK_BIT 0
  2621. #define NOHZ_STATS_KICK_BIT 1
  2622. #define NOHZ_NEWILB_KICK_BIT 2
  2623. #define NOHZ_NEXT_KICK_BIT 3
  2624. /* Run sched_balance_domains() */
  2625. #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
  2626. /* Update blocked load */
  2627. #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
  2628. /* Update blocked load when entering idle */
  2629. #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT)
  2630. /* Update nohz.next_balance */
  2631. #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT)
  2632. #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK)
  2633. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  2634. extern void nohz_balance_exit_idle(struct rq *rq);
  2635. #else /* !CONFIG_NO_HZ_COMMON: */
  2636. static inline void nohz_balance_exit_idle(struct rq *rq) { }
  2637. #endif /* !CONFIG_NO_HZ_COMMON */
  2638. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  2639. extern void nohz_run_idle_balance(int cpu);
  2640. #else
  2641. static inline void nohz_run_idle_balance(int cpu) { }
  2642. #endif
  2643. #include "stats.h"
  2644. #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS)
  2645. extern void __sched_core_account_forceidle(struct rq *rq);
  2646. static inline void sched_core_account_forceidle(struct rq *rq)
  2647. {
  2648. if (schedstat_enabled())
  2649. __sched_core_account_forceidle(rq);
  2650. }
  2651. extern void __sched_core_tick(struct rq *rq);
  2652. static inline void sched_core_tick(struct rq *rq)
  2653. {
  2654. if (sched_core_enabled(rq) && schedstat_enabled())
  2655. __sched_core_tick(rq);
  2656. }
  2657. #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */
  2658. static inline void sched_core_account_forceidle(struct rq *rq) { }
  2659. static inline void sched_core_tick(struct rq *rq) { }
  2660. #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */
  2661. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  2662. struct irqtime {
  2663. u64 total;
  2664. u64 tick_delta;
  2665. u64 irq_start_time;
  2666. struct u64_stats_sync sync;
  2667. };
  2668. DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
  2669. /*
  2670. * Returns the irqtime minus the softirq time computed by ksoftirqd.
  2671. * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
  2672. * and never move forward.
  2673. */
  2674. static inline u64 irq_time_read(int cpu)
  2675. {
  2676. struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
  2677. unsigned int seq;
  2678. u64 total;
  2679. do {
  2680. seq = __u64_stats_fetch_begin(&irqtime->sync);
  2681. total = irqtime->total;
  2682. } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
  2683. return total;
  2684. }
  2685. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  2686. #ifdef CONFIG_CPU_FREQ
  2687. DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
  2688. /**
  2689. * cpufreq_update_util - Take a note about CPU utilization changes.
  2690. * @rq: Runqueue to carry out the update for.
  2691. * @flags: Update reason flags.
  2692. *
  2693. * This function is called by the scheduler on the CPU whose utilization is
  2694. * being updated.
  2695. *
  2696. * It can only be called from RCU-sched read-side critical sections.
  2697. *
  2698. * The way cpufreq is currently arranged requires it to evaluate the CPU
  2699. * performance state (frequency/voltage) on a regular basis to prevent it from
  2700. * being stuck in a completely inadequate performance level for too long.
  2701. * That is not guaranteed to happen if the updates are only triggered from CFS
  2702. * and DL, though, because they may not be coming in if only RT tasks are
  2703. * active all the time (or there are RT tasks only).
  2704. *
  2705. * As a workaround for that issue, this function is called periodically by the
  2706. * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
  2707. * but that really is a band-aid. Going forward it should be replaced with
  2708. * solutions targeted more specifically at RT tasks.
  2709. */
  2710. static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
  2711. {
  2712. struct update_util_data *data;
  2713. data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
  2714. cpu_of(rq)));
  2715. if (data)
  2716. data->func(data, rq_clock(rq), flags);
  2717. }
  2718. #else /* !CONFIG_CPU_FREQ: */
  2719. static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { }
  2720. #endif /* !CONFIG_CPU_FREQ */
  2721. #ifdef arch_scale_freq_capacity
  2722. # ifndef arch_scale_freq_invariant
  2723. # define arch_scale_freq_invariant() true
  2724. # endif
  2725. #else
  2726. # define arch_scale_freq_invariant() false
  2727. #endif
  2728. #ifdef CONFIG_SMP
  2729. unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
  2730. unsigned long *min,
  2731. unsigned long *max);
  2732. unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
  2733. unsigned long min,
  2734. unsigned long max);
  2735. /*
  2736. * Verify the fitness of task @p to run on @cpu taking into account the
  2737. * CPU original capacity and the runtime/deadline ratio of the task.
  2738. *
  2739. * The function will return true if the original capacity of @cpu is
  2740. * greater than or equal to task's deadline density right shifted by
  2741. * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise.
  2742. */
  2743. static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
  2744. {
  2745. unsigned long cap = arch_scale_cpu_capacity(cpu);
  2746. return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT);
  2747. }
  2748. static inline unsigned long cpu_bw_dl(struct rq *rq)
  2749. {
  2750. return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
  2751. }
  2752. static inline unsigned long cpu_util_dl(struct rq *rq)
  2753. {
  2754. return READ_ONCE(rq->avg_dl.util_avg);
  2755. }
  2756. extern unsigned long cpu_util_cfs(int cpu);
  2757. extern unsigned long cpu_util_cfs_boost(int cpu);
  2758. static inline unsigned long cpu_util_rt(struct rq *rq)
  2759. {
  2760. return READ_ONCE(rq->avg_rt.util_avg);
  2761. }
  2762. #else /* !CONFIG_SMP */
  2763. static inline bool update_other_load_avgs(struct rq *rq) { return false; }
  2764. #endif /* CONFIG_SMP */
  2765. #ifdef CONFIG_UCLAMP_TASK
  2766. unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
  2767. static inline unsigned long uclamp_rq_get(struct rq *rq,
  2768. enum uclamp_id clamp_id)
  2769. {
  2770. return READ_ONCE(rq->uclamp[clamp_id].value);
  2771. }
  2772. static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
  2773. unsigned int value)
  2774. {
  2775. WRITE_ONCE(rq->uclamp[clamp_id].value, value);
  2776. }
  2777. static inline bool uclamp_rq_is_idle(struct rq *rq)
  2778. {
  2779. return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
  2780. }
  2781. /* Is the rq being capped/throttled by uclamp_max? */
  2782. static inline bool uclamp_rq_is_capped(struct rq *rq)
  2783. {
  2784. unsigned long rq_util;
  2785. unsigned long max_util;
  2786. if (!static_branch_likely(&sched_uclamp_used))
  2787. return false;
  2788. rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
  2789. max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
  2790. return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
  2791. }
  2792. /*
  2793. * When uclamp is compiled in, the aggregation at rq level is 'turned off'
  2794. * by default in the fast path and only gets turned on once userspace performs
  2795. * an operation that requires it.
  2796. *
  2797. * Returns true if userspace opted-in to use uclamp and aggregation at rq level
  2798. * hence is active.
  2799. */
  2800. static inline bool uclamp_is_used(void)
  2801. {
  2802. return static_branch_likely(&sched_uclamp_used);
  2803. }
  2804. #define for_each_clamp_id(clamp_id) \
  2805. for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
  2806. extern unsigned int sysctl_sched_uclamp_util_min_rt_default;
  2807. static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
  2808. {
  2809. if (clamp_id == UCLAMP_MIN)
  2810. return 0;
  2811. return SCHED_CAPACITY_SCALE;
  2812. }
  2813. /* Integer rounded range for each bucket */
  2814. #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
  2815. static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
  2816. {
  2817. return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1);
  2818. }
  2819. static inline void
  2820. uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined)
  2821. {
  2822. uc_se->value = value;
  2823. uc_se->bucket_id = uclamp_bucket_id(value);
  2824. uc_se->user_defined = user_defined;
  2825. }
  2826. #else /* !CONFIG_UCLAMP_TASK: */
  2827. static inline unsigned long
  2828. uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
  2829. {
  2830. if (clamp_id == UCLAMP_MIN)
  2831. return 0;
  2832. return SCHED_CAPACITY_SCALE;
  2833. }
  2834. static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
  2835. static inline bool uclamp_is_used(void)
  2836. {
  2837. return false;
  2838. }
  2839. static inline unsigned long
  2840. uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
  2841. {
  2842. if (clamp_id == UCLAMP_MIN)
  2843. return 0;
  2844. return SCHED_CAPACITY_SCALE;
  2845. }
  2846. static inline void
  2847. uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value)
  2848. {
  2849. }
  2850. static inline bool uclamp_rq_is_idle(struct rq *rq)
  2851. {
  2852. return false;
  2853. }
  2854. #endif /* !CONFIG_UCLAMP_TASK */
  2855. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  2856. static inline unsigned long cpu_util_irq(struct rq *rq)
  2857. {
  2858. return READ_ONCE(rq->avg_irq.util_avg);
  2859. }
  2860. static inline
  2861. unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
  2862. {
  2863. util *= (max - irq);
  2864. util /= max;
  2865. return util;
  2866. }
  2867. #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */
  2868. static inline unsigned long cpu_util_irq(struct rq *rq)
  2869. {
  2870. return 0;
  2871. }
  2872. static inline
  2873. unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
  2874. {
  2875. return util;
  2876. }
  2877. #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */
  2878. #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
  2879. #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
  2880. DECLARE_STATIC_KEY_FALSE(sched_energy_present);
  2881. static inline bool sched_energy_enabled(void)
  2882. {
  2883. return static_branch_unlikely(&sched_energy_present);
  2884. }
  2885. extern struct cpufreq_governor schedutil_gov;
  2886. #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
  2887. #define perf_domain_span(pd) NULL
  2888. static inline bool sched_energy_enabled(void) { return false; }
  2889. #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
  2890. #ifdef CONFIG_MEMBARRIER
  2891. /*
  2892. * The scheduler provides memory barriers required by membarrier between:
  2893. * - prior user-space memory accesses and store to rq->membarrier_state,
  2894. * - store to rq->membarrier_state and following user-space memory accesses.
  2895. * In the same way it provides those guarantees around store to rq->curr.
  2896. */
  2897. static inline void membarrier_switch_mm(struct rq *rq,
  2898. struct mm_struct *prev_mm,
  2899. struct mm_struct *next_mm)
  2900. {
  2901. int membarrier_state;
  2902. if (prev_mm == next_mm)
  2903. return;
  2904. membarrier_state = atomic_read(&next_mm->membarrier_state);
  2905. if (READ_ONCE(rq->membarrier_state) == membarrier_state)
  2906. return;
  2907. WRITE_ONCE(rq->membarrier_state, membarrier_state);
  2908. }
  2909. #else /* !CONFIG_MEMBARRIER :*/
  2910. static inline void membarrier_switch_mm(struct rq *rq,
  2911. struct mm_struct *prev_mm,
  2912. struct mm_struct *next_mm)
  2913. {
  2914. }
  2915. #endif /* !CONFIG_MEMBARRIER */
  2916. #ifdef CONFIG_SMP
  2917. static inline bool is_per_cpu_kthread(struct task_struct *p)
  2918. {
  2919. if (!(p->flags & PF_KTHREAD))
  2920. return false;
  2921. if (p->nr_cpus_allowed != 1)
  2922. return false;
  2923. return true;
  2924. }
  2925. #endif
  2926. extern void swake_up_all_locked(struct swait_queue_head *q);
  2927. extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  2928. extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags);
  2929. #ifdef CONFIG_PREEMPT_DYNAMIC
  2930. extern int preempt_dynamic_mode;
  2931. extern int sched_dynamic_mode(const char *str);
  2932. extern void sched_dynamic_update(int mode);
  2933. #endif
  2934. #ifdef CONFIG_SCHED_MM_CID
  2935. #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */
  2936. #define MM_CID_SCAN_DELAY 100 /* 100ms */
  2937. extern raw_spinlock_t cid_lock;
  2938. extern int use_cid_lock;
  2939. extern void sched_mm_cid_migrate_from(struct task_struct *t);
  2940. extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t);
  2941. extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr);
  2942. extern void init_sched_mm_cid(struct task_struct *t);
  2943. static inline void __mm_cid_put(struct mm_struct *mm, int cid)
  2944. {
  2945. if (cid < 0)
  2946. return;
  2947. cpumask_clear_cpu(cid, mm_cidmask(mm));
  2948. }
  2949. /*
  2950. * The per-mm/cpu cid can have the MM_CID_LAZY_PUT flag set or transition to
  2951. * the MM_CID_UNSET state without holding the rq lock, but the rq lock needs to
  2952. * be held to transition to other states.
  2953. *
  2954. * State transitions synchronized with cmpxchg or try_cmpxchg need to be
  2955. * consistent across CPUs, which prevents use of this_cpu_cmpxchg.
  2956. */
  2957. static inline void mm_cid_put_lazy(struct task_struct *t)
  2958. {
  2959. struct mm_struct *mm = t->mm;
  2960. struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
  2961. int cid;
  2962. lockdep_assert_irqs_disabled();
  2963. cid = __this_cpu_read(pcpu_cid->cid);
  2964. if (!mm_cid_is_lazy_put(cid) ||
  2965. !try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
  2966. return;
  2967. __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
  2968. }
  2969. static inline int mm_cid_pcpu_unset(struct mm_struct *mm)
  2970. {
  2971. struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
  2972. int cid, res;
  2973. lockdep_assert_irqs_disabled();
  2974. cid = __this_cpu_read(pcpu_cid->cid);
  2975. for (;;) {
  2976. if (mm_cid_is_unset(cid))
  2977. return MM_CID_UNSET;
  2978. /*
  2979. * Attempt transition from valid or lazy-put to unset.
  2980. */
  2981. res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET);
  2982. if (res == cid)
  2983. break;
  2984. cid = res;
  2985. }
  2986. return cid;
  2987. }
  2988. static inline void mm_cid_put(struct mm_struct *mm)
  2989. {
  2990. int cid;
  2991. lockdep_assert_irqs_disabled();
  2992. cid = mm_cid_pcpu_unset(mm);
  2993. if (cid == MM_CID_UNSET)
  2994. return;
  2995. __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
  2996. }
  2997. static inline int __mm_cid_try_get(struct mm_struct *mm)
  2998. {
  2999. struct cpumask *cpumask;
  3000. int cid;
  3001. cpumask = mm_cidmask(mm);
  3002. /*
  3003. * Retry finding first zero bit if the mask is temporarily
  3004. * filled. This only happens during concurrent remote-clear
  3005. * which owns a cid without holding a rq lock.
  3006. */
  3007. for (;;) {
  3008. cid = cpumask_first_zero(cpumask);
  3009. if (cid < nr_cpu_ids)
  3010. break;
  3011. cpu_relax();
  3012. }
  3013. if (cpumask_test_and_set_cpu(cid, cpumask))
  3014. return -1;
  3015. return cid;
  3016. }
  3017. /*
  3018. * Save a snapshot of the current runqueue time of this cpu
  3019. * with the per-cpu cid value, allowing to estimate how recently it was used.
  3020. */
  3021. static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm)
  3022. {
  3023. struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq));
  3024. lockdep_assert_rq_held(rq);
  3025. WRITE_ONCE(pcpu_cid->time, rq->clock);
  3026. }
  3027. static inline int __mm_cid_get(struct rq *rq, struct mm_struct *mm)
  3028. {
  3029. int cid;
  3030. /*
  3031. * All allocations (even those using the cid_lock) are lock-free. If
  3032. * use_cid_lock is set, hold the cid_lock to perform cid allocation to
  3033. * guarantee forward progress.
  3034. */
  3035. if (!READ_ONCE(use_cid_lock)) {
  3036. cid = __mm_cid_try_get(mm);
  3037. if (cid >= 0)
  3038. goto end;
  3039. raw_spin_lock(&cid_lock);
  3040. } else {
  3041. raw_spin_lock(&cid_lock);
  3042. cid = __mm_cid_try_get(mm);
  3043. if (cid >= 0)
  3044. goto unlock;
  3045. }
  3046. /*
  3047. * cid concurrently allocated. Retry while forcing following
  3048. * allocations to use the cid_lock to ensure forward progress.
  3049. */
  3050. WRITE_ONCE(use_cid_lock, 1);
  3051. /*
  3052. * Set use_cid_lock before allocation. Only care about program order
  3053. * because this is only required for forward progress.
  3054. */
  3055. barrier();
  3056. /*
  3057. * Retry until it succeeds. It is guaranteed to eventually succeed once
  3058. * all newcoming allocations observe the use_cid_lock flag set.
  3059. */
  3060. do {
  3061. cid = __mm_cid_try_get(mm);
  3062. cpu_relax();
  3063. } while (cid < 0);
  3064. /*
  3065. * Allocate before clearing use_cid_lock. Only care about
  3066. * program order because this is for forward progress.
  3067. */
  3068. barrier();
  3069. WRITE_ONCE(use_cid_lock, 0);
  3070. unlock:
  3071. raw_spin_unlock(&cid_lock);
  3072. end:
  3073. mm_cid_snapshot_time(rq, mm);
  3074. return cid;
  3075. }
  3076. static inline int mm_cid_get(struct rq *rq, struct mm_struct *mm)
  3077. {
  3078. struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
  3079. int cid;
  3080. lockdep_assert_rq_held(rq);
  3081. cid = __this_cpu_read(pcpu_cid->cid);
  3082. if (mm_cid_is_valid(cid)) {
  3083. mm_cid_snapshot_time(rq, mm);
  3084. return cid;
  3085. }
  3086. if (mm_cid_is_lazy_put(cid)) {
  3087. if (try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
  3088. __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
  3089. }
  3090. cid = __mm_cid_get(rq, mm);
  3091. __this_cpu_write(pcpu_cid->cid, cid);
  3092. return cid;
  3093. }
  3094. static inline void switch_mm_cid(struct rq *rq,
  3095. struct task_struct *prev,
  3096. struct task_struct *next)
  3097. {
  3098. /*
  3099. * Provide a memory barrier between rq->curr store and load of
  3100. * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition.
  3101. *
  3102. * Should be adapted if context_switch() is modified.
  3103. */
  3104. if (!next->mm) { // to kernel
  3105. /*
  3106. * user -> kernel transition does not guarantee a barrier, but
  3107. * we can use the fact that it performs an atomic operation in
  3108. * mmgrab().
  3109. */
  3110. if (prev->mm) // from user
  3111. smp_mb__after_mmgrab();
  3112. /*
  3113. * kernel -> kernel transition does not change rq->curr->mm
  3114. * state. It stays NULL.
  3115. */
  3116. } else { // to user
  3117. /*
  3118. * kernel -> user transition does not provide a barrier
  3119. * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu].
  3120. * Provide it here.
  3121. */
  3122. if (!prev->mm) { // from kernel
  3123. smp_mb();
  3124. } else { // from user
  3125. /*
  3126. * user->user transition relies on an implicit
  3127. * memory barrier in switch_mm() when
  3128. * current->mm changes. If the architecture
  3129. * switch_mm() does not have an implicit memory
  3130. * barrier, it is emitted here. If current->mm
  3131. * is unchanged, no barrier is needed.
  3132. */
  3133. smp_mb__after_switch_mm();
  3134. }
  3135. }
  3136. if (prev->mm_cid_active) {
  3137. mm_cid_snapshot_time(rq, prev->mm);
  3138. mm_cid_put_lazy(prev);
  3139. prev->mm_cid = -1;
  3140. }
  3141. if (next->mm_cid_active)
  3142. next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next->mm);
  3143. }
  3144. #else /* !CONFIG_SCHED_MM_CID: */
  3145. static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, struct task_struct *next) { }
  3146. static inline void sched_mm_cid_migrate_from(struct task_struct *t) { }
  3147. static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) { }
  3148. static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) { }
  3149. static inline void init_sched_mm_cid(struct task_struct *t) { }
  3150. #endif /* !CONFIG_SCHED_MM_CID */
  3151. extern u64 avg_vruntime(struct cfs_rq *cfs_rq);
  3152. extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se);
  3153. #ifdef CONFIG_RT_MUTEXES
  3154. static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
  3155. {
  3156. if (pi_task)
  3157. prio = min(prio, pi_task->prio);
  3158. return prio;
  3159. }
  3160. static inline int rt_effective_prio(struct task_struct *p, int prio)
  3161. {
  3162. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  3163. return __rt_effective_prio(pi_task, prio);
  3164. }
  3165. #else /* !CONFIG_RT_MUTEXES: */
  3166. static inline int rt_effective_prio(struct task_struct *p, int prio)
  3167. {
  3168. return prio;
  3169. }
  3170. #endif /* !CONFIG_RT_MUTEXES */
  3171. extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi);
  3172. extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx);
  3173. extern const struct sched_class *__setscheduler_class(int policy, int prio);
  3174. extern void set_load_weight(struct task_struct *p, bool update_load);
  3175. extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags);
  3176. extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags);
  3177. extern void check_class_changing(struct rq *rq, struct task_struct *p,
  3178. const struct sched_class *prev_class);
  3179. extern void check_class_changed(struct rq *rq, struct task_struct *p,
  3180. const struct sched_class *prev_class,
  3181. int oldprio);
  3182. #ifdef CONFIG_SMP
  3183. extern struct balance_callback *splice_balance_callbacks(struct rq *rq);
  3184. extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
  3185. #else
  3186. static inline struct balance_callback *splice_balance_callbacks(struct rq *rq)
  3187. {
  3188. return NULL;
  3189. }
  3190. static inline void balance_callbacks(struct rq *rq, struct balance_callback *head)
  3191. {
  3192. }
  3193. #endif
  3194. #ifdef CONFIG_SCHED_CLASS_EXT
  3195. /*
  3196. * Used by SCX in the enable/disable paths to move tasks between sched_classes
  3197. * and establish invariants.
  3198. */
  3199. struct sched_enq_and_set_ctx {
  3200. struct task_struct *p;
  3201. int queue_flags;
  3202. bool queued;
  3203. bool running;
  3204. };
  3205. void sched_deq_and_put_task(struct task_struct *p, int queue_flags,
  3206. struct sched_enq_and_set_ctx *ctx);
  3207. void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx);
  3208. #endif /* CONFIG_SCHED_CLASS_EXT */
  3209. #include "ext.h"
  3210. #endif /* _KERNEL_SCHED_SCHED_H */