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