sched.h 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Scheduler internal types and methods:
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/sched/autogroup.h>
  7. #include <linux/sched/clock.h>
  8. #include <linux/sched/coredump.h>
  9. #include <linux/sched/cpufreq.h>
  10. #include <linux/sched/cputime.h>
  11. #include <linux/sched/deadline.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/sched/hotplug.h>
  14. #include <linux/sched/idle.h>
  15. #include <linux/sched/init.h>
  16. #include <linux/sched/isolation.h>
  17. #include <linux/sched/jobctl.h>
  18. #include <linux/sched/loadavg.h>
  19. #include <linux/sched/mm.h>
  20. #include <linux/sched/nohz.h>
  21. #include <linux/sched/numa_balancing.h>
  22. #include <linux/sched/prio.h>
  23. #include <linux/sched/rt.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/sched/stat.h>
  26. #include <linux/sched/sysctl.h>
  27. #include <linux/sched/task.h>
  28. #include <linux/sched/task_stack.h>
  29. #include <linux/sched/topology.h>
  30. #include <linux/sched/user.h>
  31. #include <linux/sched/wake_q.h>
  32. #include <linux/sched/xacct.h>
  33. #include <uapi/linux/sched/types.h>
  34. #include <linux/binfmts.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/compat.h>
  37. #include <linux/context_tracking.h>
  38. #include <linux/cpufreq.h>
  39. #include <linux/cpuidle.h>
  40. #include <linux/cpuset.h>
  41. #include <linux/ctype.h>
  42. #include <linux/debugfs.h>
  43. #include <linux/delayacct.h>
  44. #include <linux/init_task.h>
  45. #include <linux/kprobes.h>
  46. #include <linux/kthread.h>
  47. #include <linux/membarrier.h>
  48. #include <linux/migrate.h>
  49. #include <linux/mmu_context.h>
  50. #include <linux/nmi.h>
  51. #include <linux/proc_fs.h>
  52. #include <linux/prefetch.h>
  53. #include <linux/profile.h>
  54. #include <linux/psi.h>
  55. #include <linux/rcupdate_wait.h>
  56. #include <linux/security.h>
  57. #include <linux/stop_machine.h>
  58. #include <linux/suspend.h>
  59. #include <linux/swait.h>
  60. #include <linux/syscalls.h>
  61. #include <linux/task_work.h>
  62. #include <linux/tsacct_kern.h>
  63. #include <asm/tlb.h>
  64. #ifdef CONFIG_PARAVIRT
  65. # include <asm/paravirt.h>
  66. #endif
  67. #include "cpupri.h"
  68. #include "cpudeadline.h"
  69. #ifdef CONFIG_SCHED_DEBUG
  70. # define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
  71. #else
  72. # define SCHED_WARN_ON(x) ({ (void)(x), 0; })
  73. #endif
  74. struct rq;
  75. struct cpuidle_state;
  76. /* task_struct::on_rq states: */
  77. #define TASK_ON_RQ_QUEUED 1
  78. #define TASK_ON_RQ_MIGRATING 2
  79. extern __read_mostly int scheduler_running;
  80. extern unsigned long calc_load_update;
  81. extern atomic_long_t calc_load_tasks;
  82. extern void calc_global_load_tick(struct rq *this_rq);
  83. extern long calc_load_fold_active(struct rq *this_rq, long adjust);
  84. #ifdef CONFIG_SMP
  85. extern void cpu_load_update_active(struct rq *this_rq);
  86. #else
  87. static inline void cpu_load_update_active(struct rq *this_rq) { }
  88. #endif
  89. /*
  90. * Helpers for converting nanosecond timing to jiffy resolution
  91. */
  92. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  93. /*
  94. * Increase resolution of nice-level calculations for 64-bit architectures.
  95. * The extra resolution improves shares distribution and load balancing of
  96. * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
  97. * hierarchies, especially on larger systems. This is not a user-visible change
  98. * and does not change the user-interface for setting shares/weights.
  99. *
  100. * We increase resolution only if we have enough bits to allow this increased
  101. * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
  102. * are pretty high and the returns do not justify the increased costs.
  103. *
  104. * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
  105. * increase coverage and consistency always enable it on 64-bit platforms.
  106. */
  107. #ifdef CONFIG_64BIT
  108. # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
  109. # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
  110. # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
  111. #else
  112. # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
  113. # define scale_load(w) (w)
  114. # define scale_load_down(w) (w)
  115. #endif
  116. /*
  117. * Task weight (visible to users) and its load (invisible to users) have
  118. * independent resolution, but they should be well calibrated. We use
  119. * scale_load() and scale_load_down(w) to convert between them. The
  120. * following must be true:
  121. *
  122. * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
  123. *
  124. */
  125. #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
  126. /*
  127. * Single value that decides SCHED_DEADLINE internal math precision.
  128. * 10 -> just above 1us
  129. * 9 -> just above 0.5us
  130. */
  131. #define DL_SCALE 10
  132. /*
  133. * Single value that denotes runtime == period, ie unlimited time.
  134. */
  135. #define RUNTIME_INF ((u64)~0ULL)
  136. static inline int idle_policy(int policy)
  137. {
  138. return policy == SCHED_IDLE;
  139. }
  140. static inline int fair_policy(int policy)
  141. {
  142. return policy == SCHED_NORMAL || policy == SCHED_BATCH;
  143. }
  144. static inline int rt_policy(int policy)
  145. {
  146. return policy == SCHED_FIFO || policy == SCHED_RR;
  147. }
  148. static inline int dl_policy(int policy)
  149. {
  150. return policy == SCHED_DEADLINE;
  151. }
  152. static inline bool valid_policy(int policy)
  153. {
  154. return idle_policy(policy) || fair_policy(policy) ||
  155. rt_policy(policy) || dl_policy(policy);
  156. }
  157. static inline int task_has_rt_policy(struct task_struct *p)
  158. {
  159. return rt_policy(p->policy);
  160. }
  161. static inline int task_has_dl_policy(struct task_struct *p)
  162. {
  163. return dl_policy(p->policy);
  164. }
  165. #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
  166. /*
  167. * !! For sched_setattr_nocheck() (kernel) only !!
  168. *
  169. * This is actually gross. :(
  170. *
  171. * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
  172. * tasks, but still be able to sleep. We need this on platforms that cannot
  173. * atomically change clock frequency. Remove once fast switching will be
  174. * available on such platforms.
  175. *
  176. * SUGOV stands for SchedUtil GOVernor.
  177. */
  178. #define SCHED_FLAG_SUGOV 0x10000000
  179. static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se)
  180. {
  181. #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
  182. return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
  183. #else
  184. return false;
  185. #endif
  186. }
  187. /*
  188. * Tells if entity @a should preempt entity @b.
  189. */
  190. static inline bool
  191. dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
  192. {
  193. return dl_entity_is_special(a) ||
  194. dl_time_before(a->deadline, b->deadline);
  195. }
  196. /*
  197. * This is the priority-queue data structure of the RT scheduling class:
  198. */
  199. struct rt_prio_array {
  200. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  201. struct list_head queue[MAX_RT_PRIO];
  202. };
  203. struct rt_bandwidth {
  204. /* nests inside the rq lock: */
  205. raw_spinlock_t rt_runtime_lock;
  206. ktime_t rt_period;
  207. u64 rt_runtime;
  208. struct hrtimer rt_period_timer;
  209. unsigned int rt_period_active;
  210. };
  211. void __dl_clear_params(struct task_struct *p);
  212. /*
  213. * To keep the bandwidth of -deadline tasks and groups under control
  214. * we need some place where:
  215. * - store the maximum -deadline bandwidth of the system (the group);
  216. * - cache the fraction of that bandwidth that is currently allocated.
  217. *
  218. * This is all done in the data structure below. It is similar to the
  219. * one used for RT-throttling (rt_bandwidth), with the main difference
  220. * that, since here we are only interested in admission control, we
  221. * do not decrease any runtime while the group "executes", neither we
  222. * need a timer to replenish it.
  223. *
  224. * With respect to SMP, the bandwidth is given on a per-CPU basis,
  225. * meaning that:
  226. * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
  227. * - dl_total_bw array contains, in the i-eth element, the currently
  228. * allocated bandwidth on the i-eth CPU.
  229. * Moreover, groups consume bandwidth on each CPU, while tasks only
  230. * consume bandwidth on the CPU they're running on.
  231. * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
  232. * that will be shown the next time the proc or cgroup controls will
  233. * be red. It on its turn can be changed by writing on its own
  234. * control.
  235. */
  236. struct dl_bandwidth {
  237. raw_spinlock_t dl_runtime_lock;
  238. u64 dl_runtime;
  239. u64 dl_period;
  240. };
  241. static inline int dl_bandwidth_enabled(void)
  242. {
  243. return sysctl_sched_rt_runtime >= 0;
  244. }
  245. struct dl_bw {
  246. raw_spinlock_t lock;
  247. u64 bw;
  248. u64 total_bw;
  249. };
  250. static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
  251. static inline
  252. void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
  253. {
  254. dl_b->total_bw -= tsk_bw;
  255. __dl_update(dl_b, (s32)tsk_bw / cpus);
  256. }
  257. static inline
  258. void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
  259. {
  260. dl_b->total_bw += tsk_bw;
  261. __dl_update(dl_b, -((s32)tsk_bw / cpus));
  262. }
  263. static inline
  264. bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
  265. {
  266. return dl_b->bw != -1 &&
  267. dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
  268. }
  269. extern void dl_change_utilization(struct task_struct *p, u64 new_bw);
  270. extern void init_dl_bw(struct dl_bw *dl_b);
  271. extern int sched_dl_global_validate(void);
  272. extern void sched_dl_do_global(void);
  273. extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
  274. extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
  275. extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
  276. extern bool __checkparam_dl(const struct sched_attr *attr);
  277. extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
  278. extern int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
  279. extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
  280. extern bool dl_cpu_busy(unsigned int cpu);
  281. #ifdef CONFIG_CGROUP_SCHED
  282. #include <linux/cgroup.h>
  283. #include <linux/psi.h>
  284. struct cfs_rq;
  285. struct rt_rq;
  286. extern struct list_head task_groups;
  287. struct cfs_bandwidth {
  288. #ifdef CONFIG_CFS_BANDWIDTH
  289. raw_spinlock_t lock;
  290. ktime_t period;
  291. u64 quota;
  292. u64 runtime;
  293. s64 hierarchical_quota;
  294. u64 runtime_expires;
  295. int expires_seq;
  296. short idle;
  297. short period_active;
  298. struct hrtimer period_timer;
  299. struct hrtimer slack_timer;
  300. struct list_head throttled_cfs_rq;
  301. /* Statistics: */
  302. int nr_periods;
  303. int nr_throttled;
  304. u64 throttled_time;
  305. bool distribute_running;
  306. #endif
  307. };
  308. /* Task group related information */
  309. struct task_group {
  310. struct cgroup_subsys_state css;
  311. #ifdef CONFIG_FAIR_GROUP_SCHED
  312. /* schedulable entities of this group on each CPU */
  313. struct sched_entity **se;
  314. /* runqueue "owned" by this group on each CPU */
  315. struct cfs_rq **cfs_rq;
  316. unsigned long shares;
  317. #ifdef CONFIG_SMP
  318. /*
  319. * load_avg can be heavily contended at clock tick time, so put
  320. * it in its own cacheline separated from the fields above which
  321. * will also be accessed at each tick.
  322. */
  323. atomic_long_t load_avg ____cacheline_aligned;
  324. #endif
  325. #endif
  326. #ifdef CONFIG_RT_GROUP_SCHED
  327. struct sched_rt_entity **rt_se;
  328. struct rt_rq **rt_rq;
  329. struct rt_bandwidth rt_bandwidth;
  330. #endif
  331. struct rcu_head rcu;
  332. struct list_head list;
  333. struct task_group *parent;
  334. struct list_head siblings;
  335. struct list_head children;
  336. #ifdef CONFIG_SCHED_AUTOGROUP
  337. struct autogroup *autogroup;
  338. #endif
  339. struct cfs_bandwidth cfs_bandwidth;
  340. };
  341. #ifdef CONFIG_FAIR_GROUP_SCHED
  342. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  343. /*
  344. * A weight of 0 or 1 can cause arithmetics problems.
  345. * A weight of a cfs_rq is the sum of weights of which entities
  346. * are queued on this cfs_rq, so a weight of a entity should not be
  347. * too large, so as the shares value of a task group.
  348. * (The default weight is 1024 - so there's no practical
  349. * limitation from this.)
  350. */
  351. #define MIN_SHARES (1UL << 1)
  352. #define MAX_SHARES (1UL << 18)
  353. #endif
  354. typedef int (*tg_visitor)(struct task_group *, void *);
  355. extern int walk_tg_tree_from(struct task_group *from,
  356. tg_visitor down, tg_visitor up, void *data);
  357. /*
  358. * Iterate the full tree, calling @down when first entering a node and @up when
  359. * leaving it for the final time.
  360. *
  361. * Caller must hold rcu_lock or sufficient equivalent.
  362. */
  363. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  364. {
  365. return walk_tg_tree_from(&root_task_group, down, up, data);
  366. }
  367. extern int tg_nop(struct task_group *tg, void *data);
  368. extern void free_fair_sched_group(struct task_group *tg);
  369. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  370. extern void online_fair_sched_group(struct task_group *tg);
  371. extern void unregister_fair_sched_group(struct task_group *tg);
  372. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  373. struct sched_entity *se, int cpu,
  374. struct sched_entity *parent);
  375. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  376. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  377. extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  378. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  379. extern void free_rt_sched_group(struct task_group *tg);
  380. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  381. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  382. struct sched_rt_entity *rt_se, int cpu,
  383. struct sched_rt_entity *parent);
  384. extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
  385. extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
  386. extern long sched_group_rt_runtime(struct task_group *tg);
  387. extern long sched_group_rt_period(struct task_group *tg);
  388. extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
  389. extern struct task_group *sched_create_group(struct task_group *parent);
  390. extern void sched_online_group(struct task_group *tg,
  391. struct task_group *parent);
  392. extern void sched_destroy_group(struct task_group *tg);
  393. extern void sched_offline_group(struct task_group *tg);
  394. extern void sched_move_task(struct task_struct *tsk);
  395. #ifdef CONFIG_FAIR_GROUP_SCHED
  396. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  397. #ifdef CONFIG_SMP
  398. extern void set_task_rq_fair(struct sched_entity *se,
  399. struct cfs_rq *prev, struct cfs_rq *next);
  400. #else /* !CONFIG_SMP */
  401. static inline void set_task_rq_fair(struct sched_entity *se,
  402. struct cfs_rq *prev, struct cfs_rq *next) { }
  403. #endif /* CONFIG_SMP */
  404. #endif /* CONFIG_FAIR_GROUP_SCHED */
  405. #else /* CONFIG_CGROUP_SCHED */
  406. struct cfs_bandwidth { };
  407. #endif /* CONFIG_CGROUP_SCHED */
  408. /* CFS-related fields in a runqueue */
  409. struct cfs_rq {
  410. struct load_weight load;
  411. unsigned long runnable_weight;
  412. unsigned int nr_running;
  413. unsigned int h_nr_running;
  414. u64 exec_clock;
  415. u64 min_vruntime;
  416. #ifndef CONFIG_64BIT
  417. u64 min_vruntime_copy;
  418. #endif
  419. struct rb_root_cached tasks_timeline;
  420. /*
  421. * 'curr' points to currently running entity on this cfs_rq.
  422. * It is set to NULL otherwise (i.e when none are currently running).
  423. */
  424. struct sched_entity *curr;
  425. struct sched_entity *next;
  426. struct sched_entity *last;
  427. struct sched_entity *skip;
  428. #ifdef CONFIG_SCHED_DEBUG
  429. unsigned int nr_spread_over;
  430. #endif
  431. #ifdef CONFIG_SMP
  432. /*
  433. * CFS load tracking
  434. */
  435. struct sched_avg avg;
  436. #ifndef CONFIG_64BIT
  437. u64 load_last_update_time_copy;
  438. #endif
  439. struct {
  440. raw_spinlock_t lock ____cacheline_aligned;
  441. int nr;
  442. unsigned long load_avg;
  443. unsigned long util_avg;
  444. unsigned long runnable_sum;
  445. } removed;
  446. #ifdef CONFIG_FAIR_GROUP_SCHED
  447. unsigned long tg_load_avg_contrib;
  448. long propagate;
  449. long prop_runnable_sum;
  450. /*
  451. * h_load = weight * f(tg)
  452. *
  453. * Where f(tg) is the recursive weight fraction assigned to
  454. * this group.
  455. */
  456. unsigned long h_load;
  457. u64 last_h_load_update;
  458. struct sched_entity *h_load_next;
  459. #endif /* CONFIG_FAIR_GROUP_SCHED */
  460. #endif /* CONFIG_SMP */
  461. #ifdef CONFIG_FAIR_GROUP_SCHED
  462. struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
  463. /*
  464. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  465. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  466. * (like users, containers etc.)
  467. *
  468. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
  469. * This list is used during load balance.
  470. */
  471. int on_list;
  472. struct list_head leaf_cfs_rq_list;
  473. struct task_group *tg; /* group that "owns" this runqueue */
  474. #ifdef CONFIG_CFS_BANDWIDTH
  475. int runtime_enabled;
  476. int expires_seq;
  477. u64 runtime_expires;
  478. s64 runtime_remaining;
  479. u64 throttled_clock;
  480. u64 throttled_clock_task;
  481. u64 throttled_clock_task_time;
  482. int throttled;
  483. int throttle_count;
  484. struct list_head throttled_list;
  485. #endif /* CONFIG_CFS_BANDWIDTH */
  486. #endif /* CONFIG_FAIR_GROUP_SCHED */
  487. };
  488. static inline int rt_bandwidth_enabled(void)
  489. {
  490. return sysctl_sched_rt_runtime >= 0;
  491. }
  492. /* RT IPI pull logic requires IRQ_WORK */
  493. #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
  494. # define HAVE_RT_PUSH_IPI
  495. #endif
  496. /* Real-Time classes' related field in a runqueue: */
  497. struct rt_rq {
  498. struct rt_prio_array active;
  499. unsigned int rt_nr_running;
  500. unsigned int rr_nr_running;
  501. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  502. struct {
  503. int curr; /* highest queued rt task prio */
  504. #ifdef CONFIG_SMP
  505. int next; /* next highest */
  506. #endif
  507. } highest_prio;
  508. #endif
  509. #ifdef CONFIG_SMP
  510. unsigned long rt_nr_migratory;
  511. unsigned long rt_nr_total;
  512. int overloaded;
  513. struct plist_head pushable_tasks;
  514. #endif /* CONFIG_SMP */
  515. int rt_queued;
  516. int rt_throttled;
  517. u64 rt_time;
  518. u64 rt_runtime;
  519. /* Nests inside the rq lock: */
  520. raw_spinlock_t rt_runtime_lock;
  521. #ifdef CONFIG_RT_GROUP_SCHED
  522. unsigned long rt_nr_boosted;
  523. struct rq *rq;
  524. struct task_group *tg;
  525. #endif
  526. };
  527. static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
  528. {
  529. return rt_rq->rt_queued && rt_rq->rt_nr_running;
  530. }
  531. /* Deadline class' related fields in a runqueue */
  532. struct dl_rq {
  533. /* runqueue is an rbtree, ordered by deadline */
  534. struct rb_root_cached root;
  535. unsigned long dl_nr_running;
  536. #ifdef CONFIG_SMP
  537. /*
  538. * Deadline values of the currently executing and the
  539. * earliest ready task on this rq. Caching these facilitates
  540. * the decision wether or not a ready but not running task
  541. * should migrate somewhere else.
  542. */
  543. struct {
  544. u64 curr;
  545. u64 next;
  546. } earliest_dl;
  547. unsigned long dl_nr_migratory;
  548. int overloaded;
  549. /*
  550. * Tasks on this rq that can be pushed away. They are kept in
  551. * an rb-tree, ordered by tasks' deadlines, with caching
  552. * of the leftmost (earliest deadline) element.
  553. */
  554. struct rb_root_cached pushable_dl_tasks_root;
  555. #else
  556. struct dl_bw dl_bw;
  557. #endif
  558. /*
  559. * "Active utilization" for this runqueue: increased when a
  560. * task wakes up (becomes TASK_RUNNING) and decreased when a
  561. * task blocks
  562. */
  563. u64 running_bw;
  564. /*
  565. * Utilization of the tasks "assigned" to this runqueue (including
  566. * the tasks that are in runqueue and the tasks that executed on this
  567. * CPU and blocked). Increased when a task moves to this runqueue, and
  568. * decreased when the task moves away (migrates, changes scheduling
  569. * policy, or terminates).
  570. * This is needed to compute the "inactive utilization" for the
  571. * runqueue (inactive utilization = this_bw - running_bw).
  572. */
  573. u64 this_bw;
  574. u64 extra_bw;
  575. /*
  576. * Inverse of the fraction of CPU utilization that can be reclaimed
  577. * by the GRUB algorithm.
  578. */
  579. u64 bw_ratio;
  580. };
  581. #ifdef CONFIG_FAIR_GROUP_SCHED
  582. /* An entity is a task if it doesn't "own" a runqueue */
  583. #define entity_is_task(se) (!se->my_q)
  584. #else
  585. #define entity_is_task(se) 1
  586. #endif
  587. #ifdef CONFIG_SMP
  588. /*
  589. * XXX we want to get rid of these helpers and use the full load resolution.
  590. */
  591. static inline long se_weight(struct sched_entity *se)
  592. {
  593. return scale_load_down(se->load.weight);
  594. }
  595. static inline long se_runnable(struct sched_entity *se)
  596. {
  597. return scale_load_down(se->runnable_weight);
  598. }
  599. static inline bool sched_asym_prefer(int a, int b)
  600. {
  601. return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
  602. }
  603. /*
  604. * We add the notion of a root-domain which will be used to define per-domain
  605. * variables. Each exclusive cpuset essentially defines an island domain by
  606. * fully partitioning the member CPUs from any other cpuset. Whenever a new
  607. * exclusive cpuset is created, we also create and attach a new root-domain
  608. * object.
  609. *
  610. */
  611. struct root_domain {
  612. atomic_t refcount;
  613. atomic_t rto_count;
  614. struct rcu_head rcu;
  615. cpumask_var_t span;
  616. cpumask_var_t online;
  617. /*
  618. * Indicate pullable load on at least one CPU, e.g:
  619. * - More than one runnable task
  620. * - Running task is misfit
  621. */
  622. int overload;
  623. /*
  624. * The bit corresponding to a CPU gets set here if such CPU has more
  625. * than one runnable -deadline task (as it is below for RT tasks).
  626. */
  627. cpumask_var_t dlo_mask;
  628. atomic_t dlo_count;
  629. struct dl_bw dl_bw;
  630. struct cpudl cpudl;
  631. #ifdef HAVE_RT_PUSH_IPI
  632. /*
  633. * For IPI pull requests, loop across the rto_mask.
  634. */
  635. struct irq_work rto_push_work;
  636. raw_spinlock_t rto_lock;
  637. /* These are only updated and read within rto_lock */
  638. int rto_loop;
  639. int rto_cpu;
  640. /* These atomics are updated outside of a lock */
  641. atomic_t rto_loop_next;
  642. atomic_t rto_loop_start;
  643. #endif
  644. /*
  645. * The "RT overload" flag: it gets set if a CPU has more than
  646. * one runnable RT task.
  647. */
  648. cpumask_var_t rto_mask;
  649. struct cpupri cpupri;
  650. unsigned long max_cpu_capacity;
  651. };
  652. extern struct root_domain def_root_domain;
  653. extern struct mutex sched_domains_mutex;
  654. extern void init_defrootdomain(void);
  655. extern int sched_init_domains(const struct cpumask *cpu_map);
  656. extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
  657. extern void sched_get_rd(struct root_domain *rd);
  658. extern void sched_put_rd(struct root_domain *rd);
  659. #ifdef HAVE_RT_PUSH_IPI
  660. extern void rto_push_irq_work_func(struct irq_work *work);
  661. #endif
  662. #endif /* CONFIG_SMP */
  663. /*
  664. * This is the main, per-CPU runqueue data structure.
  665. *
  666. * Locking rule: those places that want to lock multiple runqueues
  667. * (such as the load balancing or the thread migration code), lock
  668. * acquire operations must be ordered by ascending &runqueue.
  669. */
  670. struct rq {
  671. /* runqueue lock: */
  672. raw_spinlock_t lock;
  673. /*
  674. * nr_running and cpu_load should be in the same cacheline because
  675. * remote CPUs use both these fields when doing load calculation.
  676. */
  677. unsigned int nr_running;
  678. #ifdef CONFIG_NUMA_BALANCING
  679. unsigned int nr_numa_running;
  680. unsigned int nr_preferred_running;
  681. unsigned int numa_migrate_on;
  682. #endif
  683. #define CPU_LOAD_IDX_MAX 5
  684. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  685. #ifdef CONFIG_NO_HZ_COMMON
  686. #ifdef CONFIG_SMP
  687. unsigned long last_load_update_tick;
  688. unsigned long last_blocked_load_update_tick;
  689. unsigned int has_blocked_load;
  690. #endif /* CONFIG_SMP */
  691. unsigned int nohz_tick_stopped;
  692. atomic_t nohz_flags;
  693. #endif /* CONFIG_NO_HZ_COMMON */
  694. /* capture load from *all* tasks on this CPU: */
  695. struct load_weight load;
  696. unsigned long nr_load_updates;
  697. u64 nr_switches;
  698. struct cfs_rq cfs;
  699. struct rt_rq rt;
  700. struct dl_rq dl;
  701. #ifdef CONFIG_FAIR_GROUP_SCHED
  702. /* list of leaf cfs_rq on this CPU: */
  703. struct list_head leaf_cfs_rq_list;
  704. struct list_head *tmp_alone_branch;
  705. #endif /* CONFIG_FAIR_GROUP_SCHED */
  706. /*
  707. * This is part of a global counter where only the total sum
  708. * over all CPUs matters. A task can increase this counter on
  709. * one CPU and if it got migrated afterwards it may decrease
  710. * it on another CPU. Always updated under the runqueue lock:
  711. */
  712. unsigned long nr_uninterruptible;
  713. struct task_struct *curr;
  714. struct task_struct *idle;
  715. struct task_struct *stop;
  716. unsigned long next_balance;
  717. struct mm_struct *prev_mm;
  718. unsigned int clock_update_flags;
  719. u64 clock;
  720. u64 clock_task;
  721. atomic_t nr_iowait;
  722. #ifdef CONFIG_SMP
  723. struct root_domain *rd;
  724. struct sched_domain *sd;
  725. unsigned long cpu_capacity;
  726. unsigned long cpu_capacity_orig;
  727. struct callback_head *balance_callback;
  728. unsigned char idle_balance;
  729. unsigned long misfit_task_load;
  730. /* For active balancing */
  731. int active_balance;
  732. int push_cpu;
  733. struct cpu_stop_work active_balance_work;
  734. /* CPU of this runqueue: */
  735. int cpu;
  736. int online;
  737. struct list_head cfs_tasks;
  738. struct sched_avg avg_rt;
  739. struct sched_avg avg_dl;
  740. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  741. struct sched_avg avg_irq;
  742. #endif
  743. u64 idle_stamp;
  744. u64 avg_idle;
  745. /* This is used to determine avg_idle's max value */
  746. u64 max_idle_balance_cost;
  747. #endif
  748. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  749. u64 prev_irq_time;
  750. #endif
  751. #ifdef CONFIG_PARAVIRT
  752. u64 prev_steal_time;
  753. #endif
  754. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  755. u64 prev_steal_time_rq;
  756. #endif
  757. /* calc_load related fields */
  758. unsigned long calc_load_update;
  759. long calc_load_active;
  760. #ifdef CONFIG_SCHED_HRTICK
  761. #ifdef CONFIG_SMP
  762. int hrtick_csd_pending;
  763. call_single_data_t hrtick_csd;
  764. #endif
  765. struct hrtimer hrtick_timer;
  766. #endif
  767. #ifdef CONFIG_SCHEDSTATS
  768. /* latency stats */
  769. struct sched_info rq_sched_info;
  770. unsigned long long rq_cpu_time;
  771. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  772. /* sys_sched_yield() stats */
  773. unsigned int yld_count;
  774. /* schedule() stats */
  775. unsigned int sched_count;
  776. unsigned int sched_goidle;
  777. /* try_to_wake_up() stats */
  778. unsigned int ttwu_count;
  779. unsigned int ttwu_local;
  780. #endif
  781. #ifdef CONFIG_SMP
  782. struct llist_head wake_list;
  783. #endif
  784. #ifdef CONFIG_CPU_IDLE
  785. /* Must be inspected within a rcu lock section */
  786. struct cpuidle_state *idle_state;
  787. #endif
  788. };
  789. static inline int cpu_of(struct rq *rq)
  790. {
  791. #ifdef CONFIG_SMP
  792. return rq->cpu;
  793. #else
  794. return 0;
  795. #endif
  796. }
  797. #ifdef CONFIG_SCHED_SMT
  798. extern struct static_key_false sched_smt_present;
  799. extern void __update_idle_core(struct rq *rq);
  800. static inline void update_idle_core(struct rq *rq)
  801. {
  802. if (static_branch_unlikely(&sched_smt_present))
  803. __update_idle_core(rq);
  804. }
  805. #else
  806. static inline void update_idle_core(struct rq *rq) { }
  807. #endif
  808. DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  809. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  810. #define this_rq() this_cpu_ptr(&runqueues)
  811. #define task_rq(p) cpu_rq(task_cpu(p))
  812. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  813. #define raw_rq() raw_cpu_ptr(&runqueues)
  814. extern void update_rq_clock(struct rq *rq);
  815. static inline u64 __rq_clock_broken(struct rq *rq)
  816. {
  817. return READ_ONCE(rq->clock);
  818. }
  819. /*
  820. * rq::clock_update_flags bits
  821. *
  822. * %RQCF_REQ_SKIP - will request skipping of clock update on the next
  823. * call to __schedule(). This is an optimisation to avoid
  824. * neighbouring rq clock updates.
  825. *
  826. * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
  827. * in effect and calls to update_rq_clock() are being ignored.
  828. *
  829. * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
  830. * made to update_rq_clock() since the last time rq::lock was pinned.
  831. *
  832. * If inside of __schedule(), clock_update_flags will have been
  833. * shifted left (a left shift is a cheap operation for the fast path
  834. * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
  835. *
  836. * if (rq-clock_update_flags >= RQCF_UPDATED)
  837. *
  838. * to check if %RQCF_UPADTED is set. It'll never be shifted more than
  839. * one position though, because the next rq_unpin_lock() will shift it
  840. * back.
  841. */
  842. #define RQCF_REQ_SKIP 0x01
  843. #define RQCF_ACT_SKIP 0x02
  844. #define RQCF_UPDATED 0x04
  845. static inline void assert_clock_updated(struct rq *rq)
  846. {
  847. /*
  848. * The only reason for not seeing a clock update since the
  849. * last rq_pin_lock() is if we're currently skipping updates.
  850. */
  851. SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
  852. }
  853. static inline u64 rq_clock(struct rq *rq)
  854. {
  855. lockdep_assert_held(&rq->lock);
  856. assert_clock_updated(rq);
  857. return rq->clock;
  858. }
  859. static inline u64 rq_clock_task(struct rq *rq)
  860. {
  861. lockdep_assert_held(&rq->lock);
  862. assert_clock_updated(rq);
  863. return rq->clock_task;
  864. }
  865. static inline void rq_clock_skip_update(struct rq *rq)
  866. {
  867. lockdep_assert_held(&rq->lock);
  868. rq->clock_update_flags |= RQCF_REQ_SKIP;
  869. }
  870. /*
  871. * See rt task throttling, which is the only time a skip
  872. * request is cancelled.
  873. */
  874. static inline void rq_clock_cancel_skipupdate(struct rq *rq)
  875. {
  876. lockdep_assert_held(&rq->lock);
  877. rq->clock_update_flags &= ~RQCF_REQ_SKIP;
  878. }
  879. struct rq_flags {
  880. unsigned long flags;
  881. struct pin_cookie cookie;
  882. #ifdef CONFIG_SCHED_DEBUG
  883. /*
  884. * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
  885. * current pin context is stashed here in case it needs to be
  886. * restored in rq_repin_lock().
  887. */
  888. unsigned int clock_update_flags;
  889. #endif
  890. };
  891. static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
  892. {
  893. rf->cookie = lockdep_pin_lock(&rq->lock);
  894. #ifdef CONFIG_SCHED_DEBUG
  895. rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
  896. rf->clock_update_flags = 0;
  897. #endif
  898. }
  899. static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
  900. {
  901. #ifdef CONFIG_SCHED_DEBUG
  902. if (rq->clock_update_flags > RQCF_ACT_SKIP)
  903. rf->clock_update_flags = RQCF_UPDATED;
  904. #endif
  905. lockdep_unpin_lock(&rq->lock, rf->cookie);
  906. }
  907. static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
  908. {
  909. lockdep_repin_lock(&rq->lock, rf->cookie);
  910. #ifdef CONFIG_SCHED_DEBUG
  911. /*
  912. * Restore the value we stashed in @rf for this pin context.
  913. */
  914. rq->clock_update_flags |= rf->clock_update_flags;
  915. #endif
  916. }
  917. struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
  918. __acquires(rq->lock);
  919. struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
  920. __acquires(p->pi_lock)
  921. __acquires(rq->lock);
  922. static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
  923. __releases(rq->lock)
  924. {
  925. rq_unpin_lock(rq, rf);
  926. raw_spin_unlock(&rq->lock);
  927. }
  928. static inline void
  929. task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
  930. __releases(rq->lock)
  931. __releases(p->pi_lock)
  932. {
  933. rq_unpin_lock(rq, rf);
  934. raw_spin_unlock(&rq->lock);
  935. raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
  936. }
  937. static inline void
  938. rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
  939. __acquires(rq->lock)
  940. {
  941. raw_spin_lock_irqsave(&rq->lock, rf->flags);
  942. rq_pin_lock(rq, rf);
  943. }
  944. static inline void
  945. rq_lock_irq(struct rq *rq, struct rq_flags *rf)
  946. __acquires(rq->lock)
  947. {
  948. raw_spin_lock_irq(&rq->lock);
  949. rq_pin_lock(rq, rf);
  950. }
  951. static inline void
  952. rq_lock(struct rq *rq, struct rq_flags *rf)
  953. __acquires(rq->lock)
  954. {
  955. raw_spin_lock(&rq->lock);
  956. rq_pin_lock(rq, rf);
  957. }
  958. static inline void
  959. rq_relock(struct rq *rq, struct rq_flags *rf)
  960. __acquires(rq->lock)
  961. {
  962. raw_spin_lock(&rq->lock);
  963. rq_repin_lock(rq, rf);
  964. }
  965. static inline void
  966. rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
  967. __releases(rq->lock)
  968. {
  969. rq_unpin_lock(rq, rf);
  970. raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
  971. }
  972. static inline void
  973. rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
  974. __releases(rq->lock)
  975. {
  976. rq_unpin_lock(rq, rf);
  977. raw_spin_unlock_irq(&rq->lock);
  978. }
  979. static inline void
  980. rq_unlock(struct rq *rq, struct rq_flags *rf)
  981. __releases(rq->lock)
  982. {
  983. rq_unpin_lock(rq, rf);
  984. raw_spin_unlock(&rq->lock);
  985. }
  986. static inline struct rq *
  987. this_rq_lock_irq(struct rq_flags *rf)
  988. __acquires(rq->lock)
  989. {
  990. struct rq *rq;
  991. local_irq_disable();
  992. rq = this_rq();
  993. rq_lock(rq, rf);
  994. return rq;
  995. }
  996. #ifdef CONFIG_NUMA
  997. enum numa_topology_type {
  998. NUMA_DIRECT,
  999. NUMA_GLUELESS_MESH,
  1000. NUMA_BACKPLANE,
  1001. };
  1002. extern enum numa_topology_type sched_numa_topology_type;
  1003. extern int sched_max_numa_distance;
  1004. extern bool find_numa_distance(int distance);
  1005. #endif
  1006. #ifdef CONFIG_NUMA
  1007. extern void sched_init_numa(void);
  1008. extern void sched_domains_numa_masks_set(unsigned int cpu);
  1009. extern void sched_domains_numa_masks_clear(unsigned int cpu);
  1010. #else
  1011. static inline void sched_init_numa(void) { }
  1012. static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
  1013. static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
  1014. #endif
  1015. #ifdef CONFIG_NUMA_BALANCING
  1016. /* The regions in numa_faults array from task_struct */
  1017. enum numa_faults_stats {
  1018. NUMA_MEM = 0,
  1019. NUMA_CPU,
  1020. NUMA_MEMBUF,
  1021. NUMA_CPUBUF
  1022. };
  1023. extern void sched_setnuma(struct task_struct *p, int node);
  1024. extern int migrate_task_to(struct task_struct *p, int cpu);
  1025. extern int migrate_swap(struct task_struct *p, struct task_struct *t,
  1026. int cpu, int scpu);
  1027. extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
  1028. #else
  1029. static inline void
  1030. init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
  1031. {
  1032. }
  1033. #endif /* CONFIG_NUMA_BALANCING */
  1034. #ifdef CONFIG_SMP
  1035. static inline void
  1036. queue_balance_callback(struct rq *rq,
  1037. struct callback_head *head,
  1038. void (*func)(struct rq *rq))
  1039. {
  1040. lockdep_assert_held(&rq->lock);
  1041. if (unlikely(head->next))
  1042. return;
  1043. head->func = (void (*)(struct callback_head *))func;
  1044. head->next = rq->balance_callback;
  1045. rq->balance_callback = head;
  1046. }
  1047. extern void sched_ttwu_pending(void);
  1048. #define rcu_dereference_check_sched_domain(p) \
  1049. rcu_dereference_check((p), \
  1050. lockdep_is_held(&sched_domains_mutex))
  1051. /*
  1052. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  1053. * See detach_destroy_domains: synchronize_sched for details.
  1054. *
  1055. * The domain tree of any CPU may only be accessed from within
  1056. * preempt-disabled sections.
  1057. */
  1058. #define for_each_domain(cpu, __sd) \
  1059. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  1060. __sd; __sd = __sd->parent)
  1061. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  1062. /**
  1063. * highest_flag_domain - Return highest sched_domain containing flag.
  1064. * @cpu: The CPU whose highest level of sched domain is to
  1065. * be returned.
  1066. * @flag: The flag to check for the highest sched_domain
  1067. * for the given CPU.
  1068. *
  1069. * Returns the highest sched_domain of a CPU which contains the given flag.
  1070. */
  1071. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  1072. {
  1073. struct sched_domain *sd, *hsd = NULL;
  1074. for_each_domain(cpu, sd) {
  1075. if (!(sd->flags & flag))
  1076. break;
  1077. hsd = sd;
  1078. }
  1079. return hsd;
  1080. }
  1081. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  1082. {
  1083. struct sched_domain *sd;
  1084. for_each_domain(cpu, sd) {
  1085. if (sd->flags & flag)
  1086. break;
  1087. }
  1088. return sd;
  1089. }
  1090. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  1091. DECLARE_PER_CPU(int, sd_llc_size);
  1092. DECLARE_PER_CPU(int, sd_llc_id);
  1093. DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
  1094. DECLARE_PER_CPU(struct sched_domain *, sd_numa);
  1095. DECLARE_PER_CPU(struct sched_domain *, sd_asym);
  1096. extern struct static_key_false sched_asym_cpucapacity;
  1097. struct sched_group_capacity {
  1098. atomic_t ref;
  1099. /*
  1100. * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
  1101. * for a single CPU.
  1102. */
  1103. unsigned long capacity;
  1104. unsigned long min_capacity; /* Min per-CPU capacity in group */
  1105. unsigned long max_capacity; /* Max per-CPU capacity in group */
  1106. unsigned long next_update;
  1107. int imbalance; /* XXX unrelated to capacity but shared group state */
  1108. #ifdef CONFIG_SCHED_DEBUG
  1109. int id;
  1110. #endif
  1111. unsigned long cpumask[0]; /* Balance mask */
  1112. };
  1113. struct sched_group {
  1114. struct sched_group *next; /* Must be a circular list */
  1115. atomic_t ref;
  1116. unsigned int group_weight;
  1117. struct sched_group_capacity *sgc;
  1118. int asym_prefer_cpu; /* CPU of highest priority in group */
  1119. /*
  1120. * The CPUs this group covers.
  1121. *
  1122. * NOTE: this field is variable length. (Allocated dynamically
  1123. * by attaching extra space to the end of the structure,
  1124. * depending on how many CPUs the kernel has booted up with)
  1125. */
  1126. unsigned long cpumask[0];
  1127. };
  1128. static inline struct cpumask *sched_group_span(struct sched_group *sg)
  1129. {
  1130. return to_cpumask(sg->cpumask);
  1131. }
  1132. /*
  1133. * See build_balance_mask().
  1134. */
  1135. static inline struct cpumask *group_balance_mask(struct sched_group *sg)
  1136. {
  1137. return to_cpumask(sg->sgc->cpumask);
  1138. }
  1139. /**
  1140. * group_first_cpu - Returns the first CPU in the cpumask of a sched_group.
  1141. * @group: The group whose first CPU is to be returned.
  1142. */
  1143. static inline unsigned int group_first_cpu(struct sched_group *group)
  1144. {
  1145. return cpumask_first(sched_group_span(group));
  1146. }
  1147. extern int group_balance_cpu(struct sched_group *sg);
  1148. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  1149. void register_sched_domain_sysctl(void);
  1150. void dirty_sched_domain_sysctl(int cpu);
  1151. void unregister_sched_domain_sysctl(void);
  1152. #else
  1153. static inline void register_sched_domain_sysctl(void)
  1154. {
  1155. }
  1156. static inline void dirty_sched_domain_sysctl(int cpu)
  1157. {
  1158. }
  1159. static inline void unregister_sched_domain_sysctl(void)
  1160. {
  1161. }
  1162. #endif
  1163. #else
  1164. static inline void sched_ttwu_pending(void) { }
  1165. #endif /* CONFIG_SMP */
  1166. #include "stats.h"
  1167. #include "autogroup.h"
  1168. #ifdef CONFIG_CGROUP_SCHED
  1169. /*
  1170. * Return the group to which this tasks belongs.
  1171. *
  1172. * We cannot use task_css() and friends because the cgroup subsystem
  1173. * changes that value before the cgroup_subsys::attach() method is called,
  1174. * therefore we cannot pin it and might observe the wrong value.
  1175. *
  1176. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  1177. * core changes this before calling sched_move_task().
  1178. *
  1179. * Instead we use a 'copy' which is updated from sched_move_task() while
  1180. * holding both task_struct::pi_lock and rq::lock.
  1181. */
  1182. static inline struct task_group *task_group(struct task_struct *p)
  1183. {
  1184. return p->sched_task_group;
  1185. }
  1186. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  1187. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  1188. {
  1189. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  1190. struct task_group *tg = task_group(p);
  1191. #endif
  1192. #ifdef CONFIG_FAIR_GROUP_SCHED
  1193. set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
  1194. p->se.cfs_rq = tg->cfs_rq[cpu];
  1195. p->se.parent = tg->se[cpu];
  1196. #endif
  1197. #ifdef CONFIG_RT_GROUP_SCHED
  1198. p->rt.rt_rq = tg->rt_rq[cpu];
  1199. p->rt.parent = tg->rt_se[cpu];
  1200. #endif
  1201. }
  1202. #else /* CONFIG_CGROUP_SCHED */
  1203. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  1204. static inline struct task_group *task_group(struct task_struct *p)
  1205. {
  1206. return NULL;
  1207. }
  1208. #endif /* CONFIG_CGROUP_SCHED */
  1209. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  1210. {
  1211. set_task_rq(p, cpu);
  1212. #ifdef CONFIG_SMP
  1213. /*
  1214. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  1215. * successfuly executed on another CPU. We must ensure that updates of
  1216. * per-task data have been completed by this moment.
  1217. */
  1218. smp_wmb();
  1219. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1220. p->cpu = cpu;
  1221. #else
  1222. task_thread_info(p)->cpu = cpu;
  1223. #endif
  1224. p->wake_cpu = cpu;
  1225. #endif
  1226. }
  1227. /*
  1228. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  1229. */
  1230. #ifdef CONFIG_SCHED_DEBUG
  1231. # include <linux/static_key.h>
  1232. # define const_debug __read_mostly
  1233. #else
  1234. # define const_debug const
  1235. #endif
  1236. #define SCHED_FEAT(name, enabled) \
  1237. __SCHED_FEAT_##name ,
  1238. enum {
  1239. #include "features.h"
  1240. __SCHED_FEAT_NR,
  1241. };
  1242. #undef SCHED_FEAT
  1243. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  1244. /*
  1245. * To support run-time toggling of sched features, all the translation units
  1246. * (but core.c) reference the sysctl_sched_features defined in core.c.
  1247. */
  1248. extern const_debug unsigned int sysctl_sched_features;
  1249. #define SCHED_FEAT(name, enabled) \
  1250. static __always_inline bool static_branch_##name(struct static_key *key) \
  1251. { \
  1252. return static_key_##enabled(key); \
  1253. }
  1254. #include "features.h"
  1255. #undef SCHED_FEAT
  1256. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  1257. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  1258. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  1259. /*
  1260. * Each translation unit has its own copy of sysctl_sched_features to allow
  1261. * constants propagation at compile time and compiler optimization based on
  1262. * features default.
  1263. */
  1264. #define SCHED_FEAT(name, enabled) \
  1265. (1UL << __SCHED_FEAT_##name) * enabled |
  1266. static const_debug __maybe_unused unsigned int sysctl_sched_features =
  1267. #include "features.h"
  1268. 0;
  1269. #undef SCHED_FEAT
  1270. #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  1271. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  1272. extern struct static_key_false sched_numa_balancing;
  1273. extern struct static_key_false sched_schedstats;
  1274. static inline u64 global_rt_period(void)
  1275. {
  1276. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  1277. }
  1278. static inline u64 global_rt_runtime(void)
  1279. {
  1280. if (sysctl_sched_rt_runtime < 0)
  1281. return RUNTIME_INF;
  1282. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  1283. }
  1284. static inline int task_current(struct rq *rq, struct task_struct *p)
  1285. {
  1286. return rq->curr == p;
  1287. }
  1288. static inline int task_running(struct rq *rq, struct task_struct *p)
  1289. {
  1290. #ifdef CONFIG_SMP
  1291. return p->on_cpu;
  1292. #else
  1293. return task_current(rq, p);
  1294. #endif
  1295. }
  1296. static inline int task_on_rq_queued(struct task_struct *p)
  1297. {
  1298. return p->on_rq == TASK_ON_RQ_QUEUED;
  1299. }
  1300. static inline int task_on_rq_migrating(struct task_struct *p)
  1301. {
  1302. return p->on_rq == TASK_ON_RQ_MIGRATING;
  1303. }
  1304. /*
  1305. * wake flags
  1306. */
  1307. #define WF_SYNC 0x01 /* Waker goes to sleep after wakeup */
  1308. #define WF_FORK 0x02 /* Child wakeup after fork */
  1309. #define WF_MIGRATED 0x4 /* Internal use, task got migrated */
  1310. /*
  1311. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  1312. * of tasks with abnormal "nice" values across CPUs the contribution that
  1313. * each task makes to its run queue's load is weighted according to its
  1314. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  1315. * scaled version of the new time slice allocation that they receive on time
  1316. * slice expiry etc.
  1317. */
  1318. #define WEIGHT_IDLEPRIO 3
  1319. #define WMULT_IDLEPRIO 1431655765
  1320. extern const int sched_prio_to_weight[40];
  1321. extern const u32 sched_prio_to_wmult[40];
  1322. /*
  1323. * {de,en}queue flags:
  1324. *
  1325. * DEQUEUE_SLEEP - task is no longer runnable
  1326. * ENQUEUE_WAKEUP - task just became runnable
  1327. *
  1328. * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
  1329. * are in a known state which allows modification. Such pairs
  1330. * should preserve as much state as possible.
  1331. *
  1332. * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
  1333. * in the runqueue.
  1334. *
  1335. * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
  1336. * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
  1337. * ENQUEUE_MIGRATED - the task was migrated during wakeup
  1338. *
  1339. */
  1340. #define DEQUEUE_SLEEP 0x01
  1341. #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
  1342. #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
  1343. #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
  1344. #define ENQUEUE_WAKEUP 0x01
  1345. #define ENQUEUE_RESTORE 0x02
  1346. #define ENQUEUE_MOVE 0x04
  1347. #define ENQUEUE_NOCLOCK 0x08
  1348. #define ENQUEUE_HEAD 0x10
  1349. #define ENQUEUE_REPLENISH 0x20
  1350. #ifdef CONFIG_SMP
  1351. #define ENQUEUE_MIGRATED 0x40
  1352. #else
  1353. #define ENQUEUE_MIGRATED 0x00
  1354. #endif
  1355. #define RETRY_TASK ((void *)-1UL)
  1356. struct sched_class {
  1357. const struct sched_class *next;
  1358. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  1359. void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  1360. void (*yield_task) (struct rq *rq);
  1361. bool (*yield_to_task)(struct rq *rq, struct task_struct *p, bool preempt);
  1362. void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags);
  1363. /*
  1364. * It is the responsibility of the pick_next_task() method that will
  1365. * return the next task to call put_prev_task() on the @prev task or
  1366. * something equivalent.
  1367. *
  1368. * May return RETRY_TASK when it finds a higher prio class has runnable
  1369. * tasks.
  1370. */
  1371. struct task_struct * (*pick_next_task)(struct rq *rq,
  1372. struct task_struct *prev,
  1373. struct rq_flags *rf);
  1374. void (*put_prev_task)(struct rq *rq, struct task_struct *p);
  1375. #ifdef CONFIG_SMP
  1376. int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  1377. void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
  1378. void (*task_woken)(struct rq *this_rq, struct task_struct *task);
  1379. void (*set_cpus_allowed)(struct task_struct *p,
  1380. const struct cpumask *newmask);
  1381. void (*rq_online)(struct rq *rq);
  1382. void (*rq_offline)(struct rq *rq);
  1383. #endif
  1384. void (*set_curr_task)(struct rq *rq);
  1385. void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
  1386. void (*task_fork)(struct task_struct *p);
  1387. void (*task_dead)(struct task_struct *p);
  1388. /*
  1389. * The switched_from() call is allowed to drop rq->lock, therefore we
  1390. * cannot assume the switched_from/switched_to pair is serliazed by
  1391. * rq->lock. They are however serialized by p->pi_lock.
  1392. */
  1393. void (*switched_from)(struct rq *this_rq, struct task_struct *task);
  1394. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  1395. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  1396. int oldprio);
  1397. unsigned int (*get_rr_interval)(struct rq *rq,
  1398. struct task_struct *task);
  1399. void (*update_curr)(struct rq *rq);
  1400. #define TASK_SET_GROUP 0
  1401. #define TASK_MOVE_GROUP 1
  1402. #ifdef CONFIG_FAIR_GROUP_SCHED
  1403. void (*task_change_group)(struct task_struct *p, int type);
  1404. #endif
  1405. };
  1406. static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
  1407. {
  1408. prev->sched_class->put_prev_task(rq, prev);
  1409. }
  1410. static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
  1411. {
  1412. curr->sched_class->set_curr_task(rq);
  1413. }
  1414. #ifdef CONFIG_SMP
  1415. #define sched_class_highest (&stop_sched_class)
  1416. #else
  1417. #define sched_class_highest (&dl_sched_class)
  1418. #endif
  1419. #define for_each_class(class) \
  1420. for (class = sched_class_highest; class; class = class->next)
  1421. extern const struct sched_class stop_sched_class;
  1422. extern const struct sched_class dl_sched_class;
  1423. extern const struct sched_class rt_sched_class;
  1424. extern const struct sched_class fair_sched_class;
  1425. extern const struct sched_class idle_sched_class;
  1426. #ifdef CONFIG_SMP
  1427. extern void update_group_capacity(struct sched_domain *sd, int cpu);
  1428. extern void trigger_load_balance(struct rq *rq);
  1429. extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
  1430. #endif
  1431. #ifdef CONFIG_CPU_IDLE
  1432. static inline void idle_set_state(struct rq *rq,
  1433. struct cpuidle_state *idle_state)
  1434. {
  1435. rq->idle_state = idle_state;
  1436. }
  1437. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  1438. {
  1439. SCHED_WARN_ON(!rcu_read_lock_held());
  1440. return rq->idle_state;
  1441. }
  1442. #else
  1443. static inline void idle_set_state(struct rq *rq,
  1444. struct cpuidle_state *idle_state)
  1445. {
  1446. }
  1447. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  1448. {
  1449. return NULL;
  1450. }
  1451. #endif
  1452. extern void schedule_idle(void);
  1453. extern void sysrq_sched_debug_show(void);
  1454. extern void sched_init_granularity(void);
  1455. extern void update_max_interval(void);
  1456. extern void init_sched_dl_class(void);
  1457. extern void init_sched_rt_class(void);
  1458. extern void init_sched_fair_class(void);
  1459. extern void reweight_task(struct task_struct *p, int prio);
  1460. extern void resched_curr(struct rq *rq);
  1461. extern void resched_cpu(int cpu);
  1462. extern struct rt_bandwidth def_rt_bandwidth;
  1463. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  1464. extern struct dl_bandwidth def_dl_bandwidth;
  1465. extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
  1466. extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
  1467. extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
  1468. extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
  1469. #define BW_SHIFT 20
  1470. #define BW_UNIT (1 << BW_SHIFT)
  1471. #define RATIO_SHIFT 8
  1472. unsigned long to_ratio(u64 period, u64 runtime);
  1473. extern void init_entity_runnable_average(struct sched_entity *se);
  1474. extern void post_init_entity_util_avg(struct sched_entity *se);
  1475. #ifdef CONFIG_NO_HZ_FULL
  1476. extern bool sched_can_stop_tick(struct rq *rq);
  1477. extern int __init sched_tick_offload_init(void);
  1478. /*
  1479. * Tick may be needed by tasks in the runqueue depending on their policy and
  1480. * requirements. If tick is needed, lets send the target an IPI to kick it out of
  1481. * nohz mode if necessary.
  1482. */
  1483. static inline void sched_update_tick_dependency(struct rq *rq)
  1484. {
  1485. int cpu;
  1486. if (!tick_nohz_full_enabled())
  1487. return;
  1488. cpu = cpu_of(rq);
  1489. if (!tick_nohz_full_cpu(cpu))
  1490. return;
  1491. if (sched_can_stop_tick(rq))
  1492. tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
  1493. else
  1494. tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
  1495. }
  1496. #else
  1497. static inline int sched_tick_offload_init(void) { return 0; }
  1498. static inline void sched_update_tick_dependency(struct rq *rq) { }
  1499. #endif
  1500. static inline void add_nr_running(struct rq *rq, unsigned count)
  1501. {
  1502. unsigned prev_nr = rq->nr_running;
  1503. rq->nr_running = prev_nr + count;
  1504. if (prev_nr < 2 && rq->nr_running >= 2) {
  1505. #ifdef CONFIG_SMP
  1506. if (!READ_ONCE(rq->rd->overload))
  1507. WRITE_ONCE(rq->rd->overload, 1);
  1508. #endif
  1509. }
  1510. sched_update_tick_dependency(rq);
  1511. }
  1512. static inline void sub_nr_running(struct rq *rq, unsigned count)
  1513. {
  1514. rq->nr_running -= count;
  1515. /* Check if we still need preemption */
  1516. sched_update_tick_dependency(rq);
  1517. }
  1518. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  1519. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  1520. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  1521. extern const_debug unsigned int sysctl_sched_nr_migrate;
  1522. extern const_debug unsigned int sysctl_sched_migration_cost;
  1523. #ifdef CONFIG_SCHED_HRTICK
  1524. /*
  1525. * Use hrtick when:
  1526. * - enabled by features
  1527. * - hrtimer is actually high res
  1528. */
  1529. static inline int hrtick_enabled(struct rq *rq)
  1530. {
  1531. if (!sched_feat(HRTICK))
  1532. return 0;
  1533. if (!cpu_active(cpu_of(rq)))
  1534. return 0;
  1535. return hrtimer_is_hres_active(&rq->hrtick_timer);
  1536. }
  1537. void hrtick_start(struct rq *rq, u64 delay);
  1538. #else
  1539. static inline int hrtick_enabled(struct rq *rq)
  1540. {
  1541. return 0;
  1542. }
  1543. #endif /* CONFIG_SCHED_HRTICK */
  1544. #ifndef arch_scale_freq_capacity
  1545. static __always_inline
  1546. unsigned long arch_scale_freq_capacity(int cpu)
  1547. {
  1548. return SCHED_CAPACITY_SCALE;
  1549. }
  1550. #endif
  1551. #ifdef CONFIG_SMP
  1552. #ifndef arch_scale_cpu_capacity
  1553. static __always_inline
  1554. unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
  1555. {
  1556. if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
  1557. return sd->smt_gain / sd->span_weight;
  1558. return SCHED_CAPACITY_SCALE;
  1559. }
  1560. #endif
  1561. #else
  1562. #ifndef arch_scale_cpu_capacity
  1563. static __always_inline
  1564. unsigned long arch_scale_cpu_capacity(void __always_unused *sd, int cpu)
  1565. {
  1566. return SCHED_CAPACITY_SCALE;
  1567. }
  1568. #endif
  1569. #endif
  1570. #ifdef CONFIG_SMP
  1571. #ifdef CONFIG_PREEMPT
  1572. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  1573. /*
  1574. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1575. * way at the expense of forcing extra atomic operations in all
  1576. * invocations. This assures that the double_lock is acquired using the
  1577. * same underlying policy as the spinlock_t on this architecture, which
  1578. * reduces latency compared to the unfair variant below. However, it
  1579. * also adds more overhead and therefore may reduce throughput.
  1580. */
  1581. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1582. __releases(this_rq->lock)
  1583. __acquires(busiest->lock)
  1584. __acquires(this_rq->lock)
  1585. {
  1586. raw_spin_unlock(&this_rq->lock);
  1587. double_rq_lock(this_rq, busiest);
  1588. return 1;
  1589. }
  1590. #else
  1591. /*
  1592. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1593. * latency by eliminating extra atomic operations when the locks are
  1594. * already in proper order on entry. This favors lower CPU-ids and will
  1595. * grant the double lock to lower CPUs over higher ids under contention,
  1596. * regardless of entry order into the function.
  1597. */
  1598. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1599. __releases(this_rq->lock)
  1600. __acquires(busiest->lock)
  1601. __acquires(this_rq->lock)
  1602. {
  1603. int ret = 0;
  1604. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  1605. if (busiest < this_rq) {
  1606. raw_spin_unlock(&this_rq->lock);
  1607. raw_spin_lock(&busiest->lock);
  1608. raw_spin_lock_nested(&this_rq->lock,
  1609. SINGLE_DEPTH_NESTING);
  1610. ret = 1;
  1611. } else
  1612. raw_spin_lock_nested(&busiest->lock,
  1613. SINGLE_DEPTH_NESTING);
  1614. }
  1615. return ret;
  1616. }
  1617. #endif /* CONFIG_PREEMPT */
  1618. /*
  1619. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1620. */
  1621. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1622. {
  1623. if (unlikely(!irqs_disabled())) {
  1624. /* printk() doesn't work well under rq->lock */
  1625. raw_spin_unlock(&this_rq->lock);
  1626. BUG_ON(1);
  1627. }
  1628. return _double_lock_balance(this_rq, busiest);
  1629. }
  1630. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1631. __releases(busiest->lock)
  1632. {
  1633. raw_spin_unlock(&busiest->lock);
  1634. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1635. }
  1636. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  1637. {
  1638. if (l1 > l2)
  1639. swap(l1, l2);
  1640. spin_lock(l1);
  1641. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1642. }
  1643. static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
  1644. {
  1645. if (l1 > l2)
  1646. swap(l1, l2);
  1647. spin_lock_irq(l1);
  1648. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1649. }
  1650. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  1651. {
  1652. if (l1 > l2)
  1653. swap(l1, l2);
  1654. raw_spin_lock(l1);
  1655. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1656. }
  1657. /*
  1658. * double_rq_lock - safely lock two runqueues
  1659. *
  1660. * Note this does not disable interrupts like task_rq_lock,
  1661. * you need to do so manually before calling.
  1662. */
  1663. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1664. __acquires(rq1->lock)
  1665. __acquires(rq2->lock)
  1666. {
  1667. BUG_ON(!irqs_disabled());
  1668. if (rq1 == rq2) {
  1669. raw_spin_lock(&rq1->lock);
  1670. __acquire(rq2->lock); /* Fake it out ;) */
  1671. } else {
  1672. if (rq1 < rq2) {
  1673. raw_spin_lock(&rq1->lock);
  1674. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  1675. } else {
  1676. raw_spin_lock(&rq2->lock);
  1677. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  1678. }
  1679. }
  1680. }
  1681. /*
  1682. * double_rq_unlock - safely unlock two runqueues
  1683. *
  1684. * Note this does not restore interrupts like task_rq_unlock,
  1685. * you need to do so manually after calling.
  1686. */
  1687. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1688. __releases(rq1->lock)
  1689. __releases(rq2->lock)
  1690. {
  1691. raw_spin_unlock(&rq1->lock);
  1692. if (rq1 != rq2)
  1693. raw_spin_unlock(&rq2->lock);
  1694. else
  1695. __release(rq2->lock);
  1696. }
  1697. extern void set_rq_online (struct rq *rq);
  1698. extern void set_rq_offline(struct rq *rq);
  1699. extern bool sched_smp_initialized;
  1700. #else /* CONFIG_SMP */
  1701. /*
  1702. * double_rq_lock - safely lock two runqueues
  1703. *
  1704. * Note this does not disable interrupts like task_rq_lock,
  1705. * you need to do so manually before calling.
  1706. */
  1707. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1708. __acquires(rq1->lock)
  1709. __acquires(rq2->lock)
  1710. {
  1711. BUG_ON(!irqs_disabled());
  1712. BUG_ON(rq1 != rq2);
  1713. raw_spin_lock(&rq1->lock);
  1714. __acquire(rq2->lock); /* Fake it out ;) */
  1715. }
  1716. /*
  1717. * double_rq_unlock - safely unlock two runqueues
  1718. *
  1719. * Note this does not restore interrupts like task_rq_unlock,
  1720. * you need to do so manually after calling.
  1721. */
  1722. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1723. __releases(rq1->lock)
  1724. __releases(rq2->lock)
  1725. {
  1726. BUG_ON(rq1 != rq2);
  1727. raw_spin_unlock(&rq1->lock);
  1728. __release(rq2->lock);
  1729. }
  1730. #endif
  1731. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  1732. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  1733. #ifdef CONFIG_SCHED_DEBUG
  1734. extern bool sched_debug_enabled;
  1735. extern void print_cfs_stats(struct seq_file *m, int cpu);
  1736. extern void print_rt_stats(struct seq_file *m, int cpu);
  1737. extern void print_dl_stats(struct seq_file *m, int cpu);
  1738. extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
  1739. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1740. extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
  1741. #ifdef CONFIG_NUMA_BALANCING
  1742. extern void
  1743. show_numa_stats(struct task_struct *p, struct seq_file *m);
  1744. extern void
  1745. print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
  1746. unsigned long tpf, unsigned long gsf, unsigned long gpf);
  1747. #endif /* CONFIG_NUMA_BALANCING */
  1748. #endif /* CONFIG_SCHED_DEBUG */
  1749. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  1750. extern void init_rt_rq(struct rt_rq *rt_rq);
  1751. extern void init_dl_rq(struct dl_rq *dl_rq);
  1752. extern void cfs_bandwidth_usage_inc(void);
  1753. extern void cfs_bandwidth_usage_dec(void);
  1754. #ifdef CONFIG_NO_HZ_COMMON
  1755. #define NOHZ_BALANCE_KICK_BIT 0
  1756. #define NOHZ_STATS_KICK_BIT 1
  1757. #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
  1758. #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
  1759. #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
  1760. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  1761. extern void nohz_balance_exit_idle(struct rq *rq);
  1762. #else
  1763. static inline void nohz_balance_exit_idle(struct rq *rq) { }
  1764. #endif
  1765. #ifdef CONFIG_SMP
  1766. static inline
  1767. void __dl_update(struct dl_bw *dl_b, s64 bw)
  1768. {
  1769. struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
  1770. int i;
  1771. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  1772. "sched RCU must be held");
  1773. for_each_cpu_and(i, rd->span, cpu_active_mask) {
  1774. struct rq *rq = cpu_rq(i);
  1775. rq->dl.extra_bw += bw;
  1776. }
  1777. }
  1778. #else
  1779. static inline
  1780. void __dl_update(struct dl_bw *dl_b, s64 bw)
  1781. {
  1782. struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
  1783. dl->extra_bw += bw;
  1784. }
  1785. #endif
  1786. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1787. struct irqtime {
  1788. u64 total;
  1789. u64 tick_delta;
  1790. u64 irq_start_time;
  1791. struct u64_stats_sync sync;
  1792. };
  1793. DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
  1794. /*
  1795. * Returns the irqtime minus the softirq time computed by ksoftirqd.
  1796. * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
  1797. * and never move forward.
  1798. */
  1799. static inline u64 irq_time_read(int cpu)
  1800. {
  1801. struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
  1802. unsigned int seq;
  1803. u64 total;
  1804. do {
  1805. seq = __u64_stats_fetch_begin(&irqtime->sync);
  1806. total = irqtime->total;
  1807. } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
  1808. return total;
  1809. }
  1810. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  1811. #ifdef CONFIG_CPU_FREQ
  1812. DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
  1813. /**
  1814. * cpufreq_update_util - Take a note about CPU utilization changes.
  1815. * @rq: Runqueue to carry out the update for.
  1816. * @flags: Update reason flags.
  1817. *
  1818. * This function is called by the scheduler on the CPU whose utilization is
  1819. * being updated.
  1820. *
  1821. * It can only be called from RCU-sched read-side critical sections.
  1822. *
  1823. * The way cpufreq is currently arranged requires it to evaluate the CPU
  1824. * performance state (frequency/voltage) on a regular basis to prevent it from
  1825. * being stuck in a completely inadequate performance level for too long.
  1826. * That is not guaranteed to happen if the updates are only triggered from CFS
  1827. * and DL, though, because they may not be coming in if only RT tasks are
  1828. * active all the time (or there are RT tasks only).
  1829. *
  1830. * As a workaround for that issue, this function is called periodically by the
  1831. * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
  1832. * but that really is a band-aid. Going forward it should be replaced with
  1833. * solutions targeted more specifically at RT tasks.
  1834. */
  1835. static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
  1836. {
  1837. struct update_util_data *data;
  1838. data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
  1839. cpu_of(rq)));
  1840. if (data)
  1841. data->func(data, rq_clock(rq), flags);
  1842. }
  1843. #else
  1844. static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
  1845. #endif /* CONFIG_CPU_FREQ */
  1846. #ifdef arch_scale_freq_capacity
  1847. # ifndef arch_scale_freq_invariant
  1848. # define arch_scale_freq_invariant() true
  1849. # endif
  1850. #else
  1851. # define arch_scale_freq_invariant() false
  1852. #endif
  1853. #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
  1854. static inline unsigned long cpu_bw_dl(struct rq *rq)
  1855. {
  1856. return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
  1857. }
  1858. static inline unsigned long cpu_util_dl(struct rq *rq)
  1859. {
  1860. return READ_ONCE(rq->avg_dl.util_avg);
  1861. }
  1862. static inline unsigned long cpu_util_cfs(struct rq *rq)
  1863. {
  1864. unsigned long util = READ_ONCE(rq->cfs.avg.util_avg);
  1865. if (sched_feat(UTIL_EST)) {
  1866. util = max_t(unsigned long, util,
  1867. READ_ONCE(rq->cfs.avg.util_est.enqueued));
  1868. }
  1869. return util;
  1870. }
  1871. static inline unsigned long cpu_util_rt(struct rq *rq)
  1872. {
  1873. return READ_ONCE(rq->avg_rt.util_avg);
  1874. }
  1875. #endif
  1876. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  1877. static inline unsigned long cpu_util_irq(struct rq *rq)
  1878. {
  1879. return rq->avg_irq.util_avg;
  1880. }
  1881. static inline
  1882. unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
  1883. {
  1884. util *= (max - irq);
  1885. util /= max;
  1886. return util;
  1887. }
  1888. #else
  1889. static inline unsigned long cpu_util_irq(struct rq *rq)
  1890. {
  1891. return 0;
  1892. }
  1893. static inline
  1894. unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
  1895. {
  1896. return util;
  1897. }
  1898. #endif