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