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