xref: /openbmc/linux/kernel/sched/sched.h (revision 0413d7f3)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
297fb7a0aSIngo Molnar /*
397fb7a0aSIngo Molnar  * Scheduler internal types and methods:
497fb7a0aSIngo Molnar  */
5391e43daSPeter Zijlstra #include <linux/sched.h>
6325ea10cSIngo Molnar 
7dfc3401aSIngo Molnar #include <linux/sched/autogroup.h>
8e6017571SIngo Molnar #include <linux/sched/clock.h>
9325ea10cSIngo Molnar #include <linux/sched/coredump.h>
1055687da1SIngo Molnar #include <linux/sched/cpufreq.h>
11325ea10cSIngo Molnar #include <linux/sched/cputime.h>
12325ea10cSIngo Molnar #include <linux/sched/deadline.h>
13b17b0153SIngo Molnar #include <linux/sched/debug.h>
14ef8bd77fSIngo Molnar #include <linux/sched/hotplug.h>
15325ea10cSIngo Molnar #include <linux/sched/idle.h>
16325ea10cSIngo Molnar #include <linux/sched/init.h>
17325ea10cSIngo Molnar #include <linux/sched/isolation.h>
18325ea10cSIngo Molnar #include <linux/sched/jobctl.h>
19325ea10cSIngo Molnar #include <linux/sched/loadavg.h>
20325ea10cSIngo Molnar #include <linux/sched/mm.h>
21325ea10cSIngo Molnar #include <linux/sched/nohz.h>
22325ea10cSIngo Molnar #include <linux/sched/numa_balancing.h>
23325ea10cSIngo Molnar #include <linux/sched/prio.h>
24325ea10cSIngo Molnar #include <linux/sched/rt.h>
25325ea10cSIngo Molnar #include <linux/sched/signal.h>
26321a874aSThomas Gleixner #include <linux/sched/smt.h>
27325ea10cSIngo Molnar #include <linux/sched/stat.h>
28325ea10cSIngo Molnar #include <linux/sched/sysctl.h>
2929930025SIngo Molnar #include <linux/sched/task.h>
3068db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
31325ea10cSIngo Molnar #include <linux/sched/topology.h>
32325ea10cSIngo Molnar #include <linux/sched/user.h>
33325ea10cSIngo Molnar #include <linux/sched/wake_q.h>
34325ea10cSIngo Molnar #include <linux/sched/xacct.h>
35ef8bd77fSIngo Molnar 
36325ea10cSIngo Molnar #include <uapi/linux/sched/types.h>
37325ea10cSIngo Molnar 
383866e845SSteven Rostedt (Red Hat) #include <linux/binfmts.h>
39325ea10cSIngo Molnar #include <linux/blkdev.h>
40325ea10cSIngo Molnar #include <linux/compat.h>
41325ea10cSIngo Molnar #include <linux/context_tracking.h>
42325ea10cSIngo Molnar #include <linux/cpufreq.h>
43325ea10cSIngo Molnar #include <linux/cpuidle.h>
44325ea10cSIngo Molnar #include <linux/cpuset.h>
45325ea10cSIngo Molnar #include <linux/ctype.h>
46325ea10cSIngo Molnar #include <linux/debugfs.h>
47325ea10cSIngo Molnar #include <linux/delayacct.h>
486aa140faSQuentin Perret #include <linux/energy_model.h>
49325ea10cSIngo Molnar #include <linux/init_task.h>
50325ea10cSIngo Molnar #include <linux/kprobes.h>
51325ea10cSIngo Molnar #include <linux/kthread.h>
52325ea10cSIngo Molnar #include <linux/membarrier.h>
53325ea10cSIngo Molnar #include <linux/migrate.h>
54325ea10cSIngo Molnar #include <linux/mmu_context.h>
55325ea10cSIngo Molnar #include <linux/nmi.h>
56325ea10cSIngo Molnar #include <linux/proc_fs.h>
57325ea10cSIngo Molnar #include <linux/prefetch.h>
58325ea10cSIngo Molnar #include <linux/profile.h>
59eb414681SJohannes Weiner #include <linux/psi.h>
60325ea10cSIngo Molnar #include <linux/rcupdate_wait.h>
61325ea10cSIngo Molnar #include <linux/security.h>
62391e43daSPeter Zijlstra #include <linux/stop_machine.h>
63325ea10cSIngo Molnar #include <linux/suspend.h>
64325ea10cSIngo Molnar #include <linux/swait.h>
65325ea10cSIngo Molnar #include <linux/syscalls.h>
66325ea10cSIngo Molnar #include <linux/task_work.h>
67325ea10cSIngo Molnar #include <linux/tsacct_kern.h>
68325ea10cSIngo Molnar 
69325ea10cSIngo Molnar #include <asm/tlb.h>
70391e43daSPeter Zijlstra 
717fce777cSIngo Molnar #ifdef CONFIG_PARAVIRT
727fce777cSIngo Molnar # include <asm/paravirt.h>
737fce777cSIngo Molnar #endif
747fce777cSIngo Molnar 
75391e43daSPeter Zijlstra #include "cpupri.h"
766bfd6d72SJuri Lelli #include "cpudeadline.h"
77391e43daSPeter Zijlstra 
789148a3a1SPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
799148a3a1SPeter Zijlstra # define SCHED_WARN_ON(x)	WARN_ONCE(x, #x)
809148a3a1SPeter Zijlstra #else
816d3aed3dSIngo Molnar # define SCHED_WARN_ON(x)	({ (void)(x), 0; })
829148a3a1SPeter Zijlstra #endif
839148a3a1SPeter Zijlstra 
8445ceebf7SPaul Gortmaker struct rq;
85442bf3aaSDaniel Lezcano struct cpuidle_state;
8645ceebf7SPaul Gortmaker 
87da0c1e65SKirill Tkhai /* task_struct::on_rq states: */
88da0c1e65SKirill Tkhai #define TASK_ON_RQ_QUEUED	1
89cca26e80SKirill Tkhai #define TASK_ON_RQ_MIGRATING	2
90da0c1e65SKirill Tkhai 
91391e43daSPeter Zijlstra extern __read_mostly int scheduler_running;
92391e43daSPeter Zijlstra 
9345ceebf7SPaul Gortmaker extern unsigned long calc_load_update;
9445ceebf7SPaul Gortmaker extern atomic_long_t calc_load_tasks;
9545ceebf7SPaul Gortmaker 
963289bdb4SPeter Zijlstra extern void calc_global_load_tick(struct rq *this_rq);
97d60585c5SThomas Gleixner extern long calc_load_fold_active(struct rq *this_rq, long adjust);
983289bdb4SPeter Zijlstra 
99391e43daSPeter Zijlstra /*
100391e43daSPeter Zijlstra  * Helpers for converting nanosecond timing to jiffy resolution
101391e43daSPeter Zijlstra  */
102391e43daSPeter Zijlstra #define NS_TO_JIFFIES(TIME)	((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
103391e43daSPeter Zijlstra 
104cc1f4b1fSLi Zefan /*
105cc1f4b1fSLi Zefan  * Increase resolution of nice-level calculations for 64-bit architectures.
106cc1f4b1fSLi Zefan  * The extra resolution improves shares distribution and load balancing of
107cc1f4b1fSLi Zefan  * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
108cc1f4b1fSLi Zefan  * hierarchies, especially on larger systems. This is not a user-visible change
109cc1f4b1fSLi Zefan  * and does not change the user-interface for setting shares/weights.
110cc1f4b1fSLi Zefan  *
111cc1f4b1fSLi Zefan  * We increase resolution only if we have enough bits to allow this increased
11297fb7a0aSIngo Molnar  * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
11397fb7a0aSIngo Molnar  * are pretty high and the returns do not justify the increased costs.
1142159197dSPeter Zijlstra  *
11597fb7a0aSIngo Molnar  * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
11697fb7a0aSIngo Molnar  * increase coverage and consistency always enable it on 64-bit platforms.
117cc1f4b1fSLi Zefan  */
1182159197dSPeter Zijlstra #ifdef CONFIG_64BIT
119172895e6SYuyang Du # define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
1206ecdd749SYuyang Du # define scale_load(w)		((w) << SCHED_FIXEDPOINT_SHIFT)
1216ecdd749SYuyang Du # define scale_load_down(w)	((w) >> SCHED_FIXEDPOINT_SHIFT)
122cc1f4b1fSLi Zefan #else
123172895e6SYuyang Du # define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT)
124cc1f4b1fSLi Zefan # define scale_load(w)		(w)
125cc1f4b1fSLi Zefan # define scale_load_down(w)	(w)
126cc1f4b1fSLi Zefan #endif
127cc1f4b1fSLi Zefan 
1286ecdd749SYuyang Du /*
129172895e6SYuyang Du  * Task weight (visible to users) and its load (invisible to users) have
130172895e6SYuyang Du  * independent resolution, but they should be well calibrated. We use
131172895e6SYuyang Du  * scale_load() and scale_load_down(w) to convert between them. The
132172895e6SYuyang Du  * following must be true:
133172895e6SYuyang Du  *
134172895e6SYuyang Du  *  scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
135172895e6SYuyang Du  *
1366ecdd749SYuyang Du  */
137172895e6SYuyang Du #define NICE_0_LOAD		(1L << NICE_0_LOAD_SHIFT)
138391e43daSPeter Zijlstra 
139391e43daSPeter Zijlstra /*
140332ac17eSDario Faggioli  * Single value that decides SCHED_DEADLINE internal math precision.
141332ac17eSDario Faggioli  * 10 -> just above 1us
142332ac17eSDario Faggioli  * 9  -> just above 0.5us
143332ac17eSDario Faggioli  */
14497fb7a0aSIngo Molnar #define DL_SCALE		10
145332ac17eSDario Faggioli 
146332ac17eSDario Faggioli /*
14797fb7a0aSIngo Molnar  * Single value that denotes runtime == period, ie unlimited time.
148391e43daSPeter Zijlstra  */
149391e43daSPeter Zijlstra #define RUNTIME_INF		((u64)~0ULL)
150391e43daSPeter Zijlstra 
15120f9cd2aSHenrik Austad static inline int idle_policy(int policy)
15220f9cd2aSHenrik Austad {
15320f9cd2aSHenrik Austad 	return policy == SCHED_IDLE;
15420f9cd2aSHenrik Austad }
155d50dde5aSDario Faggioli static inline int fair_policy(int policy)
156d50dde5aSDario Faggioli {
157d50dde5aSDario Faggioli 	return policy == SCHED_NORMAL || policy == SCHED_BATCH;
158d50dde5aSDario Faggioli }
159d50dde5aSDario Faggioli 
160391e43daSPeter Zijlstra static inline int rt_policy(int policy)
161391e43daSPeter Zijlstra {
162d50dde5aSDario Faggioli 	return policy == SCHED_FIFO || policy == SCHED_RR;
163391e43daSPeter Zijlstra }
164391e43daSPeter Zijlstra 
165aab03e05SDario Faggioli static inline int dl_policy(int policy)
166aab03e05SDario Faggioli {
167aab03e05SDario Faggioli 	return policy == SCHED_DEADLINE;
168aab03e05SDario Faggioli }
16920f9cd2aSHenrik Austad static inline bool valid_policy(int policy)
17020f9cd2aSHenrik Austad {
17120f9cd2aSHenrik Austad 	return idle_policy(policy) || fair_policy(policy) ||
17220f9cd2aSHenrik Austad 		rt_policy(policy) || dl_policy(policy);
17320f9cd2aSHenrik Austad }
174aab03e05SDario Faggioli 
1751da1843fSViresh Kumar static inline int task_has_idle_policy(struct task_struct *p)
1761da1843fSViresh Kumar {
1771da1843fSViresh Kumar 	return idle_policy(p->policy);
1781da1843fSViresh Kumar }
1791da1843fSViresh Kumar 
180391e43daSPeter Zijlstra static inline int task_has_rt_policy(struct task_struct *p)
181391e43daSPeter Zijlstra {
182391e43daSPeter Zijlstra 	return rt_policy(p->policy);
183391e43daSPeter Zijlstra }
184391e43daSPeter Zijlstra 
185aab03e05SDario Faggioli static inline int task_has_dl_policy(struct task_struct *p)
186aab03e05SDario Faggioli {
187aab03e05SDario Faggioli 	return dl_policy(p->policy);
188aab03e05SDario Faggioli }
189aab03e05SDario Faggioli 
19007881166SJuri Lelli #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
19107881166SJuri Lelli 
1922d3d891dSDario Faggioli /*
193794a56ebSJuri Lelli  * !! For sched_setattr_nocheck() (kernel) only !!
194794a56ebSJuri Lelli  *
195794a56ebSJuri Lelli  * This is actually gross. :(
196794a56ebSJuri Lelli  *
197794a56ebSJuri Lelli  * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
198794a56ebSJuri Lelli  * tasks, but still be able to sleep. We need this on platforms that cannot
199794a56ebSJuri Lelli  * atomically change clock frequency. Remove once fast switching will be
200794a56ebSJuri Lelli  * available on such platforms.
201794a56ebSJuri Lelli  *
202794a56ebSJuri Lelli  * SUGOV stands for SchedUtil GOVernor.
203794a56ebSJuri Lelli  */
204794a56ebSJuri Lelli #define SCHED_FLAG_SUGOV	0x10000000
205794a56ebSJuri Lelli 
206794a56ebSJuri Lelli static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se)
207794a56ebSJuri Lelli {
208794a56ebSJuri Lelli #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
209794a56ebSJuri Lelli 	return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
210794a56ebSJuri Lelli #else
211794a56ebSJuri Lelli 	return false;
212794a56ebSJuri Lelli #endif
213794a56ebSJuri Lelli }
214794a56ebSJuri Lelli 
215794a56ebSJuri Lelli /*
2162d3d891dSDario Faggioli  * Tells if entity @a should preempt entity @b.
2172d3d891dSDario Faggioli  */
218332ac17eSDario Faggioli static inline bool
219332ac17eSDario Faggioli dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
2202d3d891dSDario Faggioli {
221794a56ebSJuri Lelli 	return dl_entity_is_special(a) ||
222794a56ebSJuri Lelli 	       dl_time_before(a->deadline, b->deadline);
2232d3d891dSDario Faggioli }
2242d3d891dSDario Faggioli 
225391e43daSPeter Zijlstra /*
226391e43daSPeter Zijlstra  * This is the priority-queue data structure of the RT scheduling class:
227391e43daSPeter Zijlstra  */
228391e43daSPeter Zijlstra struct rt_prio_array {
229391e43daSPeter Zijlstra 	DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
230391e43daSPeter Zijlstra 	struct list_head queue[MAX_RT_PRIO];
231391e43daSPeter Zijlstra };
232391e43daSPeter Zijlstra 
233391e43daSPeter Zijlstra struct rt_bandwidth {
234391e43daSPeter Zijlstra 	/* nests inside the rq lock: */
235391e43daSPeter Zijlstra 	raw_spinlock_t		rt_runtime_lock;
236391e43daSPeter Zijlstra 	ktime_t			rt_period;
237391e43daSPeter Zijlstra 	u64			rt_runtime;
238391e43daSPeter Zijlstra 	struct hrtimer		rt_period_timer;
2394cfafd30SPeter Zijlstra 	unsigned int		rt_period_active;
240391e43daSPeter Zijlstra };
241a5e7be3bSJuri Lelli 
242a5e7be3bSJuri Lelli void __dl_clear_params(struct task_struct *p);
243a5e7be3bSJuri Lelli 
244332ac17eSDario Faggioli /*
245332ac17eSDario Faggioli  * To keep the bandwidth of -deadline tasks and groups under control
246332ac17eSDario Faggioli  * we need some place where:
247332ac17eSDario Faggioli  *  - store the maximum -deadline bandwidth of the system (the group);
248332ac17eSDario Faggioli  *  - cache the fraction of that bandwidth that is currently allocated.
249332ac17eSDario Faggioli  *
250332ac17eSDario Faggioli  * This is all done in the data structure below. It is similar to the
251332ac17eSDario Faggioli  * one used for RT-throttling (rt_bandwidth), with the main difference
252332ac17eSDario Faggioli  * that, since here we are only interested in admission control, we
253332ac17eSDario Faggioli  * do not decrease any runtime while the group "executes", neither we
254332ac17eSDario Faggioli  * need a timer to replenish it.
255332ac17eSDario Faggioli  *
256332ac17eSDario Faggioli  * With respect to SMP, the bandwidth is given on a per-CPU basis,
257332ac17eSDario Faggioli  * meaning that:
258332ac17eSDario Faggioli  *  - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
259332ac17eSDario Faggioli  *  - dl_total_bw array contains, in the i-eth element, the currently
260332ac17eSDario Faggioli  *    allocated bandwidth on the i-eth CPU.
261332ac17eSDario Faggioli  * Moreover, groups consume bandwidth on each CPU, while tasks only
262332ac17eSDario Faggioli  * consume bandwidth on the CPU they're running on.
263332ac17eSDario Faggioli  * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
264332ac17eSDario Faggioli  * that will be shown the next time the proc or cgroup controls will
265332ac17eSDario Faggioli  * be red. It on its turn can be changed by writing on its own
266332ac17eSDario Faggioli  * control.
267332ac17eSDario Faggioli  */
268332ac17eSDario Faggioli struct dl_bandwidth {
269332ac17eSDario Faggioli 	raw_spinlock_t		dl_runtime_lock;
270332ac17eSDario Faggioli 	u64			dl_runtime;
271332ac17eSDario Faggioli 	u64			dl_period;
272332ac17eSDario Faggioli };
273332ac17eSDario Faggioli 
274332ac17eSDario Faggioli static inline int dl_bandwidth_enabled(void)
275332ac17eSDario Faggioli {
2761724813dSPeter Zijlstra 	return sysctl_sched_rt_runtime >= 0;
277332ac17eSDario Faggioli }
278332ac17eSDario Faggioli 
279332ac17eSDario Faggioli struct dl_bw {
280332ac17eSDario Faggioli 	raw_spinlock_t		lock;
28197fb7a0aSIngo Molnar 	u64			bw;
28297fb7a0aSIngo Molnar 	u64			total_bw;
283332ac17eSDario Faggioli };
284332ac17eSDario Faggioli 
285daec5798SLuca Abeni static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
286daec5798SLuca Abeni 
2877f51412aSJuri Lelli static inline
2888c0944ceSPeter Zijlstra void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
2897f51412aSJuri Lelli {
2907f51412aSJuri Lelli 	dl_b->total_bw -= tsk_bw;
291daec5798SLuca Abeni 	__dl_update(dl_b, (s32)tsk_bw / cpus);
2927f51412aSJuri Lelli }
2937f51412aSJuri Lelli 
2947f51412aSJuri Lelli static inline
295daec5798SLuca Abeni void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
2967f51412aSJuri Lelli {
2977f51412aSJuri Lelli 	dl_b->total_bw += tsk_bw;
298daec5798SLuca Abeni 	__dl_update(dl_b, -((s32)tsk_bw / cpus));
2997f51412aSJuri Lelli }
3007f51412aSJuri Lelli 
3017f51412aSJuri Lelli static inline
3027f51412aSJuri Lelli bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
3037f51412aSJuri Lelli {
3047f51412aSJuri Lelli 	return dl_b->bw != -1 &&
3057f51412aSJuri Lelli 	       dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
3067f51412aSJuri Lelli }
3077f51412aSJuri Lelli 
30897fb7a0aSIngo Molnar extern void dl_change_utilization(struct task_struct *p, u64 new_bw);
309f2cb1360SIngo Molnar extern void init_dl_bw(struct dl_bw *dl_b);
31006a76fe0SNicolas Pitre extern int  sched_dl_global_validate(void);
31106a76fe0SNicolas Pitre extern void sched_dl_do_global(void);
31297fb7a0aSIngo Molnar extern int  sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
31306a76fe0SNicolas Pitre extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
31406a76fe0SNicolas Pitre extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
31506a76fe0SNicolas Pitre extern bool __checkparam_dl(const struct sched_attr *attr);
31606a76fe0SNicolas Pitre extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
31797fb7a0aSIngo Molnar extern int  dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
31897fb7a0aSIngo Molnar extern int  dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
31906a76fe0SNicolas Pitre extern bool dl_cpu_busy(unsigned int cpu);
320391e43daSPeter Zijlstra 
321391e43daSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED
322391e43daSPeter Zijlstra 
323391e43daSPeter Zijlstra #include <linux/cgroup.h>
324eb414681SJohannes Weiner #include <linux/psi.h>
325391e43daSPeter Zijlstra 
326391e43daSPeter Zijlstra struct cfs_rq;
327391e43daSPeter Zijlstra struct rt_rq;
328391e43daSPeter Zijlstra 
32935cf4e50SMike Galbraith extern struct list_head task_groups;
330391e43daSPeter Zijlstra 
331391e43daSPeter Zijlstra struct cfs_bandwidth {
332391e43daSPeter Zijlstra #ifdef CONFIG_CFS_BANDWIDTH
333391e43daSPeter Zijlstra 	raw_spinlock_t		lock;
334391e43daSPeter Zijlstra 	ktime_t			period;
33597fb7a0aSIngo Molnar 	u64			quota;
33697fb7a0aSIngo Molnar 	u64			runtime;
3379c58c79aSZhihui Zhang 	s64			hierarchical_quota;
338391e43daSPeter Zijlstra 
33966567fcbSbsegall@google.com 	u8			idle;
34066567fcbSbsegall@google.com 	u8			period_active;
34166567fcbSbsegall@google.com 	u8			distribute_running;
34266567fcbSbsegall@google.com 	u8			slack_started;
34397fb7a0aSIngo Molnar 	struct hrtimer		period_timer;
34497fb7a0aSIngo Molnar 	struct hrtimer		slack_timer;
345391e43daSPeter Zijlstra 	struct list_head	throttled_cfs_rq;
346391e43daSPeter Zijlstra 
34797fb7a0aSIngo Molnar 	/* Statistics: */
34897fb7a0aSIngo Molnar 	int			nr_periods;
34997fb7a0aSIngo Molnar 	int			nr_throttled;
350391e43daSPeter Zijlstra 	u64			throttled_time;
351391e43daSPeter Zijlstra #endif
352391e43daSPeter Zijlstra };
353391e43daSPeter Zijlstra 
35497fb7a0aSIngo Molnar /* Task group related information */
355391e43daSPeter Zijlstra struct task_group {
356391e43daSPeter Zijlstra 	struct cgroup_subsys_state css;
357391e43daSPeter Zijlstra 
358391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED
35997fb7a0aSIngo Molnar 	/* schedulable entities of this group on each CPU */
360391e43daSPeter Zijlstra 	struct sched_entity	**se;
36197fb7a0aSIngo Molnar 	/* runqueue "owned" by this group on each CPU */
362391e43daSPeter Zijlstra 	struct cfs_rq		**cfs_rq;
363391e43daSPeter Zijlstra 	unsigned long		shares;
364391e43daSPeter Zijlstra 
365fa6bddebSAlex Shi #ifdef	CONFIG_SMP
366b0367629SWaiman Long 	/*
367b0367629SWaiman Long 	 * load_avg can be heavily contended at clock tick time, so put
368b0367629SWaiman Long 	 * it in its own cacheline separated from the fields above which
369b0367629SWaiman Long 	 * will also be accessed at each tick.
370b0367629SWaiman Long 	 */
371b0367629SWaiman Long 	atomic_long_t		load_avg ____cacheline_aligned;
372391e43daSPeter Zijlstra #endif
373fa6bddebSAlex Shi #endif
374391e43daSPeter Zijlstra 
375391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
376391e43daSPeter Zijlstra 	struct sched_rt_entity	**rt_se;
377391e43daSPeter Zijlstra 	struct rt_rq		**rt_rq;
378391e43daSPeter Zijlstra 
379391e43daSPeter Zijlstra 	struct rt_bandwidth	rt_bandwidth;
380391e43daSPeter Zijlstra #endif
381391e43daSPeter Zijlstra 
382391e43daSPeter Zijlstra 	struct rcu_head		rcu;
383391e43daSPeter Zijlstra 	struct list_head	list;
384391e43daSPeter Zijlstra 
385391e43daSPeter Zijlstra 	struct task_group	*parent;
386391e43daSPeter Zijlstra 	struct list_head	siblings;
387391e43daSPeter Zijlstra 	struct list_head	children;
388391e43daSPeter Zijlstra 
389391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_AUTOGROUP
390391e43daSPeter Zijlstra 	struct autogroup	*autogroup;
391391e43daSPeter Zijlstra #endif
392391e43daSPeter Zijlstra 
393391e43daSPeter Zijlstra 	struct cfs_bandwidth	cfs_bandwidth;
3942480c093SPatrick Bellasi 
3952480c093SPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK_GROUP
3962480c093SPatrick Bellasi 	/* The two decimal precision [%] value requested from user-space */
3972480c093SPatrick Bellasi 	unsigned int		uclamp_pct[UCLAMP_CNT];
3982480c093SPatrick Bellasi 	/* Clamp values requested for a task group */
3992480c093SPatrick Bellasi 	struct uclamp_se	uclamp_req[UCLAMP_CNT];
4000b60ba2dSPatrick Bellasi 	/* Effective clamp values used for a task group */
4010b60ba2dSPatrick Bellasi 	struct uclamp_se	uclamp[UCLAMP_CNT];
4022480c093SPatrick Bellasi #endif
4032480c093SPatrick Bellasi 
404391e43daSPeter Zijlstra };
405391e43daSPeter Zijlstra 
406391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED
407391e43daSPeter Zijlstra #define ROOT_TASK_GROUP_LOAD	NICE_0_LOAD
408391e43daSPeter Zijlstra 
409391e43daSPeter Zijlstra /*
410391e43daSPeter Zijlstra  * A weight of 0 or 1 can cause arithmetics problems.
411391e43daSPeter Zijlstra  * A weight of a cfs_rq is the sum of weights of which entities
412391e43daSPeter Zijlstra  * are queued on this cfs_rq, so a weight of a entity should not be
413391e43daSPeter Zijlstra  * too large, so as the shares value of a task group.
414391e43daSPeter Zijlstra  * (The default weight is 1024 - so there's no practical
415391e43daSPeter Zijlstra  *  limitation from this.)
416391e43daSPeter Zijlstra  */
417391e43daSPeter Zijlstra #define MIN_SHARES		(1UL <<  1)
418391e43daSPeter Zijlstra #define MAX_SHARES		(1UL << 18)
419391e43daSPeter Zijlstra #endif
420391e43daSPeter Zijlstra 
421391e43daSPeter Zijlstra typedef int (*tg_visitor)(struct task_group *, void *);
422391e43daSPeter Zijlstra 
423391e43daSPeter Zijlstra extern int walk_tg_tree_from(struct task_group *from,
424391e43daSPeter Zijlstra 			     tg_visitor down, tg_visitor up, void *data);
425391e43daSPeter Zijlstra 
426391e43daSPeter Zijlstra /*
427391e43daSPeter Zijlstra  * Iterate the full tree, calling @down when first entering a node and @up when
428391e43daSPeter Zijlstra  * leaving it for the final time.
429391e43daSPeter Zijlstra  *
430391e43daSPeter Zijlstra  * Caller must hold rcu_lock or sufficient equivalent.
431391e43daSPeter Zijlstra  */
432391e43daSPeter Zijlstra static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
433391e43daSPeter Zijlstra {
434391e43daSPeter Zijlstra 	return walk_tg_tree_from(&root_task_group, down, up, data);
435391e43daSPeter Zijlstra }
436391e43daSPeter Zijlstra 
437391e43daSPeter Zijlstra extern int tg_nop(struct task_group *tg, void *data);
438391e43daSPeter Zijlstra 
439391e43daSPeter Zijlstra extern void free_fair_sched_group(struct task_group *tg);
440391e43daSPeter Zijlstra extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
4418663e24dSPeter Zijlstra extern void online_fair_sched_group(struct task_group *tg);
4426fe1f348SPeter Zijlstra extern void unregister_fair_sched_group(struct task_group *tg);
443391e43daSPeter Zijlstra extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
444391e43daSPeter Zijlstra 			struct sched_entity *se, int cpu,
445391e43daSPeter Zijlstra 			struct sched_entity *parent);
446391e43daSPeter Zijlstra extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
447391e43daSPeter Zijlstra 
448391e43daSPeter Zijlstra extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
44977a4d1a1SPeter Zijlstra extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
450391e43daSPeter Zijlstra extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
451391e43daSPeter Zijlstra 
452391e43daSPeter Zijlstra extern void free_rt_sched_group(struct task_group *tg);
453391e43daSPeter Zijlstra extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
454391e43daSPeter Zijlstra extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
455391e43daSPeter Zijlstra 		struct sched_rt_entity *rt_se, int cpu,
456391e43daSPeter Zijlstra 		struct sched_rt_entity *parent);
4578887cd99SNicolas Pitre extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
4588887cd99SNicolas Pitre extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
4598887cd99SNicolas Pitre extern long sched_group_rt_runtime(struct task_group *tg);
4608887cd99SNicolas Pitre extern long sched_group_rt_period(struct task_group *tg);
4618887cd99SNicolas Pitre extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
462391e43daSPeter Zijlstra 
46325cc7da7SLi Zefan extern struct task_group *sched_create_group(struct task_group *parent);
46425cc7da7SLi Zefan extern void sched_online_group(struct task_group *tg,
46525cc7da7SLi Zefan 			       struct task_group *parent);
46625cc7da7SLi Zefan extern void sched_destroy_group(struct task_group *tg);
46725cc7da7SLi Zefan extern void sched_offline_group(struct task_group *tg);
46825cc7da7SLi Zefan 
46925cc7da7SLi Zefan extern void sched_move_task(struct task_struct *tsk);
47025cc7da7SLi Zefan 
47125cc7da7SLi Zefan #ifdef CONFIG_FAIR_GROUP_SCHED
47225cc7da7SLi Zefan extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
473ad936d86SByungchul Park 
474ad936d86SByungchul Park #ifdef CONFIG_SMP
475ad936d86SByungchul Park extern void set_task_rq_fair(struct sched_entity *se,
476ad936d86SByungchul Park 			     struct cfs_rq *prev, struct cfs_rq *next);
477ad936d86SByungchul Park #else /* !CONFIG_SMP */
478ad936d86SByungchul Park static inline void set_task_rq_fair(struct sched_entity *se,
479ad936d86SByungchul Park 			     struct cfs_rq *prev, struct cfs_rq *next) { }
480ad936d86SByungchul Park #endif /* CONFIG_SMP */
481ad936d86SByungchul Park #endif /* CONFIG_FAIR_GROUP_SCHED */
48225cc7da7SLi Zefan 
483391e43daSPeter Zijlstra #else /* CONFIG_CGROUP_SCHED */
484391e43daSPeter Zijlstra 
485391e43daSPeter Zijlstra struct cfs_bandwidth { };
486391e43daSPeter Zijlstra 
487391e43daSPeter Zijlstra #endif	/* CONFIG_CGROUP_SCHED */
488391e43daSPeter Zijlstra 
489391e43daSPeter Zijlstra /* CFS-related fields in a runqueue */
490391e43daSPeter Zijlstra struct cfs_rq {
491391e43daSPeter Zijlstra 	struct load_weight	load;
4921ea6c46aSPeter Zijlstra 	unsigned long		runnable_weight;
49397fb7a0aSIngo Molnar 	unsigned int		nr_running;
49443e9f7f2SViresh Kumar 	unsigned int		h_nr_running;      /* SCHED_{NORMAL,BATCH,IDLE} */
49543e9f7f2SViresh Kumar 	unsigned int		idle_h_nr_running; /* SCHED_IDLE */
496391e43daSPeter Zijlstra 
497391e43daSPeter Zijlstra 	u64			exec_clock;
498391e43daSPeter Zijlstra 	u64			min_vruntime;
499391e43daSPeter Zijlstra #ifndef CONFIG_64BIT
500391e43daSPeter Zijlstra 	u64			min_vruntime_copy;
501391e43daSPeter Zijlstra #endif
502391e43daSPeter Zijlstra 
503bfb06889SDavidlohr Bueso 	struct rb_root_cached	tasks_timeline;
504391e43daSPeter Zijlstra 
505391e43daSPeter Zijlstra 	/*
506391e43daSPeter Zijlstra 	 * 'curr' points to currently running entity on this cfs_rq.
507391e43daSPeter Zijlstra 	 * It is set to NULL otherwise (i.e when none are currently running).
508391e43daSPeter Zijlstra 	 */
50997fb7a0aSIngo Molnar 	struct sched_entity	*curr;
51097fb7a0aSIngo Molnar 	struct sched_entity	*next;
51197fb7a0aSIngo Molnar 	struct sched_entity	*last;
51297fb7a0aSIngo Molnar 	struct sched_entity	*skip;
513391e43daSPeter Zijlstra 
514391e43daSPeter Zijlstra #ifdef	CONFIG_SCHED_DEBUG
515391e43daSPeter Zijlstra 	unsigned int		nr_spread_over;
516391e43daSPeter Zijlstra #endif
517391e43daSPeter Zijlstra 
5182dac754eSPaul Turner #ifdef CONFIG_SMP
5192dac754eSPaul Turner 	/*
5209d89c257SYuyang Du 	 * CFS load tracking
5212dac754eSPaul Turner 	 */
5229d89c257SYuyang Du 	struct sched_avg	avg;
5232a2f5d4eSPeter Zijlstra #ifndef CONFIG_64BIT
5242a2f5d4eSPeter Zijlstra 	u64			load_last_update_time_copy;
5252a2f5d4eSPeter Zijlstra #endif
5262a2f5d4eSPeter Zijlstra 	struct {
5272a2f5d4eSPeter Zijlstra 		raw_spinlock_t	lock ____cacheline_aligned;
5282a2f5d4eSPeter Zijlstra 		int		nr;
5292a2f5d4eSPeter Zijlstra 		unsigned long	load_avg;
5302a2f5d4eSPeter Zijlstra 		unsigned long	util_avg;
5310e2d2aaaSPeter Zijlstra 		unsigned long	runnable_sum;
5322a2f5d4eSPeter Zijlstra 	} removed;
533141965c7SAlex Shi 
534c566e8e9SPaul Turner #ifdef CONFIG_FAIR_GROUP_SCHED
5350e2d2aaaSPeter Zijlstra 	unsigned long		tg_load_avg_contrib;
5360e2d2aaaSPeter Zijlstra 	long			propagate;
5370e2d2aaaSPeter Zijlstra 	long			prop_runnable_sum;
5380e2d2aaaSPeter Zijlstra 
53982958366SPaul Turner 	/*
54082958366SPaul Turner 	 *   h_load = weight * f(tg)
54182958366SPaul Turner 	 *
54282958366SPaul Turner 	 * Where f(tg) is the recursive weight fraction assigned to
54382958366SPaul Turner 	 * this group.
54482958366SPaul Turner 	 */
54582958366SPaul Turner 	unsigned long		h_load;
54668520796SVladimir Davydov 	u64			last_h_load_update;
54768520796SVladimir Davydov 	struct sched_entity	*h_load_next;
54868520796SVladimir Davydov #endif /* CONFIG_FAIR_GROUP_SCHED */
54982958366SPaul Turner #endif /* CONFIG_SMP */
55082958366SPaul Turner 
551391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED
55297fb7a0aSIngo Molnar 	struct rq		*rq;	/* CPU runqueue to which this cfs_rq is attached */
553391e43daSPeter Zijlstra 
554391e43daSPeter Zijlstra 	/*
555391e43daSPeter Zijlstra 	 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
556391e43daSPeter Zijlstra 	 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
557391e43daSPeter Zijlstra 	 * (like users, containers etc.)
558391e43daSPeter Zijlstra 	 *
55997fb7a0aSIngo Molnar 	 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
56097fb7a0aSIngo Molnar 	 * This list is used during load balance.
561391e43daSPeter Zijlstra 	 */
562391e43daSPeter Zijlstra 	int			on_list;
563391e43daSPeter Zijlstra 	struct list_head	leaf_cfs_rq_list;
564391e43daSPeter Zijlstra 	struct task_group	*tg;	/* group that "owns" this runqueue */
565391e43daSPeter Zijlstra 
566391e43daSPeter Zijlstra #ifdef CONFIG_CFS_BANDWIDTH
567391e43daSPeter Zijlstra 	int			runtime_enabled;
568391e43daSPeter Zijlstra 	s64			runtime_remaining;
569391e43daSPeter Zijlstra 
57097fb7a0aSIngo Molnar 	u64			throttled_clock;
57197fb7a0aSIngo Molnar 	u64			throttled_clock_task;
572f1b17280SPaul Turner 	u64			throttled_clock_task_time;
57397fb7a0aSIngo Molnar 	int			throttled;
57497fb7a0aSIngo Molnar 	int			throttle_count;
575391e43daSPeter Zijlstra 	struct list_head	throttled_list;
576391e43daSPeter Zijlstra #endif /* CONFIG_CFS_BANDWIDTH */
577391e43daSPeter Zijlstra #endif /* CONFIG_FAIR_GROUP_SCHED */
578391e43daSPeter Zijlstra };
579391e43daSPeter Zijlstra 
580391e43daSPeter Zijlstra static inline int rt_bandwidth_enabled(void)
581391e43daSPeter Zijlstra {
582391e43daSPeter Zijlstra 	return sysctl_sched_rt_runtime >= 0;
583391e43daSPeter Zijlstra }
584391e43daSPeter Zijlstra 
585b6366f04SSteven Rostedt /* RT IPI pull logic requires IRQ_WORK */
5864bdced5cSSteven Rostedt (Red Hat) #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
587b6366f04SSteven Rostedt # define HAVE_RT_PUSH_IPI
588b6366f04SSteven Rostedt #endif
589b6366f04SSteven Rostedt 
590391e43daSPeter Zijlstra /* Real-Time classes' related field in a runqueue: */
591391e43daSPeter Zijlstra struct rt_rq {
592391e43daSPeter Zijlstra 	struct rt_prio_array	active;
593c82513e5SPeter Zijlstra 	unsigned int		rt_nr_running;
59401d36d0aSFrederic Weisbecker 	unsigned int		rr_nr_running;
595391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
596391e43daSPeter Zijlstra 	struct {
597391e43daSPeter Zijlstra 		int		curr; /* highest queued rt task prio */
598391e43daSPeter Zijlstra #ifdef CONFIG_SMP
599391e43daSPeter Zijlstra 		int		next; /* next highest */
600391e43daSPeter Zijlstra #endif
601391e43daSPeter Zijlstra 	} highest_prio;
602391e43daSPeter Zijlstra #endif
603391e43daSPeter Zijlstra #ifdef CONFIG_SMP
604391e43daSPeter Zijlstra 	unsigned long		rt_nr_migratory;
605391e43daSPeter Zijlstra 	unsigned long		rt_nr_total;
606391e43daSPeter Zijlstra 	int			overloaded;
607391e43daSPeter Zijlstra 	struct plist_head	pushable_tasks;
608371bf427SVincent Guittot 
609b6366f04SSteven Rostedt #endif /* CONFIG_SMP */
610f4ebcbc0SKirill Tkhai 	int			rt_queued;
611f4ebcbc0SKirill Tkhai 
612391e43daSPeter Zijlstra 	int			rt_throttled;
613391e43daSPeter Zijlstra 	u64			rt_time;
614391e43daSPeter Zijlstra 	u64			rt_runtime;
615391e43daSPeter Zijlstra 	/* Nests inside the rq lock: */
616391e43daSPeter Zijlstra 	raw_spinlock_t		rt_runtime_lock;
617391e43daSPeter Zijlstra 
618391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
619391e43daSPeter Zijlstra 	unsigned long		rt_nr_boosted;
620391e43daSPeter Zijlstra 
621391e43daSPeter Zijlstra 	struct rq		*rq;
622391e43daSPeter Zijlstra 	struct task_group	*tg;
623391e43daSPeter Zijlstra #endif
624391e43daSPeter Zijlstra };
625391e43daSPeter Zijlstra 
626296b2ffeSVincent Guittot static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
627296b2ffeSVincent Guittot {
628296b2ffeSVincent Guittot 	return rt_rq->rt_queued && rt_rq->rt_nr_running;
629296b2ffeSVincent Guittot }
630296b2ffeSVincent Guittot 
631aab03e05SDario Faggioli /* Deadline class' related fields in a runqueue */
632aab03e05SDario Faggioli struct dl_rq {
633aab03e05SDario Faggioli 	/* runqueue is an rbtree, ordered by deadline */
6342161573eSDavidlohr Bueso 	struct rb_root_cached	root;
635aab03e05SDario Faggioli 
636aab03e05SDario Faggioli 	unsigned long		dl_nr_running;
6371baca4ceSJuri Lelli 
6381baca4ceSJuri Lelli #ifdef CONFIG_SMP
6391baca4ceSJuri Lelli 	/*
6401baca4ceSJuri Lelli 	 * Deadline values of the currently executing and the
6411baca4ceSJuri Lelli 	 * earliest ready task on this rq. Caching these facilitates
642dfcb245eSIngo Molnar 	 * the decision whether or not a ready but not running task
6431baca4ceSJuri Lelli 	 * should migrate somewhere else.
6441baca4ceSJuri Lelli 	 */
6451baca4ceSJuri Lelli 	struct {
6461baca4ceSJuri Lelli 		u64		curr;
6471baca4ceSJuri Lelli 		u64		next;
6481baca4ceSJuri Lelli 	} earliest_dl;
6491baca4ceSJuri Lelli 
6501baca4ceSJuri Lelli 	unsigned long		dl_nr_migratory;
6511baca4ceSJuri Lelli 	int			overloaded;
6521baca4ceSJuri Lelli 
6531baca4ceSJuri Lelli 	/*
6541baca4ceSJuri Lelli 	 * Tasks on this rq that can be pushed away. They are kept in
6551baca4ceSJuri Lelli 	 * an rb-tree, ordered by tasks' deadlines, with caching
6561baca4ceSJuri Lelli 	 * of the leftmost (earliest deadline) element.
6571baca4ceSJuri Lelli 	 */
6582161573eSDavidlohr Bueso 	struct rb_root_cached	pushable_dl_tasks_root;
659332ac17eSDario Faggioli #else
660332ac17eSDario Faggioli 	struct dl_bw		dl_bw;
6611baca4ceSJuri Lelli #endif
662e36d8677SLuca Abeni 	/*
663e36d8677SLuca Abeni 	 * "Active utilization" for this runqueue: increased when a
664e36d8677SLuca Abeni 	 * task wakes up (becomes TASK_RUNNING) and decreased when a
665e36d8677SLuca Abeni 	 * task blocks
666e36d8677SLuca Abeni 	 */
667e36d8677SLuca Abeni 	u64			running_bw;
6684da3abceSLuca Abeni 
6694da3abceSLuca Abeni 	/*
6708fd27231SLuca Abeni 	 * Utilization of the tasks "assigned" to this runqueue (including
6718fd27231SLuca Abeni 	 * the tasks that are in runqueue and the tasks that executed on this
6728fd27231SLuca Abeni 	 * CPU and blocked). Increased when a task moves to this runqueue, and
6738fd27231SLuca Abeni 	 * decreased when the task moves away (migrates, changes scheduling
6748fd27231SLuca Abeni 	 * policy, or terminates).
6758fd27231SLuca Abeni 	 * This is needed to compute the "inactive utilization" for the
6768fd27231SLuca Abeni 	 * runqueue (inactive utilization = this_bw - running_bw).
6778fd27231SLuca Abeni 	 */
6788fd27231SLuca Abeni 	u64			this_bw;
679daec5798SLuca Abeni 	u64			extra_bw;
6808fd27231SLuca Abeni 
6818fd27231SLuca Abeni 	/*
6824da3abceSLuca Abeni 	 * Inverse of the fraction of CPU utilization that can be reclaimed
6834da3abceSLuca Abeni 	 * by the GRUB algorithm.
6844da3abceSLuca Abeni 	 */
6854da3abceSLuca Abeni 	u64			bw_ratio;
686aab03e05SDario Faggioli };
687aab03e05SDario Faggioli 
688c0796298SVincent Guittot #ifdef CONFIG_FAIR_GROUP_SCHED
689c0796298SVincent Guittot /* An entity is a task if it doesn't "own" a runqueue */
690c0796298SVincent Guittot #define entity_is_task(se)	(!se->my_q)
691c0796298SVincent Guittot #else
692c0796298SVincent Guittot #define entity_is_task(se)	1
693c0796298SVincent Guittot #endif
694c0796298SVincent Guittot 
695391e43daSPeter Zijlstra #ifdef CONFIG_SMP
696c0796298SVincent Guittot /*
697c0796298SVincent Guittot  * XXX we want to get rid of these helpers and use the full load resolution.
698c0796298SVincent Guittot  */
699c0796298SVincent Guittot static inline long se_weight(struct sched_entity *se)
700c0796298SVincent Guittot {
701c0796298SVincent Guittot 	return scale_load_down(se->load.weight);
702c0796298SVincent Guittot }
703c0796298SVincent Guittot 
704c0796298SVincent Guittot static inline long se_runnable(struct sched_entity *se)
705c0796298SVincent Guittot {
706c0796298SVincent Guittot 	return scale_load_down(se->runnable_weight);
707c0796298SVincent Guittot }
708391e43daSPeter Zijlstra 
709afe06efdSTim Chen static inline bool sched_asym_prefer(int a, int b)
710afe06efdSTim Chen {
711afe06efdSTim Chen 	return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
712afe06efdSTim Chen }
713afe06efdSTim Chen 
7146aa140faSQuentin Perret struct perf_domain {
7156aa140faSQuentin Perret 	struct em_perf_domain *em_pd;
7166aa140faSQuentin Perret 	struct perf_domain *next;
7176aa140faSQuentin Perret 	struct rcu_head rcu;
7186aa140faSQuentin Perret };
7196aa140faSQuentin Perret 
720630246a0SQuentin Perret /* Scheduling group status flags */
721630246a0SQuentin Perret #define SG_OVERLOAD		0x1 /* More than one runnable task on a CPU. */
7222802bf3cSMorten Rasmussen #define SG_OVERUTILIZED		0x2 /* One or more CPUs are over-utilized. */
723630246a0SQuentin Perret 
724391e43daSPeter Zijlstra /*
725391e43daSPeter Zijlstra  * We add the notion of a root-domain which will be used to define per-domain
726391e43daSPeter Zijlstra  * variables. Each exclusive cpuset essentially defines an island domain by
72797fb7a0aSIngo Molnar  * fully partitioning the member CPUs from any other cpuset. Whenever a new
728391e43daSPeter Zijlstra  * exclusive cpuset is created, we also create and attach a new root-domain
729391e43daSPeter Zijlstra  * object.
730391e43daSPeter Zijlstra  *
731391e43daSPeter Zijlstra  */
732391e43daSPeter Zijlstra struct root_domain {
733391e43daSPeter Zijlstra 	atomic_t		refcount;
734391e43daSPeter Zijlstra 	atomic_t		rto_count;
735391e43daSPeter Zijlstra 	struct rcu_head		rcu;
736391e43daSPeter Zijlstra 	cpumask_var_t		span;
737391e43daSPeter Zijlstra 	cpumask_var_t		online;
738391e43daSPeter Zijlstra 
739757ffdd7SValentin Schneider 	/*
740757ffdd7SValentin Schneider 	 * Indicate pullable load on at least one CPU, e.g:
741757ffdd7SValentin Schneider 	 * - More than one runnable task
742757ffdd7SValentin Schneider 	 * - Running task is misfit
743757ffdd7SValentin Schneider 	 */
744575638d1SValentin Schneider 	int			overload;
7454486edd1STim Chen 
7462802bf3cSMorten Rasmussen 	/* Indicate one or more cpus over-utilized (tipping point) */
7472802bf3cSMorten Rasmussen 	int			overutilized;
7482802bf3cSMorten Rasmussen 
749391e43daSPeter Zijlstra 	/*
7501baca4ceSJuri Lelli 	 * The bit corresponding to a CPU gets set here if such CPU has more
7511baca4ceSJuri Lelli 	 * than one runnable -deadline task (as it is below for RT tasks).
7521baca4ceSJuri Lelli 	 */
7531baca4ceSJuri Lelli 	cpumask_var_t		dlo_mask;
7541baca4ceSJuri Lelli 	atomic_t		dlo_count;
755332ac17eSDario Faggioli 	struct dl_bw		dl_bw;
7566bfd6d72SJuri Lelli 	struct cpudl		cpudl;
7571baca4ceSJuri Lelli 
7584bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI
7594bdced5cSSteven Rostedt (Red Hat) 	/*
7604bdced5cSSteven Rostedt (Red Hat) 	 * For IPI pull requests, loop across the rto_mask.
7614bdced5cSSteven Rostedt (Red Hat) 	 */
7624bdced5cSSteven Rostedt (Red Hat) 	struct irq_work		rto_push_work;
7634bdced5cSSteven Rostedt (Red Hat) 	raw_spinlock_t		rto_lock;
7644bdced5cSSteven Rostedt (Red Hat) 	/* These are only updated and read within rto_lock */
7654bdced5cSSteven Rostedt (Red Hat) 	int			rto_loop;
7664bdced5cSSteven Rostedt (Red Hat) 	int			rto_cpu;
7674bdced5cSSteven Rostedt (Red Hat) 	/* These atomics are updated outside of a lock */
7684bdced5cSSteven Rostedt (Red Hat) 	atomic_t		rto_loop_next;
7694bdced5cSSteven Rostedt (Red Hat) 	atomic_t		rto_loop_start;
7704bdced5cSSteven Rostedt (Red Hat) #endif
7711baca4ceSJuri Lelli 	/*
772391e43daSPeter Zijlstra 	 * The "RT overload" flag: it gets set if a CPU has more than
773391e43daSPeter Zijlstra 	 * one runnable RT task.
774391e43daSPeter Zijlstra 	 */
775391e43daSPeter Zijlstra 	cpumask_var_t		rto_mask;
776391e43daSPeter Zijlstra 	struct cpupri		cpupri;
777cd92bfd3SDietmar Eggemann 
778cd92bfd3SDietmar Eggemann 	unsigned long		max_cpu_capacity;
7796aa140faSQuentin Perret 
7806aa140faSQuentin Perret 	/*
7816aa140faSQuentin Perret 	 * NULL-terminated list of performance domains intersecting with the
7826aa140faSQuentin Perret 	 * CPUs of the rd. Protected by RCU.
7836aa140faSQuentin Perret 	 */
7847ba7319fSJoel Fernandes (Google) 	struct perf_domain __rcu *pd;
785391e43daSPeter Zijlstra };
786391e43daSPeter Zijlstra 
787f2cb1360SIngo Molnar extern void init_defrootdomain(void);
7888d5dc512SPeter Zijlstra extern int sched_init_domains(const struct cpumask *cpu_map);
789f2cb1360SIngo Molnar extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
790364f5665SSteven Rostedt (VMware) extern void sched_get_rd(struct root_domain *rd);
791364f5665SSteven Rostedt (VMware) extern void sched_put_rd(struct root_domain *rd);
792391e43daSPeter Zijlstra 
7934bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI
7944bdced5cSSteven Rostedt (Red Hat) extern void rto_push_irq_work_func(struct irq_work *work);
7954bdced5cSSteven Rostedt (Red Hat) #endif
796391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
797391e43daSPeter Zijlstra 
79869842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
79969842cbaSPatrick Bellasi /*
80069842cbaSPatrick Bellasi  * struct uclamp_bucket - Utilization clamp bucket
80169842cbaSPatrick Bellasi  * @value: utilization clamp value for tasks on this clamp bucket
80269842cbaSPatrick Bellasi  * @tasks: number of RUNNABLE tasks on this clamp bucket
80369842cbaSPatrick Bellasi  *
80469842cbaSPatrick Bellasi  * Keep track of how many tasks are RUNNABLE for a given utilization
80569842cbaSPatrick Bellasi  * clamp value.
80669842cbaSPatrick Bellasi  */
80769842cbaSPatrick Bellasi struct uclamp_bucket {
80869842cbaSPatrick Bellasi 	unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
80969842cbaSPatrick Bellasi 	unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
81069842cbaSPatrick Bellasi };
81169842cbaSPatrick Bellasi 
81269842cbaSPatrick Bellasi /*
81369842cbaSPatrick Bellasi  * struct uclamp_rq - rq's utilization clamp
81469842cbaSPatrick Bellasi  * @value: currently active clamp values for a rq
81569842cbaSPatrick Bellasi  * @bucket: utilization clamp buckets affecting a rq
81669842cbaSPatrick Bellasi  *
81769842cbaSPatrick Bellasi  * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
81869842cbaSPatrick Bellasi  * A clamp value is affecting a rq when there is at least one task RUNNABLE
81969842cbaSPatrick Bellasi  * (or actually running) with that value.
82069842cbaSPatrick Bellasi  *
82169842cbaSPatrick Bellasi  * There are up to UCLAMP_CNT possible different clamp values, currently there
82269842cbaSPatrick Bellasi  * are only two: minimum utilization and maximum utilization.
82369842cbaSPatrick Bellasi  *
82469842cbaSPatrick Bellasi  * All utilization clamping values are MAX aggregated, since:
82569842cbaSPatrick Bellasi  * - for util_min: we want to run the CPU at least at the max of the minimum
82669842cbaSPatrick Bellasi  *   utilization required by its currently RUNNABLE tasks.
82769842cbaSPatrick Bellasi  * - for util_max: we want to allow the CPU to run up to the max of the
82869842cbaSPatrick Bellasi  *   maximum utilization allowed by its currently RUNNABLE tasks.
82969842cbaSPatrick Bellasi  *
83069842cbaSPatrick Bellasi  * Since on each system we expect only a limited number of different
83169842cbaSPatrick Bellasi  * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
83269842cbaSPatrick Bellasi  * the metrics required to compute all the per-rq utilization clamp values.
83369842cbaSPatrick Bellasi  */
83469842cbaSPatrick Bellasi struct uclamp_rq {
83569842cbaSPatrick Bellasi 	unsigned int value;
83669842cbaSPatrick Bellasi 	struct uclamp_bucket bucket[UCLAMP_BUCKETS];
83769842cbaSPatrick Bellasi };
83869842cbaSPatrick Bellasi #endif /* CONFIG_UCLAMP_TASK */
83969842cbaSPatrick Bellasi 
840391e43daSPeter Zijlstra /*
841391e43daSPeter Zijlstra  * This is the main, per-CPU runqueue data structure.
842391e43daSPeter Zijlstra  *
843391e43daSPeter Zijlstra  * Locking rule: those places that want to lock multiple runqueues
844391e43daSPeter Zijlstra  * (such as the load balancing or the thread migration code), lock
845391e43daSPeter Zijlstra  * acquire operations must be ordered by ascending &runqueue.
846391e43daSPeter Zijlstra  */
847391e43daSPeter Zijlstra struct rq {
848391e43daSPeter Zijlstra 	/* runqueue lock: */
849391e43daSPeter Zijlstra 	raw_spinlock_t		lock;
850391e43daSPeter Zijlstra 
851391e43daSPeter Zijlstra 	/*
852391e43daSPeter Zijlstra 	 * nr_running and cpu_load should be in the same cacheline because
853391e43daSPeter Zijlstra 	 * remote CPUs use both these fields when doing load calculation.
854391e43daSPeter Zijlstra 	 */
855c82513e5SPeter Zijlstra 	unsigned int		nr_running;
8560ec8aa00SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
8570ec8aa00SPeter Zijlstra 	unsigned int		nr_numa_running;
8580ec8aa00SPeter Zijlstra 	unsigned int		nr_preferred_running;
859a4739ecaSSrikar Dronamraju 	unsigned int		numa_migrate_on;
8600ec8aa00SPeter Zijlstra #endif
8613451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON
8629fd81dd5SFrederic Weisbecker #ifdef CONFIG_SMP
8639fd81dd5SFrederic Weisbecker 	unsigned long		last_load_update_tick;
864e022e0d3SPeter Zijlstra 	unsigned long		last_blocked_load_update_tick;
865f643ea22SVincent Guittot 	unsigned int		has_blocked_load;
8669fd81dd5SFrederic Weisbecker #endif /* CONFIG_SMP */
86700357f5eSPeter Zijlstra 	unsigned int		nohz_tick_stopped;
868a22e47a4SPeter Zijlstra 	atomic_t nohz_flags;
8699fd81dd5SFrederic Weisbecker #endif /* CONFIG_NO_HZ_COMMON */
870dcdedb24SFrederic Weisbecker 
871391e43daSPeter Zijlstra 	unsigned long		nr_load_updates;
872391e43daSPeter Zijlstra 	u64			nr_switches;
873391e43daSPeter Zijlstra 
87469842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
87569842cbaSPatrick Bellasi 	/* Utilization clamp values based on CPU's RUNNABLE tasks */
87669842cbaSPatrick Bellasi 	struct uclamp_rq	uclamp[UCLAMP_CNT] ____cacheline_aligned;
877e496187dSPatrick Bellasi 	unsigned int		uclamp_flags;
878e496187dSPatrick Bellasi #define UCLAMP_FLAG_IDLE 0x01
87969842cbaSPatrick Bellasi #endif
88069842cbaSPatrick Bellasi 
881391e43daSPeter Zijlstra 	struct cfs_rq		cfs;
882391e43daSPeter Zijlstra 	struct rt_rq		rt;
883aab03e05SDario Faggioli 	struct dl_rq		dl;
884391e43daSPeter Zijlstra 
885391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED
88697fb7a0aSIngo Molnar 	/* list of leaf cfs_rq on this CPU: */
887391e43daSPeter Zijlstra 	struct list_head	leaf_cfs_rq_list;
8889c2791f9SVincent Guittot 	struct list_head	*tmp_alone_branch;
889a35b6466SPeter Zijlstra #endif /* CONFIG_FAIR_GROUP_SCHED */
890a35b6466SPeter Zijlstra 
891391e43daSPeter Zijlstra 	/*
892391e43daSPeter Zijlstra 	 * This is part of a global counter where only the total sum
893391e43daSPeter Zijlstra 	 * over all CPUs matters. A task can increase this counter on
894391e43daSPeter Zijlstra 	 * one CPU and if it got migrated afterwards it may decrease
895391e43daSPeter Zijlstra 	 * it on another CPU. Always updated under the runqueue lock:
896391e43daSPeter Zijlstra 	 */
897391e43daSPeter Zijlstra 	unsigned long		nr_uninterruptible;
898391e43daSPeter Zijlstra 
89997fb7a0aSIngo Molnar 	struct task_struct	*curr;
90097fb7a0aSIngo Molnar 	struct task_struct	*idle;
90197fb7a0aSIngo Molnar 	struct task_struct	*stop;
902391e43daSPeter Zijlstra 	unsigned long		next_balance;
903391e43daSPeter Zijlstra 	struct mm_struct	*prev_mm;
904391e43daSPeter Zijlstra 
905cb42c9a3SMatt Fleming 	unsigned int		clock_update_flags;
906391e43daSPeter Zijlstra 	u64			clock;
90723127296SVincent Guittot 	/* Ensure that all clocks are in the same cache line */
90823127296SVincent Guittot 	u64			clock_task ____cacheline_aligned;
90923127296SVincent Guittot 	u64			clock_pelt;
91023127296SVincent Guittot 	unsigned long		lost_idle_time;
911391e43daSPeter Zijlstra 
912391e43daSPeter Zijlstra 	atomic_t		nr_iowait;
913391e43daSPeter Zijlstra 
914391e43daSPeter Zijlstra #ifdef CONFIG_SMP
915391e43daSPeter Zijlstra 	struct root_domain		*rd;
916994aeb7aSJoel Fernandes (Google) 	struct sched_domain __rcu	*sd;
917391e43daSPeter Zijlstra 
918ced549faSNicolas Pitre 	unsigned long		cpu_capacity;
919ca6d75e6SVincent Guittot 	unsigned long		cpu_capacity_orig;
920391e43daSPeter Zijlstra 
921e3fca9e7SPeter Zijlstra 	struct callback_head	*balance_callback;
922e3fca9e7SPeter Zijlstra 
923391e43daSPeter Zijlstra 	unsigned char		idle_balance;
92497fb7a0aSIngo Molnar 
9253b1baa64SMorten Rasmussen 	unsigned long		misfit_task_load;
9263b1baa64SMorten Rasmussen 
927391e43daSPeter Zijlstra 	/* For active balancing */
928391e43daSPeter Zijlstra 	int			active_balance;
929391e43daSPeter Zijlstra 	int			push_cpu;
930391e43daSPeter Zijlstra 	struct cpu_stop_work	active_balance_work;
93197fb7a0aSIngo Molnar 
93297fb7a0aSIngo Molnar 	/* CPU of this runqueue: */
933391e43daSPeter Zijlstra 	int			cpu;
934391e43daSPeter Zijlstra 	int			online;
935391e43daSPeter Zijlstra 
936367456c7SPeter Zijlstra 	struct list_head cfs_tasks;
937367456c7SPeter Zijlstra 
938371bf427SVincent Guittot 	struct sched_avg	avg_rt;
9393727e0e1SVincent Guittot 	struct sched_avg	avg_dl;
94011d4afd4SVincent Guittot #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
94191c27493SVincent Guittot 	struct sched_avg	avg_irq;
94291c27493SVincent Guittot #endif
943391e43daSPeter Zijlstra 	u64			idle_stamp;
944391e43daSPeter Zijlstra 	u64			avg_idle;
9459bd721c5SJason Low 
9469bd721c5SJason Low 	/* This is used to determine avg_idle's max value */
9479bd721c5SJason Low 	u64			max_idle_balance_cost;
948391e43daSPeter Zijlstra #endif
949391e43daSPeter Zijlstra 
950391e43daSPeter Zijlstra #ifdef CONFIG_IRQ_TIME_ACCOUNTING
951391e43daSPeter Zijlstra 	u64			prev_irq_time;
952391e43daSPeter Zijlstra #endif
953391e43daSPeter Zijlstra #ifdef CONFIG_PARAVIRT
954391e43daSPeter Zijlstra 	u64			prev_steal_time;
955391e43daSPeter Zijlstra #endif
956391e43daSPeter Zijlstra #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
957391e43daSPeter Zijlstra 	u64			prev_steal_time_rq;
958391e43daSPeter Zijlstra #endif
959391e43daSPeter Zijlstra 
960391e43daSPeter Zijlstra 	/* calc_load related fields */
961391e43daSPeter Zijlstra 	unsigned long		calc_load_update;
962391e43daSPeter Zijlstra 	long			calc_load_active;
963391e43daSPeter Zijlstra 
964391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_HRTICK
965391e43daSPeter Zijlstra #ifdef CONFIG_SMP
966391e43daSPeter Zijlstra 	int			hrtick_csd_pending;
967966a9671SYing Huang 	call_single_data_t	hrtick_csd;
968391e43daSPeter Zijlstra #endif
969391e43daSPeter Zijlstra 	struct hrtimer		hrtick_timer;
970391e43daSPeter Zijlstra #endif
971391e43daSPeter Zijlstra 
972391e43daSPeter Zijlstra #ifdef CONFIG_SCHEDSTATS
973391e43daSPeter Zijlstra 	/* latency stats */
974391e43daSPeter Zijlstra 	struct sched_info	rq_sched_info;
975391e43daSPeter Zijlstra 	unsigned long long	rq_cpu_time;
976391e43daSPeter Zijlstra 	/* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
977391e43daSPeter Zijlstra 
978391e43daSPeter Zijlstra 	/* sys_sched_yield() stats */
979391e43daSPeter Zijlstra 	unsigned int		yld_count;
980391e43daSPeter Zijlstra 
981391e43daSPeter Zijlstra 	/* schedule() stats */
982391e43daSPeter Zijlstra 	unsigned int		sched_count;
983391e43daSPeter Zijlstra 	unsigned int		sched_goidle;
984391e43daSPeter Zijlstra 
985391e43daSPeter Zijlstra 	/* try_to_wake_up() stats */
986391e43daSPeter Zijlstra 	unsigned int		ttwu_count;
987391e43daSPeter Zijlstra 	unsigned int		ttwu_local;
988391e43daSPeter Zijlstra #endif
989391e43daSPeter Zijlstra 
990391e43daSPeter Zijlstra #ifdef CONFIG_SMP
991391e43daSPeter Zijlstra 	struct llist_head	wake_list;
992391e43daSPeter Zijlstra #endif
993442bf3aaSDaniel Lezcano 
994442bf3aaSDaniel Lezcano #ifdef CONFIG_CPU_IDLE
995442bf3aaSDaniel Lezcano 	/* Must be inspected within a rcu lock section */
996442bf3aaSDaniel Lezcano 	struct cpuidle_state	*idle_state;
997442bf3aaSDaniel Lezcano #endif
998391e43daSPeter Zijlstra };
999391e43daSPeter Zijlstra 
100062478d99SVincent Guittot #ifdef CONFIG_FAIR_GROUP_SCHED
100162478d99SVincent Guittot 
100262478d99SVincent Guittot /* CPU runqueue to which this cfs_rq is attached */
100362478d99SVincent Guittot static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
100462478d99SVincent Guittot {
100562478d99SVincent Guittot 	return cfs_rq->rq;
100662478d99SVincent Guittot }
100762478d99SVincent Guittot 
100862478d99SVincent Guittot #else
100962478d99SVincent Guittot 
101062478d99SVincent Guittot static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
101162478d99SVincent Guittot {
101262478d99SVincent Guittot 	return container_of(cfs_rq, struct rq, cfs);
101362478d99SVincent Guittot }
101462478d99SVincent Guittot #endif
101562478d99SVincent Guittot 
1016391e43daSPeter Zijlstra static inline int cpu_of(struct rq *rq)
1017391e43daSPeter Zijlstra {
1018391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1019391e43daSPeter Zijlstra 	return rq->cpu;
1020391e43daSPeter Zijlstra #else
1021391e43daSPeter Zijlstra 	return 0;
1022391e43daSPeter Zijlstra #endif
1023391e43daSPeter Zijlstra }
1024391e43daSPeter Zijlstra 
10251b568f0aSPeter Zijlstra 
10261b568f0aSPeter Zijlstra #ifdef CONFIG_SCHED_SMT
10271b568f0aSPeter Zijlstra extern void __update_idle_core(struct rq *rq);
10281b568f0aSPeter Zijlstra 
10291b568f0aSPeter Zijlstra static inline void update_idle_core(struct rq *rq)
10301b568f0aSPeter Zijlstra {
10311b568f0aSPeter Zijlstra 	if (static_branch_unlikely(&sched_smt_present))
10321b568f0aSPeter Zijlstra 		__update_idle_core(rq);
10331b568f0aSPeter Zijlstra }
10341b568f0aSPeter Zijlstra 
10351b568f0aSPeter Zijlstra #else
10361b568f0aSPeter Zijlstra static inline void update_idle_core(struct rq *rq) { }
10371b568f0aSPeter Zijlstra #endif
10381b568f0aSPeter Zijlstra 
10398b06c55bSPranith Kumar DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1040391e43daSPeter Zijlstra 
1041518cd623SPeter Zijlstra #define cpu_rq(cpu)		(&per_cpu(runqueues, (cpu)))
10424a32fea9SChristoph Lameter #define this_rq()		this_cpu_ptr(&runqueues)
1043518cd623SPeter Zijlstra #define task_rq(p)		cpu_rq(task_cpu(p))
1044518cd623SPeter Zijlstra #define cpu_curr(cpu)		(cpu_rq(cpu)->curr)
10454a32fea9SChristoph Lameter #define raw_rq()		raw_cpu_ptr(&runqueues)
1046518cd623SPeter Zijlstra 
10471f351d7fSJohannes Weiner extern void update_rq_clock(struct rq *rq);
10481f351d7fSJohannes Weiner 
1049cebde6d6SPeter Zijlstra static inline u64 __rq_clock_broken(struct rq *rq)
1050cebde6d6SPeter Zijlstra {
1051316c1608SJason Low 	return READ_ONCE(rq->clock);
1052cebde6d6SPeter Zijlstra }
1053cebde6d6SPeter Zijlstra 
1054cb42c9a3SMatt Fleming /*
1055cb42c9a3SMatt Fleming  * rq::clock_update_flags bits
1056cb42c9a3SMatt Fleming  *
1057cb42c9a3SMatt Fleming  * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1058cb42c9a3SMatt Fleming  *  call to __schedule(). This is an optimisation to avoid
1059cb42c9a3SMatt Fleming  *  neighbouring rq clock updates.
1060cb42c9a3SMatt Fleming  *
1061cb42c9a3SMatt Fleming  * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1062cb42c9a3SMatt Fleming  *  in effect and calls to update_rq_clock() are being ignored.
1063cb42c9a3SMatt Fleming  *
1064cb42c9a3SMatt Fleming  * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1065cb42c9a3SMatt Fleming  *  made to update_rq_clock() since the last time rq::lock was pinned.
1066cb42c9a3SMatt Fleming  *
1067cb42c9a3SMatt Fleming  * If inside of __schedule(), clock_update_flags will have been
1068cb42c9a3SMatt Fleming  * shifted left (a left shift is a cheap operation for the fast path
1069cb42c9a3SMatt Fleming  * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1070cb42c9a3SMatt Fleming  *
1071cb42c9a3SMatt Fleming  *	if (rq-clock_update_flags >= RQCF_UPDATED)
1072cb42c9a3SMatt Fleming  *
1073cb42c9a3SMatt Fleming  * to check if %RQCF_UPADTED is set. It'll never be shifted more than
1074cb42c9a3SMatt Fleming  * one position though, because the next rq_unpin_lock() will shift it
1075cb42c9a3SMatt Fleming  * back.
1076cb42c9a3SMatt Fleming  */
1077cb42c9a3SMatt Fleming #define RQCF_REQ_SKIP		0x01
1078cb42c9a3SMatt Fleming #define RQCF_ACT_SKIP		0x02
1079cb42c9a3SMatt Fleming #define RQCF_UPDATED		0x04
1080cb42c9a3SMatt Fleming 
1081cb42c9a3SMatt Fleming static inline void assert_clock_updated(struct rq *rq)
1082cb42c9a3SMatt Fleming {
1083cb42c9a3SMatt Fleming 	/*
1084cb42c9a3SMatt Fleming 	 * The only reason for not seeing a clock update since the
1085cb42c9a3SMatt Fleming 	 * last rq_pin_lock() is if we're currently skipping updates.
1086cb42c9a3SMatt Fleming 	 */
1087cb42c9a3SMatt Fleming 	SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
1088cb42c9a3SMatt Fleming }
1089cb42c9a3SMatt Fleming 
109078becc27SFrederic Weisbecker static inline u64 rq_clock(struct rq *rq)
109178becc27SFrederic Weisbecker {
1092cebde6d6SPeter Zijlstra 	lockdep_assert_held(&rq->lock);
1093cb42c9a3SMatt Fleming 	assert_clock_updated(rq);
1094cb42c9a3SMatt Fleming 
109578becc27SFrederic Weisbecker 	return rq->clock;
109678becc27SFrederic Weisbecker }
109778becc27SFrederic Weisbecker 
109878becc27SFrederic Weisbecker static inline u64 rq_clock_task(struct rq *rq)
109978becc27SFrederic Weisbecker {
1100cebde6d6SPeter Zijlstra 	lockdep_assert_held(&rq->lock);
1101cb42c9a3SMatt Fleming 	assert_clock_updated(rq);
1102cb42c9a3SMatt Fleming 
110378becc27SFrederic Weisbecker 	return rq->clock_task;
110478becc27SFrederic Weisbecker }
110578becc27SFrederic Weisbecker 
1106adcc8da8SDavidlohr Bueso static inline void rq_clock_skip_update(struct rq *rq)
11079edfbfedSPeter Zijlstra {
11089edfbfedSPeter Zijlstra 	lockdep_assert_held(&rq->lock);
1109cb42c9a3SMatt Fleming 	rq->clock_update_flags |= RQCF_REQ_SKIP;
1110adcc8da8SDavidlohr Bueso }
1111adcc8da8SDavidlohr Bueso 
1112adcc8da8SDavidlohr Bueso /*
1113595058b6SDavidlohr Bueso  * See rt task throttling, which is the only time a skip
1114adcc8da8SDavidlohr Bueso  * request is cancelled.
1115adcc8da8SDavidlohr Bueso  */
1116adcc8da8SDavidlohr Bueso static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1117adcc8da8SDavidlohr Bueso {
1118adcc8da8SDavidlohr Bueso 	lockdep_assert_held(&rq->lock);
1119cb42c9a3SMatt Fleming 	rq->clock_update_flags &= ~RQCF_REQ_SKIP;
11209edfbfedSPeter Zijlstra }
11219edfbfedSPeter Zijlstra 
1122d8ac8971SMatt Fleming struct rq_flags {
1123d8ac8971SMatt Fleming 	unsigned long flags;
1124d8ac8971SMatt Fleming 	struct pin_cookie cookie;
1125cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG
1126cb42c9a3SMatt Fleming 	/*
1127cb42c9a3SMatt Fleming 	 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1128cb42c9a3SMatt Fleming 	 * current pin context is stashed here in case it needs to be
1129cb42c9a3SMatt Fleming 	 * restored in rq_repin_lock().
1130cb42c9a3SMatt Fleming 	 */
1131cb42c9a3SMatt Fleming 	unsigned int clock_update_flags;
1132cb42c9a3SMatt Fleming #endif
1133d8ac8971SMatt Fleming };
1134d8ac8971SMatt Fleming 
1135d8ac8971SMatt Fleming static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1136d8ac8971SMatt Fleming {
1137d8ac8971SMatt Fleming 	rf->cookie = lockdep_pin_lock(&rq->lock);
1138cb42c9a3SMatt Fleming 
1139cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG
1140cb42c9a3SMatt Fleming 	rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1141cb42c9a3SMatt Fleming 	rf->clock_update_flags = 0;
1142cb42c9a3SMatt Fleming #endif
1143d8ac8971SMatt Fleming }
1144d8ac8971SMatt Fleming 
1145d8ac8971SMatt Fleming static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1146d8ac8971SMatt Fleming {
1147cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG
1148cb42c9a3SMatt Fleming 	if (rq->clock_update_flags > RQCF_ACT_SKIP)
1149cb42c9a3SMatt Fleming 		rf->clock_update_flags = RQCF_UPDATED;
1150cb42c9a3SMatt Fleming #endif
1151cb42c9a3SMatt Fleming 
1152d8ac8971SMatt Fleming 	lockdep_unpin_lock(&rq->lock, rf->cookie);
1153d8ac8971SMatt Fleming }
1154d8ac8971SMatt Fleming 
1155d8ac8971SMatt Fleming static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1156d8ac8971SMatt Fleming {
1157d8ac8971SMatt Fleming 	lockdep_repin_lock(&rq->lock, rf->cookie);
1158cb42c9a3SMatt Fleming 
1159cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG
1160cb42c9a3SMatt Fleming 	/*
1161cb42c9a3SMatt Fleming 	 * Restore the value we stashed in @rf for this pin context.
1162cb42c9a3SMatt Fleming 	 */
1163cb42c9a3SMatt Fleming 	rq->clock_update_flags |= rf->clock_update_flags;
1164cb42c9a3SMatt Fleming #endif
1165d8ac8971SMatt Fleming }
1166d8ac8971SMatt Fleming 
11671f351d7fSJohannes Weiner struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
11681f351d7fSJohannes Weiner 	__acquires(rq->lock);
11691f351d7fSJohannes Weiner 
11701f351d7fSJohannes Weiner struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
11711f351d7fSJohannes Weiner 	__acquires(p->pi_lock)
11721f351d7fSJohannes Weiner 	__acquires(rq->lock);
11731f351d7fSJohannes Weiner 
11741f351d7fSJohannes Weiner static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
11751f351d7fSJohannes Weiner 	__releases(rq->lock)
11761f351d7fSJohannes Weiner {
11771f351d7fSJohannes Weiner 	rq_unpin_lock(rq, rf);
11781f351d7fSJohannes Weiner 	raw_spin_unlock(&rq->lock);
11791f351d7fSJohannes Weiner }
11801f351d7fSJohannes Weiner 
11811f351d7fSJohannes Weiner static inline void
11821f351d7fSJohannes Weiner task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
11831f351d7fSJohannes Weiner 	__releases(rq->lock)
11841f351d7fSJohannes Weiner 	__releases(p->pi_lock)
11851f351d7fSJohannes Weiner {
11861f351d7fSJohannes Weiner 	rq_unpin_lock(rq, rf);
11871f351d7fSJohannes Weiner 	raw_spin_unlock(&rq->lock);
11881f351d7fSJohannes Weiner 	raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
11891f351d7fSJohannes Weiner }
11901f351d7fSJohannes Weiner 
11911f351d7fSJohannes Weiner static inline void
11921f351d7fSJohannes Weiner rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
11931f351d7fSJohannes Weiner 	__acquires(rq->lock)
11941f351d7fSJohannes Weiner {
11951f351d7fSJohannes Weiner 	raw_spin_lock_irqsave(&rq->lock, rf->flags);
11961f351d7fSJohannes Weiner 	rq_pin_lock(rq, rf);
11971f351d7fSJohannes Weiner }
11981f351d7fSJohannes Weiner 
11991f351d7fSJohannes Weiner static inline void
12001f351d7fSJohannes Weiner rq_lock_irq(struct rq *rq, struct rq_flags *rf)
12011f351d7fSJohannes Weiner 	__acquires(rq->lock)
12021f351d7fSJohannes Weiner {
12031f351d7fSJohannes Weiner 	raw_spin_lock_irq(&rq->lock);
12041f351d7fSJohannes Weiner 	rq_pin_lock(rq, rf);
12051f351d7fSJohannes Weiner }
12061f351d7fSJohannes Weiner 
12071f351d7fSJohannes Weiner static inline void
12081f351d7fSJohannes Weiner rq_lock(struct rq *rq, struct rq_flags *rf)
12091f351d7fSJohannes Weiner 	__acquires(rq->lock)
12101f351d7fSJohannes Weiner {
12111f351d7fSJohannes Weiner 	raw_spin_lock(&rq->lock);
12121f351d7fSJohannes Weiner 	rq_pin_lock(rq, rf);
12131f351d7fSJohannes Weiner }
12141f351d7fSJohannes Weiner 
12151f351d7fSJohannes Weiner static inline void
12161f351d7fSJohannes Weiner rq_relock(struct rq *rq, struct rq_flags *rf)
12171f351d7fSJohannes Weiner 	__acquires(rq->lock)
12181f351d7fSJohannes Weiner {
12191f351d7fSJohannes Weiner 	raw_spin_lock(&rq->lock);
12201f351d7fSJohannes Weiner 	rq_repin_lock(rq, rf);
12211f351d7fSJohannes Weiner }
12221f351d7fSJohannes Weiner 
12231f351d7fSJohannes Weiner static inline void
12241f351d7fSJohannes Weiner rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
12251f351d7fSJohannes Weiner 	__releases(rq->lock)
12261f351d7fSJohannes Weiner {
12271f351d7fSJohannes Weiner 	rq_unpin_lock(rq, rf);
12281f351d7fSJohannes Weiner 	raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
12291f351d7fSJohannes Weiner }
12301f351d7fSJohannes Weiner 
12311f351d7fSJohannes Weiner static inline void
12321f351d7fSJohannes Weiner rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
12331f351d7fSJohannes Weiner 	__releases(rq->lock)
12341f351d7fSJohannes Weiner {
12351f351d7fSJohannes Weiner 	rq_unpin_lock(rq, rf);
12361f351d7fSJohannes Weiner 	raw_spin_unlock_irq(&rq->lock);
12371f351d7fSJohannes Weiner }
12381f351d7fSJohannes Weiner 
12391f351d7fSJohannes Weiner static inline void
12401f351d7fSJohannes Weiner rq_unlock(struct rq *rq, struct rq_flags *rf)
12411f351d7fSJohannes Weiner 	__releases(rq->lock)
12421f351d7fSJohannes Weiner {
12431f351d7fSJohannes Weiner 	rq_unpin_lock(rq, rf);
12441f351d7fSJohannes Weiner 	raw_spin_unlock(&rq->lock);
12451f351d7fSJohannes Weiner }
12461f351d7fSJohannes Weiner 
1247246b3b33SJohannes Weiner static inline struct rq *
1248246b3b33SJohannes Weiner this_rq_lock_irq(struct rq_flags *rf)
1249246b3b33SJohannes Weiner 	__acquires(rq->lock)
1250246b3b33SJohannes Weiner {
1251246b3b33SJohannes Weiner 	struct rq *rq;
1252246b3b33SJohannes Weiner 
1253246b3b33SJohannes Weiner 	local_irq_disable();
1254246b3b33SJohannes Weiner 	rq = this_rq();
1255246b3b33SJohannes Weiner 	rq_lock(rq, rf);
1256246b3b33SJohannes Weiner 	return rq;
1257246b3b33SJohannes Weiner }
1258246b3b33SJohannes Weiner 
12599942f79bSRik van Riel #ifdef CONFIG_NUMA
1260e3fe70b1SRik van Riel enum numa_topology_type {
1261e3fe70b1SRik van Riel 	NUMA_DIRECT,
1262e3fe70b1SRik van Riel 	NUMA_GLUELESS_MESH,
1263e3fe70b1SRik van Riel 	NUMA_BACKPLANE,
1264e3fe70b1SRik van Riel };
1265e3fe70b1SRik van Riel extern enum numa_topology_type sched_numa_topology_type;
12669942f79bSRik van Riel extern int sched_max_numa_distance;
12679942f79bSRik van Riel extern bool find_numa_distance(int distance);
1268f2cb1360SIngo Molnar extern void sched_init_numa(void);
1269f2cb1360SIngo Molnar extern void sched_domains_numa_masks_set(unsigned int cpu);
1270f2cb1360SIngo Molnar extern void sched_domains_numa_masks_clear(unsigned int cpu);
1271e0e8d491SWanpeng Li extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1272f2cb1360SIngo Molnar #else
1273f2cb1360SIngo Molnar static inline void sched_init_numa(void) { }
1274f2cb1360SIngo Molnar static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
1275f2cb1360SIngo Molnar static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
1276e0e8d491SWanpeng Li static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1277e0e8d491SWanpeng Li {
1278e0e8d491SWanpeng Li 	return nr_cpu_ids;
1279e0e8d491SWanpeng Li }
1280f2cb1360SIngo Molnar #endif
1281f2cb1360SIngo Molnar 
1282f809ca9aSMel Gorman #ifdef CONFIG_NUMA_BALANCING
128344dba3d5SIulia Manda /* The regions in numa_faults array from task_struct */
128444dba3d5SIulia Manda enum numa_faults_stats {
128544dba3d5SIulia Manda 	NUMA_MEM = 0,
128644dba3d5SIulia Manda 	NUMA_CPU,
128744dba3d5SIulia Manda 	NUMA_MEMBUF,
128844dba3d5SIulia Manda 	NUMA_CPUBUF
128944dba3d5SIulia Manda };
12900ec8aa00SPeter Zijlstra extern void sched_setnuma(struct task_struct *p, int node);
1291e6628d5bSMel Gorman extern int migrate_task_to(struct task_struct *p, int cpu);
12920ad4e3dfSSrikar Dronamraju extern int migrate_swap(struct task_struct *p, struct task_struct *t,
12930ad4e3dfSSrikar Dronamraju 			int cpu, int scpu);
129413784475SMel Gorman extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
129513784475SMel Gorman #else
129613784475SMel Gorman static inline void
129713784475SMel Gorman init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
129813784475SMel Gorman {
129913784475SMel Gorman }
1300f809ca9aSMel Gorman #endif /* CONFIG_NUMA_BALANCING */
1301f809ca9aSMel Gorman 
1302518cd623SPeter Zijlstra #ifdef CONFIG_SMP
1303518cd623SPeter Zijlstra 
1304e3fca9e7SPeter Zijlstra static inline void
1305e3fca9e7SPeter Zijlstra queue_balance_callback(struct rq *rq,
1306e3fca9e7SPeter Zijlstra 		       struct callback_head *head,
1307e3fca9e7SPeter Zijlstra 		       void (*func)(struct rq *rq))
1308e3fca9e7SPeter Zijlstra {
1309e3fca9e7SPeter Zijlstra 	lockdep_assert_held(&rq->lock);
1310e3fca9e7SPeter Zijlstra 
1311e3fca9e7SPeter Zijlstra 	if (unlikely(head->next))
1312e3fca9e7SPeter Zijlstra 		return;
1313e3fca9e7SPeter Zijlstra 
1314e3fca9e7SPeter Zijlstra 	head->func = (void (*)(struct callback_head *))func;
1315e3fca9e7SPeter Zijlstra 	head->next = rq->balance_callback;
1316e3fca9e7SPeter Zijlstra 	rq->balance_callback = head;
1317e3fca9e7SPeter Zijlstra }
1318e3fca9e7SPeter Zijlstra 
1319e3baac47SPeter Zijlstra extern void sched_ttwu_pending(void);
1320e3baac47SPeter Zijlstra 
1321391e43daSPeter Zijlstra #define rcu_dereference_check_sched_domain(p) \
1322391e43daSPeter Zijlstra 	rcu_dereference_check((p), \
1323391e43daSPeter Zijlstra 			      lockdep_is_held(&sched_domains_mutex))
1324391e43daSPeter Zijlstra 
1325391e43daSPeter Zijlstra /*
1326391e43daSPeter Zijlstra  * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1327337e9b07SPaul E. McKenney  * See destroy_sched_domains: call_rcu for details.
1328391e43daSPeter Zijlstra  *
1329391e43daSPeter Zijlstra  * The domain tree of any CPU may only be accessed from within
1330391e43daSPeter Zijlstra  * preempt-disabled sections.
1331391e43daSPeter Zijlstra  */
1332391e43daSPeter Zijlstra #define for_each_domain(cpu, __sd) \
1333518cd623SPeter Zijlstra 	for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1334518cd623SPeter Zijlstra 			__sd; __sd = __sd->parent)
1335391e43daSPeter Zijlstra 
133677e81365SSuresh Siddha #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
133777e81365SSuresh Siddha 
1338518cd623SPeter Zijlstra /**
1339518cd623SPeter Zijlstra  * highest_flag_domain - Return highest sched_domain containing flag.
134097fb7a0aSIngo Molnar  * @cpu:	The CPU whose highest level of sched domain is to
1341518cd623SPeter Zijlstra  *		be returned.
1342518cd623SPeter Zijlstra  * @flag:	The flag to check for the highest sched_domain
134397fb7a0aSIngo Molnar  *		for the given CPU.
1344518cd623SPeter Zijlstra  *
134597fb7a0aSIngo Molnar  * Returns the highest sched_domain of a CPU which contains the given flag.
1346518cd623SPeter Zijlstra  */
1347518cd623SPeter Zijlstra static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1348518cd623SPeter Zijlstra {
1349518cd623SPeter Zijlstra 	struct sched_domain *sd, *hsd = NULL;
1350518cd623SPeter Zijlstra 
1351518cd623SPeter Zijlstra 	for_each_domain(cpu, sd) {
1352518cd623SPeter Zijlstra 		if (!(sd->flags & flag))
1353518cd623SPeter Zijlstra 			break;
1354518cd623SPeter Zijlstra 		hsd = sd;
1355518cd623SPeter Zijlstra 	}
1356518cd623SPeter Zijlstra 
1357518cd623SPeter Zijlstra 	return hsd;
1358518cd623SPeter Zijlstra }
1359518cd623SPeter Zijlstra 
1360fb13c7eeSMel Gorman static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1361fb13c7eeSMel Gorman {
1362fb13c7eeSMel Gorman 	struct sched_domain *sd;
1363fb13c7eeSMel Gorman 
1364fb13c7eeSMel Gorman 	for_each_domain(cpu, sd) {
1365fb13c7eeSMel Gorman 		if (sd->flags & flag)
1366fb13c7eeSMel Gorman 			break;
1367fb13c7eeSMel Gorman 	}
1368fb13c7eeSMel Gorman 
1369fb13c7eeSMel Gorman 	return sd;
1370fb13c7eeSMel Gorman }
1371fb13c7eeSMel Gorman 
1372994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
13737d9ffa89SPeter Zijlstra DECLARE_PER_CPU(int, sd_llc_size);
1374518cd623SPeter Zijlstra DECLARE_PER_CPU(int, sd_llc_id);
1375994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
1376994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
1377994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
1378994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
1379df054e84SMorten Rasmussen extern struct static_key_false sched_asym_cpucapacity;
1380518cd623SPeter Zijlstra 
138163b2ca30SNicolas Pitre struct sched_group_capacity {
13825e6521eaSLi Zefan 	atomic_t		ref;
13835e6521eaSLi Zefan 	/*
1384172895e6SYuyang Du 	 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
138563b2ca30SNicolas Pitre 	 * for a single CPU.
13865e6521eaSLi Zefan 	 */
1387bf475ce0SMorten Rasmussen 	unsigned long		capacity;
1388bf475ce0SMorten Rasmussen 	unsigned long		min_capacity;		/* Min per-CPU capacity in group */
1389e3d6d0cbSMorten Rasmussen 	unsigned long		max_capacity;		/* Max per-CPU capacity in group */
13905e6521eaSLi Zefan 	unsigned long		next_update;
139163b2ca30SNicolas Pitre 	int			imbalance;		/* XXX unrelated to capacity but shared group state */
13925e6521eaSLi Zefan 
1393005f874dSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
1394005f874dSPeter Zijlstra 	int			id;
1395005f874dSPeter Zijlstra #endif
1396005f874dSPeter Zijlstra 
139797fb7a0aSIngo Molnar 	unsigned long		cpumask[0];		/* Balance mask */
13985e6521eaSLi Zefan };
13995e6521eaSLi Zefan 
14005e6521eaSLi Zefan struct sched_group {
14015e6521eaSLi Zefan 	struct sched_group	*next;			/* Must be a circular list */
14025e6521eaSLi Zefan 	atomic_t		ref;
14035e6521eaSLi Zefan 
14045e6521eaSLi Zefan 	unsigned int		group_weight;
140563b2ca30SNicolas Pitre 	struct sched_group_capacity *sgc;
140697fb7a0aSIngo Molnar 	int			asym_prefer_cpu;	/* CPU of highest priority in group */
14075e6521eaSLi Zefan 
14085e6521eaSLi Zefan 	/*
14095e6521eaSLi Zefan 	 * The CPUs this group covers.
14105e6521eaSLi Zefan 	 *
14115e6521eaSLi Zefan 	 * NOTE: this field is variable length. (Allocated dynamically
14125e6521eaSLi Zefan 	 * by attaching extra space to the end of the structure,
14135e6521eaSLi Zefan 	 * depending on how many CPUs the kernel has booted up with)
14145e6521eaSLi Zefan 	 */
14155e6521eaSLi Zefan 	unsigned long		cpumask[0];
14165e6521eaSLi Zefan };
14175e6521eaSLi Zefan 
1418ae4df9d6SPeter Zijlstra static inline struct cpumask *sched_group_span(struct sched_group *sg)
14195e6521eaSLi Zefan {
14205e6521eaSLi Zefan 	return to_cpumask(sg->cpumask);
14215e6521eaSLi Zefan }
14225e6521eaSLi Zefan 
14235e6521eaSLi Zefan /*
1424e5c14b1fSPeter Zijlstra  * See build_balance_mask().
14255e6521eaSLi Zefan  */
1426e5c14b1fSPeter Zijlstra static inline struct cpumask *group_balance_mask(struct sched_group *sg)
14275e6521eaSLi Zefan {
142863b2ca30SNicolas Pitre 	return to_cpumask(sg->sgc->cpumask);
14295e6521eaSLi Zefan }
14305e6521eaSLi Zefan 
14315e6521eaSLi Zefan /**
143297fb7a0aSIngo Molnar  * group_first_cpu - Returns the first CPU in the cpumask of a sched_group.
143397fb7a0aSIngo Molnar  * @group: The group whose first CPU is to be returned.
14345e6521eaSLi Zefan  */
14355e6521eaSLi Zefan static inline unsigned int group_first_cpu(struct sched_group *group)
14365e6521eaSLi Zefan {
1437ae4df9d6SPeter Zijlstra 	return cpumask_first(sched_group_span(group));
14385e6521eaSLi Zefan }
14395e6521eaSLi Zefan 
1440c1174876SPeter Zijlstra extern int group_balance_cpu(struct sched_group *sg);
1441c1174876SPeter Zijlstra 
14423866e845SSteven Rostedt (Red Hat) #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
14433866e845SSteven Rostedt (Red Hat) void register_sched_domain_sysctl(void);
1444bbdacdfeSPeter Zijlstra void dirty_sched_domain_sysctl(int cpu);
14453866e845SSteven Rostedt (Red Hat) void unregister_sched_domain_sysctl(void);
14463866e845SSteven Rostedt (Red Hat) #else
14473866e845SSteven Rostedt (Red Hat) static inline void register_sched_domain_sysctl(void)
14483866e845SSteven Rostedt (Red Hat) {
14493866e845SSteven Rostedt (Red Hat) }
1450bbdacdfeSPeter Zijlstra static inline void dirty_sched_domain_sysctl(int cpu)
1451bbdacdfeSPeter Zijlstra {
1452bbdacdfeSPeter Zijlstra }
14533866e845SSteven Rostedt (Red Hat) static inline void unregister_sched_domain_sysctl(void)
14543866e845SSteven Rostedt (Red Hat) {
14553866e845SSteven Rostedt (Red Hat) }
14563866e845SSteven Rostedt (Red Hat) #endif
14573866e845SSteven Rostedt (Red Hat) 
14585ba553efSPeter Zijlstra extern int newidle_balance(struct rq *this_rq, struct rq_flags *rf);
14595ba553efSPeter Zijlstra 
1460e3baac47SPeter Zijlstra #else
1461e3baac47SPeter Zijlstra 
1462e3baac47SPeter Zijlstra static inline void sched_ttwu_pending(void) { }
1463e3baac47SPeter Zijlstra 
14645ba553efSPeter Zijlstra static inline int newidle_balance(struct rq *this_rq, struct rq_flags *rf) { return 0; }
14655ba553efSPeter Zijlstra 
1466518cd623SPeter Zijlstra #endif /* CONFIG_SMP */
1467391e43daSPeter Zijlstra 
1468391e43daSPeter Zijlstra #include "stats.h"
14691051408fSIngo Molnar #include "autogroup.h"
1470391e43daSPeter Zijlstra 
1471391e43daSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED
1472391e43daSPeter Zijlstra 
1473391e43daSPeter Zijlstra /*
1474391e43daSPeter Zijlstra  * Return the group to which this tasks belongs.
1475391e43daSPeter Zijlstra  *
14768af01f56STejun Heo  * We cannot use task_css() and friends because the cgroup subsystem
14778af01f56STejun Heo  * changes that value before the cgroup_subsys::attach() method is called,
14788af01f56STejun Heo  * therefore we cannot pin it and might observe the wrong value.
14798323f26cSPeter Zijlstra  *
14808323f26cSPeter Zijlstra  * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
14818323f26cSPeter Zijlstra  * core changes this before calling sched_move_task().
14828323f26cSPeter Zijlstra  *
14838323f26cSPeter Zijlstra  * Instead we use a 'copy' which is updated from sched_move_task() while
14848323f26cSPeter Zijlstra  * holding both task_struct::pi_lock and rq::lock.
1485391e43daSPeter Zijlstra  */
1486391e43daSPeter Zijlstra static inline struct task_group *task_group(struct task_struct *p)
1487391e43daSPeter Zijlstra {
14888323f26cSPeter Zijlstra 	return p->sched_task_group;
1489391e43daSPeter Zijlstra }
1490391e43daSPeter Zijlstra 
1491391e43daSPeter Zijlstra /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1492391e43daSPeter Zijlstra static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1493391e43daSPeter Zijlstra {
1494391e43daSPeter Zijlstra #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1495391e43daSPeter Zijlstra 	struct task_group *tg = task_group(p);
1496391e43daSPeter Zijlstra #endif
1497391e43daSPeter Zijlstra 
1498391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED
1499ad936d86SByungchul Park 	set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1500391e43daSPeter Zijlstra 	p->se.cfs_rq = tg->cfs_rq[cpu];
1501391e43daSPeter Zijlstra 	p->se.parent = tg->se[cpu];
1502391e43daSPeter Zijlstra #endif
1503391e43daSPeter Zijlstra 
1504391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
1505391e43daSPeter Zijlstra 	p->rt.rt_rq  = tg->rt_rq[cpu];
1506391e43daSPeter Zijlstra 	p->rt.parent = tg->rt_se[cpu];
1507391e43daSPeter Zijlstra #endif
1508391e43daSPeter Zijlstra }
1509391e43daSPeter Zijlstra 
1510391e43daSPeter Zijlstra #else /* CONFIG_CGROUP_SCHED */
1511391e43daSPeter Zijlstra 
1512391e43daSPeter Zijlstra static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1513391e43daSPeter Zijlstra static inline struct task_group *task_group(struct task_struct *p)
1514391e43daSPeter Zijlstra {
1515391e43daSPeter Zijlstra 	return NULL;
1516391e43daSPeter Zijlstra }
1517391e43daSPeter Zijlstra 
1518391e43daSPeter Zijlstra #endif /* CONFIG_CGROUP_SCHED */
1519391e43daSPeter Zijlstra 
1520391e43daSPeter Zijlstra static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1521391e43daSPeter Zijlstra {
1522391e43daSPeter Zijlstra 	set_task_rq(p, cpu);
1523391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1524391e43daSPeter Zijlstra 	/*
1525391e43daSPeter Zijlstra 	 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1526dfcb245eSIngo Molnar 	 * successfully executed on another CPU. We must ensure that updates of
1527391e43daSPeter Zijlstra 	 * per-task data have been completed by this moment.
1528391e43daSPeter Zijlstra 	 */
1529391e43daSPeter Zijlstra 	smp_wmb();
1530c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1531c546951dSAndrea Parri 	WRITE_ONCE(p->cpu, cpu);
1532c65eacbeSAndy Lutomirski #else
1533c546951dSAndrea Parri 	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
1534c65eacbeSAndy Lutomirski #endif
1535ac66f547SPeter Zijlstra 	p->wake_cpu = cpu;
1536391e43daSPeter Zijlstra #endif
1537391e43daSPeter Zijlstra }
1538391e43daSPeter Zijlstra 
1539391e43daSPeter Zijlstra /*
1540391e43daSPeter Zijlstra  * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1541391e43daSPeter Zijlstra  */
1542391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
1543c5905afbSIngo Molnar # include <linux/static_key.h>
1544391e43daSPeter Zijlstra # define const_debug __read_mostly
1545391e43daSPeter Zijlstra #else
1546391e43daSPeter Zijlstra # define const_debug const
1547391e43daSPeter Zijlstra #endif
1548391e43daSPeter Zijlstra 
1549391e43daSPeter Zijlstra #define SCHED_FEAT(name, enabled)	\
1550391e43daSPeter Zijlstra 	__SCHED_FEAT_##name ,
1551391e43daSPeter Zijlstra 
1552391e43daSPeter Zijlstra enum {
1553391e43daSPeter Zijlstra #include "features.h"
1554f8b6d1ccSPeter Zijlstra 	__SCHED_FEAT_NR,
1555391e43daSPeter Zijlstra };
1556391e43daSPeter Zijlstra 
1557391e43daSPeter Zijlstra #undef SCHED_FEAT
1558391e43daSPeter Zijlstra 
1559e9666d10SMasahiro Yamada #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_JUMP_LABEL)
1560765cc3a4SPatrick Bellasi 
1561765cc3a4SPatrick Bellasi /*
1562765cc3a4SPatrick Bellasi  * To support run-time toggling of sched features, all the translation units
1563765cc3a4SPatrick Bellasi  * (but core.c) reference the sysctl_sched_features defined in core.c.
1564765cc3a4SPatrick Bellasi  */
1565765cc3a4SPatrick Bellasi extern const_debug unsigned int sysctl_sched_features;
1566765cc3a4SPatrick Bellasi 
1567f8b6d1ccSPeter Zijlstra #define SCHED_FEAT(name, enabled)					\
1568c5905afbSIngo Molnar static __always_inline bool static_branch_##name(struct static_key *key) \
1569f8b6d1ccSPeter Zijlstra {									\
15706e76ea8aSJason Baron 	return static_key_##enabled(key);				\
1571f8b6d1ccSPeter Zijlstra }
1572f8b6d1ccSPeter Zijlstra 
1573f8b6d1ccSPeter Zijlstra #include "features.h"
1574f8b6d1ccSPeter Zijlstra #undef SCHED_FEAT
1575f8b6d1ccSPeter Zijlstra 
1576c5905afbSIngo Molnar extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1577f8b6d1ccSPeter Zijlstra #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1578765cc3a4SPatrick Bellasi 
1579e9666d10SMasahiro Yamada #else /* !(SCHED_DEBUG && CONFIG_JUMP_LABEL) */
1580765cc3a4SPatrick Bellasi 
1581765cc3a4SPatrick Bellasi /*
1582765cc3a4SPatrick Bellasi  * Each translation unit has its own copy of sysctl_sched_features to allow
1583765cc3a4SPatrick Bellasi  * constants propagation at compile time and compiler optimization based on
1584765cc3a4SPatrick Bellasi  * features default.
1585765cc3a4SPatrick Bellasi  */
1586765cc3a4SPatrick Bellasi #define SCHED_FEAT(name, enabled)	\
1587765cc3a4SPatrick Bellasi 	(1UL << __SCHED_FEAT_##name) * enabled |
1588765cc3a4SPatrick Bellasi static const_debug __maybe_unused unsigned int sysctl_sched_features =
1589765cc3a4SPatrick Bellasi #include "features.h"
1590765cc3a4SPatrick Bellasi 	0;
1591765cc3a4SPatrick Bellasi #undef SCHED_FEAT
1592765cc3a4SPatrick Bellasi 
15937e6f4c5dSPeter Zijlstra #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1594765cc3a4SPatrick Bellasi 
1595e9666d10SMasahiro Yamada #endif /* SCHED_DEBUG && CONFIG_JUMP_LABEL */
1596391e43daSPeter Zijlstra 
15972a595721SSrikar Dronamraju extern struct static_key_false sched_numa_balancing;
1598cb251765SMel Gorman extern struct static_key_false sched_schedstats;
1599cbee9f88SPeter Zijlstra 
1600391e43daSPeter Zijlstra static inline u64 global_rt_period(void)
1601391e43daSPeter Zijlstra {
1602391e43daSPeter Zijlstra 	return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1603391e43daSPeter Zijlstra }
1604391e43daSPeter Zijlstra 
1605391e43daSPeter Zijlstra static inline u64 global_rt_runtime(void)
1606391e43daSPeter Zijlstra {
1607391e43daSPeter Zijlstra 	if (sysctl_sched_rt_runtime < 0)
1608391e43daSPeter Zijlstra 		return RUNTIME_INF;
1609391e43daSPeter Zijlstra 
1610391e43daSPeter Zijlstra 	return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1611391e43daSPeter Zijlstra }
1612391e43daSPeter Zijlstra 
1613391e43daSPeter Zijlstra static inline int task_current(struct rq *rq, struct task_struct *p)
1614391e43daSPeter Zijlstra {
1615391e43daSPeter Zijlstra 	return rq->curr == p;
1616391e43daSPeter Zijlstra }
1617391e43daSPeter Zijlstra 
1618391e43daSPeter Zijlstra static inline int task_running(struct rq *rq, struct task_struct *p)
1619391e43daSPeter Zijlstra {
1620391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1621391e43daSPeter Zijlstra 	return p->on_cpu;
1622391e43daSPeter Zijlstra #else
1623391e43daSPeter Zijlstra 	return task_current(rq, p);
1624391e43daSPeter Zijlstra #endif
1625391e43daSPeter Zijlstra }
1626391e43daSPeter Zijlstra 
1627da0c1e65SKirill Tkhai static inline int task_on_rq_queued(struct task_struct *p)
1628da0c1e65SKirill Tkhai {
1629da0c1e65SKirill Tkhai 	return p->on_rq == TASK_ON_RQ_QUEUED;
1630da0c1e65SKirill Tkhai }
1631391e43daSPeter Zijlstra 
1632cca26e80SKirill Tkhai static inline int task_on_rq_migrating(struct task_struct *p)
1633cca26e80SKirill Tkhai {
1634c546951dSAndrea Parri 	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
1635cca26e80SKirill Tkhai }
1636cca26e80SKirill Tkhai 
1637b13095f0SLi Zefan /*
1638b13095f0SLi Zefan  * wake flags
1639b13095f0SLi Zefan  */
164097fb7a0aSIngo Molnar #define WF_SYNC			0x01		/* Waker goes to sleep after wakeup */
164197fb7a0aSIngo Molnar #define WF_FORK			0x02		/* Child wakeup after fork */
164297fb7a0aSIngo Molnar #define WF_MIGRATED		0x4		/* Internal use, task got migrated */
1643b13095f0SLi Zefan 
1644391e43daSPeter Zijlstra /*
1645391e43daSPeter Zijlstra  * To aid in avoiding the subversion of "niceness" due to uneven distribution
1646391e43daSPeter Zijlstra  * of tasks with abnormal "nice" values across CPUs the contribution that
1647391e43daSPeter Zijlstra  * each task makes to its run queue's load is weighted according to its
1648391e43daSPeter Zijlstra  * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1649391e43daSPeter Zijlstra  * scaled version of the new time slice allocation that they receive on time
1650391e43daSPeter Zijlstra  * slice expiry etc.
1651391e43daSPeter Zijlstra  */
1652391e43daSPeter Zijlstra 
1653391e43daSPeter Zijlstra #define WEIGHT_IDLEPRIO		3
1654391e43daSPeter Zijlstra #define WMULT_IDLEPRIO		1431655765
1655391e43daSPeter Zijlstra 
1656ed82b8a1SAndi Kleen extern const int		sched_prio_to_weight[40];
1657ed82b8a1SAndi Kleen extern const u32		sched_prio_to_wmult[40];
1658391e43daSPeter Zijlstra 
1659ff77e468SPeter Zijlstra /*
1660ff77e468SPeter Zijlstra  * {de,en}queue flags:
1661ff77e468SPeter Zijlstra  *
1662ff77e468SPeter Zijlstra  * DEQUEUE_SLEEP  - task is no longer runnable
1663ff77e468SPeter Zijlstra  * ENQUEUE_WAKEUP - task just became runnable
1664ff77e468SPeter Zijlstra  *
1665ff77e468SPeter Zijlstra  * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1666ff77e468SPeter Zijlstra  *                are in a known state which allows modification. Such pairs
1667ff77e468SPeter Zijlstra  *                should preserve as much state as possible.
1668ff77e468SPeter Zijlstra  *
1669ff77e468SPeter Zijlstra  * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1670ff77e468SPeter Zijlstra  *        in the runqueue.
1671ff77e468SPeter Zijlstra  *
1672ff77e468SPeter Zijlstra  * ENQUEUE_HEAD      - place at front of runqueue (tail if not specified)
1673ff77e468SPeter Zijlstra  * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
167459efa0baSPeter Zijlstra  * ENQUEUE_MIGRATED  - the task was migrated during wakeup
1675ff77e468SPeter Zijlstra  *
1676ff77e468SPeter Zijlstra  */
1677ff77e468SPeter Zijlstra 
1678ff77e468SPeter Zijlstra #define DEQUEUE_SLEEP		0x01
167997fb7a0aSIngo Molnar #define DEQUEUE_SAVE		0x02 /* Matches ENQUEUE_RESTORE */
168097fb7a0aSIngo Molnar #define DEQUEUE_MOVE		0x04 /* Matches ENQUEUE_MOVE */
168197fb7a0aSIngo Molnar #define DEQUEUE_NOCLOCK		0x08 /* Matches ENQUEUE_NOCLOCK */
1682ff77e468SPeter Zijlstra 
16831de64443SPeter Zijlstra #define ENQUEUE_WAKEUP		0x01
1684ff77e468SPeter Zijlstra #define ENQUEUE_RESTORE		0x02
1685ff77e468SPeter Zijlstra #define ENQUEUE_MOVE		0x04
16860a67d1eeSPeter Zijlstra #define ENQUEUE_NOCLOCK		0x08
1687ff77e468SPeter Zijlstra 
16880a67d1eeSPeter Zijlstra #define ENQUEUE_HEAD		0x10
16890a67d1eeSPeter Zijlstra #define ENQUEUE_REPLENISH	0x20
1690c82ba9faSLi Zefan #ifdef CONFIG_SMP
16910a67d1eeSPeter Zijlstra #define ENQUEUE_MIGRATED	0x40
1692c82ba9faSLi Zefan #else
169359efa0baSPeter Zijlstra #define ENQUEUE_MIGRATED	0x00
1694c82ba9faSLi Zefan #endif
1695c82ba9faSLi Zefan 
169637e117c0SPeter Zijlstra #define RETRY_TASK		((void *)-1UL)
169737e117c0SPeter Zijlstra 
1698c82ba9faSLi Zefan struct sched_class {
1699c82ba9faSLi Zefan 	const struct sched_class *next;
1700c82ba9faSLi Zefan 
170169842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
170269842cbaSPatrick Bellasi 	int uclamp_enabled;
170369842cbaSPatrick Bellasi #endif
170469842cbaSPatrick Bellasi 
1705c82ba9faSLi Zefan 	void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1706c82ba9faSLi Zefan 	void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1707c82ba9faSLi Zefan 	void (*yield_task)   (struct rq *rq);
1708c82ba9faSLi Zefan 	bool (*yield_to_task)(struct rq *rq, struct task_struct *p, bool preempt);
1709c82ba9faSLi Zefan 
1710c82ba9faSLi Zefan 	void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags);
1711c82ba9faSLi Zefan 
1712606dba2eSPeter Zijlstra 	/*
171367692435SPeter Zijlstra 	 * Both @prev and @rf are optional and may be NULL, in which case the
171467692435SPeter Zijlstra 	 * caller must already have invoked put_prev_task(rq, prev, rf).
171537e117c0SPeter Zijlstra 	 *
171667692435SPeter Zijlstra 	 * Otherwise it is the responsibility of the pick_next_task() to call
171767692435SPeter Zijlstra 	 * put_prev_task() on the @prev task or something equivalent, IFF it
171867692435SPeter Zijlstra 	 * returns a next task.
171967692435SPeter Zijlstra 	 *
172067692435SPeter Zijlstra 	 * In that case (@rf != NULL) it may return RETRY_TASK when it finds a
172167692435SPeter Zijlstra 	 * higher prio class has runnable tasks.
1722606dba2eSPeter Zijlstra 	 */
1723606dba2eSPeter Zijlstra 	struct task_struct * (*pick_next_task)(struct rq *rq,
1724e7904a28SPeter Zijlstra 					       struct task_struct *prev,
1725d8ac8971SMatt Fleming 					       struct rq_flags *rf);
17265f2a45fcSPeter Zijlstra 	void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct rq_flags *rf);
172703b7fad1SPeter Zijlstra 	void (*set_next_task)(struct rq *rq, struct task_struct *p);
1728c82ba9faSLi Zefan 
1729c82ba9faSLi Zefan #ifdef CONFIG_SMP
1730ac66f547SPeter Zijlstra 	int  (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
17311327237aSSrikar Dronamraju 	void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
1732c82ba9faSLi Zefan 
1733c82ba9faSLi Zefan 	void (*task_woken)(struct rq *this_rq, struct task_struct *task);
1734c82ba9faSLi Zefan 
1735c82ba9faSLi Zefan 	void (*set_cpus_allowed)(struct task_struct *p,
1736c82ba9faSLi Zefan 				 const struct cpumask *newmask);
1737c82ba9faSLi Zefan 
1738c82ba9faSLi Zefan 	void (*rq_online)(struct rq *rq);
1739c82ba9faSLi Zefan 	void (*rq_offline)(struct rq *rq);
1740c82ba9faSLi Zefan #endif
1741c82ba9faSLi Zefan 
1742c82ba9faSLi Zefan 	void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
1743c82ba9faSLi Zefan 	void (*task_fork)(struct task_struct *p);
1744e6c390f2SDario Faggioli 	void (*task_dead)(struct task_struct *p);
1745c82ba9faSLi Zefan 
174667dfa1b7SKirill Tkhai 	/*
174767dfa1b7SKirill Tkhai 	 * The switched_from() call is allowed to drop rq->lock, therefore we
174867dfa1b7SKirill Tkhai 	 * cannot assume the switched_from/switched_to pair is serliazed by
174967dfa1b7SKirill Tkhai 	 * rq->lock. They are however serialized by p->pi_lock.
175067dfa1b7SKirill Tkhai 	 */
1751c82ba9faSLi Zefan 	void (*switched_from)(struct rq *this_rq, struct task_struct *task);
1752c82ba9faSLi Zefan 	void (*switched_to)  (struct rq *this_rq, struct task_struct *task);
1753c82ba9faSLi Zefan 	void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1754c82ba9faSLi Zefan 			      int oldprio);
1755c82ba9faSLi Zefan 
1756c82ba9faSLi Zefan 	unsigned int (*get_rr_interval)(struct rq *rq,
1757c82ba9faSLi Zefan 					struct task_struct *task);
1758c82ba9faSLi Zefan 
17596e998916SStanislaw Gruszka 	void (*update_curr)(struct rq *rq);
17606e998916SStanislaw Gruszka 
1761ea86cb4bSVincent Guittot #define TASK_SET_GROUP		0
1762ea86cb4bSVincent Guittot #define TASK_MOVE_GROUP		1
1763ea86cb4bSVincent Guittot 
1764c82ba9faSLi Zefan #ifdef CONFIG_FAIR_GROUP_SCHED
1765ea86cb4bSVincent Guittot 	void (*task_change_group)(struct task_struct *p, int type);
1766c82ba9faSLi Zefan #endif
1767c82ba9faSLi Zefan };
1768391e43daSPeter Zijlstra 
17693f1d2a31SPeter Zijlstra static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
17703f1d2a31SPeter Zijlstra {
177110e7071bSPeter Zijlstra 	WARN_ON_ONCE(rq->curr != prev);
17725f2a45fcSPeter Zijlstra 	prev->sched_class->put_prev_task(rq, prev, NULL);
17733f1d2a31SPeter Zijlstra }
17743f1d2a31SPeter Zijlstra 
177503b7fad1SPeter Zijlstra static inline void set_next_task(struct rq *rq, struct task_struct *next)
1776b2bf6c31SPeter Zijlstra {
177703b7fad1SPeter Zijlstra 	WARN_ON_ONCE(rq->curr != next);
177803b7fad1SPeter Zijlstra 	next->sched_class->set_next_task(rq, next);
1779b2bf6c31SPeter Zijlstra }
1780b2bf6c31SPeter Zijlstra 
1781f5832c19SNicolas Pitre #ifdef CONFIG_SMP
1782391e43daSPeter Zijlstra #define sched_class_highest (&stop_sched_class)
1783f5832c19SNicolas Pitre #else
1784f5832c19SNicolas Pitre #define sched_class_highest (&dl_sched_class)
1785f5832c19SNicolas Pitre #endif
1786391e43daSPeter Zijlstra #define for_each_class(class) \
1787391e43daSPeter Zijlstra    for (class = sched_class_highest; class; class = class->next)
1788391e43daSPeter Zijlstra 
1789391e43daSPeter Zijlstra extern const struct sched_class stop_sched_class;
1790aab03e05SDario Faggioli extern const struct sched_class dl_sched_class;
1791391e43daSPeter Zijlstra extern const struct sched_class rt_sched_class;
1792391e43daSPeter Zijlstra extern const struct sched_class fair_sched_class;
1793391e43daSPeter Zijlstra extern const struct sched_class idle_sched_class;
1794391e43daSPeter Zijlstra 
1795391e43daSPeter Zijlstra 
1796391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1797391e43daSPeter Zijlstra 
179863b2ca30SNicolas Pitre extern void update_group_capacity(struct sched_domain *sd, int cpu);
1799b719203bSLi Zefan 
18007caff66fSDaniel Lezcano extern void trigger_load_balance(struct rq *rq);
1801391e43daSPeter Zijlstra 
1802c5b28038SPeter Zijlstra extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1803c5b28038SPeter Zijlstra 
1804391e43daSPeter Zijlstra #endif
1805391e43daSPeter Zijlstra 
1806442bf3aaSDaniel Lezcano #ifdef CONFIG_CPU_IDLE
1807442bf3aaSDaniel Lezcano static inline void idle_set_state(struct rq *rq,
1808442bf3aaSDaniel Lezcano 				  struct cpuidle_state *idle_state)
1809442bf3aaSDaniel Lezcano {
1810442bf3aaSDaniel Lezcano 	rq->idle_state = idle_state;
1811442bf3aaSDaniel Lezcano }
1812442bf3aaSDaniel Lezcano 
1813442bf3aaSDaniel Lezcano static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1814442bf3aaSDaniel Lezcano {
18159148a3a1SPeter Zijlstra 	SCHED_WARN_ON(!rcu_read_lock_held());
181697fb7a0aSIngo Molnar 
1817442bf3aaSDaniel Lezcano 	return rq->idle_state;
1818442bf3aaSDaniel Lezcano }
1819442bf3aaSDaniel Lezcano #else
1820442bf3aaSDaniel Lezcano static inline void idle_set_state(struct rq *rq,
1821442bf3aaSDaniel Lezcano 				  struct cpuidle_state *idle_state)
1822442bf3aaSDaniel Lezcano {
1823442bf3aaSDaniel Lezcano }
1824442bf3aaSDaniel Lezcano 
1825442bf3aaSDaniel Lezcano static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1826442bf3aaSDaniel Lezcano {
1827442bf3aaSDaniel Lezcano 	return NULL;
1828442bf3aaSDaniel Lezcano }
1829442bf3aaSDaniel Lezcano #endif
1830442bf3aaSDaniel Lezcano 
18318663effbSSteven Rostedt (VMware) extern void schedule_idle(void);
18328663effbSSteven Rostedt (VMware) 
1833391e43daSPeter Zijlstra extern void sysrq_sched_debug_show(void);
1834391e43daSPeter Zijlstra extern void sched_init_granularity(void);
1835391e43daSPeter Zijlstra extern void update_max_interval(void);
18361baca4ceSJuri Lelli 
18371baca4ceSJuri Lelli extern void init_sched_dl_class(void);
1838391e43daSPeter Zijlstra extern void init_sched_rt_class(void);
1839391e43daSPeter Zijlstra extern void init_sched_fair_class(void);
1840391e43daSPeter Zijlstra 
18419059393eSVincent Guittot extern void reweight_task(struct task_struct *p, int prio);
18429059393eSVincent Guittot 
18438875125eSKirill Tkhai extern void resched_curr(struct rq *rq);
1844391e43daSPeter Zijlstra extern void resched_cpu(int cpu);
1845391e43daSPeter Zijlstra 
1846391e43daSPeter Zijlstra extern struct rt_bandwidth def_rt_bandwidth;
1847391e43daSPeter Zijlstra extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1848391e43daSPeter Zijlstra 
1849332ac17eSDario Faggioli extern struct dl_bandwidth def_dl_bandwidth;
1850332ac17eSDario Faggioli extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1851aab03e05SDario Faggioli extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1852209a0cbdSLuca Abeni extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
18534da3abceSLuca Abeni extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
1854aab03e05SDario Faggioli 
1855c52f14d3SLuca Abeni #define BW_SHIFT		20
1856c52f14d3SLuca Abeni #define BW_UNIT			(1 << BW_SHIFT)
18574da3abceSLuca Abeni #define RATIO_SHIFT		8
1858332ac17eSDario Faggioli unsigned long to_ratio(u64 period, u64 runtime);
1859332ac17eSDario Faggioli 
1860540247fbSYuyang Du extern void init_entity_runnable_average(struct sched_entity *se);
1861d0fe0b9cSDietmar Eggemann extern void post_init_entity_util_avg(struct task_struct *p);
1862a75cdaa9SAlex Shi 
186376d92ac3SFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
186476d92ac3SFrederic Weisbecker extern bool sched_can_stop_tick(struct rq *rq);
1865d84b3131SFrederic Weisbecker extern int __init sched_tick_offload_init(void);
186676d92ac3SFrederic Weisbecker 
186776d92ac3SFrederic Weisbecker /*
186876d92ac3SFrederic Weisbecker  * Tick may be needed by tasks in the runqueue depending on their policy and
186976d92ac3SFrederic Weisbecker  * requirements. If tick is needed, lets send the target an IPI to kick it out of
187076d92ac3SFrederic Weisbecker  * nohz mode if necessary.
187176d92ac3SFrederic Weisbecker  */
187276d92ac3SFrederic Weisbecker static inline void sched_update_tick_dependency(struct rq *rq)
187376d92ac3SFrederic Weisbecker {
187476d92ac3SFrederic Weisbecker 	int cpu;
187576d92ac3SFrederic Weisbecker 
187676d92ac3SFrederic Weisbecker 	if (!tick_nohz_full_enabled())
187776d92ac3SFrederic Weisbecker 		return;
187876d92ac3SFrederic Weisbecker 
187976d92ac3SFrederic Weisbecker 	cpu = cpu_of(rq);
188076d92ac3SFrederic Weisbecker 
188176d92ac3SFrederic Weisbecker 	if (!tick_nohz_full_cpu(cpu))
188276d92ac3SFrederic Weisbecker 		return;
188376d92ac3SFrederic Weisbecker 
188476d92ac3SFrederic Weisbecker 	if (sched_can_stop_tick(rq))
188576d92ac3SFrederic Weisbecker 		tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
188676d92ac3SFrederic Weisbecker 	else
188776d92ac3SFrederic Weisbecker 		tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
188876d92ac3SFrederic Weisbecker }
188976d92ac3SFrederic Weisbecker #else
1890d84b3131SFrederic Weisbecker static inline int sched_tick_offload_init(void) { return 0; }
189176d92ac3SFrederic Weisbecker static inline void sched_update_tick_dependency(struct rq *rq) { }
189276d92ac3SFrederic Weisbecker #endif
189376d92ac3SFrederic Weisbecker 
189472465447SKirill Tkhai static inline void add_nr_running(struct rq *rq, unsigned count)
1895391e43daSPeter Zijlstra {
189672465447SKirill Tkhai 	unsigned prev_nr = rq->nr_running;
189772465447SKirill Tkhai 
189872465447SKirill Tkhai 	rq->nr_running = prev_nr + count;
18999f3660c2SFrederic Weisbecker 
19004486edd1STim Chen #ifdef CONFIG_SMP
19013e184501SViresh Kumar 	if (prev_nr < 2 && rq->nr_running >= 2) {
1902e90c8fe1SValentin Schneider 		if (!READ_ONCE(rq->rd->overload))
1903e90c8fe1SValentin Schneider 			WRITE_ONCE(rq->rd->overload, 1);
190476d92ac3SFrederic Weisbecker 	}
19053e184501SViresh Kumar #endif
19064486edd1STim Chen 
190776d92ac3SFrederic Weisbecker 	sched_update_tick_dependency(rq);
19084486edd1STim Chen }
1909391e43daSPeter Zijlstra 
191072465447SKirill Tkhai static inline void sub_nr_running(struct rq *rq, unsigned count)
1911391e43daSPeter Zijlstra {
191272465447SKirill Tkhai 	rq->nr_running -= count;
191376d92ac3SFrederic Weisbecker 	/* Check if we still need preemption */
191476d92ac3SFrederic Weisbecker 	sched_update_tick_dependency(rq);
1915391e43daSPeter Zijlstra }
1916391e43daSPeter Zijlstra 
1917391e43daSPeter Zijlstra extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1918391e43daSPeter Zijlstra extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1919391e43daSPeter Zijlstra 
1920391e43daSPeter Zijlstra extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1921391e43daSPeter Zijlstra 
1922391e43daSPeter Zijlstra extern const_debug unsigned int sysctl_sched_nr_migrate;
1923391e43daSPeter Zijlstra extern const_debug unsigned int sysctl_sched_migration_cost;
1924391e43daSPeter Zijlstra 
1925391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_HRTICK
1926391e43daSPeter Zijlstra 
1927391e43daSPeter Zijlstra /*
1928391e43daSPeter Zijlstra  * Use hrtick when:
1929391e43daSPeter Zijlstra  *  - enabled by features
1930391e43daSPeter Zijlstra  *  - hrtimer is actually high res
1931391e43daSPeter Zijlstra  */
1932391e43daSPeter Zijlstra static inline int hrtick_enabled(struct rq *rq)
1933391e43daSPeter Zijlstra {
1934391e43daSPeter Zijlstra 	if (!sched_feat(HRTICK))
1935391e43daSPeter Zijlstra 		return 0;
1936391e43daSPeter Zijlstra 	if (!cpu_active(cpu_of(rq)))
1937391e43daSPeter Zijlstra 		return 0;
1938391e43daSPeter Zijlstra 	return hrtimer_is_hres_active(&rq->hrtick_timer);
1939391e43daSPeter Zijlstra }
1940391e43daSPeter Zijlstra 
1941391e43daSPeter Zijlstra void hrtick_start(struct rq *rq, u64 delay);
1942391e43daSPeter Zijlstra 
1943b39e66eaSMike Galbraith #else
1944b39e66eaSMike Galbraith 
1945b39e66eaSMike Galbraith static inline int hrtick_enabled(struct rq *rq)
1946b39e66eaSMike Galbraith {
1947b39e66eaSMike Galbraith 	return 0;
1948b39e66eaSMike Galbraith }
1949b39e66eaSMike Galbraith 
1950391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_HRTICK */
1951391e43daSPeter Zijlstra 
1952dfbca41fSPeter Zijlstra #ifndef arch_scale_freq_capacity
1953dfbca41fSPeter Zijlstra static __always_inline
19547673c8a4SJuri Lelli unsigned long arch_scale_freq_capacity(int cpu)
1955dfbca41fSPeter Zijlstra {
1956dfbca41fSPeter Zijlstra 	return SCHED_CAPACITY_SCALE;
1957dfbca41fSPeter Zijlstra }
1958dfbca41fSPeter Zijlstra #endif
1959b5b4860dSVincent Guittot 
19607e1a9208SJuri Lelli #ifdef CONFIG_SMP
1961391e43daSPeter Zijlstra #ifdef CONFIG_PREEMPT
1962391e43daSPeter Zijlstra 
1963391e43daSPeter Zijlstra static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1964391e43daSPeter Zijlstra 
1965391e43daSPeter Zijlstra /*
1966391e43daSPeter Zijlstra  * fair double_lock_balance: Safely acquires both rq->locks in a fair
1967391e43daSPeter Zijlstra  * way at the expense of forcing extra atomic operations in all
1968391e43daSPeter Zijlstra  * invocations.  This assures that the double_lock is acquired using the
1969391e43daSPeter Zijlstra  * same underlying policy as the spinlock_t on this architecture, which
1970391e43daSPeter Zijlstra  * reduces latency compared to the unfair variant below.  However, it
1971391e43daSPeter Zijlstra  * also adds more overhead and therefore may reduce throughput.
1972391e43daSPeter Zijlstra  */
1973391e43daSPeter Zijlstra static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1974391e43daSPeter Zijlstra 	__releases(this_rq->lock)
1975391e43daSPeter Zijlstra 	__acquires(busiest->lock)
1976391e43daSPeter Zijlstra 	__acquires(this_rq->lock)
1977391e43daSPeter Zijlstra {
1978391e43daSPeter Zijlstra 	raw_spin_unlock(&this_rq->lock);
1979391e43daSPeter Zijlstra 	double_rq_lock(this_rq, busiest);
1980391e43daSPeter Zijlstra 
1981391e43daSPeter Zijlstra 	return 1;
1982391e43daSPeter Zijlstra }
1983391e43daSPeter Zijlstra 
1984391e43daSPeter Zijlstra #else
1985391e43daSPeter Zijlstra /*
1986391e43daSPeter Zijlstra  * Unfair double_lock_balance: Optimizes throughput at the expense of
1987391e43daSPeter Zijlstra  * latency by eliminating extra atomic operations when the locks are
198897fb7a0aSIngo Molnar  * already in proper order on entry.  This favors lower CPU-ids and will
198997fb7a0aSIngo Molnar  * grant the double lock to lower CPUs over higher ids under contention,
1990391e43daSPeter Zijlstra  * regardless of entry order into the function.
1991391e43daSPeter Zijlstra  */
1992391e43daSPeter Zijlstra static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1993391e43daSPeter Zijlstra 	__releases(this_rq->lock)
1994391e43daSPeter Zijlstra 	__acquires(busiest->lock)
1995391e43daSPeter Zijlstra 	__acquires(this_rq->lock)
1996391e43daSPeter Zijlstra {
1997391e43daSPeter Zijlstra 	int ret = 0;
1998391e43daSPeter Zijlstra 
1999391e43daSPeter Zijlstra 	if (unlikely(!raw_spin_trylock(&busiest->lock))) {
2000391e43daSPeter Zijlstra 		if (busiest < this_rq) {
2001391e43daSPeter Zijlstra 			raw_spin_unlock(&this_rq->lock);
2002391e43daSPeter Zijlstra 			raw_spin_lock(&busiest->lock);
2003391e43daSPeter Zijlstra 			raw_spin_lock_nested(&this_rq->lock,
2004391e43daSPeter Zijlstra 					      SINGLE_DEPTH_NESTING);
2005391e43daSPeter Zijlstra 			ret = 1;
2006391e43daSPeter Zijlstra 		} else
2007391e43daSPeter Zijlstra 			raw_spin_lock_nested(&busiest->lock,
2008391e43daSPeter Zijlstra 					      SINGLE_DEPTH_NESTING);
2009391e43daSPeter Zijlstra 	}
2010391e43daSPeter Zijlstra 	return ret;
2011391e43daSPeter Zijlstra }
2012391e43daSPeter Zijlstra 
2013391e43daSPeter Zijlstra #endif /* CONFIG_PREEMPT */
2014391e43daSPeter Zijlstra 
2015391e43daSPeter Zijlstra /*
2016391e43daSPeter Zijlstra  * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2017391e43daSPeter Zijlstra  */
2018391e43daSPeter Zijlstra static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2019391e43daSPeter Zijlstra {
2020391e43daSPeter Zijlstra 	if (unlikely(!irqs_disabled())) {
202197fb7a0aSIngo Molnar 		/* printk() doesn't work well under rq->lock */
2022391e43daSPeter Zijlstra 		raw_spin_unlock(&this_rq->lock);
2023391e43daSPeter Zijlstra 		BUG_ON(1);
2024391e43daSPeter Zijlstra 	}
2025391e43daSPeter Zijlstra 
2026391e43daSPeter Zijlstra 	return _double_lock_balance(this_rq, busiest);
2027391e43daSPeter Zijlstra }
2028391e43daSPeter Zijlstra 
2029391e43daSPeter Zijlstra static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2030391e43daSPeter Zijlstra 	__releases(busiest->lock)
2031391e43daSPeter Zijlstra {
2032391e43daSPeter Zijlstra 	raw_spin_unlock(&busiest->lock);
2033391e43daSPeter Zijlstra 	lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2034391e43daSPeter Zijlstra }
2035391e43daSPeter Zijlstra 
203674602315SPeter Zijlstra static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
203774602315SPeter Zijlstra {
203874602315SPeter Zijlstra 	if (l1 > l2)
203974602315SPeter Zijlstra 		swap(l1, l2);
204074602315SPeter Zijlstra 
204174602315SPeter Zijlstra 	spin_lock(l1);
204274602315SPeter Zijlstra 	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
204374602315SPeter Zijlstra }
204474602315SPeter Zijlstra 
204560e69eedSMike Galbraith static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
204660e69eedSMike Galbraith {
204760e69eedSMike Galbraith 	if (l1 > l2)
204860e69eedSMike Galbraith 		swap(l1, l2);
204960e69eedSMike Galbraith 
205060e69eedSMike Galbraith 	spin_lock_irq(l1);
205160e69eedSMike Galbraith 	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
205260e69eedSMike Galbraith }
205360e69eedSMike Galbraith 
205474602315SPeter Zijlstra static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
205574602315SPeter Zijlstra {
205674602315SPeter Zijlstra 	if (l1 > l2)
205774602315SPeter Zijlstra 		swap(l1, l2);
205874602315SPeter Zijlstra 
205974602315SPeter Zijlstra 	raw_spin_lock(l1);
206074602315SPeter Zijlstra 	raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
206174602315SPeter Zijlstra }
206274602315SPeter Zijlstra 
2063391e43daSPeter Zijlstra /*
2064391e43daSPeter Zijlstra  * double_rq_lock - safely lock two runqueues
2065391e43daSPeter Zijlstra  *
2066391e43daSPeter Zijlstra  * Note this does not disable interrupts like task_rq_lock,
2067391e43daSPeter Zijlstra  * you need to do so manually before calling.
2068391e43daSPeter Zijlstra  */
2069391e43daSPeter Zijlstra static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2070391e43daSPeter Zijlstra 	__acquires(rq1->lock)
2071391e43daSPeter Zijlstra 	__acquires(rq2->lock)
2072391e43daSPeter Zijlstra {
2073391e43daSPeter Zijlstra 	BUG_ON(!irqs_disabled());
2074391e43daSPeter Zijlstra 	if (rq1 == rq2) {
2075391e43daSPeter Zijlstra 		raw_spin_lock(&rq1->lock);
2076391e43daSPeter Zijlstra 		__acquire(rq2->lock);	/* Fake it out ;) */
2077391e43daSPeter Zijlstra 	} else {
2078391e43daSPeter Zijlstra 		if (rq1 < rq2) {
2079391e43daSPeter Zijlstra 			raw_spin_lock(&rq1->lock);
2080391e43daSPeter Zijlstra 			raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2081391e43daSPeter Zijlstra 		} else {
2082391e43daSPeter Zijlstra 			raw_spin_lock(&rq2->lock);
2083391e43daSPeter Zijlstra 			raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2084391e43daSPeter Zijlstra 		}
2085391e43daSPeter Zijlstra 	}
2086391e43daSPeter Zijlstra }
2087391e43daSPeter Zijlstra 
2088391e43daSPeter Zijlstra /*
2089391e43daSPeter Zijlstra  * double_rq_unlock - safely unlock two runqueues
2090391e43daSPeter Zijlstra  *
2091391e43daSPeter Zijlstra  * Note this does not restore interrupts like task_rq_unlock,
2092391e43daSPeter Zijlstra  * you need to do so manually after calling.
2093391e43daSPeter Zijlstra  */
2094391e43daSPeter Zijlstra static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2095391e43daSPeter Zijlstra 	__releases(rq1->lock)
2096391e43daSPeter Zijlstra 	__releases(rq2->lock)
2097391e43daSPeter Zijlstra {
2098391e43daSPeter Zijlstra 	raw_spin_unlock(&rq1->lock);
2099391e43daSPeter Zijlstra 	if (rq1 != rq2)
2100391e43daSPeter Zijlstra 		raw_spin_unlock(&rq2->lock);
2101391e43daSPeter Zijlstra 	else
2102391e43daSPeter Zijlstra 		__release(rq2->lock);
2103391e43daSPeter Zijlstra }
2104391e43daSPeter Zijlstra 
2105f2cb1360SIngo Molnar extern void set_rq_online (struct rq *rq);
2106f2cb1360SIngo Molnar extern void set_rq_offline(struct rq *rq);
2107f2cb1360SIngo Molnar extern bool sched_smp_initialized;
2108f2cb1360SIngo Molnar 
2109391e43daSPeter Zijlstra #else /* CONFIG_SMP */
2110391e43daSPeter Zijlstra 
2111391e43daSPeter Zijlstra /*
2112391e43daSPeter Zijlstra  * double_rq_lock - safely lock two runqueues
2113391e43daSPeter Zijlstra  *
2114391e43daSPeter Zijlstra  * Note this does not disable interrupts like task_rq_lock,
2115391e43daSPeter Zijlstra  * you need to do so manually before calling.
2116391e43daSPeter Zijlstra  */
2117391e43daSPeter Zijlstra static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2118391e43daSPeter Zijlstra 	__acquires(rq1->lock)
2119391e43daSPeter Zijlstra 	__acquires(rq2->lock)
2120391e43daSPeter Zijlstra {
2121391e43daSPeter Zijlstra 	BUG_ON(!irqs_disabled());
2122391e43daSPeter Zijlstra 	BUG_ON(rq1 != rq2);
2123391e43daSPeter Zijlstra 	raw_spin_lock(&rq1->lock);
2124391e43daSPeter Zijlstra 	__acquire(rq2->lock);	/* Fake it out ;) */
2125391e43daSPeter Zijlstra }
2126391e43daSPeter Zijlstra 
2127391e43daSPeter Zijlstra /*
2128391e43daSPeter Zijlstra  * double_rq_unlock - safely unlock two runqueues
2129391e43daSPeter Zijlstra  *
2130391e43daSPeter Zijlstra  * Note this does not restore interrupts like task_rq_unlock,
2131391e43daSPeter Zijlstra  * you need to do so manually after calling.
2132391e43daSPeter Zijlstra  */
2133391e43daSPeter Zijlstra static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2134391e43daSPeter Zijlstra 	__releases(rq1->lock)
2135391e43daSPeter Zijlstra 	__releases(rq2->lock)
2136391e43daSPeter Zijlstra {
2137391e43daSPeter Zijlstra 	BUG_ON(rq1 != rq2);
2138391e43daSPeter Zijlstra 	raw_spin_unlock(&rq1->lock);
2139391e43daSPeter Zijlstra 	__release(rq2->lock);
2140391e43daSPeter Zijlstra }
2141391e43daSPeter Zijlstra 
2142391e43daSPeter Zijlstra #endif
2143391e43daSPeter Zijlstra 
2144391e43daSPeter Zijlstra extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2145391e43daSPeter Zijlstra extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
21466b55c965SSrikar Dronamraju 
21476b55c965SSrikar Dronamraju #ifdef	CONFIG_SCHED_DEBUG
21489469eb01SPeter Zijlstra extern bool sched_debug_enabled;
21499469eb01SPeter Zijlstra 
2150391e43daSPeter Zijlstra extern void print_cfs_stats(struct seq_file *m, int cpu);
2151391e43daSPeter Zijlstra extern void print_rt_stats(struct seq_file *m, int cpu);
2152acb32132SWanpeng Li extern void print_dl_stats(struct seq_file *m, int cpu);
2153f6a34630SMathieu Malaterre extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
2154f6a34630SMathieu Malaterre extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2155f6a34630SMathieu Malaterre extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2156397f2378SSrikar Dronamraju #ifdef CONFIG_NUMA_BALANCING
2157397f2378SSrikar Dronamraju extern void
2158397f2378SSrikar Dronamraju show_numa_stats(struct task_struct *p, struct seq_file *m);
2159397f2378SSrikar Dronamraju extern void
2160397f2378SSrikar Dronamraju print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2161397f2378SSrikar Dronamraju 	unsigned long tpf, unsigned long gsf, unsigned long gpf);
2162397f2378SSrikar Dronamraju #endif /* CONFIG_NUMA_BALANCING */
2163397f2378SSrikar Dronamraju #endif /* CONFIG_SCHED_DEBUG */
2164391e43daSPeter Zijlstra 
2165391e43daSPeter Zijlstra extern void init_cfs_rq(struct cfs_rq *cfs_rq);
216607c54f7aSAbel Vesa extern void init_rt_rq(struct rt_rq *rt_rq);
216707c54f7aSAbel Vesa extern void init_dl_rq(struct dl_rq *dl_rq);
2168391e43daSPeter Zijlstra 
21691ee14e6cSBen Segall extern void cfs_bandwidth_usage_inc(void);
21701ee14e6cSBen Segall extern void cfs_bandwidth_usage_dec(void);
21711c792db7SSuresh Siddha 
21723451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON
217300357f5eSPeter Zijlstra #define NOHZ_BALANCE_KICK_BIT	0
217400357f5eSPeter Zijlstra #define NOHZ_STATS_KICK_BIT	1
2175a22e47a4SPeter Zijlstra 
2176a22e47a4SPeter Zijlstra #define NOHZ_BALANCE_KICK	BIT(NOHZ_BALANCE_KICK_BIT)
2177b7031a02SPeter Zijlstra #define NOHZ_STATS_KICK		BIT(NOHZ_STATS_KICK_BIT)
2178b7031a02SPeter Zijlstra 
2179b7031a02SPeter Zijlstra #define NOHZ_KICK_MASK	(NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
21801c792db7SSuresh Siddha 
21811c792db7SSuresh Siddha #define nohz_flags(cpu)	(&cpu_rq(cpu)->nohz_flags)
218220a5c8ccSThomas Gleixner 
218300357f5eSPeter Zijlstra extern void nohz_balance_exit_idle(struct rq *rq);
218420a5c8ccSThomas Gleixner #else
218500357f5eSPeter Zijlstra static inline void nohz_balance_exit_idle(struct rq *rq) { }
21861c792db7SSuresh Siddha #endif
218773fbec60SFrederic Weisbecker 
2188daec5798SLuca Abeni 
2189daec5798SLuca Abeni #ifdef CONFIG_SMP
2190daec5798SLuca Abeni static inline
2191daec5798SLuca Abeni void __dl_update(struct dl_bw *dl_b, s64 bw)
2192daec5798SLuca Abeni {
2193daec5798SLuca Abeni 	struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2194daec5798SLuca Abeni 	int i;
2195daec5798SLuca Abeni 
2196daec5798SLuca Abeni 	RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2197daec5798SLuca Abeni 			 "sched RCU must be held");
2198daec5798SLuca Abeni 	for_each_cpu_and(i, rd->span, cpu_active_mask) {
2199daec5798SLuca Abeni 		struct rq *rq = cpu_rq(i);
2200daec5798SLuca Abeni 
2201daec5798SLuca Abeni 		rq->dl.extra_bw += bw;
2202daec5798SLuca Abeni 	}
2203daec5798SLuca Abeni }
2204daec5798SLuca Abeni #else
2205daec5798SLuca Abeni static inline
2206daec5798SLuca Abeni void __dl_update(struct dl_bw *dl_b, s64 bw)
2207daec5798SLuca Abeni {
2208daec5798SLuca Abeni 	struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2209daec5798SLuca Abeni 
2210daec5798SLuca Abeni 	dl->extra_bw += bw;
2211daec5798SLuca Abeni }
2212daec5798SLuca Abeni #endif
2213daec5798SLuca Abeni 
2214daec5798SLuca Abeni 
221573fbec60SFrederic Weisbecker #ifdef CONFIG_IRQ_TIME_ACCOUNTING
221619d23dbfSFrederic Weisbecker struct irqtime {
221725e2d8c1SFrederic Weisbecker 	u64			total;
2218a499a5a1SFrederic Weisbecker 	u64			tick_delta;
221919d23dbfSFrederic Weisbecker 	u64			irq_start_time;
222019d23dbfSFrederic Weisbecker 	struct u64_stats_sync	sync;
222119d23dbfSFrederic Weisbecker };
222273fbec60SFrederic Weisbecker 
222319d23dbfSFrederic Weisbecker DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
222473fbec60SFrederic Weisbecker 
222525e2d8c1SFrederic Weisbecker /*
222625e2d8c1SFrederic Weisbecker  * Returns the irqtime minus the softirq time computed by ksoftirqd.
222725e2d8c1SFrederic Weisbecker  * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
222825e2d8c1SFrederic Weisbecker  * and never move forward.
222925e2d8c1SFrederic Weisbecker  */
223073fbec60SFrederic Weisbecker static inline u64 irq_time_read(int cpu)
223173fbec60SFrederic Weisbecker {
223219d23dbfSFrederic Weisbecker 	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
223319d23dbfSFrederic Weisbecker 	unsigned int seq;
223419d23dbfSFrederic Weisbecker 	u64 total;
223573fbec60SFrederic Weisbecker 
223673fbec60SFrederic Weisbecker 	do {
223719d23dbfSFrederic Weisbecker 		seq = __u64_stats_fetch_begin(&irqtime->sync);
223825e2d8c1SFrederic Weisbecker 		total = irqtime->total;
223919d23dbfSFrederic Weisbecker 	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
224073fbec60SFrederic Weisbecker 
224119d23dbfSFrederic Weisbecker 	return total;
224273fbec60SFrederic Weisbecker }
224373fbec60SFrederic Weisbecker #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2244adaf9fcdSRafael J. Wysocki 
2245adaf9fcdSRafael J. Wysocki #ifdef CONFIG_CPU_FREQ
2246b10abd0aSJoel Fernandes (Google) DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
2247adaf9fcdSRafael J. Wysocki 
2248adaf9fcdSRafael J. Wysocki /**
2249adaf9fcdSRafael J. Wysocki  * cpufreq_update_util - Take a note about CPU utilization changes.
225012bde33dSRafael J. Wysocki  * @rq: Runqueue to carry out the update for.
225158919e83SRafael J. Wysocki  * @flags: Update reason flags.
2252adaf9fcdSRafael J. Wysocki  *
225358919e83SRafael J. Wysocki  * This function is called by the scheduler on the CPU whose utilization is
225458919e83SRafael J. Wysocki  * being updated.
2255adaf9fcdSRafael J. Wysocki  *
2256adaf9fcdSRafael J. Wysocki  * It can only be called from RCU-sched read-side critical sections.
2257adaf9fcdSRafael J. Wysocki  *
2258adaf9fcdSRafael J. Wysocki  * The way cpufreq is currently arranged requires it to evaluate the CPU
2259adaf9fcdSRafael J. Wysocki  * performance state (frequency/voltage) on a regular basis to prevent it from
2260adaf9fcdSRafael J. Wysocki  * being stuck in a completely inadequate performance level for too long.
2261e0367b12SJuri Lelli  * That is not guaranteed to happen if the updates are only triggered from CFS
2262e0367b12SJuri Lelli  * and DL, though, because they may not be coming in if only RT tasks are
2263e0367b12SJuri Lelli  * active all the time (or there are RT tasks only).
2264adaf9fcdSRafael J. Wysocki  *
2265e0367b12SJuri Lelli  * As a workaround for that issue, this function is called periodically by the
2266e0367b12SJuri Lelli  * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
2267adaf9fcdSRafael J. Wysocki  * but that really is a band-aid.  Going forward it should be replaced with
2268e0367b12SJuri Lelli  * solutions targeted more specifically at RT tasks.
2269adaf9fcdSRafael J. Wysocki  */
227012bde33dSRafael J. Wysocki static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2271adaf9fcdSRafael J. Wysocki {
227258919e83SRafael J. Wysocki 	struct update_util_data *data;
227358919e83SRafael J. Wysocki 
2274674e7541SViresh Kumar 	data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2275674e7541SViresh Kumar 						  cpu_of(rq)));
227658919e83SRafael J. Wysocki 	if (data)
227712bde33dSRafael J. Wysocki 		data->func(data, rq_clock(rq), flags);
227812bde33dSRafael J. Wysocki }
2279adaf9fcdSRafael J. Wysocki #else
228012bde33dSRafael J. Wysocki static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2281adaf9fcdSRafael J. Wysocki #endif /* CONFIG_CPU_FREQ */
2282be53f58fSLinus Torvalds 
2283982d9cdcSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
2284*0413d7f3SPatrick Bellasi enum uclamp_id uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
22859d20ad7dSPatrick Bellasi 
22869d20ad7dSPatrick Bellasi static __always_inline
22879d20ad7dSPatrick Bellasi unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
22889d20ad7dSPatrick Bellasi 			      struct task_struct *p)
2289982d9cdcSPatrick Bellasi {
2290982d9cdcSPatrick Bellasi 	unsigned int min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
2291982d9cdcSPatrick Bellasi 	unsigned int max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
2292982d9cdcSPatrick Bellasi 
22939d20ad7dSPatrick Bellasi 	if (p) {
22949d20ad7dSPatrick Bellasi 		min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
22959d20ad7dSPatrick Bellasi 		max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
22969d20ad7dSPatrick Bellasi 	}
22979d20ad7dSPatrick Bellasi 
2298982d9cdcSPatrick Bellasi 	/*
2299982d9cdcSPatrick Bellasi 	 * Since CPU's {min,max}_util clamps are MAX aggregated considering
2300982d9cdcSPatrick Bellasi 	 * RUNNABLE tasks with _different_ clamps, we can end up with an
2301982d9cdcSPatrick Bellasi 	 * inversion. Fix it now when the clamps are applied.
2302982d9cdcSPatrick Bellasi 	 */
2303982d9cdcSPatrick Bellasi 	if (unlikely(min_util >= max_util))
2304982d9cdcSPatrick Bellasi 		return min_util;
2305982d9cdcSPatrick Bellasi 
2306982d9cdcSPatrick Bellasi 	return clamp(util, min_util, max_util);
2307982d9cdcSPatrick Bellasi }
23089d20ad7dSPatrick Bellasi 
23099d20ad7dSPatrick Bellasi static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
23109d20ad7dSPatrick Bellasi {
23119d20ad7dSPatrick Bellasi 	return uclamp_util_with(rq, util, NULL);
23129d20ad7dSPatrick Bellasi }
2313982d9cdcSPatrick Bellasi #else /* CONFIG_UCLAMP_TASK */
23149d20ad7dSPatrick Bellasi static inline unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
23159d20ad7dSPatrick Bellasi 					    struct task_struct *p)
23169d20ad7dSPatrick Bellasi {
23179d20ad7dSPatrick Bellasi 	return util;
23189d20ad7dSPatrick Bellasi }
2319982d9cdcSPatrick Bellasi static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
2320982d9cdcSPatrick Bellasi {
2321982d9cdcSPatrick Bellasi 	return util;
2322982d9cdcSPatrick Bellasi }
2323982d9cdcSPatrick Bellasi #endif /* CONFIG_UCLAMP_TASK */
2324982d9cdcSPatrick Bellasi 
23259bdcb44eSRafael J. Wysocki #ifdef arch_scale_freq_capacity
23269bdcb44eSRafael J. Wysocki # ifndef arch_scale_freq_invariant
232797fb7a0aSIngo Molnar #  define arch_scale_freq_invariant()	true
23289bdcb44eSRafael J. Wysocki # endif
232997fb7a0aSIngo Molnar #else
233097fb7a0aSIngo Molnar # define arch_scale_freq_invariant()	false
23319bdcb44eSRafael J. Wysocki #endif
2332d4edd662SJuri Lelli 
233310a35e68SVincent Guittot #ifdef CONFIG_SMP
233410a35e68SVincent Guittot static inline unsigned long capacity_orig_of(int cpu)
233510a35e68SVincent Guittot {
233610a35e68SVincent Guittot 	return cpu_rq(cpu)->cpu_capacity_orig;
233710a35e68SVincent Guittot }
233810a35e68SVincent Guittot #endif
233910a35e68SVincent Guittot 
2340938e5e4bSQuentin Perret /**
2341938e5e4bSQuentin Perret  * enum schedutil_type - CPU utilization type
2342938e5e4bSQuentin Perret  * @FREQUENCY_UTIL:	Utilization used to select frequency
2343938e5e4bSQuentin Perret  * @ENERGY_UTIL:	Utilization used during energy calculation
2344938e5e4bSQuentin Perret  *
2345938e5e4bSQuentin Perret  * The utilization signals of all scheduling classes (CFS/RT/DL) and IRQ time
2346938e5e4bSQuentin Perret  * need to be aggregated differently depending on the usage made of them. This
2347938e5e4bSQuentin Perret  * enum is used within schedutil_freq_util() to differentiate the types of
2348938e5e4bSQuentin Perret  * utilization expected by the callers, and adjust the aggregation accordingly.
2349938e5e4bSQuentin Perret  */
2350938e5e4bSQuentin Perret enum schedutil_type {
2351938e5e4bSQuentin Perret 	FREQUENCY_UTIL,
2352938e5e4bSQuentin Perret 	ENERGY_UTIL,
2353938e5e4bSQuentin Perret };
2354938e5e4bSQuentin Perret 
2355af24bde8SPatrick Bellasi #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
2356938e5e4bSQuentin Perret 
2357af24bde8SPatrick Bellasi unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2358af24bde8SPatrick Bellasi 				 unsigned long max, enum schedutil_type type,
2359af24bde8SPatrick Bellasi 				 struct task_struct *p);
2360938e5e4bSQuentin Perret 
23618cc90515SVincent Guittot static inline unsigned long cpu_bw_dl(struct rq *rq)
2362d4edd662SJuri Lelli {
2363d4edd662SJuri Lelli 	return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
2364d4edd662SJuri Lelli }
2365d4edd662SJuri Lelli 
23668cc90515SVincent Guittot static inline unsigned long cpu_util_dl(struct rq *rq)
23678cc90515SVincent Guittot {
23688cc90515SVincent Guittot 	return READ_ONCE(rq->avg_dl.util_avg);
23698cc90515SVincent Guittot }
23708cc90515SVincent Guittot 
2371d4edd662SJuri Lelli static inline unsigned long cpu_util_cfs(struct rq *rq)
2372d4edd662SJuri Lelli {
2373a07630b8SPatrick Bellasi 	unsigned long util = READ_ONCE(rq->cfs.avg.util_avg);
2374a07630b8SPatrick Bellasi 
2375a07630b8SPatrick Bellasi 	if (sched_feat(UTIL_EST)) {
2376a07630b8SPatrick Bellasi 		util = max_t(unsigned long, util,
2377a07630b8SPatrick Bellasi 			     READ_ONCE(rq->cfs.avg.util_est.enqueued));
2378a07630b8SPatrick Bellasi 	}
2379a07630b8SPatrick Bellasi 
2380a07630b8SPatrick Bellasi 	return util;
2381d4edd662SJuri Lelli }
2382371bf427SVincent Guittot 
2383371bf427SVincent Guittot static inline unsigned long cpu_util_rt(struct rq *rq)
2384371bf427SVincent Guittot {
2385dfa444dcSVincent Guittot 	return READ_ONCE(rq->avg_rt.util_avg);
2386371bf427SVincent Guittot }
2387938e5e4bSQuentin Perret #else /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2388af24bde8SPatrick Bellasi static inline unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2389af24bde8SPatrick Bellasi 				 unsigned long max, enum schedutil_type type,
2390af24bde8SPatrick Bellasi 				 struct task_struct *p)
2391938e5e4bSQuentin Perret {
2392af24bde8SPatrick Bellasi 	return 0;
2393938e5e4bSQuentin Perret }
2394af24bde8SPatrick Bellasi #endif /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
23959033ea11SVincent Guittot 
239611d4afd4SVincent Guittot #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
23979033ea11SVincent Guittot static inline unsigned long cpu_util_irq(struct rq *rq)
23989033ea11SVincent Guittot {
23999033ea11SVincent Guittot 	return rq->avg_irq.util_avg;
24009033ea11SVincent Guittot }
24012e62c474SVincent Guittot 
24022e62c474SVincent Guittot static inline
24032e62c474SVincent Guittot unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
24042e62c474SVincent Guittot {
24052e62c474SVincent Guittot 	util *= (max - irq);
24062e62c474SVincent Guittot 	util /= max;
24072e62c474SVincent Guittot 
24082e62c474SVincent Guittot 	return util;
24092e62c474SVincent Guittot 
24102e62c474SVincent Guittot }
24119033ea11SVincent Guittot #else
24129033ea11SVincent Guittot static inline unsigned long cpu_util_irq(struct rq *rq)
24139033ea11SVincent Guittot {
24149033ea11SVincent Guittot 	return 0;
24159033ea11SVincent Guittot }
24169033ea11SVincent Guittot 
24172e62c474SVincent Guittot static inline
24182e62c474SVincent Guittot unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
24192e62c474SVincent Guittot {
24202e62c474SVincent Guittot 	return util;
24212e62c474SVincent Guittot }
2422794a56ebSJuri Lelli #endif
24236aa140faSQuentin Perret 
2424531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
24251f74de87SQuentin Perret 
2426f8a696f2SPeter Zijlstra #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
2427f8a696f2SPeter Zijlstra 
2428f8a696f2SPeter Zijlstra DECLARE_STATIC_KEY_FALSE(sched_energy_present);
2429f8a696f2SPeter Zijlstra 
2430f8a696f2SPeter Zijlstra static inline bool sched_energy_enabled(void)
2431f8a696f2SPeter Zijlstra {
2432f8a696f2SPeter Zijlstra 	return static_branch_unlikely(&sched_energy_present);
2433f8a696f2SPeter Zijlstra }
2434f8a696f2SPeter Zijlstra 
2435f8a696f2SPeter Zijlstra #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
2436f8a696f2SPeter Zijlstra 
2437f8a696f2SPeter Zijlstra #define perf_domain_span(pd) NULL
2438f8a696f2SPeter Zijlstra static inline bool sched_energy_enabled(void) { return false; }
2439f8a696f2SPeter Zijlstra 
2440f8a696f2SPeter Zijlstra #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2441