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