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