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