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