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