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