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