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