sched.h 85 KB

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