xref: /linux/kernel/sched/sched.h (revision 729beb5aa80ce4bcf40f9ff74ed845269393a6ae)
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/prandom.h>
9 #include <linux/sched/affinity.h>
10 #include <linux/sched/autogroup.h>
11 #include <linux/sched/cpufreq.h>
12 #include <linux/sched/deadline.h>
13 #include <linux/sched.h>
14 #include <linux/sched/loadavg.h>
15 #include <linux/sched/mm.h>
16 #include <linux/sched/rseq_api.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/smt.h>
19 #include <linux/sched/stat.h>
20 #include <linux/sched/sysctl.h>
21 #include <linux/sched/task_flags.h>
22 #include <linux/sched/task.h>
23 #include <linux/sched/topology.h>
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/cpuset.h>
34 #include <linux/ctype.h>
35 #include <linux/file.h>
36 #include <linux/fs_api.h>
37 #include <linux/hrtimer_api.h>
38 #include <linux/interrupt.h>
39 #include <linux/irq_work.h>
40 #include <linux/jiffies.h>
41 #include <linux/kref_api.h>
42 #include <linux/kthread.h>
43 #include <linux/ktime_api.h>
44 #include <linux/lockdep_api.h>
45 #include <linux/lockdep.h>
46 #include <linux/memblock.h>
47 #include <linux/memcontrol.h>
48 #include <linux/minmax.h>
49 #include <linux/mm.h>
50 #include <linux/module.h>
51 #include <linux/mutex_api.h>
52 #include <linux/plist.h>
53 #include <linux/poll.h>
54 #include <linux/proc_fs.h>
55 #include <linux/profile.h>
56 #include <linux/psi.h>
57 #include <linux/rcupdate.h>
58 #include <linux/seq_file.h>
59 #include <linux/seqlock.h>
60 #include <linux/softirq.h>
61 #include <linux/spinlock_api.h>
62 #include <linux/static_key.h>
63 #include <linux/stop_machine.h>
64 #include <linux/syscalls_api.h>
65 #include <linux/syscalls.h>
66 #include <linux/tick.h>
67 #include <linux/topology.h>
68 #include <linux/types.h>
69 #include <linux/u64_stats_sync_api.h>
70 #include <linux/uaccess.h>
71 #include <linux/vmstat.h>
72 #include <linux/wait_api.h>
73 #include <linux/wait_bit.h>
74 #include <linux/workqueue_api.h>
75 #include <linux/delayacct.h>
76 #include <linux/mmu_context.h>
77 
78 #include <trace/events/power.h>
79 #include <trace/events/sched.h>
80 
81 #include "../workqueue_internal.h"
82 
83 struct rq;
84 struct cfs_rq;
85 struct rt_rq;
86 struct sched_group;
87 struct cpuidle_state;
88 
89 #if defined(CONFIG_PARAVIRT) && !defined(CONFIG_HAVE_PV_STEAL_CLOCK_GEN)
90 # include <asm/paravirt.h>
91 #endif
92 
93 #include <asm/barrier.h>
94 
95 #include "cpupri.h"
96 #include "cpudeadline.h"
97 
98 /* task_struct::on_rq states: */
99 #define TASK_ON_RQ_QUEUED	1
100 #define TASK_ON_RQ_MIGRATING	2
101 
102 extern __read_mostly int scheduler_running;
103 
104 extern unsigned long calc_load_update;
105 extern atomic_long_t calc_load_tasks;
106 
107 extern void calc_global_load_tick(struct rq *this_rq);
108 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
109 
110 extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
111 
112 extern int sysctl_sched_rt_period;
113 extern int sysctl_sched_rt_runtime;
114 extern int sched_rr_timeslice;
115 
116 /*
117  * Asymmetric CPU capacity bits
118  */
119 struct asym_cap_data {
120 	struct list_head link;
121 	struct rcu_head rcu;
122 	unsigned long capacity;
123 	unsigned long cpus[];
124 };
125 
126 extern struct list_head asym_cap_list;
127 
128 #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
129 
130 /*
131  * Helpers for converting nanosecond timing to jiffy resolution
132  */
133 #define NS_TO_JIFFIES(time)	((unsigned long)(time) / (NSEC_PER_SEC/HZ))
134 
135 /*
136  * Increase resolution of nice-level calculations for 64-bit architectures.
137  * The extra resolution improves shares distribution and load balancing of
138  * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group
139  * hierarchies, especially on larger systems. This is not a user-visible change
140  * and does not change the user-interface for setting shares/weights.
141  *
142  * We increase resolution only if we have enough bits to allow this increased
143  * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
144  * are pretty high and the returns do not justify the increased costs.
145  *
146  * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
147  * increase coverage and consistency always enable it on 64-bit platforms.
148  */
149 #ifdef CONFIG_64BIT
150 # define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
151 # define scale_load(w)		((w) << SCHED_FIXEDPOINT_SHIFT)
152 # define scale_load_down(w)					\
153 ({								\
154 	unsigned long __w = (w);				\
155 								\
156 	if (__w)						\
157 		__w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT);	\
158 	__w;							\
159 })
160 #else
161 # define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT)
162 # define scale_load(w)		(w)
163 # define scale_load_down(w)	(w)
164 #endif
165 
166 /*
167  * Task weight (visible to users) and its load (invisible to users) have
168  * independent resolution, but they should be well calibrated. We use
169  * scale_load() and scale_load_down(w) to convert between them. The
170  * following must be true:
171  *
172  *  scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD
173  *
174  */
175 #define NICE_0_LOAD		(1L << NICE_0_LOAD_SHIFT)
176 
177 /*
178  * Single value that decides SCHED_DEADLINE internal math precision.
179  * 10 -> just above 1us
180  * 9  -> just above 0.5us
181  */
182 #define DL_SCALE		10
183 
184 /*
185  * Single value that denotes runtime == period, ie unlimited time.
186  */
187 #define RUNTIME_INF		((u64)~0ULL)
188 
189 static inline int idle_policy(int policy)
190 {
191 	return policy == SCHED_IDLE;
192 }
193 
194 static inline int normal_policy(int policy)
195 {
196 #ifdef CONFIG_SCHED_CLASS_EXT
197 	if (policy == SCHED_EXT)
198 		return true;
199 #endif
200 	return policy == SCHED_NORMAL;
201 }
202 
203 static inline int fair_policy(int policy)
204 {
205 	return normal_policy(policy) || policy == SCHED_BATCH;
206 }
207 
208 static inline int rt_policy(int policy)
209 {
210 	return policy == SCHED_FIFO || policy == SCHED_RR;
211 }
212 
213 static inline int dl_policy(int policy)
214 {
215 	return policy == SCHED_DEADLINE;
216 }
217 
218 static inline bool valid_policy(int policy)
219 {
220 	return idle_policy(policy) || fair_policy(policy) ||
221 		rt_policy(policy) || dl_policy(policy);
222 }
223 
224 static inline int task_has_idle_policy(struct task_struct *p)
225 {
226 	return idle_policy(p->policy);
227 }
228 
229 static inline int task_has_rt_policy(struct task_struct *p)
230 {
231 	return rt_policy(p->policy);
232 }
233 
234 static inline int task_has_dl_policy(struct task_struct *p)
235 {
236 	return dl_policy(p->policy);
237 }
238 
239 #define cap_scale(v, s)		((v)*(s) >> SCHED_CAPACITY_SHIFT)
240 
241 static inline void update_avg(u64 *avg, u64 sample)
242 {
243 	s64 diff = sample - *avg;
244 
245 	*avg += diff / 8;
246 }
247 
248 /*
249  * Shifting a value by an exponent greater *or equal* to the size of said value
250  * is UB; cap at size-1.
251  */
252 #define shr_bound(val, shift)							\
253 	(val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
254 
255 /*
256  * cgroup weight knobs should use the common MIN, DFL and MAX values which are
257  * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it
258  * maps pretty well onto the shares value used by scheduler and the round-trip
259  * conversions preserve the original value over the entire range.
260  */
261 static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight)
262 {
263 	return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL);
264 }
265 
266 static inline unsigned long sched_weight_to_cgroup(unsigned long weight)
267 {
268 	return clamp_t(unsigned long,
269 		       DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024),
270 		       CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
271 }
272 
273 /*
274  * !! For sched_setattr_nocheck() (kernel) only !!
275  *
276  * This is actually gross. :(
277  *
278  * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
279  * tasks, but still be able to sleep. We need this on platforms that cannot
280  * atomically change clock frequency. Remove once fast switching will be
281  * available on such platforms.
282  *
283  * SUGOV stands for SchedUtil GOVernor.
284  */
285 #define SCHED_FLAG_SUGOV	0x10000000
286 
287 #define SCHED_DL_FLAGS		(SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
288 
289 static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se)
290 {
291 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
292 	return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
293 #else
294 	return false;
295 #endif
296 }
297 
298 /*
299  * Tells if entity @a should preempt entity @b.
300  */
301 static inline bool dl_entity_preempt(const struct sched_dl_entity *a,
302 				     const struct sched_dl_entity *b)
303 {
304 	return dl_entity_is_special(a) ||
305 	       dl_time_before(a->deadline, b->deadline);
306 }
307 
308 /*
309  * This is the priority-queue data structure of the RT scheduling class:
310  */
311 struct rt_prio_array {
312 	DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
313 	struct list_head queue[MAX_RT_PRIO];
314 };
315 
316 struct rt_bandwidth {
317 	/* nests inside the rq lock: */
318 	raw_spinlock_t		rt_runtime_lock;
319 	ktime_t			rt_period;
320 	u64			rt_runtime;
321 	struct hrtimer		rt_period_timer;
322 	unsigned int		rt_period_active;
323 };
324 
325 static inline int dl_bandwidth_enabled(void)
326 {
327 	return sysctl_sched_rt_runtime >= 0;
328 }
329 
330 /*
331  * To keep the bandwidth of -deadline tasks under control
332  * we need some place where:
333  *  - store the maximum -deadline bandwidth of each cpu;
334  *  - cache the fraction of bandwidth that is currently allocated in
335  *    each root domain;
336  *
337  * This is all done in the data structure below. It is similar to the
338  * one used for RT-throttling (rt_bandwidth), with the main difference
339  * that, since here we are only interested in admission control, we
340  * do not decrease any runtime while the group "executes", neither we
341  * need a timer to replenish it.
342  *
343  * With respect to SMP, bandwidth is given on a per root domain basis,
344  * meaning that:
345  *  - bw (< 100%) is the deadline bandwidth of each CPU;
346  *  - total_bw is the currently allocated bandwidth in each root domain;
347  */
348 struct dl_bw {
349 	raw_spinlock_t		lock;
350 	u64			bw;
351 	u64			total_bw;
352 };
353 
354 extern void init_dl_bw(struct dl_bw *dl_b);
355 extern int  sched_dl_global_validate(void);
356 extern void sched_dl_do_global(void);
357 extern int  sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
358 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
359 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr, unsigned int flags);
360 extern bool __checkparam_dl(const struct sched_attr *attr);
361 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
362 extern int  dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
363 extern int  dl_bw_deactivate(int cpu);
364 extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec);
365 /*
366  * SCHED_DEADLINE supports servers (nested scheduling) with the following
367  * interface:
368  *
369  *   dl_se::rq -- runqueue we belong to.
370  *
371  *   dl_se::server_pick() -- nested pick_next_task(); we yield the period if this
372  *                           returns NULL.
373  *
374  *   dl_server_update() -- called from update_curr_common(), propagates runtime
375  *                         to the server.
376  *
377  *   dl_server_start() -- start the server when it has tasks; it will stop
378  *			  automatically when there are no more tasks, per
379  *			  dl_se::server_pick() returning NULL.
380  *
381  *   dl_server_stop() -- (force) stop the server; use when updating
382  *                       parameters.
383  *
384  *   dl_server_init() -- initializes the server.
385  *
386  * When started the dl_server will (per dl_defer) schedule a timer for its
387  * zero-laxity point -- that is, unlike regular EDF tasks which run ASAP, a
388  * server will run at the very end of its period.
389  *
390  * This is done such that any runtime from the target class can be accounted
391  * against the server -- through dl_server_update() above -- such that when it
392  * becomes time to run, it might already be out of runtime and get deferred
393  * until the next period. In this case dl_server_timer() will alternate
394  * between defer and replenish but never actually enqueue the server.
395  *
396  * Only when the target class does not manage to exhaust the server's runtime
397  * (there's actualy starvation in the given period), will the dl_server get on
398  * the runqueue. Once queued it will pick tasks from the target class and run
399  * them until either its runtime is exhaused, at which point its back to
400  * dl_server_timer, or until there are no more tasks to run, at which point
401  * the dl_server stops itself.
402  *
403  * By stopping at this point the dl_server retains bandwidth, which, if a new
404  * task wakes up imminently (starting the server again), can be used --
405  * subject to CBS wakeup rules -- without having to wait for the next period.
406  *
407  * Additionally, because of the dl_defer behaviour the start/stop behaviour is
408  * naturally thottled to once per period, avoiding high context switch
409  * workloads from spamming the hrtimer program/cancel paths.
410  */
411 extern void dl_server_update_idle(struct sched_dl_entity *dl_se, s64 delta_exec);
412 extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec);
413 extern void dl_server_start(struct sched_dl_entity *dl_se);
414 extern void dl_server_stop(struct sched_dl_entity *dl_se);
415 extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
416 		    dl_server_pick_f pick_task);
417 extern void sched_init_dl_servers(void);
418 
419 extern void fair_server_init(struct rq *rq);
420 extern void ext_server_init(struct rq *rq);
421 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
422 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
423 		    u64 runtime, u64 period, bool init);
424 extern int dl_server_attach_bw(struct sched_dl_entity *dl_se);
425 extern void dl_server_detach_bw(struct sched_dl_entity *dl_se);
426 extern int dl_server_swap_bw(struct sched_dl_entity *detach_se,
427 			     struct sched_dl_entity *attach_se);
428 
429 static inline bool dl_server_active(struct sched_dl_entity *dl_se)
430 {
431 	return dl_se->dl_server_active;
432 }
433 
434 #ifdef CONFIG_CGROUP_SCHED
435 
436 extern struct list_head task_groups;
437 
438 #ifdef CONFIG_GROUP_SCHED_BANDWIDTH
439 extern const u64 max_bw_quota_period_us;
440 
441 /*
442  * default period for group bandwidth.
443  * default: 0.1s, units: microseconds
444  */
445 static inline u64 default_bw_period_us(void)
446 {
447 	return 100000ULL;
448 }
449 #endif /* CONFIG_GROUP_SCHED_BANDWIDTH */
450 
451 struct cfs_bandwidth {
452 #ifdef CONFIG_CFS_BANDWIDTH
453 	raw_spinlock_t		lock;
454 	ktime_t			period;
455 	u64			quota;
456 	u64			runtime;
457 	u64			burst;
458 	u64			runtime_snap;
459 	s64			hierarchical_quota;
460 
461 	u8			idle;
462 	u8			period_active;
463 	u8			slack_started;
464 	struct hrtimer		period_timer;
465 	struct hrtimer		slack_timer;
466 	struct list_head	throttled_cfs_rq;
467 
468 	/* Statistics: */
469 	int			nr_periods;
470 	int			nr_throttled;
471 	int			nr_burst;
472 	u64			throttled_time;
473 	u64			burst_time;
474 #endif /* CONFIG_CFS_BANDWIDTH */
475 };
476 
477 /* Task group related information */
478 struct task_group {
479 	struct cgroup_subsys_state css;
480 
481 #ifdef CONFIG_GROUP_SCHED_WEIGHT
482 	/* A positive value indicates that this is a SCHED_IDLE group. */
483 	int			idle;
484 #endif
485 
486 #ifdef CONFIG_FAIR_GROUP_SCHED
487 	/* runqueue "owned" by this group on each CPU */
488 	struct cfs_rq __percpu	*cfs_rq;
489 	unsigned long		shares;
490 	/*
491 	 * load_avg can be heavily contended at clock tick time, so put
492 	 * it in its own cache-line separated from the fields above which
493 	 * will also be accessed at each tick.
494 	 */
495 	atomic_long_t		load_avg ____cacheline_aligned;
496 	atomic_long_t		runnable_avg;
497 
498 #endif /* CONFIG_FAIR_GROUP_SCHED */
499 
500 #ifdef CONFIG_RT_GROUP_SCHED
501 	struct sched_rt_entity	**rt_se;
502 	struct rt_rq		**rt_rq;
503 
504 	struct rt_bandwidth	rt_bandwidth;
505 #endif
506 
507 	struct scx_task_group	scx;
508 
509 	struct rcu_head		rcu;
510 	struct list_head	list;
511 
512 	struct task_group	*parent;
513 	struct list_head	siblings;
514 	struct list_head	children;
515 
516 #ifdef CONFIG_SCHED_AUTOGROUP
517 	struct autogroup	*autogroup;
518 #endif
519 
520 	struct cfs_bandwidth	cfs_bandwidth;
521 
522 #ifdef CONFIG_UCLAMP_TASK_GROUP
523 	/* The two decimal precision [%] value requested from user-space */
524 	unsigned int		uclamp_pct[UCLAMP_CNT];
525 	/* Clamp values requested for a task group */
526 	struct uclamp_se	uclamp_req[UCLAMP_CNT];
527 	/* Effective clamp values used for a task group */
528 	struct uclamp_se	uclamp[UCLAMP_CNT];
529 #endif
530 
531 };
532 
533 #define ROOT_TASK_GROUP_LOAD	NICE_0_LOAD
534 
535 typedef int (*tg_visitor)(struct task_group *, void *);
536 
537 extern int walk_tg_tree_from(struct task_group *from,
538 			     tg_visitor down, tg_visitor up, void *data);
539 
540 /*
541  * Iterate the full tree, calling @down when first entering a node and @up when
542  * leaving it for the final time.
543  *
544  * Caller must hold rcu_lock or sufficient equivalent.
545  */
546 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
547 {
548 	return walk_tg_tree_from(&root_task_group, down, up, data);
549 }
550 
551 static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
552 {
553 	return css ? container_of(css, struct task_group, css) : NULL;
554 }
555 
556 extern int tg_nop(struct task_group *tg, void *data);
557 
558 #ifdef CONFIG_FAIR_GROUP_SCHED
559 extern void free_fair_sched_group(struct task_group *tg);
560 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
561 extern void online_fair_sched_group(struct task_group *tg);
562 extern void unregister_fair_sched_group(struct task_group *tg);
563 extern void __sched_cgroup_mode_update(int mode);
564 #else /* !CONFIG_FAIR_GROUP_SCHED: */
565 static inline void free_fair_sched_group(struct task_group *tg) { }
566 static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
567 {
568        return 1;
569 }
570 static inline void online_fair_sched_group(struct task_group *tg) { }
571 static inline void unregister_fair_sched_group(struct task_group *tg) { }
572 #endif /* !CONFIG_FAIR_GROUP_SCHED */
573 
574 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
575 			struct sched_entity *se, int cpu,
576 			struct sched_entity *parent);
577 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
578 
579 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
580 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
581 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
582 extern bool cfs_task_bw_constrained(struct task_struct *p);
583 
584 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
585 		struct sched_rt_entity *rt_se, int cpu,
586 		struct sched_rt_entity *parent);
587 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
588 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
589 extern long sched_group_rt_runtime(struct task_group *tg);
590 extern long sched_group_rt_period(struct task_group *tg);
591 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
592 
593 extern struct task_group *sched_create_group(struct task_group *parent);
594 extern void sched_online_group(struct task_group *tg,
595 			       struct task_group *parent);
596 extern void sched_destroy_group(struct task_group *tg);
597 extern void sched_release_group(struct task_group *tg);
598 
599 extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
600 
601 #ifdef CONFIG_FAIR_GROUP_SCHED
602 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
603 
604 extern int sched_group_set_idle(struct task_group *tg, long idle);
605 
606 extern void set_task_rq_fair(struct sched_entity *se,
607 			     struct cfs_rq *prev, struct cfs_rq *next);
608 #else /* !CONFIG_FAIR_GROUP_SCHED: */
609 static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; }
610 static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; }
611 #endif /* !CONFIG_FAIR_GROUP_SCHED */
612 
613 #else /* !CONFIG_CGROUP_SCHED: */
614 
615 struct cfs_bandwidth { };
616 
617 static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; }
618 
619 #endif /* !CONFIG_CGROUP_SCHED */
620 
621 /*
622  * A weight of 0 or 1 can cause arithmetics problems.
623  * A weight of a cfs_rq is the sum of weights of which entities
624  * are queued on this cfs_rq, so a weight of a entity should not be
625  * too large, so as the shares value of a task group.
626  * (The default weight is 1024 - so there's no practical
627  *  limitation from this.)
628  */
629 #define MIN_SHARES		(1UL <<  1)
630 #define MAX_SHARES		(1UL << 18)
631 
632 extern void unregister_rt_sched_group(struct task_group *tg);
633 extern void free_rt_sched_group(struct task_group *tg);
634 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
635 
636 /*
637  * u64_u32_load/u64_u32_store
638  *
639  * Use a copy of a u64 value to protect against data race. This is only
640  * applicable for 32-bits architectures.
641  */
642 #ifdef CONFIG_64BIT
643 # define u64_u32_load_copy(var, copy)		var
644 # define u64_u32_store_copy(var, copy, val)	(var = val)
645 #else
646 # define u64_u32_load_copy(var, copy)					\
647 ({									\
648 	u64 __val, __val_copy;						\
649 	do {								\
650 		__val_copy = copy;					\
651 		/*							\
652 		 * paired with u64_u32_store_copy(), ordering access	\
653 		 * to var and copy.					\
654 		 */							\
655 		smp_rmb();						\
656 		__val = var;						\
657 	} while (__val != __val_copy);					\
658 	__val;								\
659 })
660 # define u64_u32_store_copy(var, copy, val)				\
661 do {									\
662 	typeof(val) __val = (val);					\
663 	var = __val;							\
664 	/*								\
665 	 * paired with u64_u32_load_copy(), ordering access to var and	\
666 	 * copy.							\
667 	 */								\
668 	smp_wmb();							\
669 	copy = __val;							\
670 } while (0)
671 #endif
672 # define u64_u32_load(var)		u64_u32_load_copy(var, var##_copy)
673 # define u64_u32_store(var, val)	u64_u32_store_copy(var, var##_copy, val)
674 
675 struct balance_callback {
676 	struct balance_callback *next;
677 	void (*func)(struct rq *rq);
678 };
679 
680 /* Fair scheduling SCHED_{NORMAL,BATCH,IDLE} related fields in a runqueue: */
681 struct cfs_rq {
682 	struct load_weight	load;
683 	unsigned int		nr_queued;
684 	unsigned int		h_nr_queued;		/* SCHED_{NORMAL,BATCH,IDLE} */
685 	unsigned int		h_nr_runnable;		/* SCHED_{NORMAL,BATCH,IDLE} */
686 	unsigned int		h_nr_idle;		/* SCHED_IDLE */
687 
688 	s64			sum_w_vruntime;
689 	u64			sum_weight;
690 	u64			zero_vruntime;
691 	unsigned int		sum_shift;
692 
693 #ifdef CONFIG_SCHED_CORE
694 	unsigned int		forceidle_seq;
695 	u64			zero_vruntime_fi;
696 #endif
697 
698 	struct rb_root_cached	tasks_timeline;
699 
700 	/*
701 	 * 'curr' points to the currently running entity on this cfs_rq.
702 	 * It is set to NULL otherwise (i.e when none are currently running).
703 	 */
704 	struct sched_entity	*curr;
705 	struct sched_entity	*next;
706 
707 	/*
708 	 * CFS load tracking
709 	 */
710 	struct sched_entity	*h_curr;
711 	struct sched_avg	avg;
712 #ifndef CONFIG_64BIT
713 	u64			last_update_time_copy;
714 #endif
715 	struct {
716 		raw_spinlock_t	lock ____cacheline_aligned;
717 		int		nr;
718 		unsigned long	load_avg;
719 		unsigned long	util_avg;
720 		unsigned long	runnable_avg;
721 	} removed;
722 
723 #ifdef CONFIG_FAIR_GROUP_SCHED
724 	u64			last_update_tg_load_avg;
725 	unsigned long		tg_load_avg_contrib;
726 	unsigned long		tg_runnable_avg_contrib;
727 	long			propagate;
728 	long			prop_runnable_sum;
729 
730 	/*
731 	 *   h_load = weight * f(tg)
732 	 *
733 	 * Where f(tg) is the recursive weight fraction assigned to
734 	 * this group.
735 	 */
736 	unsigned long		h_load;
737 	u64			last_h_load_update;
738 	struct sched_entity	*h_load_next;
739 
740 	struct rq		*rq;	/* CPU runqueue to which this cfs_rq is attached */
741 
742 	/*
743 	 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
744 	 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
745 	 * (like users, containers etc.)
746 	 *
747 	 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
748 	 * This list is used during load balance.
749 	 */
750 	int			on_list;
751 	struct list_head	leaf_cfs_rq_list;
752 	struct task_group	*tg;	/* Group that "owns" this runqueue */
753 
754 	/* Locally cached copy of our task_group's idle value */
755 	int			idle;
756 
757 # ifdef CONFIG_CFS_BANDWIDTH
758 	int			runtime_enabled;
759 	s64			runtime_remaining;
760 
761 	u64			throttled_pelt_idle;
762 #  ifndef CONFIG_64BIT
763 	u64                     throttled_pelt_idle_copy;
764 #  endif
765 	u64			throttled_clock;
766 	u64			throttled_clock_pelt;
767 	u64			throttled_clock_pelt_time;
768 	u64			throttled_clock_self;
769 	u64			throttled_clock_self_time;
770 	bool			throttled:1;
771 	bool			pelt_clock_throttled:1;
772 	int			throttle_count;
773 	struct list_head	throttled_list;
774 	struct list_head	throttled_csd_list;
775 	struct list_head        throttled_limbo_list;
776 # endif /* CONFIG_CFS_BANDWIDTH */
777 #endif /* CONFIG_FAIR_GROUP_SCHED */
778 };
779 
780 #ifdef CONFIG_SCHED_CLASS_EXT
781 /* scx_rq->flags, protected by the rq lock */
782 enum scx_rq_flags {
783 	/*
784 	 * Set while the core has the rq online, see rq_online_scx() and
785 	 * rq_offline_scx(). A hotplugged cpu starts scheduling before
786 	 * rq_online_scx(). ops.enqueue() and ops.dispatch() are called for the
787 	 * cpu only while it is set, see scx_rq_online() and
788 	 * scx_dispatch_sched().
789 	 */
790 	SCX_RQ_ONLINE		= 1 << 0,
791 	SCX_RQ_CAN_STOP_TICK	= 1 << 1,
792 	/*
793 	 * Set while the root considers the cpu online, from its
794 	 * ops.cpu_online() until its ops.cpu_offline(). ecaps are delivered
795 	 * only while it is set. %SCX_RQ_ONLINE follows the core's rq state
796 	 * instead, which changes ahead of the root's view on both edges.
797 	 */
798 	SCX_RQ_OPS_ONLINE	= 1 << 2,
799 	SCX_RQ_CLK_VALID	= 1 << 5, /* RQ clock is fresh and valid */
800 	SCX_RQ_BAL_CB_PENDING	= 1 << 6, /* must queue a cb after dispatching */
801 	SCX_RQ_SUB_IDLE_RENOTIFY	= 1 << 7, /* sub-scheds are owed update_idle() */
802 	SCX_RQ_ROOT_IDLE_RENOTIFY	= 1 << 8, /* the root is owed update_idle() */
803 	SCX_RQ_PROXY_RETRY	= 1 << 9, /* proxy-rejected tasks need retry */
804 	SCX_RQ_PROXY_TICK	= 1 << 10, /* proxy execution requires the tick */
805 	SCX_RQ_PROXY_PENDING	= 1 << 11, /* blocked donor awaits proxy resolution */
806 	SCX_RQ_PROXY_BLOCKING	= 1 << 12, /* donor is being removed for sched change */
807 
808 	SCX_RQ_IN_WAKEUP	= 1 << 16,
809 	SCX_RQ_IN_DISPATCH	= 1 << 17,
810 };
811 
812 /* per-rq rescue execution state, see scx_rescue_timerfn() */
813 struct scx_rq_rescue {
814 	struct scx_dispatch_q	dsq;			/* stranded tasks awaiting rescue */
815 	s64			budget;			/* execution token bucket, ns */
816 	u64			clock;			/* last budget accrual timestamp */
817 	struct task_struct	*curr;			/* task being rescued, one at a time */
818 	s64			slice;			/* curr's admitted slice */
819 	s64			served;			/* curr's served time */
820 	struct timer_list	timer;			/* paces admission and escalation */
821 	u64			kill_at;		/* last ejection, init before any */
822 };
823 
824 struct scx_rq {
825 	struct scx_dispatch_q	local_dsq;
826 	struct scx_dispatch_q	reject_dsq;		/* staging for rejected tasks */
827 #ifdef CONFIG_EXT_SUB_SCHED
828 	struct scx_rq_rescue	rescue;
829 #endif
830 	struct list_head	runnable_list;		/* runnable tasks on this rq */
831 	struct list_head	ddsp_deferred_locals;	/* deferred ddsps from enq */
832 	/* both stashed across the activate_task() in move_remote_task_to_local_dsq() */
833 	u64			remote_activate_enq_flags;
834 	struct scx_sched	*remote_activate_sch;
835 	u32			nr_running;
836 	u32			cpuperf_target;		/* [0, SCHED_CAPACITY_SCALE] */
837 	bool			in_select_cpu;
838 	bool			cpu_released;
839 	u32			flags;
840 	u32			nr_immed;		/* ENQ_IMMED tasks on local_dsq */
841 #ifdef CONFIG_SCHED_CORE
842 	u32			lock_drop_seq;	/* nr dispatch lock releases */
843 #endif
844 	u64			clock;			/* current per-rq clock -- see scx_bpf_now() */
845 #ifdef CONFIG_EXT_SUB_SCHED
846 	/* sched of the open running session, see scx_cid_sched_update() */
847 	struct scx_sched	*sched;
848 	struct llist_head	ecaps_to_sync;		/* pending ecaps syncs */
849 	struct task_struct	*sub_dispatch_prev;
850 	u32			nr_ecaps_offline;	/* marked syncs a down waits for */
851 #endif
852 	bool			kick_sync_pending;
853 	cpumask_var_t		cpus_to_sync;
854 	unsigned long		kick_sync;
855 
856 	struct list_head	sched_pcpus_to_kick;	/* see kick_cpus_irq_workfn() */
857 
858 	raw_spinlock_t		deferred_reenq_lock;
859 	struct list_head	deferred_reenq_locals;	/* scheds requesting reenq of local DSQ */
860 	struct list_head	deferred_reenq_users;	/* user DSQs requesting reenq */
861 	struct balance_callback	deferred_bal_cb;
862 	struct balance_callback	kick_sync_bal_cb;
863 #ifdef CONFIG_NO_HZ_FULL
864 	struct balance_callback	proxy_tick_bal_cb;
865 #endif
866 	struct irq_work		deferred_irq_work;
867 	struct irq_work		kick_cpus_irq_work;
868 	struct irq_work		resched_irq_work;
869 };
870 #endif /* CONFIG_SCHED_CLASS_EXT */
871 
872 static inline int rt_bandwidth_enabled(void)
873 {
874 	return sysctl_sched_rt_runtime >= 0;
875 }
876 
877 /* RT IPI pull logic requires IRQ_WORK */
878 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
879 # define HAVE_RT_PUSH_IPI
880 #endif
881 
882 /* Real-Time classes' related field in a runqueue: */
883 struct rt_rq {
884 	struct rt_prio_array	active;
885 	unsigned int		rt_nr_running;
886 	unsigned int		rr_nr_running;
887 	struct {
888 		int		curr; /* highest queued rt task prio */
889 		int		next; /* next highest */
890 	} highest_prio;
891 	bool			overloaded;
892 	struct plist_head	pushable_tasks;
893 
894 	int			rt_queued;
895 
896 #ifdef CONFIG_RT_GROUP_SCHED
897 	int			rt_throttled;
898 	u64			rt_time; /* consumed RT time, goes up in update_curr_rt */
899 	u64			rt_runtime; /* allotted RT time, "slice" from rt_bandwidth, RT sharing/balancing */
900 	/* Nests inside the rq lock: */
901 	raw_spinlock_t		rt_runtime_lock;
902 
903 	unsigned int		rt_nr_boosted;
904 
905 	struct rq		*rq; /* this is always top-level rq, cache? */
906 #endif
907 #ifdef CONFIG_CGROUP_SCHED
908 	struct task_group	*tg; /* this tg has "this" rt_rq on given CPU for runnable entities */
909 #endif
910 };
911 
912 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
913 {
914 	return rt_rq->rt_queued && rt_rq->rt_nr_running;
915 }
916 
917 /* Deadline class' related fields in a runqueue */
918 struct dl_rq {
919 	/* runqueue is an rbtree, ordered by deadline */
920 	struct rb_root_cached	root;
921 
922 	unsigned int		dl_nr_running;
923 
924 	/*
925 	 * Deadline values of the currently executing and the
926 	 * earliest ready task on this rq. Caching these facilitates
927 	 * the decision whether or not a ready but not running task
928 	 * should migrate somewhere else.
929 	 */
930 	struct {
931 		u64		curr;
932 		u64		next;
933 	} earliest_dl;
934 
935 	bool			overloaded;
936 
937 	struct sched_dl_entity	*curr;
938 	/*
939 	 * Tasks on this rq that can be pushed away. They are kept in
940 	 * an rb-tree, ordered by tasks' deadlines, with caching
941 	 * of the leftmost (earliest deadline) element.
942 	 */
943 	struct rb_root_cached	pushable_dl_tasks_root;
944 
945 	/*
946 	 * "Active utilization" for this runqueue: increased when a
947 	 * task wakes up (becomes TASK_RUNNING) and decreased when a
948 	 * task blocks
949 	 */
950 	u64			running_bw;
951 
952 	/*
953 	 * Utilization of the tasks "assigned" to this runqueue (including
954 	 * the tasks that are in runqueue and the tasks that executed on this
955 	 * CPU and blocked). Increased when a task moves to this runqueue, and
956 	 * decreased when the task moves away (migrates, changes scheduling
957 	 * policy, or terminates).
958 	 * This is needed to compute the "inactive utilization" for the
959 	 * runqueue (inactive utilization = this_bw - running_bw).
960 	 */
961 	u64			this_bw;
962 	u64			extra_bw;
963 
964 	/*
965 	 * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM
966 	 * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB).
967 	 */
968 	u64			max_bw;
969 
970 	/*
971 	 * Inverse of the fraction of CPU utilization that can be reclaimed
972 	 * by the GRUB algorithm.
973 	 */
974 	u64			bw_ratio;
975 };
976 
977 #ifdef CONFIG_FAIR_GROUP_SCHED
978 /* Check whether a task group is root tg */
979 #define is_root_task_group(tg) ((tg) == &root_task_group)
980 /* An entity is a task if it doesn't "own" a runqueue */
981 #define entity_is_task(se)	(!se->my_q)
982 
983 static inline void se_update_runnable(struct sched_entity *se)
984 {
985 	if (!entity_is_task(se))
986 		se->runnable_weight = se->my_q->h_nr_runnable;
987 }
988 
989 static inline long se_runnable(struct sched_entity *se)
990 {
991 	if (se->sched_delayed)
992 		return false;
993 
994 	if (entity_is_task(se))
995 		return !!se->on_rq;
996 	else
997 		return se->runnable_weight;
998 }
999 
1000 #else /* !CONFIG_FAIR_GROUP_SCHED: */
1001 
1002 #define entity_is_task(se)	1
1003 
1004 static inline void se_update_runnable(struct sched_entity *se) { }
1005 
1006 static inline long se_runnable(struct sched_entity *se)
1007 {
1008 	if (se->sched_delayed)
1009 		return false;
1010 
1011 	return !!se->on_rq;
1012 }
1013 
1014 #endif /* !CONFIG_FAIR_GROUP_SCHED */
1015 
1016 /*
1017  * XXX we want to get rid of these helpers and use the full load resolution.
1018  */
1019 static inline long se_weight(struct sched_entity *se)
1020 {
1021 	return scale_load_down(se->load.weight);
1022 }
1023 
1024 
1025 static inline bool sched_asym_prefer(int a, int b)
1026 {
1027 	return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
1028 }
1029 
1030 struct perf_domain {
1031 	struct em_perf_domain *em_pd;
1032 	struct perf_domain *next;
1033 	struct rcu_head rcu;
1034 };
1035 
1036 /*
1037  * We add the notion of a root-domain which will be used to define per-domain
1038  * variables. Each exclusive cpuset essentially defines an island domain by
1039  * fully partitioning the member CPUs from any other cpuset. Whenever a new
1040  * exclusive cpuset is created, we also create and attach a new root-domain
1041  * object.
1042  *
1043  */
1044 struct root_domain {
1045 	atomic_t		refcount;
1046 	atomic_t		rto_count;
1047 	struct rcu_head		rcu;
1048 	cpumask_var_t		span;
1049 	cpumask_var_t		online;
1050 
1051 	/*
1052 	 * Indicate pullable load on at least one CPU, e.g:
1053 	 * - More than one runnable task
1054 	 * - Running task is misfit
1055 	 */
1056 	bool			overloaded;
1057 
1058 	/* Indicate one or more CPUs over-utilized (tipping point) */
1059 	bool			overutilized;
1060 
1061 	/*
1062 	 * The bit corresponding to a CPU gets set here if such CPU has more
1063 	 * than one runnable -deadline task (as it is below for RT tasks).
1064 	 */
1065 	cpumask_var_t		dlo_mask;
1066 	atomic_t		dlo_count;
1067 	struct dl_bw		dl_bw;
1068 	struct cpudl		cpudl;
1069 
1070 	/*
1071 	 * Indicate whether a root_domain's dl_bw has been checked or
1072 	 * updated. It's monotonously increasing value.
1073 	 *
1074 	 * Also, some corner cases, like 'wrap around' is dangerous, but given
1075 	 * that u64 is 'big enough'. So that shouldn't be a concern.
1076 	 */
1077 	u64 visit_cookie;
1078 
1079 #ifdef HAVE_RT_PUSH_IPI
1080 	/*
1081 	 * For IPI pull requests, loop across the rto_mask.
1082 	 */
1083 	struct irq_work		rto_push_work;
1084 	raw_spinlock_t		rto_lock;
1085 	/* These are only updated and read within rto_lock */
1086 	int			rto_loop;
1087 	int			rto_cpu;
1088 	/* These atomics are updated outside of a lock */
1089 	atomic_t		rto_loop_next;
1090 	atomic_t		rto_loop_start;
1091 #endif /* HAVE_RT_PUSH_IPI */
1092 	/*
1093 	 * The "RT overload" flag: it gets set if a CPU has more than
1094 	 * one runnable RT task.
1095 	 */
1096 	cpumask_var_t		rto_mask;
1097 	struct cpupri		cpupri;
1098 
1099 	/*
1100 	 * NULL-terminated list of performance domains intersecting with the
1101 	 * CPUs of the rd. Protected by RCU.
1102 	 */
1103 	struct perf_domain __rcu *pd;
1104 };
1105 
1106 extern void init_defrootdomain(void);
1107 extern int sched_init_domains(const struct cpumask *cpu_map);
1108 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
1109 extern void sched_get_rd(struct root_domain *rd);
1110 extern void sched_put_rd(struct root_domain *rd);
1111 
1112 static inline int get_rd_overloaded(struct root_domain *rd)
1113 {
1114 	return READ_ONCE(rd->overloaded);
1115 }
1116 
1117 static inline void set_rd_overloaded(struct root_domain *rd, int status)
1118 {
1119 	if (get_rd_overloaded(rd) != status)
1120 		WRITE_ONCE(rd->overloaded, status);
1121 }
1122 
1123 #ifdef HAVE_RT_PUSH_IPI
1124 extern void rto_push_irq_work_func(struct irq_work *work);
1125 #endif
1126 
1127 #ifdef CONFIG_UCLAMP_TASK
1128 /*
1129  * struct uclamp_bucket - Utilization clamp bucket
1130  * @value: utilization clamp value for tasks on this clamp bucket
1131  * @tasks: number of RUNNABLE tasks on this clamp bucket
1132  *
1133  * Keep track of how many tasks are RUNNABLE for a given utilization
1134  * clamp value.
1135  */
1136 struct uclamp_bucket {
1137 	unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
1138 	unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
1139 };
1140 
1141 /*
1142  * struct uclamp_rq - rq's utilization clamp
1143  * @value: currently active clamp values for a rq
1144  * @bucket: utilization clamp buckets affecting a rq
1145  *
1146  * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
1147  * A clamp value is affecting a rq when there is at least one task RUNNABLE
1148  * (or actually running) with that value.
1149  *
1150  * There are up to UCLAMP_CNT possible different clamp values, currently there
1151  * are only two: minimum utilization and maximum utilization.
1152  *
1153  * All utilization clamping values are MAX aggregated, since:
1154  * - for util_min: we want to run the CPU at least at the max of the minimum
1155  *   utilization required by its currently RUNNABLE tasks.
1156  * - for util_max: we want to allow the CPU to run up to the max of the
1157  *   maximum utilization allowed by its currently RUNNABLE tasks.
1158  *
1159  * Since on each system we expect only a limited number of different
1160  * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
1161  * the metrics required to compute all the per-rq utilization clamp values.
1162  */
1163 struct uclamp_rq {
1164 	unsigned int value;
1165 	struct uclamp_bucket bucket[UCLAMP_BUCKETS];
1166 };
1167 
1168 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
1169 #endif /* CONFIG_UCLAMP_TASK */
1170 
1171 /*
1172  * This is the main, per-CPU runqueue data structure.
1173  *
1174  * Locking rule: those places that want to lock multiple runqueues
1175  * (such as the load balancing or the thread migration code), lock
1176  * acquire operations must be ordered by ascending &runqueue.
1177  */
1178 struct rq {
1179 	/*
1180 	 * The following members are loaded together, without holding the
1181 	 * rq->lock, in an extremely hot loop in update_sg_lb_stats()
1182 	 * (called from pick_next_task()). To reduce cache pollution from
1183 	 * this operation, they are placed together on this dedicated cache
1184 	 * line. Even though some of them are frequently modified, they are
1185 	 * loaded much more frequently than they are stored.
1186 	 */
1187 	unsigned int		nr_running;
1188 #ifdef CONFIG_NUMA_BALANCING
1189 	unsigned int		nr_numa_running;
1190 	unsigned int		nr_preferred_running;
1191 #endif
1192 	unsigned int		ttwu_pending;
1193 	unsigned long		cpu_capacity;
1194 #ifdef CONFIG_SCHED_PROXY_EXEC
1195 	struct task_struct __rcu	*donor;  /* Scheduling context */
1196 	struct task_struct __rcu	*curr;   /* Execution context */
1197 #else
1198 	union {
1199 		struct task_struct __rcu *donor; /* Scheduler context */
1200 		struct task_struct __rcu *curr;  /* Execution context */
1201 	};
1202 #endif
1203 	struct task_struct	*idle;
1204 	/* padding left here deliberately */
1205 
1206 	/*
1207 	 * The next cacheline holds the (hot) runqueue lock, as well as
1208 	 * some other less performance-critical fields.
1209 	 */
1210 	u64			nr_switches	____cacheline_aligned;
1211 
1212 	/* runqueue lock: */
1213 	raw_spinlock_t		__lock;
1214 
1215 #ifdef CONFIG_NO_HZ_COMMON
1216 	unsigned int		nohz_tick_stopped;
1217 	atomic_t		nohz_flags;
1218 	unsigned int		has_blocked_load;
1219 	unsigned long		last_blocked_load_update_tick;
1220 	call_single_data_t	nohz_csd;
1221 #endif /* CONFIG_NO_HZ_COMMON */
1222 
1223 #ifdef CONFIG_UCLAMP_TASK
1224 	/* Utilization clamp values based on CPU's RUNNABLE tasks */
1225 	struct uclamp_rq	uclamp[UCLAMP_CNT] ____cacheline_aligned;
1226 	unsigned int		uclamp_flags;
1227 #define UCLAMP_FLAG_IDLE 0x01
1228 #endif
1229 
1230 	struct cfs_rq		cfs;
1231 	struct rt_rq		rt;
1232 	struct dl_rq		dl;
1233 #ifdef CONFIG_SCHED_CLASS_EXT
1234 	struct scx_rq		scx;
1235 	struct sched_dl_entity	ext_server;
1236 #endif
1237 #ifdef CONFIG_SCHED_CACHE
1238 	raw_spinlock_t		cpu_epoch_lock ____cacheline_aligned;
1239 	u64			cpu_runtime;
1240 	unsigned long		cpu_epoch;
1241 	unsigned long		cpu_epoch_next;
1242 #endif
1243 
1244 	struct sched_dl_entity	fair_server;
1245 
1246 #ifdef CONFIG_FAIR_GROUP_SCHED
1247 	/* list of leaf cfs_rq on this CPU: */
1248 	struct list_head	leaf_cfs_rq_list;
1249 	struct list_head	*tmp_alone_branch;
1250 #endif /* CONFIG_FAIR_GROUP_SCHED */
1251 
1252 #ifdef CONFIG_NUMA_BALANCING
1253 	unsigned int		numa_migrate_on;
1254 #endif
1255 
1256 #ifdef CONFIG_SCHED_CACHE
1257 	unsigned int		nr_pref_llc_running;
1258 	unsigned int		nr_llc_running;
1259 #endif
1260 
1261 	/*
1262 	 * This is part of a global counter where only the total sum
1263 	 * over all CPUs matters. A task can increase this counter on
1264 	 * one CPU and if it got migrated afterwards it may decrease
1265 	 * it on another CPU. Always updated under the runqueue lock:
1266 	 */
1267 	unsigned long		nr_uninterruptible;
1268 
1269 	struct sched_dl_entity	*dl_server;
1270 	struct task_struct	*stop;
1271 	const struct sched_class *next_class;
1272 	unsigned long		next_balance;
1273 	struct mm_struct	*prev_mm;
1274 
1275 	/*
1276 	 * The following fields of clock data are frequently referenced
1277 	 * and updated together, and should go on their own cache line.
1278 	 */
1279 	u64			clock_task ____cacheline_aligned;
1280 	u64			clock_pelt;
1281 	u64			clock;
1282 	unsigned long		lost_idle_time;
1283 	unsigned int		clock_update_flags;
1284 	u64			clock_pelt_idle;
1285 	u64			clock_idle;
1286 
1287 #ifndef CONFIG_64BIT
1288 	u64			clock_pelt_idle_copy;
1289 	u64			clock_idle_copy;
1290 #endif
1291 
1292 	u64 last_seen_need_resched_ns;
1293 	int ticks_without_resched;
1294 
1295 #ifdef CONFIG_MEMBARRIER
1296 	int membarrier_state;
1297 #endif
1298 
1299 	struct root_domain		*rd;
1300 	struct sched_domain __rcu	*sd;
1301 
1302 	struct balance_callback *balance_callback;
1303 
1304 	unsigned char		nohz_idle_balance;
1305 	unsigned char		idle_balance;
1306 
1307 	unsigned long		misfit_task_load;
1308 
1309 	/* For active balancing */
1310 	int			active_balance;
1311 	int			push_cpu;
1312 	struct cpu_stop_work	active_balance_work;
1313 
1314 	/* CPU of this runqueue: */
1315 	int			cpu;
1316 	int			online;
1317 
1318 	struct list_head cfs_tasks;
1319 
1320 	struct sched_avg	avg_rt;
1321 	struct sched_avg	avg_dl;
1322 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1323 	struct sched_avg	avg_irq;
1324 #endif
1325 #ifdef CONFIG_SCHED_HW_PRESSURE
1326 	struct sched_avg	avg_hw;
1327 #endif
1328 	u64			idle_stamp;
1329 	u64			avg_idle;
1330 
1331 	/* This is used to determine avg_idle's max value */
1332 	u64			max_idle_balance_cost;
1333 
1334 #ifdef CONFIG_HOTPLUG_CPU
1335 	struct rcuwait		hotplug_wait;
1336 #endif
1337 
1338 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1339 	u64			prev_irq_time;
1340 	u64			psi_irq_time;
1341 #endif
1342 #ifdef CONFIG_PARAVIRT
1343 	u64			prev_steal_time;
1344 #endif
1345 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1346 	u64			prev_steal_time_rq;
1347 #endif
1348 #ifdef CONFIG_PREFERRED_CPU
1349 	bool			npc_push_work_pending;
1350 #endif
1351 
1352 	/* calc_load related fields */
1353 	unsigned long		calc_load_update;
1354 	long			calc_load_active;
1355 
1356 #ifdef CONFIG_SCHED_HRTICK
1357 	call_single_data_t	hrtick_csd;
1358 	struct hrtimer		hrtick_timer;
1359 	ktime_t			hrtick_time;
1360 	ktime_t			hrtick_delay;
1361 	unsigned int		hrtick_sched;
1362 #endif
1363 
1364 #ifdef CONFIG_SCHEDSTATS
1365 	/* latency stats */
1366 	struct sched_info	rq_sched_info;
1367 	unsigned long long	rq_cpu_time;
1368 
1369 	/* sys_sched_yield() stats */
1370 	unsigned int		yld_count;
1371 
1372 	/* schedule() stats */
1373 	unsigned int		sched_count;
1374 	unsigned int		sched_goidle;
1375 
1376 	/* try_to_wake_up() stats */
1377 	unsigned int		ttwu_count;
1378 	unsigned int		ttwu_local;
1379 #endif
1380 
1381 #ifdef CONFIG_CPU_IDLE
1382 	/* Must be inspected within a RCU lock section */
1383 	struct cpuidle_state	*idle_state;
1384 #endif
1385 
1386 	unsigned int		nr_pinned;
1387 	unsigned int		push_busy;
1388 	struct cpu_stop_work	push_work;
1389 
1390 #ifdef CONFIG_SCHED_CORE
1391 	/* per rq */
1392 	struct rq		*core;
1393 	struct task_struct	*core_pick;
1394 	struct sched_dl_entity	*core_dl_server;
1395 	unsigned int		core_enabled;
1396 	struct rb_root		core_tree;
1397 
1398 	/* shared state -- careful with sched_core_cpu_deactivate() */
1399 	unsigned int		core_task_seq;
1400 	unsigned int		core_pick_seq;
1401 	unsigned long		core_cookie;
1402 	unsigned int		core_forceidle_count;
1403 	unsigned int		core_forceidle_seq;
1404 	unsigned int		core_forceidle_occupation;
1405 	u64			core_forceidle_start;
1406 	unsigned int		core_pick_in_flight;
1407 #endif /* CONFIG_SCHED_CORE */
1408 
1409 	/* Scratch cpumask to be temporarily used under rq_lock */
1410 	cpumask_var_t		scratch_mask;
1411 
1412 #ifdef CONFIG_CFS_BANDWIDTH
1413 	call_single_data_t	cfsb_csd;
1414 	struct list_head	cfsb_csd_list;
1415 #endif
1416 
1417 	atomic_t		nr_iowait;
1418 } __no_randomize_layout;
1419 
1420 #ifdef CONFIG_FAIR_GROUP_SCHED
1421 
1422 /* CPU runqueue to which this cfs_rq is attached */
1423 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1424 {
1425 	return cfs_rq->rq;
1426 }
1427 
1428 #else /* !CONFIG_FAIR_GROUP_SCHED: */
1429 
1430 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1431 {
1432 	return container_of(cfs_rq, struct rq, cfs);
1433 }
1434 #endif /* !CONFIG_FAIR_GROUP_SCHED */
1435 
1436 static inline int cpu_of(struct rq *rq)
1437 {
1438 	return rq->cpu;
1439 }
1440 
1441 #define MDF_PUSH		0x01
1442 
1443 static inline bool is_migration_disabled(struct task_struct *p)
1444 {
1445 	return p->migration_disabled;
1446 }
1447 
1448 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1449 DECLARE_PER_CPU(struct rnd_state, sched_rnd_state);
1450 
1451 static inline u32 sched_rng(void)
1452 {
1453 	return prandom_u32_state(this_cpu_ptr(&sched_rnd_state));
1454 }
1455 
1456 static __always_inline struct rq *__this_rq(void)
1457 {
1458 	return this_cpu_ptr(&runqueues);
1459 }
1460 
1461 #define cpu_rq(cpu)		(&per_cpu(runqueues, (cpu)))
1462 #define this_rq()		__this_rq()
1463 #define task_rq(p)		cpu_rq(task_cpu(p))
1464 #define cpu_curr(cpu)		(cpu_rq(cpu)->curr)
1465 #define raw_rq()		raw_cpu_ptr(&runqueues)
1466 
1467 static inline bool idle_rq(struct rq *rq)
1468 {
1469 	return rq->curr == rq->idle && !rq->nr_running && !rq->ttwu_pending;
1470 }
1471 
1472 /**
1473  * available_idle_cpu - is a given CPU idle for enqueuing work.
1474  * @cpu: the CPU in question.
1475  *
1476  * Return: 1 if the CPU is currently idle. 0 otherwise.
1477  */
1478 static inline bool available_idle_cpu(int cpu)
1479 {
1480 	if (!idle_rq(cpu_rq(cpu)))
1481 		return 0;
1482 
1483 	if (vcpu_is_preempted(cpu))
1484 		return 0;
1485 
1486 	return 1;
1487 }
1488 
1489 #ifdef CONFIG_SCHED_PROXY_EXEC
1490 static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1491 {
1492 	rcu_assign_pointer(rq->donor, t);
1493 }
1494 #else
1495 static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1496 {
1497 	/* Do nothing */
1498 }
1499 #endif
1500 
1501 #ifdef CONFIG_SCHED_CORE
1502 static inline struct cpumask *sched_group_span(struct sched_group *sg);
1503 
1504 DECLARE_STATIC_KEY_FALSE(__sched_core_enabled);
1505 
1506 static inline bool sched_core_enabled(struct rq *rq)
1507 {
1508 	return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled;
1509 }
1510 
1511 static inline bool sched_core_disabled(void)
1512 {
1513 	return !static_branch_unlikely(&__sched_core_enabled);
1514 }
1515 
1516 /*
1517  * Be careful with this function; not for general use. The return value isn't
1518  * stable unless you actually hold a relevant rq->__lock.
1519  */
1520 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1521 {
1522 	if (sched_core_enabled(rq))
1523 		return &rq->core->__lock;
1524 
1525 	return &rq->__lock;
1526 }
1527 
1528 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1529 	__returns_ctx_lock(rq_lockp(rq)) /* alias them */
1530 {
1531 	if (rq->core_enabled)
1532 		return &rq->core->__lock;
1533 
1534 	return &rq->__lock;
1535 }
1536 
1537 extern bool
1538 cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);
1539 
1540 extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
1541 
1542 /*
1543  * Helpers to check if the CPU's core cookie matches with the task's cookie
1544  * when core scheduling is enabled.
1545  * A special case is that the task's cookie always matches with CPU's core
1546  * cookie if the CPU is in an idle core.
1547  */
1548 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1549 {
1550 	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1551 	if (!sched_core_enabled(rq))
1552 		return true;
1553 
1554 	return rq->core->core_cookie == p->core_cookie;
1555 }
1556 
1557 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1558 {
1559 	bool idle_core = true;
1560 	int cpu;
1561 
1562 	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1563 	if (!sched_core_enabled(rq))
1564 		return true;
1565 
1566 	if (rq->core->core_cookie == p->core_cookie)
1567 		return true;
1568 
1569 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
1570 		if (!available_idle_cpu(cpu)) {
1571 			idle_core = false;
1572 			break;
1573 		}
1574 	}
1575 
1576 	/*
1577 	 * A CPU in an idle core is always the best choice for tasks with
1578 	 * cookies.
1579 	 */
1580 	return idle_core;
1581 }
1582 
1583 static inline bool sched_group_cookie_match(struct rq *rq,
1584 					    struct task_struct *p,
1585 					    struct sched_group *group)
1586 {
1587 	int cpu;
1588 
1589 	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1590 	if (!sched_core_enabled(rq))
1591 		return true;
1592 
1593 	for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
1594 		if (sched_core_cookie_match(cpu_rq(cpu), p))
1595 			return true;
1596 	}
1597 	return false;
1598 }
1599 
1600 static inline bool sched_core_enqueued(struct task_struct *p)
1601 {
1602 	return !RB_EMPTY_NODE(&p->core_node);
1603 }
1604 
1605 extern void sched_core_enqueue(struct rq *rq, struct task_struct *p);
1606 extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags);
1607 
1608 extern void sched_core_get(void);
1609 extern void sched_core_put(void);
1610 
1611 static inline bool task_has_sched_core(struct task_struct *p)
1612 {
1613 	if (sched_core_disabled())
1614 		return false;
1615 
1616 	return !!p->core_cookie;
1617 }
1618 
1619 #else /* !CONFIG_SCHED_CORE: */
1620 
1621 static inline bool sched_core_enabled(struct rq *rq)
1622 {
1623 	return false;
1624 }
1625 
1626 static inline bool sched_core_disabled(void)
1627 {
1628 	return true;
1629 }
1630 
1631 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1632 {
1633 	return &rq->__lock;
1634 }
1635 
1636 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1637 	__returns_ctx_lock(rq_lockp(rq)) /* alias them */
1638 {
1639 	return &rq->__lock;
1640 }
1641 
1642 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1643 {
1644 	return true;
1645 }
1646 
1647 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1648 {
1649 	return true;
1650 }
1651 
1652 static inline bool sched_group_cookie_match(struct rq *rq,
1653 					    struct task_struct *p,
1654 					    struct sched_group *group)
1655 {
1656 	return true;
1657 }
1658 
1659 static inline bool task_has_sched_core(struct task_struct *p)
1660 {
1661 	return false;
1662 }
1663 
1664 #endif /* !CONFIG_SCHED_CORE */
1665 
1666 #ifdef CONFIG_RT_GROUP_SCHED
1667 # ifdef CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED
1668 DECLARE_STATIC_KEY_FALSE(rt_group_sched);
1669 static inline bool rt_group_sched_enabled(void)
1670 {
1671 	return static_branch_unlikely(&rt_group_sched);
1672 }
1673 # else /* !CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED: */
1674 DECLARE_STATIC_KEY_TRUE(rt_group_sched);
1675 static inline bool rt_group_sched_enabled(void)
1676 {
1677 	return static_branch_likely(&rt_group_sched);
1678 }
1679 # endif /* !CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED */
1680 #else /* !CONFIG_RT_GROUP_SCHED: */
1681 # define rt_group_sched_enabled()	false
1682 #endif /* !CONFIG_RT_GROUP_SCHED */
1683 
1684 static inline void lockdep_assert_rq_held(struct rq *rq)
1685 	__assumes_ctx_lock(__rq_lockp(rq))
1686 {
1687 	lockdep_assert_held(__rq_lockp(rq));
1688 }
1689 
1690 extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass)
1691 	__acquires(__rq_lockp(rq));
1692 
1693 extern bool raw_spin_rq_trylock(struct rq *rq)
1694 	__cond_acquires(true, __rq_lockp(rq));
1695 
1696 static inline void raw_spin_rq_lock(struct rq *rq)
1697 	__acquires(__rq_lockp(rq))
1698 {
1699 	raw_spin_rq_lock_nested(rq, 0);
1700 }
1701 
1702 static inline void raw_spin_rq_unlock(struct rq *rq)
1703 	__releases(__rq_lockp(rq))
1704 {
1705 	raw_spin_unlock(rq_lockp(rq));
1706 }
1707 
1708 static inline void raw_spin_rq_lock_irq(struct rq *rq)
1709 	__acquires(__rq_lockp(rq))
1710 {
1711 	local_irq_disable();
1712 	raw_spin_rq_lock(rq);
1713 }
1714 
1715 static inline void raw_spin_rq_unlock_irq(struct rq *rq)
1716 	__releases(__rq_lockp(rq))
1717 {
1718 	raw_spin_rq_unlock(rq);
1719 	local_irq_enable();
1720 }
1721 
1722 static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq)
1723 	__acquires(__rq_lockp(rq))
1724 {
1725 	unsigned long flags;
1726 
1727 	local_irq_save(flags);
1728 	raw_spin_rq_lock(rq);
1729 
1730 	return flags;
1731 }
1732 
1733 static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags)
1734 	__releases(__rq_lockp(rq))
1735 {
1736 	raw_spin_rq_unlock(rq);
1737 	local_irq_restore(flags);
1738 }
1739 
1740 #define raw_spin_rq_lock_irqsave(rq, flags)	\
1741 do {						\
1742 	flags = _raw_spin_rq_lock_irqsave(rq);	\
1743 } while (0)
1744 
1745 extern void __update_idle_core(struct rq *rq);
1746 
1747 static inline void update_idle_core(struct rq *rq)
1748 {
1749 	if (sched_smt_active())
1750 		__update_idle_core(rq);
1751 }
1752 
1753 #ifdef CONFIG_FAIR_GROUP_SCHED
1754 static inline struct task_struct *task_of(struct sched_entity *se)
1755 {
1756 	WARN_ON_ONCE(!entity_is_task(se));
1757 	return container_of(se, struct task_struct, se);
1758 }
1759 
1760 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
1761 {
1762 	return p->se.cfs_rq;
1763 }
1764 
1765 /* runqueue on which this entity is (to be) queued */
1766 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1767 {
1768 	return se->cfs_rq;
1769 }
1770 
1771 /* runqueue "owned" by this group */
1772 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1773 {
1774 	return grp->my_q;
1775 }
1776 
1777 #else /* !CONFIG_FAIR_GROUP_SCHED: */
1778 
1779 #define task_of(_se)		container_of(_se, struct task_struct, se)
1780 
1781 static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p)
1782 {
1783 	return &task_rq(p)->cfs;
1784 }
1785 
1786 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1787 {
1788 	const struct task_struct *p = task_of(se);
1789 	struct rq *rq = task_rq(p);
1790 
1791 	return &rq->cfs;
1792 }
1793 
1794 /* runqueue "owned" by this group */
1795 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1796 {
1797 	return NULL;
1798 }
1799 
1800 #endif /* !CONFIG_FAIR_GROUP_SCHED */
1801 
1802 extern void update_rq_avg_idle(struct rq *rq);
1803 extern void update_rq_clock(struct rq *rq);
1804 
1805 /*
1806  * rq::clock_update_flags bits
1807  *
1808  * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1809  *  call to __schedule(). This is an optimisation to avoid
1810  *  neighbouring rq clock updates.
1811  *
1812  * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1813  *  in effect and calls to update_rq_clock() are being ignored.
1814  *
1815  * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1816  *  made to update_rq_clock() since the last time rq::lock was pinned.
1817  *
1818  * If inside of __schedule(), clock_update_flags will have been
1819  * shifted left (a left shift is a cheap operation for the fast path
1820  * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1821  *
1822  *	if (rq-clock_update_flags >= RQCF_UPDATED)
1823  *
1824  * to check if %RQCF_UPDATED is set. It'll never be shifted more than
1825  * one position though, because the next rq_unpin_lock() will shift it
1826  * back.
1827  */
1828 #define RQCF_REQ_SKIP		0x01
1829 #define RQCF_ACT_SKIP		0x02
1830 #define RQCF_UPDATED		0x04
1831 
1832 static inline void assert_clock_updated(struct rq *rq)
1833 {
1834 	/*
1835 	 * The only reason for not seeing a clock update since the
1836 	 * last rq_pin_lock() is if we're currently skipping updates.
1837 	 */
1838 	WARN_ON_ONCE(rq->clock_update_flags < RQCF_ACT_SKIP);
1839 }
1840 
1841 static inline u64 rq_clock(struct rq *rq)
1842 {
1843 	lockdep_assert_rq_held(rq);
1844 	assert_clock_updated(rq);
1845 
1846 	return rq->clock;
1847 }
1848 
1849 static inline u64 rq_clock_task(struct rq *rq)
1850 {
1851 	lockdep_assert_rq_held(rq);
1852 	assert_clock_updated(rq);
1853 
1854 	return rq->clock_task;
1855 }
1856 
1857 static inline void rq_clock_skip_update(struct rq *rq)
1858 {
1859 	lockdep_assert_rq_held(rq);
1860 	rq->clock_update_flags |= RQCF_REQ_SKIP;
1861 }
1862 
1863 /*
1864  * See rt task throttling, which is the only time a skip
1865  * request is canceled.
1866  */
1867 static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1868 {
1869 	lockdep_assert_rq_held(rq);
1870 	rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1871 }
1872 
1873 /*
1874  * During cpu offlining and rq wide unthrottling, we can trigger
1875  * an update_rq_clock() for several cfs and rt runqueues (Typically
1876  * when using list_for_each_entry_*)
1877  * rq_clock_start_loop_update() can be called after updating the clock
1878  * once and before iterating over the list to prevent multiple update.
1879  * After the iterative traversal, we need to call rq_clock_stop_loop_update()
1880  * to clear RQCF_ACT_SKIP of rq->clock_update_flags.
1881  */
1882 static inline void rq_clock_start_loop_update(struct rq *rq)
1883 {
1884 	lockdep_assert_rq_held(rq);
1885 	WARN_ON_ONCE(rq->clock_update_flags & RQCF_ACT_SKIP);
1886 	rq->clock_update_flags |= RQCF_ACT_SKIP;
1887 }
1888 
1889 static inline void rq_clock_stop_loop_update(struct rq *rq)
1890 {
1891 	lockdep_assert_rq_held(rq);
1892 	rq->clock_update_flags &= ~RQCF_ACT_SKIP;
1893 }
1894 
1895 struct rq_flags {
1896 	unsigned long flags;
1897 	struct pin_cookie cookie;
1898 	/*
1899 	 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1900 	 * current pin context is stashed here in case it needs to be
1901 	 * restored in rq_repin_lock().
1902 	 */
1903 	unsigned int clock_update_flags;
1904 };
1905 
1906 extern struct balance_callback balance_push_callback;
1907 
1908 #ifdef CONFIG_SCHED_CLASS_EXT
1909 extern const struct sched_class ext_sched_class;
1910 
1911 DECLARE_STATIC_KEY_FALSE(__scx_enabled);	/* SCX BPF scheduler loaded */
1912 DECLARE_STATIC_KEY_FALSE(__scx_switched_all);	/* all fair class tasks on SCX */
1913 
1914 #define scx_enabled()		static_branch_unlikely(&__scx_enabled)
1915 #define scx_switched_all()	static_branch_unlikely(&__scx_switched_all)
1916 
1917 static inline void scx_rq_clock_update(struct rq *rq, u64 clock)
1918 {
1919 	if (!scx_enabled())
1920 		return;
1921 	WRITE_ONCE(rq->scx.clock, clock);
1922 	smp_store_release(&rq->scx.flags, rq->scx.flags | SCX_RQ_CLK_VALID);
1923 }
1924 
1925 static inline void scx_rq_clock_invalidate(struct rq *rq)
1926 {
1927 	if (!scx_enabled())
1928 		return;
1929 	WRITE_ONCE(rq->scx.flags, rq->scx.flags & ~SCX_RQ_CLK_VALID);
1930 }
1931 
1932 #else /* !CONFIG_SCHED_CLASS_EXT: */
1933 #define scx_enabled()		false
1934 #define scx_switched_all()	false
1935 
1936 static inline void scx_rq_clock_update(struct rq *rq, u64 clock) {}
1937 static inline void scx_rq_clock_invalidate(struct rq *rq) {}
1938 #endif /* !CONFIG_SCHED_CLASS_EXT */
1939 
1940 static inline void assert_balance_callbacks_empty(struct rq *rq)
1941 {
1942 	WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_LOCKING) &&
1943 		     rq->balance_callback &&
1944 		     rq->balance_callback != &balance_push_callback);
1945 }
1946 
1947 /*
1948  * Lockdep annotation that avoids accidental unlocks; it's like a
1949  * sticky/continuous lockdep_assert_held().
1950  *
1951  * This avoids code that has access to 'struct rq *rq' (basically everything in
1952  * the scheduler) from accidentally unlocking the rq if they do not also have a
1953  * copy of the (on-stack) 'struct rq_flags rf'.
1954  *
1955  * Also see Documentation/locking/lockdep-design.rst.
1956  */
1957 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1958 {
1959 	rf->cookie = lockdep_pin_lock(__rq_lockp(rq));
1960 
1961 	rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1962 	rf->clock_update_flags = 0;
1963 	assert_balance_callbacks_empty(rq);
1964 }
1965 
1966 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1967 {
1968 	if (rq->clock_update_flags > RQCF_ACT_SKIP)
1969 		rf->clock_update_flags = RQCF_UPDATED;
1970 
1971 	scx_rq_clock_invalidate(rq);
1972 	lockdep_unpin_lock(__rq_lockp(rq), rf->cookie);
1973 }
1974 
1975 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1976 {
1977 	lockdep_repin_lock(__rq_lockp(rq), rf->cookie);
1978 
1979 	/*
1980 	 * Restore the value we stashed in @rf for this pin context.
1981 	 */
1982 	rq->clock_update_flags |= rf->clock_update_flags;
1983 }
1984 
1985 #define __task_rq_lock(...) __acquire_ret(___task_rq_lock(__VA_ARGS__), __rq_lockp(__ret))
1986 extern struct rq *___task_rq_lock(struct task_struct *p, struct rq_flags *rf) __acquires_ret;
1987 
1988 #define task_rq_lock(...) __acquire_ret(_task_rq_lock(__VA_ARGS__), __rq_lockp(__ret))
1989 extern struct rq *_task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1990 	__acquires(&p->pi_lock) __acquires_ret;
1991 
1992 static inline void
1993 __task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1994 	__releases(__rq_lockp(rq))
1995 {
1996 	rq_unpin_lock(rq, rf);
1997 	raw_spin_rq_unlock(rq);
1998 }
1999 
2000 static inline void
2001 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
2002 	__releases(__rq_lockp(rq), &p->pi_lock)
2003 {
2004 	__task_rq_unlock(rq, p, rf);
2005 	raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
2006 }
2007 
2008 DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct,
2009 		    _T->rq = task_rq_lock(_T->lock, &_T->rf),
2010 		    task_rq_unlock(_T->rq, _T->lock, &_T->rf),
2011 		    struct rq *rq; struct rq_flags rf)
2012 DECLARE_LOCK_GUARD_1_ATTRS(task_rq_lock, __acquires(_T->pi_lock), __releases((*(struct task_struct **)_T)->pi_lock))
2013 #define class_task_rq_lock_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(task_rq_lock, _T)
2014 
2015 DEFINE_LOCK_GUARD_1(__task_rq_lock, struct task_struct,
2016 		    _T->rq = __task_rq_lock(_T->lock, &_T->rf),
2017 		    __task_rq_unlock(_T->rq, _T->lock, &_T->rf),
2018 		    struct rq *rq; struct rq_flags rf)
2019 
2020 static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
2021 	__acquires(__rq_lockp(rq))
2022 {
2023 	raw_spin_rq_lock_irqsave(rq, rf->flags);
2024 	rq_pin_lock(rq, rf);
2025 }
2026 
2027 static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf)
2028 	__acquires(__rq_lockp(rq))
2029 {
2030 	raw_spin_rq_lock_irq(rq);
2031 	rq_pin_lock(rq, rf);
2032 }
2033 
2034 static inline void rq_lock(struct rq *rq, struct rq_flags *rf)
2035 	__acquires(__rq_lockp(rq))
2036 {
2037 	raw_spin_rq_lock(rq);
2038 	rq_pin_lock(rq, rf);
2039 }
2040 
2041 static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
2042 	__releases(__rq_lockp(rq))
2043 {
2044 	rq_unpin_lock(rq, rf);
2045 	raw_spin_rq_unlock_irqrestore(rq, rf->flags);
2046 }
2047 
2048 static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
2049 	__releases(__rq_lockp(rq))
2050 {
2051 	rq_unpin_lock(rq, rf);
2052 	raw_spin_rq_unlock_irq(rq);
2053 }
2054 
2055 static inline void rq_unlock(struct rq *rq, struct rq_flags *rf)
2056 	__releases(__rq_lockp(rq))
2057 {
2058 	rq_unpin_lock(rq, rf);
2059 	raw_spin_rq_unlock(rq);
2060 }
2061 
2062 DEFINE_LOCK_GUARD_1(rq_lock, struct rq,
2063 		    rq_lock(_T->lock, &_T->rf),
2064 		    rq_unlock(_T->lock, &_T->rf),
2065 		    struct rq_flags rf)
2066 
2067 DECLARE_LOCK_GUARD_1_ATTRS(rq_lock, __acquires(__rq_lockp(_T)),
2068 			   __releases(__rq_lockp(*(struct rq **)_T)));
2069 #define class_rq_lock_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(rq_lock, _T)
2070 
2071 DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq,
2072 		    rq_lock_irq(_T->lock, &_T->rf),
2073 		    rq_unlock_irq(_T->lock, &_T->rf),
2074 		    struct rq_flags rf)
2075 
2076 DECLARE_LOCK_GUARD_1_ATTRS(rq_lock_irq, __acquires(__rq_lockp(_T)),
2077 			   __releases(__rq_lockp(*(struct rq **)_T)));
2078 #define class_rq_lock_irq_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(rq_lock_irq, _T)
2079 
2080 DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
2081 		    rq_lock_irqsave(_T->lock, &_T->rf),
2082 		    rq_unlock_irqrestore(_T->lock, &_T->rf),
2083 		    struct rq_flags rf)
2084 
2085 DECLARE_LOCK_GUARD_1_ATTRS(rq_lock_irqsave, __acquires(__rq_lockp(_T)),
2086 			   __releases(__rq_lockp(*(struct rq **)_T)));
2087 #define class_rq_lock_irqsave_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(rq_lock_irqsave, _T)
2088 
2089 DEFINE_LOCK_GUARD_1(raw_spin_rq_lock_irqsave, struct rq,
2090 		    raw_spin_rq_lock_irqsave(_T->lock, _T->flags),
2091 		    raw_spin_rq_unlock_irqrestore(_T->lock, _T->flags),
2092 		    unsigned long flags)
2093 
2094 DECLARE_LOCK_GUARD_1_ATTRS(raw_spin_rq_lock_irqsave, __acquires(__rq_lockp(_T)),
2095 			   __releases(__rq_lockp(*(struct rq **)_T)));
2096 #define class_raw_spin_rq_lock_irqsave_constructor(_T) \
2097 	WITH_LOCK_GUARD_1_ATTRS(raw_spin_rq_lock_irqsave, _T)
2098 
2099 #define this_rq_lock_irq(...) __acquire_ret(_this_rq_lock_irq(__VA_ARGS__), __rq_lockp(__ret))
2100 static inline struct rq *_this_rq_lock_irq(struct rq_flags *rf) __acquires_ret
2101 {
2102 	struct rq *rq;
2103 
2104 	local_irq_disable();
2105 	rq = this_rq();
2106 	rq_lock(rq, rf);
2107 
2108 	return rq;
2109 }
2110 
2111 #ifdef CONFIG_NUMA
2112 
2113 enum numa_topology_type {
2114 	NUMA_DIRECT,
2115 	NUMA_GLUELESS_MESH,
2116 	NUMA_BACKPLANE,
2117 };
2118 
2119 extern enum numa_topology_type sched_numa_topology_type;
2120 extern int sched_max_numa_distance;
2121 extern bool find_numa_distance(int distance);
2122 extern void sched_init_numa(int offline_node);
2123 extern void sched_update_numa(int cpu, bool online);
2124 extern void sched_domains_numa_masks_set(unsigned int cpu);
2125 extern void sched_domains_numa_masks_clear(unsigned int cpu);
2126 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
2127 
2128 #else /* !CONFIG_NUMA: */
2129 
2130 static inline void sched_init_numa(int offline_node) { }
2131 static inline void sched_update_numa(int cpu, bool online) { }
2132 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
2133 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
2134 
2135 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
2136 {
2137 	return nr_cpu_ids;
2138 }
2139 
2140 #endif /* !CONFIG_NUMA */
2141 
2142 #ifdef CONFIG_NUMA_BALANCING
2143 
2144 /* The regions in numa_faults array from task_struct */
2145 enum numa_faults_stats {
2146 	NUMA_MEM = 0,
2147 	NUMA_CPU,
2148 	NUMA_MEMBUF,
2149 	NUMA_CPUBUF
2150 };
2151 
2152 extern void sched_setnuma(struct task_struct *p, int node);
2153 extern int migrate_task_to(struct task_struct *p, int cpu);
2154 extern int migrate_swap(struct task_struct *p, struct task_struct *t,
2155 			int cpu, int scpu);
2156 extern void init_numa_balancing(u64 clone_flags, struct task_struct *p);
2157 
2158 #else /* !CONFIG_NUMA_BALANCING: */
2159 
2160 static inline void
2161 init_numa_balancing(u64 clone_flags, struct task_struct *p)
2162 {
2163 }
2164 
2165 #endif /* !CONFIG_NUMA_BALANCING */
2166 
2167 int task_llc(const struct task_struct *p);
2168 
2169 static inline void
2170 queue_balance_callback(struct rq *rq,
2171 		       struct balance_callback *head,
2172 		       void (*func)(struct rq *rq))
2173 {
2174 	lockdep_assert_rq_held(rq);
2175 
2176 	/*
2177 	 * Don't (re)queue an already queued item; nor queue anything when
2178 	 * balance_push() is active, see the comment with
2179 	 * balance_push_callback.
2180 	 */
2181 	if (unlikely(head->next || rq->balance_callback == head ||
2182 		     rq->balance_callback == &balance_push_callback))
2183 		return;
2184 
2185 	head->func = func;
2186 	head->next = rq->balance_callback;
2187 	rq->balance_callback = head;
2188 }
2189 
2190 #define rcu_dereference_sched_domain(p) \
2191 	rcu_dereference_all_check((p), lockdep_is_held(&sched_domains_mutex))
2192 
2193 /*
2194  * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
2195  * See destroy_sched_domains: call_rcu for details.
2196  *
2197  * The domain tree of any CPU may only be accessed from within
2198  * preempt-disabled sections.
2199  */
2200 #define for_each_domain(cpu, __sd) \
2201 	for (__sd = rcu_dereference_sched_domain(cpu_rq(cpu)->sd); \
2202 			__sd; __sd = __sd->parent)
2203 
2204 /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */
2205 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) |
2206 static const unsigned int SD_SHARED_CHILD_MASK =
2207 #include <linux/sched/sd_flags.h>
2208 0;
2209 #undef SD_FLAG
2210 
2211 /**
2212  * highest_flag_domain - Return highest sched_domain containing flag.
2213  * @cpu:	The CPU whose highest level of sched domain is to
2214  *		be returned.
2215  * @flag:	The flag to check for the highest sched_domain
2216  *		for the given CPU.
2217  *
2218  * Returns the highest sched_domain of a CPU which contains @flag. If @flag has
2219  * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag.
2220  */
2221 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
2222 {
2223 	struct sched_domain *sd, *hsd = NULL;
2224 
2225 	for_each_domain(cpu, sd) {
2226 		if (sd->flags & flag) {
2227 			hsd = sd;
2228 			continue;
2229 		}
2230 
2231 		/*
2232 		 * Stop the search if @flag is known to be shared at lower
2233 		 * levels. It will not be found further up.
2234 		 */
2235 		if (flag & SD_SHARED_CHILD_MASK)
2236 			break;
2237 	}
2238 
2239 	return hsd;
2240 }
2241 
2242 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
2243 {
2244 	struct sched_domain *sd;
2245 
2246 	for_each_domain(cpu, sd) {
2247 		if (sd->flags & flag)
2248 			break;
2249 	}
2250 
2251 	return sd;
2252 }
2253 
2254 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
2255 DECLARE_PER_CPU(int, sd_llc_size);
2256 DECLARE_PER_CPU(int, sd_llc_id);
2257 DECLARE_PER_CPU(int, sd_share_id);
2258 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
2259 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_balance_shared);
2260 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
2261 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
2262 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
2263 
2264 extern struct static_key_false sched_asym_cpucapacity;
2265 extern struct static_key_false sched_cluster_active;
2266 
2267 static __always_inline bool sched_asym_cpucap_active(void)
2268 {
2269 	return static_branch_unlikely(&sched_asym_cpucapacity);
2270 }
2271 
2272 struct sched_group_capacity {
2273 	atomic_t		ref;
2274 	/*
2275 	 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
2276 	 * for a single CPU.
2277 	 */
2278 	unsigned long		capacity;
2279 	unsigned long		min_capacity;		/* Min per-CPU capacity in group */
2280 	unsigned long		max_capacity;		/* Max per-CPU capacity in group */
2281 	unsigned long		next_update;
2282 	int			imbalance;		/* XXX unrelated to capacity but shared group state */
2283 
2284 	int			id;
2285 
2286 	unsigned long		cpumask[];		/* Balance mask */
2287 };
2288 
2289 struct sched_group {
2290 	struct sched_group	*next;			/* Must be a circular list */
2291 	atomic_t		ref;
2292 
2293 	unsigned int		group_weight;
2294 	unsigned int		cores;
2295 	struct sched_group_capacity *sgc;
2296 	int			asym_prefer_cpu;	/* CPU of highest priority in group */
2297 	int			flags;
2298 
2299 	/*
2300 	 * The CPUs this group covers.
2301 	 *
2302 	 * NOTE: this field is variable length. (Allocated dynamically
2303 	 * by attaching extra space to the end of the structure,
2304 	 * depending on how many CPUs the kernel has booted up with)
2305 	 */
2306 	unsigned long		cpumask[];
2307 };
2308 
2309 static inline struct cpumask *sched_group_span(struct sched_group *sg)
2310 {
2311 	return to_cpumask(sg->cpumask);
2312 }
2313 
2314 /*
2315  * See build_balance_mask().
2316  */
2317 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
2318 {
2319 	return to_cpumask(sg->sgc->cpumask);
2320 }
2321 
2322 extern int group_balance_cpu(struct sched_group *sg);
2323 
2324 extern void update_sched_domain_debugfs(void);
2325 extern void dirty_sched_domain_sysctl(int cpu);
2326 
2327 extern int sched_update_scaling(void);
2328 
2329 static inline const struct cpumask *task_user_cpus(struct task_struct *p)
2330 {
2331 	if (!p->user_cpus_ptr)
2332 		return cpu_possible_mask; /* &init_task.cpus_mask */
2333 	return p->user_cpus_ptr;
2334 }
2335 
2336 #ifdef CONFIG_CGROUP_SCHED
2337 
2338 /*
2339  * Return the group to which this tasks belongs.
2340  *
2341  * We cannot use task_css() and friends because the cgroup subsystem
2342  * changes that value before the cgroup_subsys::attach() method is called,
2343  * therefore we cannot pin it and might observe the wrong value.
2344  *
2345  * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
2346  * core changes this before calling sched_move_task().
2347  *
2348  * Instead we use a 'copy' which is updated from sched_move_task() while
2349  * holding both task_struct::pi_lock and rq::lock.
2350  */
2351 static inline struct task_group *task_group(struct task_struct *p)
2352 {
2353 	return p->sched_task_group;
2354 }
2355 
2356 #ifdef CONFIG_FAIR_GROUP_SCHED
2357 /*
2358  * Defined here to be available before stats.h is included, since
2359  * stats.h has dependencies on things defined later in this file.
2360  */
2361 struct cfs_tg_state {
2362 	struct cfs_rq		cfs_rq;
2363 	struct sched_entity	se;
2364 	struct sched_statistics	stats;
2365 } __no_randomize_layout;
2366 
2367 /* Access a specific CPU's cfs_rq from a task group */
2368 static inline struct cfs_rq *tg_cfs_rq(struct task_group *tg, int cpu)
2369 {
2370 	return per_cpu_ptr(tg->cfs_rq, cpu);
2371 }
2372 
2373 static inline struct sched_entity *tg_se(struct task_group *tg, int cpu)
2374 {
2375 	struct cfs_tg_state *state;
2376 
2377 	if (is_root_task_group(tg))
2378 		return NULL;
2379 
2380 	state = container_of(tg_cfs_rq(tg, cpu), struct cfs_tg_state, cfs_rq);
2381 	return &state->se;
2382 }
2383 
2384 static inline struct sched_entity *cfs_rq_se(struct cfs_rq *cfs_rq)
2385 {
2386 	struct cfs_tg_state *state;
2387 
2388 	if (is_root_task_group(cfs_rq->tg))
2389 		return NULL;
2390 
2391 	state = container_of(cfs_rq, struct cfs_tg_state, cfs_rq);
2392 	return &state->se;
2393 }
2394 #endif
2395 
2396 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
2397 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
2398 {
2399 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
2400 	struct task_group *tg = task_group(p);
2401 #endif
2402 
2403 #ifdef CONFIG_FAIR_GROUP_SCHED
2404 	set_task_rq_fair(&p->se, p->se.cfs_rq, tg_cfs_rq(tg, cpu));
2405 	p->se.cfs_rq = tg_cfs_rq(tg, cpu);
2406 	p->se.parent = tg_se(tg, cpu);
2407 	p->se.depth = p->se.parent ? p->se.parent->depth + 1 : 0;
2408 #endif
2409 
2410 #ifdef CONFIG_RT_GROUP_SCHED
2411 	/*
2412 	 * p->rt.rt_rq is NULL initially and it is easier to assign
2413 	 * root_task_group's rt_rq than switching in rt_rq_of_se()
2414 	 * Clobbers tg(!)
2415 	 */
2416 	if (!rt_group_sched_enabled())
2417 		tg = &root_task_group;
2418 	p->rt.rt_rq  = tg->rt_rq[cpu];
2419 	p->rt.parent = tg->rt_se[cpu];
2420 #endif /* CONFIG_RT_GROUP_SCHED */
2421 }
2422 
2423 #else /* !CONFIG_CGROUP_SCHED: */
2424 
2425 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
2426 
2427 static inline struct task_group *task_group(struct task_struct *p)
2428 {
2429 	return NULL;
2430 }
2431 
2432 #endif /* !CONFIG_CGROUP_SCHED */
2433 
2434 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2435 {
2436 	set_task_rq(p, cpu);
2437 #ifdef CONFIG_SMP
2438 	/*
2439 	 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2440 	 * successfully executed on another CPU. We must ensure that updates of
2441 	 * per-task data have been completed by this moment.
2442 	 */
2443 	smp_wmb();
2444 	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
2445 	p->wake_cpu = cpu;
2446 	rseq_sched_set_ids_changed(p);
2447 #endif /* CONFIG_SMP */
2448 }
2449 
2450 /*
2451  * Tunables:
2452  */
2453 
2454 #define SCHED_FEAT(name, enabled)	\
2455 	__SCHED_FEAT_##name ,
2456 
2457 enum {
2458 #include "features.h"
2459 	__SCHED_FEAT_NR,
2460 };
2461 
2462 #undef SCHED_FEAT
2463 
2464 /*
2465  * To support run-time toggling of sched features, all the translation units
2466  * (but core.c) reference the sysctl_sched_features defined in core.c.
2467  */
2468 extern __read_mostly unsigned int sysctl_sched_features;
2469 
2470 #ifdef CONFIG_JUMP_LABEL
2471 
2472 union sched_feat_key {
2473 	struct static_key_true	key_true;
2474 	struct static_key_false	key_false;
2475 };
2476 
2477 #define sched_feat_branch_true(key)	static_branch_likely(&(key)->key_true)
2478 #define sched_feat_branch_false(key)	static_branch_unlikely(&(key)->key_false)
2479 
2480 #define SCHED_FEAT(name, enabled)			\
2481 static __always_inline bool				\
2482 static_branch_##name(union sched_feat_key *key) 	\
2483 {							\
2484 	return sched_feat_branch_##enabled(key);	\
2485 }
2486 
2487 #include "features.h"
2488 #undef SCHED_FEAT
2489 
2490 extern union sched_feat_key sched_feat_keys[__SCHED_FEAT_NR];
2491 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
2492 
2493 #else /* !CONFIG_JUMP_LABEL: */
2494 
2495 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
2496 
2497 #endif /* !CONFIG_JUMP_LABEL */
2498 
2499 extern struct static_key_false sched_numa_balancing;
2500 extern struct static_key_false sched_schedstats;
2501 
2502 static inline u64 global_rt_period(void)
2503 {
2504 	return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
2505 }
2506 
2507 static inline u64 global_rt_runtime(void)
2508 {
2509 	if (sysctl_sched_rt_runtime < 0)
2510 		return RUNTIME_INF;
2511 
2512 	return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
2513 }
2514 
2515 /*
2516  * Is p the current execution context?
2517  */
2518 static inline int task_current(struct rq *rq, struct task_struct *p)
2519 {
2520 	return rq->curr == p;
2521 }
2522 
2523 /*
2524  * Is p the current scheduling context?
2525  *
2526  * Note that it might be the current execution context at the same time if
2527  * rq->curr == rq->donor == p.
2528  */
2529 static inline int task_current_donor(struct rq *rq, struct task_struct *p)
2530 {
2531 	return rq->donor == p;
2532 }
2533 
2534 static inline bool task_is_blocked(struct task_struct *p)
2535 {
2536 	if (!sched_proxy_exec())
2537 		return false;
2538 
2539 	return !!p->blocked_on;
2540 }
2541 
2542 #ifdef CONFIG_SCHED_PROXY_EXEC
2543 void sched_proxy_block_task(struct rq *rq, struct task_struct *p);
2544 #else
2545 static inline void sched_proxy_block_task(struct rq *rq, struct task_struct *p) {}
2546 #endif
2547 
2548 static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
2549 {
2550 	return p->on_cpu;
2551 }
2552 
2553 static inline int task_on_rq_queued(struct task_struct *p)
2554 {
2555 	return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
2556 }
2557 
2558 static inline int task_on_rq_migrating(struct task_struct *p)
2559 {
2560 	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
2561 }
2562 
2563 /* Wake flags. The first three directly map to some SD flag value */
2564 #define WF_EXEC			0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */
2565 #define WF_FORK			0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */
2566 #define WF_TTWU			0x08 /* Wakeup;            maps to SD_BALANCE_WAKE */
2567 /*
2568  * Hint that the caller expects the waker to sleep soon.
2569  * Scheduler classes may use it for placement or preemption.
2570  * Callers must not rely on it to prevent migration,
2571  * preserve CPU locality or make the wakee run next.
2572  */
2573 #define WF_SYNC			0x10
2574 #define WF_MIGRATED		0x20 /* Internal use, task got migrated */
2575 #define WF_CURRENT_CPU		0x40 /* Prefer to move the wakee to the current CPU. */
2576 #define WF_RQ_SELECTED		0x80 /* ->select_task_rq() was called */
2577 #define WF_TTWU_RQ		0x100 /* Wakeup completed through ttwu_runnable() */
2578 
2579 static_assert(WF_EXEC == SD_BALANCE_EXEC);
2580 static_assert(WF_FORK == SD_BALANCE_FORK);
2581 static_assert(WF_TTWU == SD_BALANCE_WAKE);
2582 
2583 /*
2584  * To aid in avoiding the subversion of "niceness" due to uneven distribution
2585  * of tasks with abnormal "nice" values across CPUs the contribution that
2586  * each task makes to its run queue's load is weighted according to its
2587  * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
2588  * scaled version of the new time slice allocation that they receive on time
2589  * slice expiry etc.
2590  */
2591 
2592 #define WEIGHT_IDLEPRIO		3
2593 #define WMULT_IDLEPRIO		1431655765
2594 
2595 extern const int		sched_prio_to_weight[40];
2596 extern const u32		sched_prio_to_wmult[40];
2597 
2598 /*
2599  * {de,en}queue flags:
2600  *
2601  * SLEEP/WAKEUP - task is no-longer/just-became runnable
2602  *
2603  * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
2604  *                are in a known state which allows modification. Such pairs
2605  *                should preserve as much state as possible.
2606  *
2607  * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
2608  *        in the runqueue. IOW the priority is allowed to change. Callers
2609  *        must expect to deal with balance callbacks.
2610  *
2611  * NOCLOCK - skip the update_rq_clock() (avoids double updates)
2612  *
2613  * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE)
2614  *
2615  * DELAYED - de/re-queue a sched_delayed task
2616  *
2617  * CLASS - going to update p->sched_class; makes sched_change call the
2618  *         various switch methods.
2619  *
2620  * ENQUEUE_HEAD      - place at front of runqueue (tail if not specified)
2621  * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
2622  * ENQUEUE_MIGRATED  - the task was migrated during wakeup
2623  * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called
2624  *
2625  * XXX SAVE/RESTORE in combination with CLASS doesn't really make sense, but
2626  * SCHED_DEADLINE seems to rely on this for now.
2627  */
2628 
2629 #define DEQUEUE_SLEEP		0x0001 /* Matches ENQUEUE_WAKEUP */
2630 #define DEQUEUE_SAVE		0x0002 /* Matches ENQUEUE_RESTORE */
2631 #define DEQUEUE_MOVE		0x0004 /* Matches ENQUEUE_MOVE */
2632 #define DEQUEUE_NOCLOCK		0x0008 /* Matches ENQUEUE_NOCLOCK */
2633 
2634 #define DEQUEUE_MIGRATING	0x0010 /* Matches ENQUEUE_MIGRATING */
2635 #define DEQUEUE_DELAYED		0x0020 /* Matches ENQUEUE_DELAYED */
2636 #define DEQUEUE_CLASS		0x0040 /* Matches ENQUEUE_CLASS */
2637 
2638 #define DEQUEUE_SPECIAL		0x00010000
2639 #define DEQUEUE_THROTTLE	0x00020000
2640 
2641 #define ENQUEUE_WAKEUP		0x0001
2642 #define ENQUEUE_RESTORE		0x0002
2643 #define ENQUEUE_MOVE		0x0004
2644 #define ENQUEUE_NOCLOCK		0x0008
2645 
2646 #define ENQUEUE_MIGRATING	0x0010
2647 #define ENQUEUE_DELAYED		0x0020
2648 #define ENQUEUE_CLASS		0x0040
2649 
2650 #define ENQUEUE_HEAD		0x00010000
2651 #define ENQUEUE_REPLENISH	0x00020000
2652 #define ENQUEUE_MIGRATED	0x00040000
2653 #define ENQUEUE_INITIAL		0x00080000
2654 #define ENQUEUE_RQ_SELECTED	0x00100000
2655 #define ENQUEUE_QUEUED		0x00200000
2656 
2657 #define RETRY_TASK		((void *)-1UL)
2658 
2659 struct affinity_context {
2660 	const struct cpumask	*new_mask;
2661 	struct cpumask		*user_mask;
2662 	unsigned int		flags;
2663 };
2664 
2665 extern s64 update_curr_common(struct rq *rq);
2666 
2667 enum snt_e {
2668 	SNT_NORMAL,	/* set_next_task() */
2669 	SNT_PICK,	/* put_prev_set_next_task(): prev != next */
2670 	SNT_REPICK,	/* put_prev_set_next_task(): prev == next */
2671 };
2672 
2673 struct sched_class {
2674 
2675 #ifdef CONFIG_UCLAMP_TASK
2676 	int uclamp_enabled;
2677 #endif
2678 
2679 	/*
2680 	 * move_queued_task/activate_task/enqueue_task: rq->lock
2681 	 * ttwu_do_activate/activate_task/enqueue_task: rq->lock
2682 	 * wake_up_new_task/activate_task/enqueue_task: task_rq_lock
2683 	 * ttwu_runnable/enqueue_task: task_rq_lock
2684 	 * proxy_task_current: rq->lock
2685 	 * sched_change_end
2686 	 */
2687 	void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
2688 	/*
2689 	 * move_queued_task/deactivate_task/dequeue_task: rq->lock
2690 	 * __schedule/block_task/dequeue_task: rq->lock
2691 	 * proxy_task_current: rq->lock
2692 	 * wait_task_inactive: task_rq_lock
2693 	 * sched_change_begin
2694 	 */
2695 	bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
2696 
2697 	/*
2698 	 * do_sched_yield: rq->lock
2699 	 */
2700 	void (*yield_task)   (struct rq *rq);
2701 	/*
2702 	 * yield_to: rq->lock (double)
2703 	 */
2704 	bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
2705 
2706 	/*
2707 	 * move_queued_task: rq->lock
2708 	 * __migrate_swap_task: rq->lock
2709 	 * ttwu_do_activate: rq->lock
2710 	 * ttwu_runnable: task_rq_lock
2711 	 * wake_up_new_task: task_rq_lock
2712 	 */
2713 	void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
2714 
2715 	/*
2716 	 * schedule/pick_next_task/prev_balance: rq->lock
2717 	 */
2718 	int (*balance)(struct rq *rq, struct rq_flags *rf);
2719 
2720 	/*
2721 	 * schedule/pick_next_task: rq->lock
2722 	 */
2723 	struct task_struct *(*pick_task)(struct rq *rq, struct rq_flags *rf);
2724 
2725 	/*
2726 	 * sched_change:
2727 	 * __schedule: rq->lock
2728 	 */
2729 	void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
2730 	void (*set_next_task)(struct rq *rq, struct task_struct *p, enum snt_e type);
2731 
2732 	/*
2733 	 * select_task_rq: p->pi_lock
2734 	 * sched_exec: p->pi_lock
2735 	 */
2736 	int  (*select_task_rq)(struct task_struct *p, int task_cpu, int flags);
2737 
2738 	/*
2739 	 * set_task_cpu: p->pi_lock || rq->lock (ttwu like)
2740 	 */
2741 	void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
2742 
2743 	/*
2744 	 * ttwu_do_activate: rq->lock
2745 	 * wake_up_new_task: task_rq_lock
2746 	 */
2747 	void (*task_woken)(struct rq *this_rq, struct task_struct *task);
2748 
2749 	/*
2750 	 * do_set_cpus_allowed: task_rq_lock + sched_change
2751 	 */
2752 	void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx);
2753 
2754 	/*
2755 	 * sched_set_rq_{on,off}line: rq->lock
2756 	 */
2757 	void (*rq_online)(struct rq *rq);
2758 	void (*rq_offline)(struct rq *rq);
2759 
2760 	/*
2761 	 * push_cpu_stop: p->pi_lock && rq->lock
2762 	 */
2763 	struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
2764 
2765 	/*
2766 	 * hrtick: rq->lock
2767 	 * sched_tick: rq->lock
2768 	 * sched_tick_remote: rq->lock
2769 	 */
2770 	void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
2771 	/*
2772 	 * sched_cgroup_fork: p->pi_lock
2773 	 */
2774 	void (*task_fork)(struct task_struct *p);
2775 	/*
2776 	 * finish_task_switch: no locks
2777 	 */
2778 	void (*task_dead)(struct task_struct *p);
2779 
2780 	/*
2781 	 * sched_change
2782 	 */
2783 	void (*switching_from)(struct rq *this_rq, struct task_struct *task);
2784 	void (*switched_from) (struct rq *this_rq, struct task_struct *task);
2785 	void (*switching_to)  (struct rq *this_rq, struct task_struct *task);
2786 	void (*switched_to)   (struct rq *this_rq, struct task_struct *task);
2787 	u64  (*get_prio)     (struct rq *this_rq, struct task_struct *task);
2788 	void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
2789 			      u64 oldprio);
2790 
2791 	/*
2792 	 * set_load_weight: task_rq_lock + sched_change
2793 	 * __setscheduler_parms: task_rq_lock + sched_change
2794 	 */
2795 	void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
2796 			      const struct load_weight *lw);
2797 
2798 	/*
2799 	 * sched_rr_get_interval: task_rq_lock
2800 	 */
2801 	unsigned int (*get_rr_interval)(struct rq *rq,
2802 					struct task_struct *task);
2803 
2804 	/*
2805 	 * task_sched_runtime: task_rq_lock
2806 	 */
2807 	void (*update_curr)(struct rq *rq);
2808 
2809 #ifdef CONFIG_FAIR_GROUP_SCHED
2810 	/*
2811 	 * sched_change_group: task_rq_lock + sched_change
2812 	 */
2813 	void (*task_change_group)(struct task_struct *p);
2814 #endif
2815 
2816 #ifdef CONFIG_SCHED_CORE
2817 	/*
2818 	 * pick_next_task: rq->lock
2819 	 * try_steal_cookie: rq->lock (double)
2820 	 */
2821 	int (*task_is_throttled)(struct task_struct *p, int cpu);
2822 #endif
2823 };
2824 
2825 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
2826 {
2827 	WARN_ON_ONCE(rq->donor != prev);
2828 	prev->sched_class->put_prev_task(rq, prev, NULL);
2829 }
2830 
2831 static inline void set_next_task(struct rq *rq, struct task_struct *next)
2832 {
2833 	next->sched_class->set_next_task(rq, next, SNT_NORMAL);
2834 }
2835 
2836 static inline void
2837 __put_prev_set_next_dl_server(struct rq *rq,
2838 			      struct task_struct *prev,
2839 			      struct task_struct *next)
2840 {
2841 	prev->dl_server = NULL;
2842 	next->dl_server = rq->dl_server;
2843 	rq->dl_server = NULL;
2844 }
2845 
2846 static inline void put_prev_set_next_task(struct rq *rq,
2847 					  struct task_struct *prev,
2848 					  struct task_struct *next)
2849 {
2850 	WARN_ON_ONCE(rq->donor != prev);
2851 
2852 	__put_prev_set_next_dl_server(rq, prev, next);
2853 
2854 	if (next == prev) {
2855 		next->sched_class->set_next_task(rq, next, SNT_REPICK);
2856 		return;
2857 	}
2858 
2859 	prev->sched_class->put_prev_task(rq, prev, next);
2860 	next->sched_class->set_next_task(rq, next, SNT_PICK);
2861 }
2862 
2863 /*
2864  * Helper to define a sched_class instance; each one is placed in a separate
2865  * section which is ordered by the linker script:
2866  *
2867  *   include/asm-generic/vmlinux.lds.h
2868  *
2869  * *CAREFUL* they are laid out in *REVERSE* order!!!
2870  *
2871  * Also enforce alignment on the instance, not the type, to guarantee layout.
2872  */
2873 #define DEFINE_SCHED_CLASS(name) \
2874 const struct sched_class name##_sched_class \
2875 	__aligned(__alignof__(struct sched_class)) \
2876 	__section("__" #name "_sched_class")
2877 
2878 /* Defined in include/asm-generic/vmlinux.lds.h */
2879 extern struct sched_class __sched_class_highest[];
2880 extern struct sched_class __sched_class_lowest[];
2881 
2882 extern const struct sched_class stop_sched_class;
2883 extern const struct sched_class dl_sched_class;
2884 extern const struct sched_class rt_sched_class;
2885 extern const struct sched_class fair_sched_class;
2886 extern const struct sched_class idle_sched_class;
2887 
2888 /*
2889  * Iterate only active classes. SCX can take over all fair tasks or be
2890  * completely disabled. If the former, skip fair. If the latter, skip SCX.
2891  */
2892 static inline const struct sched_class *next_active_class(const struct sched_class *class)
2893 {
2894 	class++;
2895 #ifdef CONFIG_SCHED_CLASS_EXT
2896 	if (scx_switched_all() && class == &fair_sched_class)
2897 		class++;
2898 	if (!scx_enabled() && class == &ext_sched_class)
2899 		class++;
2900 #endif
2901 	return class;
2902 }
2903 
2904 #define for_class_range(class, _from, _to) \
2905 	for (class = (_from); class < (_to); class++)
2906 
2907 #define for_each_class(class) \
2908 	for_class_range(class, __sched_class_highest, __sched_class_lowest)
2909 
2910 #define for_active_class_range(class, _from, _to)				\
2911 	for (class = (_from); class != (_to); class = next_active_class(class))
2912 
2913 #define for_each_active_class(class)						\
2914 	for_active_class_range(class, __sched_class_highest, __sched_class_lowest)
2915 
2916 #define sched_class_above(_a, _b)	((_a) < (_b))
2917 
2918 static inline void rq_modified_begin(struct rq *rq, const struct sched_class *class)
2919 {
2920 	if (sched_class_above(rq->next_class, class))
2921 		rq->next_class = class;
2922 }
2923 
2924 static inline bool rq_modified_above(struct rq *rq, const struct sched_class *class)
2925 {
2926 	return sched_class_above(rq->next_class, class);
2927 }
2928 
2929 static inline bool sched_stop_runnable(struct rq *rq)
2930 {
2931 	return rq->stop && task_on_rq_queued(rq->stop);
2932 }
2933 
2934 static inline bool sched_dl_runnable(struct rq *rq)
2935 {
2936 	return rq->dl.dl_nr_running > 0;
2937 }
2938 
2939 static inline bool sched_rt_runnable(struct rq *rq)
2940 {
2941 	return rq->rt.rt_queued > 0;
2942 }
2943 
2944 static inline bool sched_fair_runnable(struct rq *rq)
2945 {
2946 	return rq->cfs.nr_queued > 0;
2947 }
2948 
2949 extern struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf);
2950 extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
2951 
2952 #define SCA_CHECK		0x01
2953 #define SCA_MIGRATE_DISABLE	0x02
2954 #define SCA_MIGRATE_ENABLE	0x04
2955 #define SCA_USER		0x08
2956 
2957 extern void update_group_capacity(struct sched_domain *sd, int cpu);
2958 
2959 extern void sched_balance_trigger(struct rq *rq);
2960 
2961 extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
2962 extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
2963 
2964 static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2965 {
2966 	/* When not in the task's cpumask, no point in looking further. */
2967 	if (!cpumask_test_cpu(cpu, p->cpus_ptr))
2968 		return false;
2969 
2970 	/* Can @cpu run a user thread? */
2971 	if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p))
2972 		return false;
2973 
2974 	return true;
2975 }
2976 
2977 static inline cpumask_t *alloc_user_cpus_ptr(int node)
2978 {
2979 	/*
2980 	 * See set_cpus_allowed_force() above for the rcu_head usage.
2981 	 */
2982 	int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
2983 
2984 	return kmalloc_node(size, GFP_KERNEL, node);
2985 }
2986 
2987 static inline struct task_struct *get_push_task(struct rq *rq)
2988 {
2989 	struct task_struct *p = rq->donor;
2990 
2991 	lockdep_assert_rq_held(rq);
2992 
2993 	if (rq->push_busy)
2994 		return NULL;
2995 
2996 	if (p->nr_cpus_allowed == 1)
2997 		return NULL;
2998 
2999 	if (p->migration_disabled)
3000 		return NULL;
3001 
3002 	rq->push_busy = true;
3003 	return get_task_struct(p);
3004 }
3005 
3006 extern int push_cpu_stop(void *arg);
3007 
3008 #ifdef CONFIG_CPU_IDLE
3009 
3010 static inline void idle_set_state(struct rq *rq,
3011 				  struct cpuidle_state *idle_state)
3012 {
3013 	rq->idle_state = idle_state;
3014 }
3015 
3016 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
3017 {
3018 	lockdep_assert(rcu_read_lock_any_held());
3019 
3020 	return rq->idle_state;
3021 }
3022 
3023 #else /* !CONFIG_CPU_IDLE: */
3024 
3025 static inline void idle_set_state(struct rq *rq,
3026 				  struct cpuidle_state *idle_state)
3027 {
3028 }
3029 
3030 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
3031 {
3032 	return NULL;
3033 }
3034 
3035 #endif /* !CONFIG_CPU_IDLE */
3036 
3037 extern void schedule_idle(void);
3038 asmlinkage void schedule_user(void);
3039 
3040 extern void sysrq_sched_debug_show(void);
3041 extern void sched_init_granularity(void);
3042 extern void update_max_interval(void);
3043 
3044 extern void init_sched_dl_class(void);
3045 extern void init_sched_rt_class(void);
3046 extern void init_sched_fair_class(void);
3047 
3048 extern void resched_curr(struct rq *rq);
3049 extern void resched_curr_lazy(struct rq *rq);
3050 extern void resched_cpu(int cpu);
3051 
3052 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
3053 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
3054 
3055 extern void init_dl_entity(struct sched_dl_entity *dl_se);
3056 
3057 extern void init_cfs_throttle_work(struct task_struct *p);
3058 
3059 #define BW_SHIFT		20
3060 #define BW_UNIT			(1 << BW_SHIFT)
3061 #define RATIO_SHIFT		8
3062 #define MAX_BW_BITS		(64 - BW_SHIFT)
3063 #define MAX_BW			((1ULL << MAX_BW_BITS) - 1)
3064 
3065 extern u64 to_ratio(u64 period, u64 runtime);
3066 
3067 extern void init_entity_runnable_average(struct sched_entity *se);
3068 extern void post_init_entity_util_avg(struct task_struct *p);
3069 
3070 #ifdef CONFIG_NO_HZ_FULL
3071 extern bool sched_can_stop_tick(struct rq *rq);
3072 extern int __init sched_tick_offload_init(void);
3073 
3074 /*
3075  * Tick may be needed by tasks in the runqueue depending on their policy and
3076  * requirements. If tick is needed, lets send the target an IPI to kick it out of
3077  * nohz mode if necessary.
3078  */
3079 static inline void sched_update_tick_dependency(struct rq *rq)
3080 {
3081 	int cpu = cpu_of(rq);
3082 
3083 	if (!tick_nohz_full_cpu(cpu))
3084 		return;
3085 
3086 	if (sched_can_stop_tick(rq))
3087 		tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
3088 	else
3089 		tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
3090 }
3091 #else /* !CONFIG_NO_HZ_FULL: */
3092 static inline int sched_tick_offload_init(void) { return 0; }
3093 static inline void sched_update_tick_dependency(struct rq *rq) { }
3094 #endif /* !CONFIG_NO_HZ_FULL */
3095 
3096 static inline void add_nr_running(struct rq *rq, unsigned count)
3097 {
3098 	unsigned prev_nr = rq->nr_running;
3099 
3100 	rq->nr_running = prev_nr + count;
3101 	if (trace_sched_update_nr_running_tp_enabled()) {
3102 		call_trace_sched_update_nr_running(rq, count);
3103 	}
3104 
3105 	if (prev_nr < 2 && rq->nr_running >= 2)
3106 		set_rd_overloaded(rq->rd, 1);
3107 
3108 	sched_update_tick_dependency(rq);
3109 }
3110 
3111 static inline void sub_nr_running(struct rq *rq, unsigned count)
3112 {
3113 	rq->nr_running -= count;
3114 	if (trace_sched_update_nr_running_tp_enabled()) {
3115 		call_trace_sched_update_nr_running(rq, -count);
3116 	}
3117 
3118 	/* Check if we still need preemption */
3119 	sched_update_tick_dependency(rq);
3120 }
3121 
3122 static inline void __block_task(struct rq *rq, struct task_struct *p)
3123 {
3124 	if (p->sched_contributes_to_load)
3125 		rq->nr_uninterruptible++;
3126 
3127 	if (p->in_iowait) {
3128 		atomic_inc(&rq->nr_iowait);
3129 		delayacct_blkio_start();
3130 	}
3131 
3132 	ASSERT_EXCLUSIVE_WRITER(p->on_rq);
3133 
3134 	/*
3135 	 * The moment this write goes through, ttwu() can swoop in and migrate
3136 	 * this task, rendering our rq->__lock ineffective.
3137 	 *
3138 	 * __schedule()				try_to_wake_up()
3139 	 *   LOCK rq->__lock			  LOCK p->pi_lock
3140 	 *   pick_next_task()
3141 	 *     pick_next_task_fair()
3142 	 *       pick_next_entity()
3143 	 *         dequeue_entities()
3144 	 *           __block_task()
3145 	 *             RELEASE p->on_rq = 0	  if (p->on_rq && ...)
3146 	 *					    break;
3147 	 *
3148 	 *					  ACQUIRE (after ctrl-dep)
3149 	 *
3150 	 *					  cpu = select_task_rq();
3151 	 *					  set_task_cpu(p, cpu);
3152 	 *					  ttwu_queue()
3153 	 *					    ttwu_do_activate()
3154 	 *					      LOCK rq->__lock
3155 	 *					      activate_task()
3156 	 *					        STORE p->on_rq = 1
3157 	 *   UNLOCK rq->__lock
3158 	 *
3159 	 * Callers must ensure to not reference @p after this -- we no longer
3160 	 * own it.
3161 	 */
3162 	smp_store_release(&p->on_rq, 0);
3163 }
3164 
3165 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
3166 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
3167 
3168 extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
3169 
3170 /*
3171  * attach_task() -- attach the task detached by detach_task() to its new rq.
3172  */
3173 static inline void attach_task(struct rq *rq, struct task_struct *p)
3174 {
3175 	lockdep_assert_rq_held(rq);
3176 
3177 	WARN_ON_ONCE(task_rq(p) != rq);
3178 	activate_task(rq, p, ENQUEUE_NOCLOCK);
3179 	wakeup_preempt(rq, p, 0);
3180 }
3181 
3182 /*
3183  * attach_one_task() -- attaches the task returned from detach_one_task() to
3184  * its new rq.
3185  */
3186 static inline void attach_one_task(struct rq *rq, struct task_struct *p)
3187 {
3188 	guard(rq_lock)(rq);
3189 	update_rq_clock(rq);
3190 	attach_task(rq, p);
3191 }
3192 
3193 /*
3194  * __attach_tasks() - attaches a list of tasks (using se.group_node) to
3195  * the new rq
3196  */
3197 static inline void __attach_tasks(struct rq *rq, struct list_head *tasks)
3198 {
3199 	guard(rq_lock)(rq);
3200 	update_rq_clock(rq);
3201 
3202 	while (!list_empty(tasks)) {
3203 		struct task_struct *p;
3204 
3205 		p = list_first_entry(tasks, struct task_struct, se.group_node);
3206 		list_del_init(&p->se.group_node);
3207 
3208 		attach_task(rq, p);
3209 	}
3210 }
3211 
3212 #ifdef CONFIG_PREEMPT_RT
3213 # define SCHED_NR_MIGRATE_BREAK 8
3214 #else
3215 # define SCHED_NR_MIGRATE_BREAK 32
3216 #endif
3217 
3218 extern __read_mostly unsigned int sysctl_sched_nr_migrate;
3219 extern __read_mostly unsigned int sysctl_sched_migration_cost;
3220 
3221 extern unsigned int sysctl_sched_base_slice;
3222 
3223 extern int sysctl_resched_latency_warn_ms;
3224 extern int sysctl_resched_latency_warn_once;
3225 
3226 extern unsigned int sysctl_sched_tunable_scaling;
3227 
3228 extern unsigned int sysctl_numa_balancing_scan_delay;
3229 extern unsigned int sysctl_numa_balancing_scan_period_min;
3230 extern unsigned int sysctl_numa_balancing_scan_period_max;
3231 extern unsigned int sysctl_numa_balancing_scan_size;
3232 extern unsigned int sysctl_numa_balancing_hot_threshold;
3233 
3234 #ifdef CONFIG_SCHED_HRTICK
3235 
3236 /*
3237  * Use hrtick when:
3238  *  - enabled by features
3239  *  - hrtimer is actually high res
3240  */
3241 static inline bool hrtick_enabled(struct rq *rq)
3242 {
3243 	return cpu_active(cpu_of(rq)) && hrtimer_highres_enabled();
3244 }
3245 
3246 static inline bool hrtick_enabled_fair(struct rq *rq)
3247 {
3248 	return sched_feat(HRTICK) && hrtick_enabled(rq);
3249 }
3250 
3251 static inline bool hrtick_enabled_dl(struct rq *rq)
3252 {
3253 	return sched_feat(HRTICK_DL) && hrtick_enabled(rq);
3254 }
3255 
3256 extern void hrtick_start(struct rq *rq, u64 delay);
3257 static inline bool hrtick_active(struct rq *rq)
3258 {
3259 	return hrtimer_active(&rq->hrtick_timer);
3260 }
3261 
3262 #else /* !CONFIG_SCHED_HRTICK: */
3263 static inline bool hrtick_enabled_fair(struct rq *rq) { return false; }
3264 static inline bool hrtick_enabled_dl(struct rq *rq) { return false; }
3265 static inline bool hrtick_enabled(struct rq *rq) { return false; }
3266 #endif /* !CONFIG_SCHED_HRTICK */
3267 
3268 #ifndef arch_scale_freq_tick
3269 static __always_inline void arch_scale_freq_tick(void) { }
3270 #endif
3271 
3272 #ifndef arch_scale_freq_capacity
3273 /**
3274  * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
3275  * @cpu: the CPU in question.
3276  *
3277  * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
3278  *
3279  *     f_curr
3280  *     ------ * SCHED_CAPACITY_SCALE
3281  *     f_max
3282  */
3283 static __always_inline
3284 unsigned long arch_scale_freq_capacity(int cpu)
3285 {
3286 	return SCHED_CAPACITY_SCALE;
3287 }
3288 #endif
3289 
3290 /*
3291  * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to
3292  * acquire rq lock instead of rq_lock(). So at the end of these two functions
3293  * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of
3294  * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning.
3295  */
3296 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
3297 {
3298 	rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3299 	rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3300 }
3301 
3302 #define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...)				\
3303 __DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__)			\
3304 static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2)	\
3305 	__no_context_analysis								\
3306 { class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t;			\
3307   _lock; return _t; }
3308 #define DECLARE_LOCK_GUARD_2_ATTRS(_name, _lock, _unlock1, _unlock2)			\
3309 static inline class_##_name##_t class_##_name##_constructor(lock_##_name##_t *_T1,	\
3310 							    lock_##_name##_t *_T2) _lock; \
3311 static __always_inline void __class_##_name##_cleanup_ctx1(class_##_name##_t **_T1)	\
3312 	__no_context_analysis _unlock1 { }						\
3313 static __always_inline void __class_##_name##_cleanup_ctx2(class_##_name##_t **_T2)	\
3314 	__no_context_analysis _unlock2 { }
3315 #define WITH_LOCK_GUARD_2_ATTRS(_name, _T1, _T2)					\
3316 	class_##_name##_constructor(_T1, _T2),						\
3317 	*__UNIQUE_ID(unlock1) __cleanup(__class_##_name##_cleanup_ctx1) = (void *)(_T1),\
3318 	*__UNIQUE_ID(unlock2) __cleanup(__class_##_name##_cleanup_ctx2) = (void *)(_T2)
3319 
3320 static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
3321 {
3322 #ifdef CONFIG_SCHED_CORE
3323 	/*
3324 	 * In order to not have {0,2},{1,3} turn into into an AB-BA,
3325 	 * order by core-id first and cpu-id second.
3326 	 *
3327 	 * Notably:
3328 	 *
3329 	 *	double_rq_lock(0,3); will take core-0, core-1 lock
3330 	 *	double_rq_lock(1,2); will take core-1, core-0 lock
3331 	 *
3332 	 * when only cpu-id is considered.
3333 	 */
3334 	if (rq1->core->cpu < rq2->core->cpu)
3335 		return true;
3336 	if (rq1->core->cpu > rq2->core->cpu)
3337 		return false;
3338 
3339 	/*
3340 	 * __sched_core_flip() relies on SMT having cpu-id lock order.
3341 	 */
3342 #endif /* CONFIG_SCHED_CORE */
3343 	return rq1->cpu < rq2->cpu;
3344 }
3345 
3346 extern void double_rq_lock(struct rq *rq1, struct rq *rq2)
3347 	__acquires(__rq_lockp(rq1), __rq_lockp(rq2));
3348 
3349 #ifdef CONFIG_PREEMPTION
3350 
3351 /*
3352  * fair double_lock_balance: Safely acquires both rq->locks in a fair
3353  * way at the expense of forcing extra atomic operations in all
3354  * invocations.  This assures that the double_lock is acquired using the
3355  * same underlying policy as the spinlock_t on this architecture, which
3356  * reduces latency compared to the unfair variant below.  However, it
3357  * also adds more overhead and therefore may reduce throughput.
3358  */
3359 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3360 	__must_hold(__rq_lockp(this_rq))
3361 	__acquires(__rq_lockp(busiest))
3362 {
3363 	raw_spin_rq_unlock(this_rq);
3364 	double_rq_lock(this_rq, busiest);
3365 
3366 	return 1;
3367 }
3368 
3369 #else /* !CONFIG_PREEMPTION: */
3370 /*
3371  * Unfair double_lock_balance: Optimizes throughput at the expense of
3372  * latency by eliminating extra atomic operations when the locks are
3373  * already in proper order on entry.  This favors lower CPU-ids and will
3374  * grant the double lock to lower CPUs over higher ids under contention,
3375  * regardless of entry order into the function.
3376  */
3377 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3378 	__must_hold(__rq_lockp(this_rq))
3379 	__acquires(__rq_lockp(busiest))
3380 {
3381 	if (__rq_lockp(this_rq) == __rq_lockp(busiest)) {
3382 		__acquire(__rq_lockp(busiest)); /* already held */
3383 		double_rq_clock_clear_update(this_rq, busiest);
3384 		return 0;
3385 	}
3386 
3387 	if (likely(raw_spin_rq_trylock(busiest))) {
3388 		double_rq_clock_clear_update(this_rq, busiest);
3389 		return 0;
3390 	}
3391 
3392 	if (rq_order_less(this_rq, busiest)) {
3393 		raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING);
3394 		double_rq_clock_clear_update(this_rq, busiest);
3395 		return 0;
3396 	}
3397 
3398 	raw_spin_rq_unlock(this_rq);
3399 	double_rq_lock(this_rq, busiest);
3400 
3401 	return 1;
3402 }
3403 
3404 #endif /* !CONFIG_PREEMPTION */
3405 
3406 /*
3407  * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3408  */
3409 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
3410 	__must_hold(__rq_lockp(this_rq))
3411 	__acquires(__rq_lockp(busiest))
3412 {
3413 	lockdep_assert_irqs_disabled();
3414 
3415 	return _double_lock_balance(this_rq, busiest);
3416 }
3417 
3418 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
3419 	__releases(__rq_lockp(busiest))
3420 {
3421 	if (__rq_lockp(this_rq) != __rq_lockp(busiest))
3422 		raw_spin_rq_unlock(busiest);
3423 	else
3424 		__release(__rq_lockp(busiest)); /* fake release */
3425 	lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_);
3426 }
3427 
3428 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
3429 	__acquires(l1, l2)
3430 {
3431 	if (l1 > l2)
3432 		swap(l1, l2);
3433 
3434 	spin_lock(l1);
3435 	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3436 }
3437 
3438 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
3439 	__acquires(l1, l2)
3440 {
3441 	if (l1 > l2)
3442 		swap(l1, l2);
3443 
3444 	spin_lock_irq(l1);
3445 	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3446 }
3447 
3448 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3449 	__acquires(l1, l2)
3450 {
3451 	if (l1 > l2)
3452 		swap(l1, l2);
3453 
3454 	raw_spin_lock(l1);
3455 	raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3456 }
3457 
3458 static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3459 	__releases(l1, l2)
3460 {
3461 	raw_spin_unlock(l1);
3462 	raw_spin_unlock(l2);
3463 }
3464 
3465 DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
3466 		    double_raw_lock(_T->lock, _T->lock2),
3467 		    double_raw_unlock(_T->lock, _T->lock2))
3468 
3469 DECLARE_LOCK_GUARD_2_ATTRS(double_raw_spinlock,
3470 			   __acquires(_T1, _T2),
3471 			   __releases(*(raw_spinlock_t **)_T1),
3472 			   __releases(*(raw_spinlock_t **)_T2));
3473 #define class_double_raw_spinlock_constructor(_T1, _T2) \
3474 	WITH_LOCK_GUARD_2_ATTRS(double_raw_spinlock, _T1, _T2)
3475 
3476 /*
3477  * double_rq_unlock - safely unlock two runqueues
3478  *
3479  * Note this does not restore interrupts like task_rq_unlock,
3480  * you need to do so manually after calling.
3481  */
3482 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3483 	__releases(__rq_lockp(rq1), __rq_lockp(rq2))
3484 {
3485 	if (__rq_lockp(rq1) != __rq_lockp(rq2))
3486 		raw_spin_rq_unlock(rq2);
3487 	else
3488 		__release(__rq_lockp(rq2)); /* fake release */
3489 	raw_spin_rq_unlock(rq1);
3490 }
3491 
3492 extern void set_rq_online (struct rq *rq);
3493 extern void set_rq_offline(struct rq *rq);
3494 
3495 extern bool sched_smp_initialized;
3496 
3497 DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
3498 		    double_rq_lock(_T->lock, _T->lock2),
3499 		    double_rq_unlock(_T->lock, _T->lock2))
3500 
3501 extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq);
3502 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
3503 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
3504 
3505 extern bool sched_debug_verbose;
3506 
3507 extern void print_cfs_stats(struct seq_file *m, int cpu);
3508 extern void print_rt_stats(struct seq_file *m, int cpu);
3509 extern void print_dl_stats(struct seq_file *m, int cpu);
3510 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
3511 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
3512 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
3513 
3514 extern void resched_latency_warn(int cpu, u64 latency);
3515 
3516 #ifdef CONFIG_NUMA_BALANCING
3517 extern void show_numa_stats(struct task_struct *p, struct seq_file *m);
3518 extern void
3519 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
3520 		 unsigned long tpf, unsigned long gsf, unsigned long gpf);
3521 #endif /* CONFIG_NUMA_BALANCING */
3522 
3523 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
3524 extern void init_rt_rq(struct rt_rq *rt_rq);
3525 extern void init_dl_rq(struct dl_rq *dl_rq);
3526 
3527 extern void cfs_bandwidth_usage_inc(void);
3528 extern void cfs_bandwidth_usage_dec(void);
3529 
3530 #ifdef CONFIG_NO_HZ_COMMON
3531 
3532 #define NOHZ_BALANCE_KICK_BIT	0
3533 #define NOHZ_STATS_KICK_BIT	1
3534 #define NOHZ_NEWILB_KICK_BIT	2
3535 #define NOHZ_NEXT_KICK_BIT	3
3536 
3537 /* Run sched_balance_domains() */
3538 #define NOHZ_BALANCE_KICK	BIT(NOHZ_BALANCE_KICK_BIT)
3539 /* Update blocked load */
3540 #define NOHZ_STATS_KICK		BIT(NOHZ_STATS_KICK_BIT)
3541 /* Update blocked load when entering idle */
3542 #define NOHZ_NEWILB_KICK	BIT(NOHZ_NEWILB_KICK_BIT)
3543 /* Update nohz.next_balance */
3544 #define NOHZ_NEXT_KICK		BIT(NOHZ_NEXT_KICK_BIT)
3545 
3546 #define NOHZ_KICK_MASK		(NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK)
3547 
3548 #define nohz_flags(cpu)		(&cpu_rq(cpu)->nohz_flags)
3549 
3550 extern void nohz_balance_exit_idle(struct rq *rq);
3551 #else /* !CONFIG_NO_HZ_COMMON: */
3552 static inline void nohz_balance_exit_idle(struct rq *rq) { }
3553 #endif /* !CONFIG_NO_HZ_COMMON */
3554 
3555 #ifdef CONFIG_NO_HZ_COMMON
3556 extern void nohz_run_idle_balance(int cpu);
3557 #else
3558 static inline void nohz_run_idle_balance(int cpu) { }
3559 #endif
3560 
3561 #include "stats.h"
3562 
3563 #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS)
3564 
3565 extern void __sched_core_account_forceidle(struct rq *rq);
3566 
3567 static inline void sched_core_account_forceidle(struct rq *rq)
3568 {
3569 	if (schedstat_enabled())
3570 		__sched_core_account_forceidle(rq);
3571 }
3572 
3573 extern void __sched_core_tick(struct rq *rq);
3574 
3575 static inline void sched_core_tick(struct rq *rq)
3576 {
3577 	if (sched_core_enabled(rq) && schedstat_enabled())
3578 		__sched_core_tick(rq);
3579 }
3580 
3581 #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */
3582 
3583 static inline void sched_core_account_forceidle(struct rq *rq) { }
3584 
3585 static inline void sched_core_tick(struct rq *rq) { }
3586 
3587 #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */
3588 
3589 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
3590 
3591 struct irqtime {
3592 	u64			total;
3593 	u64			tick_delta;
3594 	u64			irq_start_time;
3595 	struct u64_stats_sync	sync;
3596 };
3597 
3598 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
3599 DECLARE_STATIC_KEY_FALSE(sched_clock_irqtime);
3600 
3601 static inline int irqtime_enabled(void)
3602 {
3603 	return static_branch_likely(&sched_clock_irqtime);
3604 }
3605 
3606 /*
3607  * Returns the irqtime minus the softirq time computed by ksoftirqd.
3608  * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
3609  * and never move forward.
3610  */
3611 static inline u64 irq_time_read(int cpu)
3612 {
3613 	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
3614 	unsigned int seq;
3615 	u64 total;
3616 
3617 	do {
3618 		seq = __u64_stats_fetch_begin(&irqtime->sync);
3619 		total = irqtime->total;
3620 	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
3621 
3622 	return total;
3623 }
3624 
3625 #else /* !CONFIG_IRQ_TIME_ACCOUNTING: */
3626 
3627 static inline int irqtime_enabled(void)
3628 {
3629 	return 0;
3630 }
3631 
3632 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
3633 
3634 #ifdef CONFIG_CPU_FREQ
3635 
3636 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
3637 
3638 /**
3639  * cpufreq_update_util - Take a note about CPU utilization changes.
3640  * @rq: Runqueue to carry out the update for.
3641  * @flags: Update reason flags.
3642  *
3643  * This function is called by the scheduler on the CPU whose utilization is
3644  * being updated.
3645  *
3646  * It can only be called from RCU-sched read-side critical sections.
3647  *
3648  * The way cpufreq is currently arranged requires it to evaluate the CPU
3649  * performance state (frequency/voltage) on a regular basis to prevent it from
3650  * being stuck in a completely inadequate performance level for too long.
3651  * That is not guaranteed to happen if the updates are only triggered from CFS
3652  * and DL, though, because they may not be coming in if only RT tasks are
3653  * active all the time (or there are RT tasks only).
3654  *
3655  * As a workaround for that issue, this function is called periodically by the
3656  * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
3657  * but that really is a band-aid.  Going forward it should be replaced with
3658  * solutions targeted more specifically at RT tasks.
3659  */
3660 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
3661 {
3662 	struct update_util_data *data;
3663 
3664 	data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
3665 						  cpu_of(rq)));
3666 	if (data)
3667 		data->func(data, rq_clock(rq), flags);
3668 }
3669 #else /* !CONFIG_CPU_FREQ: */
3670 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { }
3671 #endif /* !CONFIG_CPU_FREQ */
3672 
3673 #ifdef arch_scale_freq_capacity
3674 # ifndef arch_scale_freq_invariant
3675 #  define arch_scale_freq_invariant()	true
3676 # endif
3677 #else
3678 # define arch_scale_freq_invariant()	false
3679 #endif
3680 
3681 unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
3682 				 unsigned long *min,
3683 				 unsigned long *max);
3684 
3685 unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
3686 				 unsigned long min,
3687 				 unsigned long max);
3688 
3689 
3690 /*
3691  * Verify the fitness of task @p to run on @cpu taking into account the
3692  * CPU original capacity and the runtime/deadline ratio of the task.
3693  *
3694  * The function will return true if the original capacity of @cpu is
3695  * greater than or equal to task's deadline density right shifted by
3696  * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise.
3697  */
3698 static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
3699 {
3700 	unsigned long cap = arch_scale_cpu_capacity(cpu);
3701 
3702 	return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT);
3703 }
3704 
3705 static inline unsigned long cpu_bw_dl(struct rq *rq)
3706 {
3707 	return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
3708 }
3709 
3710 static inline unsigned long cpu_util_dl(struct rq *rq)
3711 {
3712 	return READ_ONCE(rq->avg_dl.util_avg);
3713 }
3714 
3715 
3716 extern unsigned long cpu_util_cfs(int cpu);
3717 extern unsigned long cpu_util_cfs_boost(int cpu);
3718 
3719 static inline unsigned long cpu_util_rt(struct rq *rq)
3720 {
3721 	return READ_ONCE(rq->avg_rt.util_avg);
3722 }
3723 
3724 #ifdef CONFIG_UCLAMP_TASK
3725 
3726 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
3727 
3728 /*
3729  * When uclamp is compiled in, the aggregation at rq level is 'turned off'
3730  * by default in the fast path and only gets turned on once userspace performs
3731  * an operation that requires it.
3732  *
3733  * Returns true if userspace opted-in to use uclamp and aggregation at rq level
3734  * hence is active.
3735  */
3736 static inline bool uclamp_is_used(void)
3737 {
3738 	return static_branch_likely(&sched_uclamp_used);
3739 }
3740 
3741 /*
3742  * Enabling static branches would get the cpus_read_lock(),
3743  * check whether uclamp_is_used before enable it to avoid always
3744  * calling cpus_read_lock(). Because we never disable this
3745  * static key once enable it.
3746  */
3747 static inline void sched_uclamp_enable(void)
3748 {
3749 	if (!uclamp_is_used())
3750 		static_branch_enable(&sched_uclamp_used);
3751 }
3752 
3753 static inline unsigned long uclamp_rq_get(struct rq *rq,
3754 					  enum uclamp_id clamp_id)
3755 {
3756 	return READ_ONCE(rq->uclamp[clamp_id].value);
3757 }
3758 
3759 static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
3760 				 unsigned int value)
3761 {
3762 	WRITE_ONCE(rq->uclamp[clamp_id].value, value);
3763 }
3764 
3765 static inline bool uclamp_rq_is_idle(struct rq *rq)
3766 {
3767 	return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
3768 }
3769 
3770 /* Is the rq being capped/throttled by uclamp_max? */
3771 static inline bool uclamp_rq_is_capped(struct rq *rq)
3772 {
3773 	unsigned long rq_util;
3774 	unsigned long max_util;
3775 
3776 	if (!uclamp_is_used())
3777 		return false;
3778 
3779 	rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
3780 	max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
3781 
3782 	return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
3783 }
3784 
3785 #define for_each_clamp_id(clamp_id) \
3786 	for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
3787 
3788 extern unsigned int sysctl_sched_uclamp_util_min_rt_default;
3789 
3790 
3791 static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
3792 {
3793 	if (clamp_id == UCLAMP_MIN)
3794 		return 0;
3795 	return SCHED_CAPACITY_SCALE;
3796 }
3797 
3798 /* Integer rounded range for each bucket */
3799 #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
3800 
3801 static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
3802 {
3803 	return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1);
3804 }
3805 
3806 static inline void
3807 uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined)
3808 {
3809 	uc_se->value = value;
3810 	uc_se->bucket_id = uclamp_bucket_id(value);
3811 	uc_se->user_defined = user_defined;
3812 }
3813 
3814 #else /* !CONFIG_UCLAMP_TASK: */
3815 
3816 static inline unsigned long
3817 uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
3818 {
3819 	if (clamp_id == UCLAMP_MIN)
3820 		return 0;
3821 
3822 	return SCHED_CAPACITY_SCALE;
3823 }
3824 
3825 static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
3826 
3827 static inline bool uclamp_is_used(void)
3828 {
3829 	return false;
3830 }
3831 
3832 static inline void sched_uclamp_enable(void) {}
3833 
3834 static inline unsigned long
3835 uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
3836 {
3837 	if (clamp_id == UCLAMP_MIN)
3838 		return 0;
3839 
3840 	return SCHED_CAPACITY_SCALE;
3841 }
3842 
3843 static inline void
3844 uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value)
3845 {
3846 }
3847 
3848 static inline bool uclamp_rq_is_idle(struct rq *rq)
3849 {
3850 	return false;
3851 }
3852 
3853 #endif /* !CONFIG_UCLAMP_TASK */
3854 
3855 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
3856 
3857 static inline unsigned long cpu_util_irq(struct rq *rq)
3858 {
3859 	return READ_ONCE(rq->avg_irq.util_avg);
3860 }
3861 
3862 static inline
3863 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3864 {
3865 	util *= (max - irq);
3866 	util /= max;
3867 
3868 	return util;
3869 
3870 }
3871 
3872 #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */
3873 
3874 static inline unsigned long cpu_util_irq(struct rq *rq)
3875 {
3876 	return 0;
3877 }
3878 
3879 static inline
3880 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3881 {
3882 	return util;
3883 }
3884 
3885 #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */
3886 
3887 extern void __setparam_fair(struct task_struct *p, const struct sched_attr *attr);
3888 
3889 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
3890 
3891 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
3892 
3893 DECLARE_STATIC_KEY_FALSE(sched_energy_present);
3894 
3895 static inline bool sched_energy_enabled(void)
3896 {
3897 	return static_branch_unlikely(&sched_energy_present);
3898 }
3899 
3900 #else /* !(CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL): */
3901 
3902 #define perf_domain_span(pd) NULL
3903 
3904 static inline bool sched_energy_enabled(void) { return false; }
3905 
3906 #endif /* !(CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
3907 
3908 #ifdef CONFIG_MEMBARRIER
3909 
3910 /*
3911  * The scheduler provides memory barriers required by membarrier between:
3912  * - prior user-space memory accesses and store to rq->membarrier_state,
3913  * - store to rq->membarrier_state and following user-space memory accesses.
3914  * In the same way it provides those guarantees around store to rq->curr.
3915  */
3916 static inline void membarrier_switch_mm(struct rq *rq,
3917 					struct mm_struct *prev_mm,
3918 					struct mm_struct *next_mm)
3919 {
3920 	int membarrier_state;
3921 
3922 	if (prev_mm == next_mm)
3923 		return;
3924 
3925 	membarrier_state = atomic_read(&next_mm->membarrier_state);
3926 	if (READ_ONCE(rq->membarrier_state) == membarrier_state)
3927 		return;
3928 
3929 	WRITE_ONCE(rq->membarrier_state, membarrier_state);
3930 }
3931 
3932 #else /* !CONFIG_MEMBARRIER: */
3933 
3934 static inline void membarrier_switch_mm(struct rq *rq,
3935 					struct mm_struct *prev_mm,
3936 					struct mm_struct *next_mm)
3937 {
3938 }
3939 
3940 #endif /* !CONFIG_MEMBARRIER */
3941 
3942 static inline bool is_per_cpu_kthread(struct task_struct *p)
3943 {
3944 	if (!(p->flags & PF_KTHREAD))
3945 		return false;
3946 
3947 	if (p->nr_cpus_allowed != 1)
3948 		return false;
3949 
3950 	return true;
3951 }
3952 
3953 extern void swake_up_all_locked(struct swait_queue_head *q);
3954 extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
3955 
3956 extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags);
3957 
3958 #ifdef CONFIG_PREEMPT_DYNAMIC
3959 extern int preempt_dynamic_mode;
3960 extern int sched_dynamic_mode(const char *str);
3961 extern void sched_dynamic_update(int mode);
3962 #endif
3963 extern const char *preempt_modes[];
3964 
3965 #ifdef CONFIG_SCHED_MM_CID
3966 
3967 static __always_inline bool cid_on_cpu(unsigned int cid)
3968 {
3969 	return cid & MM_CID_ONCPU;
3970 }
3971 
3972 static __always_inline bool cid_in_transit(unsigned int cid)
3973 {
3974 	return cid & MM_CID_TRANSIT;
3975 }
3976 
3977 static __always_inline unsigned int cpu_cid_to_cid(unsigned int cid)
3978 {
3979 	return cid & ~MM_CID_ONCPU;
3980 }
3981 
3982 static __always_inline unsigned int cid_to_cpu_cid(unsigned int cid)
3983 {
3984 	return cid | MM_CID_ONCPU;
3985 }
3986 
3987 static __always_inline unsigned int cid_to_transit_cid(unsigned int cid)
3988 {
3989 	return cid | MM_CID_TRANSIT;
3990 }
3991 
3992 static __always_inline unsigned int cid_from_transit_cid(unsigned int cid)
3993 {
3994 	return cid & ~MM_CID_TRANSIT;
3995 }
3996 
3997 static __always_inline bool cid_on_task(unsigned int cid)
3998 {
3999 	/* True if none of the MM_CID_ONCPU, MM_CID_TRANSIT, MM_CID_UNSET bits is set */
4000 	return cid < MM_CID_TRANSIT;
4001 }
4002 
4003 static __always_inline void mm_drop_cid(struct mm_struct *mm, unsigned int cid)
4004 {
4005 	clear_bit(cid, mm_cidmask(mm));
4006 }
4007 
4008 static __always_inline void mm_unset_cid_on_task(struct task_struct *t)
4009 {
4010 	unsigned int cid = t->mm_cid.cid;
4011 
4012 	t->mm_cid.cid = MM_CID_UNSET;
4013 	if (cid_on_task(cid))
4014 		mm_drop_cid(t->mm, cid);
4015 }
4016 
4017 static __always_inline void mm_drop_cid_on_cpu(struct mm_struct *mm, struct mm_cid_pcpu *pcp)
4018 {
4019 	/* Clear the ONCPU bit, but do not set UNSET in the per CPU storage */
4020 	if (cid_on_cpu(pcp->cid)) {
4021 		pcp->cid = cpu_cid_to_cid(pcp->cid);
4022 		mm_drop_cid(mm, pcp->cid);
4023 	}
4024 }
4025 
4026 static inline unsigned int __mm_get_cid(struct mm_struct *mm, unsigned int max_cids)
4027 {
4028 	unsigned int cid = find_first_zero_bit(mm_cidmask(mm), max_cids);
4029 
4030 	if (cid >= max_cids)
4031 		return MM_CID_UNSET;
4032 	if (test_and_set_bit(cid, mm_cidmask(mm)))
4033 		return MM_CID_UNSET;
4034 	return cid;
4035 }
4036 
4037 static inline unsigned int mm_get_cid(struct mm_struct *mm)
4038 {
4039 	unsigned int cid = __mm_get_cid(mm, READ_ONCE(mm->mm_cid.max_cids));
4040 
4041 	while (cid == MM_CID_UNSET) {
4042 		cpu_relax();
4043 		cid = __mm_get_cid(mm, num_possible_cpus());
4044 	}
4045 	return cid;
4046 }
4047 
4048 static inline unsigned int mm_cid_converge(struct mm_struct *mm, unsigned int orig_cid,
4049 					   unsigned int max_cids)
4050 {
4051 	unsigned int new_cid, cid = cpu_cid_to_cid(orig_cid);
4052 
4053 	/* Is it in the optimal CID space? */
4054 	if (likely(cid < max_cids))
4055 		return orig_cid;
4056 
4057 	/* Try to find one in the optimal space. Otherwise keep the provided. */
4058 	new_cid = __mm_get_cid(mm, max_cids);
4059 	if (new_cid != MM_CID_UNSET) {
4060 		mm_drop_cid(mm, cid);
4061 		/* Preserve the ONCPU mode of the original CID */
4062 		return new_cid | (orig_cid & MM_CID_ONCPU);
4063 	}
4064 	return orig_cid;
4065 }
4066 
4067 static __always_inline void mm_cid_update_task_cid(struct task_struct *t, unsigned int cid)
4068 {
4069 	if (t->mm_cid.cid != cid) {
4070 		t->mm_cid.cid = cid;
4071 		rseq_sched_set_ids_changed(t);
4072 	}
4073 }
4074 
4075 static __always_inline void mm_cid_update_pcpu_cid(struct mm_struct *mm, unsigned int cid)
4076 {
4077 	__this_cpu_write(mm->mm_cid.pcpu->cid, cid);
4078 }
4079 
4080 static __always_inline void mm_cid_from_cpu(struct task_struct *t, unsigned int cpu_cid,
4081 					    unsigned int mode)
4082 {
4083 	unsigned int max_cids, tcid = t->mm_cid.cid;
4084 	struct mm_struct *mm = t->mm;
4085 
4086 	max_cids = READ_ONCE(mm->mm_cid.max_cids);
4087 	/* Optimize for the common case where both have the ONCPU bit set */
4088 	if (likely(cid_on_cpu(cpu_cid & tcid))) {
4089 		if (likely(cpu_cid_to_cid(cpu_cid) < max_cids)) {
4090 			mm_cid_update_task_cid(t, cpu_cid);
4091 			return;
4092 		}
4093 		/* Try to converge into the optimal CID space */
4094 		cpu_cid = mm_cid_converge(mm, cpu_cid, max_cids);
4095 	} else {
4096 		/* Hand over or drop the task owned CID */
4097 		if (cid_on_task(tcid)) {
4098 			if (cid_on_cpu(cpu_cid))
4099 				mm_unset_cid_on_task(t);
4100 			else
4101 				cpu_cid = cid_to_cpu_cid(tcid);
4102 		}
4103 		/* Still nothing, allocate a new one */
4104 		if (!cid_on_cpu(cpu_cid))
4105 			cpu_cid = cid_to_cpu_cid(mm_get_cid(mm));
4106 
4107 		/* Handle the transition mode flag if required */
4108 		if (mode & MM_CID_TRANSIT)
4109 			cpu_cid = cpu_cid_to_cid(cpu_cid) | MM_CID_TRANSIT;
4110 	}
4111 	mm_cid_update_pcpu_cid(mm, cpu_cid);
4112 	mm_cid_update_task_cid(t, cpu_cid);
4113 }
4114 
4115 static __always_inline void mm_cid_from_task(struct task_struct *t, unsigned int cpu_cid,
4116 					     unsigned int mode)
4117 {
4118 	unsigned int max_cids, tcid = t->mm_cid.cid;
4119 	struct mm_struct *mm = t->mm;
4120 
4121 	max_cids = READ_ONCE(mm->mm_cid.max_cids);
4122 	/* Optimize for the common case, where both have the ONCPU bit clear */
4123 	if (likely(cid_on_task(tcid | cpu_cid))) {
4124 		if (likely(tcid < max_cids)) {
4125 			mm_cid_update_pcpu_cid(mm, tcid);
4126 			return;
4127 		}
4128 		/* Try to converge into the optimal CID space */
4129 		tcid = mm_cid_converge(mm, tcid, max_cids);
4130 	} else {
4131 		/* Hand over or drop the CPU owned CID */
4132 		if (cid_on_cpu(cpu_cid)) {
4133 			if (cid_on_task(tcid))
4134 				mm_drop_cid_on_cpu(mm, this_cpu_ptr(mm->mm_cid.pcpu));
4135 			else
4136 				tcid = cpu_cid_to_cid(cpu_cid);
4137 		}
4138 		/* Still nothing, allocate a new one */
4139 		if (!cid_on_task(tcid))
4140 			tcid = mm_get_cid(mm);
4141 		/* Set the transition mode flag if required */
4142 		tcid |= mode & MM_CID_TRANSIT;
4143 	}
4144 	mm_cid_update_pcpu_cid(mm, tcid);
4145 	mm_cid_update_task_cid(t, tcid);
4146 }
4147 
4148 static __always_inline void mm_cid_schedin(struct task_struct *next)
4149 {
4150 	struct mm_struct *mm = next->mm;
4151 	unsigned int cpu_cid, mode;
4152 
4153 	if (!next->mm_cid.active)
4154 		return;
4155 
4156 	cpu_cid = __this_cpu_read(mm->mm_cid.pcpu->cid);
4157 	mode = READ_ONCE(mm->mm_cid.mode);
4158 	if (likely(!cid_on_cpu(mode)))
4159 		mm_cid_from_task(next, cpu_cid, mode);
4160 	else
4161 		mm_cid_from_cpu(next, cpu_cid, mode);
4162 }
4163 
4164 static __always_inline void mm_cid_schedout(struct task_struct *prev)
4165 {
4166 	struct mm_struct *mm = prev->mm;
4167 	unsigned int mode, cid;
4168 
4169 	/* During mode transitions CIDs are temporary and need to be dropped */
4170 	if (likely(!cid_in_transit(prev->mm_cid.cid)))
4171 		return;
4172 
4173 	mode = READ_ONCE(mm->mm_cid.mode);
4174 	cid = cid_from_transit_cid(prev->mm_cid.cid);
4175 
4176 	/*
4177 	 * If transition mode is done, transfer ownership when the CID is
4178 	 * within the convergence range to optimize the next schedule in.
4179 	 */
4180 	if (!cid_in_transit(mode) && cid < READ_ONCE(mm->mm_cid.max_cids)) {
4181 		if (cid_on_cpu(mode))
4182 			cid = cid_to_cpu_cid(cid);
4183 
4184 		/* Update both so that the next schedule in goes into the fast path */
4185 		mm_cid_update_pcpu_cid(mm, cid);
4186 		prev->mm_cid.cid = cid;
4187 	} else {
4188 		mm_drop_cid(mm, cid);
4189 		prev->mm_cid.cid = MM_CID_UNSET;
4190 	}
4191 }
4192 
4193 static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next)
4194 {
4195 	mm_cid_schedout(prev);
4196 	mm_cid_schedin(next);
4197 }
4198 
4199 #else /* !CONFIG_SCHED_MM_CID: */
4200 static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next) { }
4201 #endif /* !CONFIG_SCHED_MM_CID */
4202 
4203 #ifdef CONFIG_SCHED_CACHE
4204 DECLARE_STATIC_KEY_FALSE(sched_cache_present);
4205 DECLARE_STATIC_KEY_FALSE(sched_cache_active);
4206 extern int sysctl_sched_cache_user;
4207 extern unsigned int llc_aggr_tolerance;
4208 extern unsigned int llc_epoch_period;
4209 extern unsigned int llc_epoch_affinity_timeout;
4210 extern unsigned int llc_imb_pct;
4211 extern unsigned int llc_overaggr_pct;
4212 
4213 static inline bool sched_cache_enabled(void)
4214 {
4215 	return static_branch_unlikely(&sched_cache_active);
4216 }
4217 
4218 extern void sched_cache_active_set(void);
4219 
4220 #endif
4221 
4222 void sched_domains_free_llc_id(int cpu);
4223 
4224 extern void init_sched_mm(struct task_struct *p);
4225 
4226 extern u64 avg_vruntime(struct cfs_rq *cfs_rq);
4227 extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se);
4228 static inline
4229 void move_queued_task_locked(struct rq *src_rq, struct rq *dst_rq, struct task_struct *task)
4230 {
4231 	lockdep_assert_rq_held(src_rq);
4232 	lockdep_assert_rq_held(dst_rq);
4233 
4234 	deactivate_task(src_rq, task, 0);
4235 	set_task_cpu(task, dst_rq->cpu);
4236 	activate_task(dst_rq, task, 0);
4237 	wakeup_preempt(dst_rq, task, 0);
4238 }
4239 
4240 static inline
4241 bool task_is_pushable(struct rq *rq, struct task_struct *p, int cpu)
4242 {
4243 	if (!task_on_cpu(rq, p) &&
4244 	    cpumask_test_cpu(cpu, &p->cpus_mask))
4245 		return true;
4246 
4247 	return false;
4248 }
4249 
4250 #ifdef CONFIG_RT_MUTEXES
4251 
4252 static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
4253 {
4254 	if (pi_task)
4255 		prio = min(prio, pi_task->prio);
4256 
4257 	return prio;
4258 }
4259 
4260 static inline int rt_effective_prio(struct task_struct *p, int prio)
4261 {
4262 	struct task_struct *pi_task = rt_mutex_get_top_task(p);
4263 
4264 	return __rt_effective_prio(pi_task, prio);
4265 }
4266 
4267 #else /* !CONFIG_RT_MUTEXES: */
4268 
4269 static inline int rt_effective_prio(struct task_struct *p, int prio)
4270 {
4271 	return prio;
4272 }
4273 
4274 #endif /* !CONFIG_RT_MUTEXES */
4275 
4276 extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi);
4277 extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx);
4278 extern const struct sched_class *__setscheduler_class(int policy, int prio);
4279 extern void set_load_weight(struct task_struct *p, bool update_load);
4280 extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags);
4281 extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags);
4282 
4283 extern struct balance_callback *splice_balance_callbacks(struct rq *rq);
4284 
4285 extern void __balance_callbacks(struct rq *rq, struct rq_flags *rf);
4286 extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
4287 
4288 /*
4289  * The 'sched_change' pattern is the safe, easy and slow way of changing a
4290  * task's scheduling properties. It dequeues a task, such that the scheduler
4291  * is fully unaware of it; at which point its properties can be modified;
4292  * after which it is enqueued again.
4293  *
4294  * Typically this must be called while holding task_rq_lock, since most/all
4295  * properties are serialized under those locks. There are currently two
4296  * exceptions to this rule in sched/ext which only hold rq->lock: scx_bypass()
4297  * and rq_offline_scx().
4298  */
4299 
4300 /*
4301  * This structure is a temporary, used to preserve/convey the queueing state
4302  * of the task between sched_change_begin() and sched_change_end(). Ensuring
4303  * the task's queueing state is idempotent across the operation.
4304  */
4305 struct sched_change_ctx {
4306 	u64			prio;
4307 	struct task_struct	*p;
4308 	const struct sched_class *class;
4309 	int			flags;
4310 	bool			queued;
4311 	bool			running;
4312 };
4313 
4314 struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned int flags);
4315 void sched_change_end(struct sched_change_ctx *ctx);
4316 
4317 DEFINE_CLASS(sched_change, struct sched_change_ctx *,
4318 	     sched_change_end(_T),
4319 	     sched_change_begin(p, flags),
4320 	     struct task_struct *p, unsigned int flags)
4321 
4322 DEFINE_CLASS_IS_UNCONDITIONAL(sched_change)
4323 
4324 #include "ext/ext.h"
4325 
4326 #ifdef CONFIG_PREFERRED_CPU
4327 void sched_push_current_non_preferred_cpu(struct rq *rq);
4328 #else	/* !CONFIG_PREFERRED_CPU */
4329 static inline void sched_push_current_non_preferred_cpu(struct rq *rq) { }
4330 #endif
4331 
4332 #endif /* _KERNEL_SCHED_SCHED_H */
4333