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