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