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