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