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