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