blob: 12c65628801c66046c4d259d83f097f385fc87ca [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Scheduler internal types and methods:
4 */
5#include <linux/sched.h>
6
7#include <linux/sched/autogroup.h>
8#include <linux/sched/clock.h>
9#include <linux/sched/coredump.h>
10#include <linux/sched/cpufreq.h>
11#include <linux/sched/cputime.h>
12#include <linux/sched/deadline.h>
13#include <linux/sched/debug.h>
14#include <linux/sched/hotplug.h>
15#include <linux/sched/idle.h>
16#include <linux/sched/init.h>
17#include <linux/sched/isolation.h>
18#include <linux/sched/jobctl.h>
19#include <linux/sched/loadavg.h>
20#include <linux/sched/mm.h>
21#include <linux/sched/nohz.h>
22#include <linux/sched/numa_balancing.h>
23#include <linux/sched/prio.h>
24#include <linux/sched/rt.h>
25#include <linux/sched/signal.h>
26#include <linux/sched/smt.h>
27#include <linux/sched/stat.h>
28#include <linux/sched/sysctl.h>
29#include <linux/sched/task.h>
30#include <linux/sched/task_stack.h>
31#include <linux/sched/topology.h>
32#include <linux/sched/user.h>
33#include <linux/sched/wake_q.h>
34#include <linux/sched/xacct.h>
35
36#include <uapi/linux/sched/types.h>
37
38#include <linux/binfmts.h>
39#include <linux/blkdev.h>
40#include <linux/compat.h>
41#include <linux/context_tracking.h>
42#include <linux/cpufreq.h>
43#include <linux/cpuidle.h>
44#include <linux/cpuset.h>
45#include <linux/ctype.h>
46#include <linux/debugfs.h>
47#include <linux/delayacct.h>
David Brazdil0f672f62019-12-10 10:32:29 +000048#include <linux/energy_model.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049#include <linux/init_task.h>
50#include <linux/kprobes.h>
51#include <linux/kthread.h>
52#include <linux/membarrier.h>
53#include <linux/migrate.h>
54#include <linux/mmu_context.h>
55#include <linux/nmi.h>
56#include <linux/proc_fs.h>
57#include <linux/prefetch.h>
58#include <linux/profile.h>
David Brazdil0f672f62019-12-10 10:32:29 +000059#include <linux/psi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060#include <linux/rcupdate_wait.h>
61#include <linux/security.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062#include <linux/stop_machine.h>
63#include <linux/suspend.h>
64#include <linux/swait.h>
65#include <linux/syscalls.h>
66#include <linux/task_work.h>
67#include <linux/tsacct_kern.h>
68
69#include <asm/tlb.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020070#include <asm-generic/vmlinux.lds.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071
72#ifdef CONFIG_PARAVIRT
73# include <asm/paravirt.h>
74#endif
75
76#include "cpupri.h"
77#include "cpudeadline.h"
78
Olivier Deprez157378f2022-04-04 15:47:50 +020079#include <trace/events/sched.h>
80
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081#ifdef CONFIG_SCHED_DEBUG
82# define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
83#else
84# define SCHED_WARN_ON(x) ({ (void)(x), 0; })
85#endif
86
87struct rq;
88struct cpuidle_state;
89
90/* task_struct::on_rq states: */
91#define TASK_ON_RQ_QUEUED 1
92#define TASK_ON_RQ_MIGRATING 2
93
94extern __read_mostly int scheduler_running;
95
96extern unsigned long calc_load_update;
97extern atomic_long_t calc_load_tasks;
98
99extern void calc_global_load_tick(struct rq *this_rq);
100extern long calc_load_fold_active(struct rq *this_rq, long adjust);
101
Olivier Deprez157378f2022-04-04 15:47:50 +0200102extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103/*
104 * Helpers for converting nanosecond timing to jiffy resolution
105 */
106#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
107
108/*
109 * Increase resolution of nice-level calculations for 64-bit architectures.
110 * The extra resolution improves shares distribution and load balancing of
111 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
112 * hierarchies, especially on larger systems. This is not a user-visible change
113 * and does not change the user-interface for setting shares/weights.
114 *
115 * We increase resolution only if we have enough bits to allow this increased
116 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
117 * are pretty high and the returns do not justify the increased costs.
118 *
119 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
120 * increase coverage and consistency always enable it on 64-bit platforms.
121 */
122#ifdef CONFIG_64BIT
123# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
124# define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
Olivier Deprez0e641232021-09-23 10:07:05 +0200125# define scale_load_down(w) \
126({ \
127 unsigned long __w = (w); \
128 if (__w) \
129 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
130 __w; \
131})
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132#else
133# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
134# define scale_load(w) (w)
135# define scale_load_down(w) (w)
136#endif
137
138/*
139 * Task weight (visible to users) and its load (invisible to users) have
140 * independent resolution, but they should be well calibrated. We use
141 * scale_load() and scale_load_down(w) to convert between them. The
142 * following must be true:
143 *
144 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
145 *
146 */
147#define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
148
149/*
150 * Single value that decides SCHED_DEADLINE internal math precision.
151 * 10 -> just above 1us
152 * 9 -> just above 0.5us
153 */
154#define DL_SCALE 10
155
156/*
157 * Single value that denotes runtime == period, ie unlimited time.
158 */
159#define RUNTIME_INF ((u64)~0ULL)
160
161static inline int idle_policy(int policy)
162{
163 return policy == SCHED_IDLE;
164}
165static inline int fair_policy(int policy)
166{
167 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
168}
169
170static inline int rt_policy(int policy)
171{
172 return policy == SCHED_FIFO || policy == SCHED_RR;
173}
174
175static inline int dl_policy(int policy)
176{
177 return policy == SCHED_DEADLINE;
178}
179static inline bool valid_policy(int policy)
180{
181 return idle_policy(policy) || fair_policy(policy) ||
182 rt_policy(policy) || dl_policy(policy);
183}
184
David Brazdil0f672f62019-12-10 10:32:29 +0000185static inline int task_has_idle_policy(struct task_struct *p)
186{
187 return idle_policy(p->policy);
188}
189
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190static inline int task_has_rt_policy(struct task_struct *p)
191{
192 return rt_policy(p->policy);
193}
194
195static inline int task_has_dl_policy(struct task_struct *p)
196{
197 return dl_policy(p->policy);
198}
199
200#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
201
Olivier Deprez157378f2022-04-04 15:47:50 +0200202static inline void update_avg(u64 *avg, u64 sample)
203{
204 s64 diff = sample - *avg;
205 *avg += diff / 8;
206}
207
208/*
209 * Shifting a value by an exponent greater *or equal* to the size of said value
210 * is UB; cap at size-1.
211 */
212#define shr_bound(val, shift) \
213 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
214
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215/*
216 * !! For sched_setattr_nocheck() (kernel) only !!
217 *
218 * This is actually gross. :(
219 *
220 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
221 * tasks, but still be able to sleep. We need this on platforms that cannot
222 * atomically change clock frequency. Remove once fast switching will be
223 * available on such platforms.
224 *
225 * SUGOV stands for SchedUtil GOVernor.
226 */
227#define SCHED_FLAG_SUGOV 0x10000000
228
Olivier Deprez0e641232021-09-23 10:07:05 +0200229#define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
230
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se)
232{
233#ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
234 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
235#else
236 return false;
237#endif
238}
239
240/*
241 * Tells if entity @a should preempt entity @b.
242 */
243static inline bool
244dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
245{
246 return dl_entity_is_special(a) ||
247 dl_time_before(a->deadline, b->deadline);
248}
249
250/*
251 * This is the priority-queue data structure of the RT scheduling class:
252 */
253struct rt_prio_array {
254 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
255 struct list_head queue[MAX_RT_PRIO];
256};
257
258struct rt_bandwidth {
259 /* nests inside the rq lock: */
260 raw_spinlock_t rt_runtime_lock;
261 ktime_t rt_period;
262 u64 rt_runtime;
263 struct hrtimer rt_period_timer;
264 unsigned int rt_period_active;
265};
266
267void __dl_clear_params(struct task_struct *p);
268
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269struct dl_bandwidth {
270 raw_spinlock_t dl_runtime_lock;
271 u64 dl_runtime;
272 u64 dl_period;
273};
274
275static inline int dl_bandwidth_enabled(void)
276{
277 return sysctl_sched_rt_runtime >= 0;
278}
279
Olivier Deprez0e641232021-09-23 10:07:05 +0200280/*
281 * To keep the bandwidth of -deadline tasks under control
282 * we need some place where:
283 * - store the maximum -deadline bandwidth of each cpu;
284 * - cache the fraction of bandwidth that is currently allocated in
285 * each root domain;
286 *
287 * This is all done in the data structure below. It is similar to the
288 * one used for RT-throttling (rt_bandwidth), with the main difference
289 * that, since here we are only interested in admission control, we
290 * do not decrease any runtime while the group "executes", neither we
291 * need a timer to replenish it.
292 *
293 * With respect to SMP, bandwidth is given on a per root domain basis,
294 * meaning that:
295 * - bw (< 100%) is the deadline bandwidth of each CPU;
296 * - total_bw is the currently allocated bandwidth in each root domain;
297 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298struct dl_bw {
299 raw_spinlock_t lock;
300 u64 bw;
301 u64 total_bw;
302};
303
304static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
305
306static inline
307void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
308{
309 dl_b->total_bw -= tsk_bw;
310 __dl_update(dl_b, (s32)tsk_bw / cpus);
311}
312
313static inline
314void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
315{
316 dl_b->total_bw += tsk_bw;
317 __dl_update(dl_b, -((s32)tsk_bw / cpus));
318}
319
Olivier Deprez157378f2022-04-04 15:47:50 +0200320static inline bool __dl_overflow(struct dl_bw *dl_b, unsigned long cap,
321 u64 old_bw, u64 new_bw)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322{
323 return dl_b->bw != -1 &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200324 cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325}
326
Olivier Deprez157378f2022-04-04 15:47:50 +0200327/*
328 * Verify the fitness of task @p to run on @cpu taking into account the
329 * CPU original capacity and the runtime/deadline ratio of the task.
330 *
331 * The function will return true if the CPU original capacity of the
332 * @cpu scaled by SCHED_CAPACITY_SCALE >= runtime/deadline ratio of the
333 * task and false otherwise.
334 */
335static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
336{
337 unsigned long cap = arch_scale_cpu_capacity(cpu);
338
339 return cap_scale(p->dl.dl_deadline, cap) >= p->dl.dl_runtime;
340}
341
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342extern void init_dl_bw(struct dl_bw *dl_b);
343extern int sched_dl_global_validate(void);
344extern void sched_dl_do_global(void);
345extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
346extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
347extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
348extern bool __checkparam_dl(const struct sched_attr *attr);
349extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
Olivier Deprez92d4c212022-12-06 15:05:30 +0100351extern int dl_cpu_busy(int cpu, struct task_struct *p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352
353#ifdef CONFIG_CGROUP_SCHED
354
355#include <linux/cgroup.h>
David Brazdil0f672f62019-12-10 10:32:29 +0000356#include <linux/psi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357
358struct cfs_rq;
359struct rt_rq;
360
361extern struct list_head task_groups;
362
363struct cfs_bandwidth {
364#ifdef CONFIG_CFS_BANDWIDTH
365 raw_spinlock_t lock;
366 ktime_t period;
367 u64 quota;
368 u64 runtime;
369 s64 hierarchical_quota;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370
David Brazdil0f672f62019-12-10 10:32:29 +0000371 u8 idle;
372 u8 period_active;
David Brazdil0f672f62019-12-10 10:32:29 +0000373 u8 slack_started;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 struct hrtimer period_timer;
375 struct hrtimer slack_timer;
376 struct list_head throttled_cfs_rq;
377
378 /* Statistics: */
379 int nr_periods;
380 int nr_throttled;
381 u64 throttled_time;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382#endif
383};
384
385/* Task group related information */
386struct task_group {
387 struct cgroup_subsys_state css;
388
389#ifdef CONFIG_FAIR_GROUP_SCHED
390 /* schedulable entities of this group on each CPU */
391 struct sched_entity **se;
392 /* runqueue "owned" by this group on each CPU */
393 struct cfs_rq **cfs_rq;
394 unsigned long shares;
395
396#ifdef CONFIG_SMP
397 /*
398 * load_avg can be heavily contended at clock tick time, so put
399 * it in its own cacheline separated from the fields above which
400 * will also be accessed at each tick.
401 */
402 atomic_long_t load_avg ____cacheline_aligned;
403#endif
404#endif
405
406#ifdef CONFIG_RT_GROUP_SCHED
407 struct sched_rt_entity **rt_se;
408 struct rt_rq **rt_rq;
409
410 struct rt_bandwidth rt_bandwidth;
411#endif
412
413 struct rcu_head rcu;
414 struct list_head list;
415
416 struct task_group *parent;
417 struct list_head siblings;
418 struct list_head children;
419
420#ifdef CONFIG_SCHED_AUTOGROUP
421 struct autogroup *autogroup;
422#endif
423
424 struct cfs_bandwidth cfs_bandwidth;
David Brazdil0f672f62019-12-10 10:32:29 +0000425
426#ifdef CONFIG_UCLAMP_TASK_GROUP
427 /* The two decimal precision [%] value requested from user-space */
428 unsigned int uclamp_pct[UCLAMP_CNT];
429 /* Clamp values requested for a task group */
430 struct uclamp_se uclamp_req[UCLAMP_CNT];
431 /* Effective clamp values used for a task group */
432 struct uclamp_se uclamp[UCLAMP_CNT];
433#endif
434
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435};
436
437#ifdef CONFIG_FAIR_GROUP_SCHED
438#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
439
440/*
441 * A weight of 0 or 1 can cause arithmetics problems.
442 * A weight of a cfs_rq is the sum of weights of which entities
443 * are queued on this cfs_rq, so a weight of a entity should not be
444 * too large, so as the shares value of a task group.
445 * (The default weight is 1024 - so there's no practical
446 * limitation from this.)
447 */
448#define MIN_SHARES (1UL << 1)
449#define MAX_SHARES (1UL << 18)
450#endif
451
452typedef int (*tg_visitor)(struct task_group *, void *);
453
454extern int walk_tg_tree_from(struct task_group *from,
455 tg_visitor down, tg_visitor up, void *data);
456
457/*
458 * Iterate the full tree, calling @down when first entering a node and @up when
459 * leaving it for the final time.
460 *
461 * Caller must hold rcu_lock or sufficient equivalent.
462 */
463static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
464{
465 return walk_tg_tree_from(&root_task_group, down, up, data);
466}
467
468extern int tg_nop(struct task_group *tg, void *data);
469
470extern void free_fair_sched_group(struct task_group *tg);
471extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
472extern void online_fair_sched_group(struct task_group *tg);
473extern void unregister_fair_sched_group(struct task_group *tg);
474extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
475 struct sched_entity *se, int cpu,
476 struct sched_entity *parent);
477extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
478
479extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
480extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
481extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
482
483extern void free_rt_sched_group(struct task_group *tg);
484extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
485extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
486 struct sched_rt_entity *rt_se, int cpu,
487 struct sched_rt_entity *parent);
488extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
489extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
490extern long sched_group_rt_runtime(struct task_group *tg);
491extern long sched_group_rt_period(struct task_group *tg);
492extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
493
494extern struct task_group *sched_create_group(struct task_group *parent);
495extern void sched_online_group(struct task_group *tg,
496 struct task_group *parent);
497extern void sched_destroy_group(struct task_group *tg);
498extern void sched_offline_group(struct task_group *tg);
499
500extern void sched_move_task(struct task_struct *tsk);
501
502#ifdef CONFIG_FAIR_GROUP_SCHED
503extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
504
505#ifdef CONFIG_SMP
506extern void set_task_rq_fair(struct sched_entity *se,
507 struct cfs_rq *prev, struct cfs_rq *next);
508#else /* !CONFIG_SMP */
509static inline void set_task_rq_fair(struct sched_entity *se,
510 struct cfs_rq *prev, struct cfs_rq *next) { }
511#endif /* CONFIG_SMP */
512#endif /* CONFIG_FAIR_GROUP_SCHED */
513
514#else /* CONFIG_CGROUP_SCHED */
515
516struct cfs_bandwidth { };
517
518#endif /* CONFIG_CGROUP_SCHED */
519
520/* CFS-related fields in a runqueue */
521struct cfs_rq {
522 struct load_weight load;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 unsigned int nr_running;
David Brazdil0f672f62019-12-10 10:32:29 +0000524 unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */
525 unsigned int idle_h_nr_running; /* SCHED_IDLE */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526
527 u64 exec_clock;
528 u64 min_vruntime;
529#ifndef CONFIG_64BIT
530 u64 min_vruntime_copy;
531#endif
532
533 struct rb_root_cached tasks_timeline;
534
535 /*
536 * 'curr' points to currently running entity on this cfs_rq.
537 * It is set to NULL otherwise (i.e when none are currently running).
538 */
539 struct sched_entity *curr;
540 struct sched_entity *next;
541 struct sched_entity *last;
542 struct sched_entity *skip;
543
544#ifdef CONFIG_SCHED_DEBUG
545 unsigned int nr_spread_over;
546#endif
547
548#ifdef CONFIG_SMP
549 /*
550 * CFS load tracking
551 */
552 struct sched_avg avg;
553#ifndef CONFIG_64BIT
554 u64 load_last_update_time_copy;
555#endif
556 struct {
557 raw_spinlock_t lock ____cacheline_aligned;
558 int nr;
559 unsigned long load_avg;
560 unsigned long util_avg;
Olivier Deprez157378f2022-04-04 15:47:50 +0200561 unsigned long runnable_avg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 } removed;
563
564#ifdef CONFIG_FAIR_GROUP_SCHED
565 unsigned long tg_load_avg_contrib;
566 long propagate;
567 long prop_runnable_sum;
568
569 /*
570 * h_load = weight * f(tg)
571 *
572 * Where f(tg) is the recursive weight fraction assigned to
573 * this group.
574 */
575 unsigned long h_load;
576 u64 last_h_load_update;
577 struct sched_entity *h_load_next;
578#endif /* CONFIG_FAIR_GROUP_SCHED */
579#endif /* CONFIG_SMP */
580
581#ifdef CONFIG_FAIR_GROUP_SCHED
582 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
583
584 /*
585 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
586 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
587 * (like users, containers etc.)
588 *
589 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
590 * This list is used during load balance.
591 */
592 int on_list;
593 struct list_head leaf_cfs_rq_list;
594 struct task_group *tg; /* group that "owns" this runqueue */
595
596#ifdef CONFIG_CFS_BANDWIDTH
597 int runtime_enabled;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598 s64 runtime_remaining;
599
600 u64 throttled_clock;
Olivier Deprez92d4c212022-12-06 15:05:30 +0100601 u64 throttled_clock_pelt;
602 u64 throttled_clock_pelt_time;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 int throttled;
604 int throttle_count;
605 struct list_head throttled_list;
606#endif /* CONFIG_CFS_BANDWIDTH */
607#endif /* CONFIG_FAIR_GROUP_SCHED */
608};
609
610static inline int rt_bandwidth_enabled(void)
611{
612 return sysctl_sched_rt_runtime >= 0;
613}
614
615/* RT IPI pull logic requires IRQ_WORK */
616#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
617# define HAVE_RT_PUSH_IPI
618#endif
619
620/* Real-Time classes' related field in a runqueue: */
621struct rt_rq {
622 struct rt_prio_array active;
623 unsigned int rt_nr_running;
624 unsigned int rr_nr_running;
625#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
626 struct {
627 int curr; /* highest queued rt task prio */
628#ifdef CONFIG_SMP
629 int next; /* next highest */
630#endif
631 } highest_prio;
632#endif
633#ifdef CONFIG_SMP
634 unsigned long rt_nr_migratory;
635 unsigned long rt_nr_total;
636 int overloaded;
637 struct plist_head pushable_tasks;
638
639#endif /* CONFIG_SMP */
640 int rt_queued;
641
642 int rt_throttled;
643 u64 rt_time;
644 u64 rt_runtime;
645 /* Nests inside the rq lock: */
646 raw_spinlock_t rt_runtime_lock;
647
648#ifdef CONFIG_RT_GROUP_SCHED
649 unsigned long rt_nr_boosted;
650
651 struct rq *rq;
652 struct task_group *tg;
653#endif
654};
655
656static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
657{
658 return rt_rq->rt_queued && rt_rq->rt_nr_running;
659}
660
661/* Deadline class' related fields in a runqueue */
662struct dl_rq {
663 /* runqueue is an rbtree, ordered by deadline */
664 struct rb_root_cached root;
665
666 unsigned long dl_nr_running;
667
668#ifdef CONFIG_SMP
669 /*
670 * Deadline values of the currently executing and the
671 * earliest ready task on this rq. Caching these facilitates
David Brazdil0f672f62019-12-10 10:32:29 +0000672 * the decision whether or not a ready but not running task
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673 * should migrate somewhere else.
674 */
675 struct {
676 u64 curr;
677 u64 next;
678 } earliest_dl;
679
680 unsigned long dl_nr_migratory;
681 int overloaded;
682
683 /*
684 * Tasks on this rq that can be pushed away. They are kept in
685 * an rb-tree, ordered by tasks' deadlines, with caching
686 * of the leftmost (earliest deadline) element.
687 */
688 struct rb_root_cached pushable_dl_tasks_root;
689#else
690 struct dl_bw dl_bw;
691#endif
692 /*
693 * "Active utilization" for this runqueue: increased when a
694 * task wakes up (becomes TASK_RUNNING) and decreased when a
695 * task blocks
696 */
697 u64 running_bw;
698
699 /*
700 * Utilization of the tasks "assigned" to this runqueue (including
701 * the tasks that are in runqueue and the tasks that executed on this
702 * CPU and blocked). Increased when a task moves to this runqueue, and
703 * decreased when the task moves away (migrates, changes scheduling
704 * policy, or terminates).
705 * This is needed to compute the "inactive utilization" for the
706 * runqueue (inactive utilization = this_bw - running_bw).
707 */
708 u64 this_bw;
709 u64 extra_bw;
710
711 /*
712 * Inverse of the fraction of CPU utilization that can be reclaimed
713 * by the GRUB algorithm.
714 */
715 u64 bw_ratio;
716};
717
718#ifdef CONFIG_FAIR_GROUP_SCHED
719/* An entity is a task if it doesn't "own" a runqueue */
720#define entity_is_task(se) (!se->my_q)
Olivier Deprez157378f2022-04-04 15:47:50 +0200721
722static inline void se_update_runnable(struct sched_entity *se)
723{
724 if (!entity_is_task(se))
725 se->runnable_weight = se->my_q->h_nr_running;
726}
727
728static inline long se_runnable(struct sched_entity *se)
729{
730 if (entity_is_task(se))
731 return !!se->on_rq;
732 else
733 return se->runnable_weight;
734}
735
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736#else
737#define entity_is_task(se) 1
Olivier Deprez157378f2022-04-04 15:47:50 +0200738
739static inline void se_update_runnable(struct sched_entity *se) {}
740
741static inline long se_runnable(struct sched_entity *se)
742{
743 return !!se->on_rq;
744}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745#endif
746
747#ifdef CONFIG_SMP
748/*
749 * XXX we want to get rid of these helpers and use the full load resolution.
750 */
751static inline long se_weight(struct sched_entity *se)
752{
753 return scale_load_down(se->load.weight);
754}
755
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000756
757static inline bool sched_asym_prefer(int a, int b)
758{
759 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
760}
761
David Brazdil0f672f62019-12-10 10:32:29 +0000762struct perf_domain {
763 struct em_perf_domain *em_pd;
764 struct perf_domain *next;
765 struct rcu_head rcu;
766};
767
768/* Scheduling group status flags */
769#define SG_OVERLOAD 0x1 /* More than one runnable task on a CPU. */
770#define SG_OVERUTILIZED 0x2 /* One or more CPUs are over-utilized. */
771
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772/*
773 * We add the notion of a root-domain which will be used to define per-domain
774 * variables. Each exclusive cpuset essentially defines an island domain by
775 * fully partitioning the member CPUs from any other cpuset. Whenever a new
776 * exclusive cpuset is created, we also create and attach a new root-domain
777 * object.
778 *
779 */
780struct root_domain {
781 atomic_t refcount;
782 atomic_t rto_count;
783 struct rcu_head rcu;
784 cpumask_var_t span;
785 cpumask_var_t online;
786
David Brazdil0f672f62019-12-10 10:32:29 +0000787 /*
788 * Indicate pullable load on at least one CPU, e.g:
789 * - More than one runnable task
790 * - Running task is misfit
791 */
792 int overload;
793
794 /* Indicate one or more cpus over-utilized (tipping point) */
795 int overutilized;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796
797 /*
798 * The bit corresponding to a CPU gets set here if such CPU has more
799 * than one runnable -deadline task (as it is below for RT tasks).
800 */
801 cpumask_var_t dlo_mask;
802 atomic_t dlo_count;
803 struct dl_bw dl_bw;
804 struct cpudl cpudl;
805
806#ifdef HAVE_RT_PUSH_IPI
807 /*
808 * For IPI pull requests, loop across the rto_mask.
809 */
810 struct irq_work rto_push_work;
811 raw_spinlock_t rto_lock;
812 /* These are only updated and read within rto_lock */
813 int rto_loop;
814 int rto_cpu;
815 /* These atomics are updated outside of a lock */
816 atomic_t rto_loop_next;
817 atomic_t rto_loop_start;
818#endif
819 /*
820 * The "RT overload" flag: it gets set if a CPU has more than
821 * one runnable RT task.
822 */
823 cpumask_var_t rto_mask;
824 struct cpupri cpupri;
825
826 unsigned long max_cpu_capacity;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827
David Brazdil0f672f62019-12-10 10:32:29 +0000828 /*
829 * NULL-terminated list of performance domains intersecting with the
830 * CPUs of the rd. Protected by RCU.
831 */
832 struct perf_domain __rcu *pd;
833};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834
835extern void init_defrootdomain(void);
836extern int sched_init_domains(const struct cpumask *cpu_map);
837extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
838extern void sched_get_rd(struct root_domain *rd);
839extern void sched_put_rd(struct root_domain *rd);
840
841#ifdef HAVE_RT_PUSH_IPI
842extern void rto_push_irq_work_func(struct irq_work *work);
843#endif
844#endif /* CONFIG_SMP */
845
David Brazdil0f672f62019-12-10 10:32:29 +0000846#ifdef CONFIG_UCLAMP_TASK
847/*
848 * struct uclamp_bucket - Utilization clamp bucket
849 * @value: utilization clamp value for tasks on this clamp bucket
850 * @tasks: number of RUNNABLE tasks on this clamp bucket
851 *
852 * Keep track of how many tasks are RUNNABLE for a given utilization
853 * clamp value.
854 */
855struct uclamp_bucket {
856 unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
857 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
858};
859
860/*
861 * struct uclamp_rq - rq's utilization clamp
862 * @value: currently active clamp values for a rq
863 * @bucket: utilization clamp buckets affecting a rq
864 *
865 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
866 * A clamp value is affecting a rq when there is at least one task RUNNABLE
867 * (or actually running) with that value.
868 *
869 * There are up to UCLAMP_CNT possible different clamp values, currently there
870 * are only two: minimum utilization and maximum utilization.
871 *
872 * All utilization clamping values are MAX aggregated, since:
873 * - for util_min: we want to run the CPU at least at the max of the minimum
874 * utilization required by its currently RUNNABLE tasks.
875 * - for util_max: we want to allow the CPU to run up to the max of the
876 * maximum utilization allowed by its currently RUNNABLE tasks.
877 *
878 * Since on each system we expect only a limited number of different
879 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
880 * the metrics required to compute all the per-rq utilization clamp values.
881 */
882struct uclamp_rq {
883 unsigned int value;
884 struct uclamp_bucket bucket[UCLAMP_BUCKETS];
885};
Olivier Deprez0e641232021-09-23 10:07:05 +0200886
887DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
David Brazdil0f672f62019-12-10 10:32:29 +0000888#endif /* CONFIG_UCLAMP_TASK */
889
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000890/*
891 * This is the main, per-CPU runqueue data structure.
892 *
893 * Locking rule: those places that want to lock multiple runqueues
894 * (such as the load balancing or the thread migration code), lock
895 * acquire operations must be ordered by ascending &runqueue.
896 */
897struct rq {
898 /* runqueue lock: */
899 raw_spinlock_t lock;
900
901 /*
902 * nr_running and cpu_load should be in the same cacheline because
903 * remote CPUs use both these fields when doing load calculation.
904 */
905 unsigned int nr_running;
906#ifdef CONFIG_NUMA_BALANCING
907 unsigned int nr_numa_running;
908 unsigned int nr_preferred_running;
909 unsigned int numa_migrate_on;
910#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911#ifdef CONFIG_NO_HZ_COMMON
912#ifdef CONFIG_SMP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 unsigned long last_blocked_load_update_tick;
914 unsigned int has_blocked_load;
Olivier Deprez157378f2022-04-04 15:47:50 +0200915 call_single_data_t nohz_csd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916#endif /* CONFIG_SMP */
917 unsigned int nohz_tick_stopped;
Olivier Deprez157378f2022-04-04 15:47:50 +0200918 atomic_t nohz_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919#endif /* CONFIG_NO_HZ_COMMON */
920
Olivier Deprez157378f2022-04-04 15:47:50 +0200921#ifdef CONFIG_SMP
922 unsigned int ttwu_pending;
923#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924 u64 nr_switches;
925
David Brazdil0f672f62019-12-10 10:32:29 +0000926#ifdef CONFIG_UCLAMP_TASK
927 /* Utilization clamp values based on CPU's RUNNABLE tasks */
928 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
929 unsigned int uclamp_flags;
930#define UCLAMP_FLAG_IDLE 0x01
931#endif
932
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933 struct cfs_rq cfs;
934 struct rt_rq rt;
935 struct dl_rq dl;
936
937#ifdef CONFIG_FAIR_GROUP_SCHED
938 /* list of leaf cfs_rq on this CPU: */
939 struct list_head leaf_cfs_rq_list;
940 struct list_head *tmp_alone_branch;
941#endif /* CONFIG_FAIR_GROUP_SCHED */
942
943 /*
944 * This is part of a global counter where only the total sum
945 * over all CPUs matters. A task can increase this counter on
946 * one CPU and if it got migrated afterwards it may decrease
947 * it on another CPU. Always updated under the runqueue lock:
948 */
949 unsigned long nr_uninterruptible;
950
Olivier Deprez157378f2022-04-04 15:47:50 +0200951 struct task_struct __rcu *curr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952 struct task_struct *idle;
953 struct task_struct *stop;
954 unsigned long next_balance;
955 struct mm_struct *prev_mm;
956
957 unsigned int clock_update_flags;
958 u64 clock;
David Brazdil0f672f62019-12-10 10:32:29 +0000959 /* Ensure that all clocks are in the same cache line */
960 u64 clock_task ____cacheline_aligned;
961 u64 clock_pelt;
962 unsigned long lost_idle_time;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963
964 atomic_t nr_iowait;
965
David Brazdil0f672f62019-12-10 10:32:29 +0000966#ifdef CONFIG_MEMBARRIER
967 int membarrier_state;
968#endif
969
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000970#ifdef CONFIG_SMP
David Brazdil0f672f62019-12-10 10:32:29 +0000971 struct root_domain *rd;
972 struct sched_domain __rcu *sd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000973
974 unsigned long cpu_capacity;
975 unsigned long cpu_capacity_orig;
976
977 struct callback_head *balance_callback;
978
Olivier Deprez157378f2022-04-04 15:47:50 +0200979 unsigned char nohz_idle_balance;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980 unsigned char idle_balance;
981
David Brazdil0f672f62019-12-10 10:32:29 +0000982 unsigned long misfit_task_load;
983
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 /* For active balancing */
985 int active_balance;
986 int push_cpu;
987 struct cpu_stop_work active_balance_work;
988
989 /* CPU of this runqueue: */
990 int cpu;
991 int online;
992
993 struct list_head cfs_tasks;
994
995 struct sched_avg avg_rt;
996 struct sched_avg avg_dl;
997#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
998 struct sched_avg avg_irq;
999#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001000#ifdef CONFIG_SCHED_THERMAL_PRESSURE
1001 struct sched_avg avg_thermal;
1002#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003 u64 idle_stamp;
1004 u64 avg_idle;
1005
1006 /* This is used to determine avg_idle's max value */
1007 u64 max_idle_balance_cost;
Olivier Deprez157378f2022-04-04 15:47:50 +02001008#endif /* CONFIG_SMP */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009
1010#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1011 u64 prev_irq_time;
1012#endif
1013#ifdef CONFIG_PARAVIRT
1014 u64 prev_steal_time;
1015#endif
1016#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1017 u64 prev_steal_time_rq;
1018#endif
1019
1020 /* calc_load related fields */
1021 unsigned long calc_load_update;
1022 long calc_load_active;
1023
1024#ifdef CONFIG_SCHED_HRTICK
1025#ifdef CONFIG_SMP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001026 call_single_data_t hrtick_csd;
1027#endif
1028 struct hrtimer hrtick_timer;
Olivier Deprez0e641232021-09-23 10:07:05 +02001029 ktime_t hrtick_time;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030#endif
1031
1032#ifdef CONFIG_SCHEDSTATS
1033 /* latency stats */
1034 struct sched_info rq_sched_info;
1035 unsigned long long rq_cpu_time;
1036 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
1037
1038 /* sys_sched_yield() stats */
1039 unsigned int yld_count;
1040
1041 /* schedule() stats */
1042 unsigned int sched_count;
1043 unsigned int sched_goidle;
1044
1045 /* try_to_wake_up() stats */
1046 unsigned int ttwu_count;
1047 unsigned int ttwu_local;
1048#endif
1049
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050#ifdef CONFIG_CPU_IDLE
1051 /* Must be inspected within a rcu lock section */
1052 struct cpuidle_state *idle_state;
1053#endif
1054};
1055
David Brazdil0f672f62019-12-10 10:32:29 +00001056#ifdef CONFIG_FAIR_GROUP_SCHED
1057
1058/* CPU runqueue to which this cfs_rq is attached */
1059static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1060{
1061 return cfs_rq->rq;
1062}
1063
1064#else
1065
1066static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1067{
1068 return container_of(cfs_rq, struct rq, cfs);
1069}
1070#endif
1071
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001072static inline int cpu_of(struct rq *rq)
1073{
1074#ifdef CONFIG_SMP
1075 return rq->cpu;
1076#else
1077 return 0;
1078#endif
1079}
1080
1081
1082#ifdef CONFIG_SCHED_SMT
1083extern void __update_idle_core(struct rq *rq);
1084
1085static inline void update_idle_core(struct rq *rq)
1086{
1087 if (static_branch_unlikely(&sched_smt_present))
1088 __update_idle_core(rq);
1089}
1090
1091#else
1092static inline void update_idle_core(struct rq *rq) { }
1093#endif
1094
1095DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1096
1097#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1098#define this_rq() this_cpu_ptr(&runqueues)
1099#define task_rq(p) cpu_rq(task_cpu(p))
1100#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1101#define raw_rq() raw_cpu_ptr(&runqueues)
1102
David Brazdil0f672f62019-12-10 10:32:29 +00001103extern void update_rq_clock(struct rq *rq);
1104
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001105static inline u64 __rq_clock_broken(struct rq *rq)
1106{
1107 return READ_ONCE(rq->clock);
1108}
1109
1110/*
1111 * rq::clock_update_flags bits
1112 *
1113 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1114 * call to __schedule(). This is an optimisation to avoid
1115 * neighbouring rq clock updates.
1116 *
1117 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1118 * in effect and calls to update_rq_clock() are being ignored.
1119 *
1120 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1121 * made to update_rq_clock() since the last time rq::lock was pinned.
1122 *
1123 * If inside of __schedule(), clock_update_flags will have been
1124 * shifted left (a left shift is a cheap operation for the fast path
1125 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1126 *
1127 * if (rq-clock_update_flags >= RQCF_UPDATED)
1128 *
1129 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
1130 * one position though, because the next rq_unpin_lock() will shift it
1131 * back.
1132 */
1133#define RQCF_REQ_SKIP 0x01
1134#define RQCF_ACT_SKIP 0x02
1135#define RQCF_UPDATED 0x04
1136
1137static inline void assert_clock_updated(struct rq *rq)
1138{
1139 /*
1140 * The only reason for not seeing a clock update since the
1141 * last rq_pin_lock() is if we're currently skipping updates.
1142 */
1143 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
1144}
1145
1146static inline u64 rq_clock(struct rq *rq)
1147{
1148 lockdep_assert_held(&rq->lock);
1149 assert_clock_updated(rq);
1150
1151 return rq->clock;
1152}
1153
1154static inline u64 rq_clock_task(struct rq *rq)
1155{
1156 lockdep_assert_held(&rq->lock);
1157 assert_clock_updated(rq);
1158
1159 return rq->clock_task;
1160}
1161
Olivier Deprez157378f2022-04-04 15:47:50 +02001162/**
1163 * By default the decay is the default pelt decay period.
1164 * The decay shift can change the decay period in
1165 * multiples of 32.
1166 * Decay shift Decay period(ms)
1167 * 0 32
1168 * 1 64
1169 * 2 128
1170 * 3 256
1171 * 4 512
1172 */
1173extern int sched_thermal_decay_shift;
1174
1175static inline u64 rq_clock_thermal(struct rq *rq)
1176{
1177 return rq_clock_task(rq) >> sched_thermal_decay_shift;
1178}
1179
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180static inline void rq_clock_skip_update(struct rq *rq)
1181{
1182 lockdep_assert_held(&rq->lock);
1183 rq->clock_update_flags |= RQCF_REQ_SKIP;
1184}
1185
1186/*
1187 * See rt task throttling, which is the only time a skip
1188 * request is cancelled.
1189 */
1190static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1191{
1192 lockdep_assert_held(&rq->lock);
1193 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1194}
1195
1196struct rq_flags {
1197 unsigned long flags;
1198 struct pin_cookie cookie;
1199#ifdef CONFIG_SCHED_DEBUG
1200 /*
1201 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1202 * current pin context is stashed here in case it needs to be
1203 * restored in rq_repin_lock().
1204 */
1205 unsigned int clock_update_flags;
1206#endif
1207};
1208
Olivier Deprez157378f2022-04-04 15:47:50 +02001209/*
1210 * Lockdep annotation that avoids accidental unlocks; it's like a
1211 * sticky/continuous lockdep_assert_held().
1212 *
1213 * This avoids code that has access to 'struct rq *rq' (basically everything in
1214 * the scheduler) from accidentally unlocking the rq if they do not also have a
1215 * copy of the (on-stack) 'struct rq_flags rf'.
1216 *
1217 * Also see Documentation/locking/lockdep-design.rst.
1218 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001219static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1220{
1221 rf->cookie = lockdep_pin_lock(&rq->lock);
1222
1223#ifdef CONFIG_SCHED_DEBUG
1224 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1225 rf->clock_update_flags = 0;
1226#endif
1227}
1228
1229static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1230{
1231#ifdef CONFIG_SCHED_DEBUG
1232 if (rq->clock_update_flags > RQCF_ACT_SKIP)
1233 rf->clock_update_flags = RQCF_UPDATED;
1234#endif
1235
1236 lockdep_unpin_lock(&rq->lock, rf->cookie);
1237}
1238
1239static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1240{
1241 lockdep_repin_lock(&rq->lock, rf->cookie);
1242
1243#ifdef CONFIG_SCHED_DEBUG
1244 /*
1245 * Restore the value we stashed in @rf for this pin context.
1246 */
1247 rq->clock_update_flags |= rf->clock_update_flags;
1248#endif
1249}
1250
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001251struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1252 __acquires(rq->lock);
1253
1254struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1255 __acquires(p->pi_lock)
1256 __acquires(rq->lock);
1257
1258static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1259 __releases(rq->lock)
1260{
1261 rq_unpin_lock(rq, rf);
1262 raw_spin_unlock(&rq->lock);
1263}
1264
1265static inline void
1266task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1267 __releases(rq->lock)
1268 __releases(p->pi_lock)
1269{
1270 rq_unpin_lock(rq, rf);
1271 raw_spin_unlock(&rq->lock);
1272 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1273}
1274
1275static inline void
1276rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1277 __acquires(rq->lock)
1278{
1279 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1280 rq_pin_lock(rq, rf);
1281}
1282
1283static inline void
1284rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1285 __acquires(rq->lock)
1286{
1287 raw_spin_lock_irq(&rq->lock);
1288 rq_pin_lock(rq, rf);
1289}
1290
1291static inline void
1292rq_lock(struct rq *rq, struct rq_flags *rf)
1293 __acquires(rq->lock)
1294{
1295 raw_spin_lock(&rq->lock);
1296 rq_pin_lock(rq, rf);
1297}
1298
1299static inline void
1300rq_relock(struct rq *rq, struct rq_flags *rf)
1301 __acquires(rq->lock)
1302{
1303 raw_spin_lock(&rq->lock);
1304 rq_repin_lock(rq, rf);
1305}
1306
1307static inline void
1308rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1309 __releases(rq->lock)
1310{
1311 rq_unpin_lock(rq, rf);
1312 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1313}
1314
1315static inline void
1316rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1317 __releases(rq->lock)
1318{
1319 rq_unpin_lock(rq, rf);
1320 raw_spin_unlock_irq(&rq->lock);
1321}
1322
1323static inline void
1324rq_unlock(struct rq *rq, struct rq_flags *rf)
1325 __releases(rq->lock)
1326{
1327 rq_unpin_lock(rq, rf);
1328 raw_spin_unlock(&rq->lock);
1329}
1330
David Brazdil0f672f62019-12-10 10:32:29 +00001331static inline struct rq *
1332this_rq_lock_irq(struct rq_flags *rf)
1333 __acquires(rq->lock)
1334{
1335 struct rq *rq;
1336
1337 local_irq_disable();
1338 rq = this_rq();
1339 rq_lock(rq, rf);
1340 return rq;
1341}
1342
1343#ifdef CONFIG_NUMA
1344enum numa_topology_type {
1345 NUMA_DIRECT,
1346 NUMA_GLUELESS_MESH,
1347 NUMA_BACKPLANE,
1348};
1349extern enum numa_topology_type sched_numa_topology_type;
1350extern int sched_max_numa_distance;
1351extern bool find_numa_distance(int distance);
1352extern void sched_init_numa(void);
1353extern void sched_domains_numa_masks_set(unsigned int cpu);
1354extern void sched_domains_numa_masks_clear(unsigned int cpu);
1355extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1356#else
1357static inline void sched_init_numa(void) { }
1358static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
1359static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
1360static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1361{
1362 return nr_cpu_ids;
1363}
1364#endif
1365
1366#ifdef CONFIG_NUMA_BALANCING
1367/* The regions in numa_faults array from task_struct */
1368enum numa_faults_stats {
1369 NUMA_MEM = 0,
1370 NUMA_CPU,
1371 NUMA_MEMBUF,
1372 NUMA_CPUBUF
1373};
1374extern void sched_setnuma(struct task_struct *p, int node);
1375extern int migrate_task_to(struct task_struct *p, int cpu);
1376extern int migrate_swap(struct task_struct *p, struct task_struct *t,
1377 int cpu, int scpu);
1378extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
1379#else
1380static inline void
1381init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
1382{
1383}
1384#endif /* CONFIG_NUMA_BALANCING */
1385
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001386#ifdef CONFIG_SMP
David Brazdil0f672f62019-12-10 10:32:29 +00001387
1388static inline void
1389queue_balance_callback(struct rq *rq,
1390 struct callback_head *head,
1391 void (*func)(struct rq *rq))
1392{
1393 lockdep_assert_held(&rq->lock);
1394
1395 if (unlikely(head->next))
1396 return;
1397
1398 head->func = (void (*)(struct callback_head *))func;
1399 head->next = rq->balance_callback;
1400 rq->balance_callback = head;
1401}
1402
David Brazdil0f672f62019-12-10 10:32:29 +00001403#define rcu_dereference_check_sched_domain(p) \
1404 rcu_dereference_check((p), \
1405 lockdep_is_held(&sched_domains_mutex))
1406
1407/*
1408 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1409 * See destroy_sched_domains: call_rcu for details.
1410 *
1411 * The domain tree of any CPU may only be accessed from within
1412 * preempt-disabled sections.
1413 */
1414#define for_each_domain(cpu, __sd) \
1415 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1416 __sd; __sd = __sd->parent)
1417
David Brazdil0f672f62019-12-10 10:32:29 +00001418/**
1419 * highest_flag_domain - Return highest sched_domain containing flag.
1420 * @cpu: The CPU whose highest level of sched domain is to
1421 * be returned.
1422 * @flag: The flag to check for the highest sched_domain
1423 * for the given CPU.
1424 *
1425 * Returns the highest sched_domain of a CPU which contains the given flag.
1426 */
1427static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1428{
1429 struct sched_domain *sd, *hsd = NULL;
1430
1431 for_each_domain(cpu, sd) {
1432 if (!(sd->flags & flag))
1433 break;
1434 hsd = sd;
1435 }
1436
1437 return hsd;
1438}
1439
1440static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1441{
1442 struct sched_domain *sd;
1443
1444 for_each_domain(cpu, sd) {
1445 if (sd->flags & flag)
1446 break;
1447 }
1448
1449 return sd;
1450}
1451
1452DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
1453DECLARE_PER_CPU(int, sd_llc_size);
1454DECLARE_PER_CPU(int, sd_llc_id);
1455DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
1456DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
1457DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
1458DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
1459extern struct static_key_false sched_asym_cpucapacity;
1460
1461struct sched_group_capacity {
1462 atomic_t ref;
1463 /*
1464 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
1465 * for a single CPU.
1466 */
1467 unsigned long capacity;
1468 unsigned long min_capacity; /* Min per-CPU capacity in group */
1469 unsigned long max_capacity; /* Max per-CPU capacity in group */
1470 unsigned long next_update;
1471 int imbalance; /* XXX unrelated to capacity but shared group state */
1472
1473#ifdef CONFIG_SCHED_DEBUG
1474 int id;
1475#endif
1476
Olivier Deprez157378f2022-04-04 15:47:50 +02001477 unsigned long cpumask[]; /* Balance mask */
David Brazdil0f672f62019-12-10 10:32:29 +00001478};
1479
1480struct sched_group {
1481 struct sched_group *next; /* Must be a circular list */
1482 atomic_t ref;
1483
1484 unsigned int group_weight;
1485 struct sched_group_capacity *sgc;
1486 int asym_prefer_cpu; /* CPU of highest priority in group */
1487
1488 /*
1489 * The CPUs this group covers.
1490 *
1491 * NOTE: this field is variable length. (Allocated dynamically
1492 * by attaching extra space to the end of the structure,
1493 * depending on how many CPUs the kernel has booted up with)
1494 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001495 unsigned long cpumask[];
David Brazdil0f672f62019-12-10 10:32:29 +00001496};
1497
1498static inline struct cpumask *sched_group_span(struct sched_group *sg)
1499{
1500 return to_cpumask(sg->cpumask);
1501}
1502
1503/*
1504 * See build_balance_mask().
1505 */
1506static inline struct cpumask *group_balance_mask(struct sched_group *sg)
1507{
1508 return to_cpumask(sg->sgc->cpumask);
1509}
1510
1511/**
1512 * group_first_cpu - Returns the first CPU in the cpumask of a sched_group.
1513 * @group: The group whose first CPU is to be returned.
1514 */
1515static inline unsigned int group_first_cpu(struct sched_group *group)
1516{
1517 return cpumask_first(sched_group_span(group));
1518}
1519
1520extern int group_balance_cpu(struct sched_group *sg);
1521
1522#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1523void register_sched_domain_sysctl(void);
1524void dirty_sched_domain_sysctl(int cpu);
1525void unregister_sched_domain_sysctl(void);
1526#else
1527static inline void register_sched_domain_sysctl(void)
1528{
1529}
1530static inline void dirty_sched_domain_sysctl(int cpu)
1531{
1532}
1533static inline void unregister_sched_domain_sysctl(void)
1534{
1535}
1536#endif
1537
Olivier Deprez157378f2022-04-04 15:47:50 +02001538extern void flush_smp_call_function_from_idle(void);
David Brazdil0f672f62019-12-10 10:32:29 +00001539
Olivier Deprez157378f2022-04-04 15:47:50 +02001540#else /* !CONFIG_SMP: */
1541static inline void flush_smp_call_function_from_idle(void) { }
1542#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001543
1544#include "stats.h"
1545#include "autogroup.h"
1546
1547#ifdef CONFIG_CGROUP_SCHED
1548
1549/*
1550 * Return the group to which this tasks belongs.
1551 *
1552 * We cannot use task_css() and friends because the cgroup subsystem
1553 * changes that value before the cgroup_subsys::attach() method is called,
1554 * therefore we cannot pin it and might observe the wrong value.
1555 *
1556 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1557 * core changes this before calling sched_move_task().
1558 *
1559 * Instead we use a 'copy' which is updated from sched_move_task() while
1560 * holding both task_struct::pi_lock and rq::lock.
1561 */
1562static inline struct task_group *task_group(struct task_struct *p)
1563{
1564 return p->sched_task_group;
1565}
1566
1567/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1568static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1569{
1570#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1571 struct task_group *tg = task_group(p);
1572#endif
1573
1574#ifdef CONFIG_FAIR_GROUP_SCHED
1575 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1576 p->se.cfs_rq = tg->cfs_rq[cpu];
1577 p->se.parent = tg->se[cpu];
1578#endif
1579
1580#ifdef CONFIG_RT_GROUP_SCHED
1581 p->rt.rt_rq = tg->rt_rq[cpu];
1582 p->rt.parent = tg->rt_se[cpu];
1583#endif
1584}
1585
1586#else /* CONFIG_CGROUP_SCHED */
1587
1588static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1589static inline struct task_group *task_group(struct task_struct *p)
1590{
1591 return NULL;
1592}
1593
1594#endif /* CONFIG_CGROUP_SCHED */
1595
1596static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1597{
1598 set_task_rq(p, cpu);
1599#ifdef CONFIG_SMP
1600 /*
1601 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1602 * successfully executed on another CPU. We must ensure that updates of
1603 * per-task data have been completed by this moment.
1604 */
1605 smp_wmb();
1606#ifdef CONFIG_THREAD_INFO_IN_TASK
1607 WRITE_ONCE(p->cpu, cpu);
1608#else
1609 WRITE_ONCE(task_thread_info(p)->cpu, cpu);
1610#endif
1611 p->wake_cpu = cpu;
1612#endif
1613}
1614
1615/*
1616 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1617 */
1618#ifdef CONFIG_SCHED_DEBUG
1619# include <linux/static_key.h>
1620# define const_debug __read_mostly
1621#else
1622# define const_debug const
1623#endif
1624
1625#define SCHED_FEAT(name, enabled) \
1626 __SCHED_FEAT_##name ,
1627
1628enum {
1629#include "features.h"
1630 __SCHED_FEAT_NR,
1631};
1632
1633#undef SCHED_FEAT
1634
Olivier Deprez0e641232021-09-23 10:07:05 +02001635#ifdef CONFIG_SCHED_DEBUG
David Brazdil0f672f62019-12-10 10:32:29 +00001636
1637/*
1638 * To support run-time toggling of sched features, all the translation units
1639 * (but core.c) reference the sysctl_sched_features defined in core.c.
1640 */
1641extern const_debug unsigned int sysctl_sched_features;
1642
Olivier Deprez0e641232021-09-23 10:07:05 +02001643#ifdef CONFIG_JUMP_LABEL
David Brazdil0f672f62019-12-10 10:32:29 +00001644#define SCHED_FEAT(name, enabled) \
1645static __always_inline bool static_branch_##name(struct static_key *key) \
1646{ \
1647 return static_key_##enabled(key); \
1648}
1649
1650#include "features.h"
1651#undef SCHED_FEAT
1652
1653extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1654#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1655
Olivier Deprez0e641232021-09-23 10:07:05 +02001656#else /* !CONFIG_JUMP_LABEL */
1657
1658#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1659
1660#endif /* CONFIG_JUMP_LABEL */
1661
1662#else /* !SCHED_DEBUG */
David Brazdil0f672f62019-12-10 10:32:29 +00001663
1664/*
1665 * Each translation unit has its own copy of sysctl_sched_features to allow
1666 * constants propagation at compile time and compiler optimization based on
1667 * features default.
1668 */
1669#define SCHED_FEAT(name, enabled) \
1670 (1UL << __SCHED_FEAT_##name) * enabled |
1671static const_debug __maybe_unused unsigned int sysctl_sched_features =
1672#include "features.h"
1673 0;
1674#undef SCHED_FEAT
1675
1676#define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1677
Olivier Deprez0e641232021-09-23 10:07:05 +02001678#endif /* SCHED_DEBUG */
David Brazdil0f672f62019-12-10 10:32:29 +00001679
1680extern struct static_key_false sched_numa_balancing;
1681extern struct static_key_false sched_schedstats;
1682
1683static inline u64 global_rt_period(void)
1684{
1685 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1686}
1687
1688static inline u64 global_rt_runtime(void)
1689{
1690 if (sysctl_sched_rt_runtime < 0)
1691 return RUNTIME_INF;
1692
1693 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1694}
1695
1696static inline int task_current(struct rq *rq, struct task_struct *p)
1697{
1698 return rq->curr == p;
1699}
1700
1701static inline int task_running(struct rq *rq, struct task_struct *p)
1702{
1703#ifdef CONFIG_SMP
1704 return p->on_cpu;
1705#else
1706 return task_current(rq, p);
1707#endif
1708}
1709
1710static inline int task_on_rq_queued(struct task_struct *p)
1711{
1712 return p->on_rq == TASK_ON_RQ_QUEUED;
1713}
1714
1715static inline int task_on_rq_migrating(struct task_struct *p)
1716{
1717 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
1718}
1719
1720/*
1721 * wake flags
1722 */
1723#define WF_SYNC 0x01 /* Waker goes to sleep after wakeup */
1724#define WF_FORK 0x02 /* Child wakeup after fork */
Olivier Deprez157378f2022-04-04 15:47:50 +02001725#define WF_MIGRATED 0x04 /* Internal use, task got migrated */
1726#define WF_ON_CPU 0x08 /* Wakee is on_cpu */
David Brazdil0f672f62019-12-10 10:32:29 +00001727
1728/*
1729 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1730 * of tasks with abnormal "nice" values across CPUs the contribution that
1731 * each task makes to its run queue's load is weighted according to its
1732 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1733 * scaled version of the new time slice allocation that they receive on time
1734 * slice expiry etc.
1735 */
1736
1737#define WEIGHT_IDLEPRIO 3
1738#define WMULT_IDLEPRIO 1431655765
1739
1740extern const int sched_prio_to_weight[40];
1741extern const u32 sched_prio_to_wmult[40];
1742
1743/*
1744 * {de,en}queue flags:
1745 *
1746 * DEQUEUE_SLEEP - task is no longer runnable
1747 * ENQUEUE_WAKEUP - task just became runnable
1748 *
1749 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1750 * are in a known state which allows modification. Such pairs
1751 * should preserve as much state as possible.
1752 *
1753 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1754 * in the runqueue.
1755 *
1756 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1757 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1758 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1759 *
1760 */
1761
1762#define DEQUEUE_SLEEP 0x01
1763#define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
1764#define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
1765#define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
1766
1767#define ENQUEUE_WAKEUP 0x01
1768#define ENQUEUE_RESTORE 0x02
1769#define ENQUEUE_MOVE 0x04
1770#define ENQUEUE_NOCLOCK 0x08
1771
1772#define ENQUEUE_HEAD 0x10
1773#define ENQUEUE_REPLENISH 0x20
1774#ifdef CONFIG_SMP
1775#define ENQUEUE_MIGRATED 0x40
1776#else
1777#define ENQUEUE_MIGRATED 0x00
1778#endif
1779
1780#define RETRY_TASK ((void *)-1UL)
1781
1782struct sched_class {
David Brazdil0f672f62019-12-10 10:32:29 +00001783
1784#ifdef CONFIG_UCLAMP_TASK
1785 int uclamp_enabled;
1786#endif
1787
1788 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1789 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1790 void (*yield_task) (struct rq *rq);
Olivier Deprez157378f2022-04-04 15:47:50 +02001791 bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
David Brazdil0f672f62019-12-10 10:32:29 +00001792
1793 void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags);
1794
Olivier Deprez157378f2022-04-04 15:47:50 +02001795 struct task_struct *(*pick_next_task)(struct rq *rq);
1796
David Brazdil0f672f62019-12-10 10:32:29 +00001797 void (*put_prev_task)(struct rq *rq, struct task_struct *p);
Olivier Deprez0e641232021-09-23 10:07:05 +02001798 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
David Brazdil0f672f62019-12-10 10:32:29 +00001799
1800#ifdef CONFIG_SMP
1801 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
1802 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1803 void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
1804
1805 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
1806
1807 void (*set_cpus_allowed)(struct task_struct *p,
1808 const struct cpumask *newmask);
1809
1810 void (*rq_online)(struct rq *rq);
1811 void (*rq_offline)(struct rq *rq);
1812#endif
1813
1814 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
1815 void (*task_fork)(struct task_struct *p);
1816 void (*task_dead)(struct task_struct *p);
1817
1818 /*
1819 * The switched_from() call is allowed to drop rq->lock, therefore we
1820 * cannot assume the switched_from/switched_to pair is serliazed by
1821 * rq->lock. They are however serialized by p->pi_lock.
1822 */
1823 void (*switched_from)(struct rq *this_rq, struct task_struct *task);
1824 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1825 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1826 int oldprio);
1827
1828 unsigned int (*get_rr_interval)(struct rq *rq,
1829 struct task_struct *task);
1830
1831 void (*update_curr)(struct rq *rq);
1832
1833#define TASK_SET_GROUP 0
1834#define TASK_MOVE_GROUP 1
1835
1836#ifdef CONFIG_FAIR_GROUP_SCHED
1837 void (*task_change_group)(struct task_struct *p, int type);
1838#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001839} __aligned(STRUCT_ALIGNMENT); /* STRUCT_ALIGN(), vmlinux.lds.h */
David Brazdil0f672f62019-12-10 10:32:29 +00001840
1841static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1842{
1843 WARN_ON_ONCE(rq->curr != prev);
1844 prev->sched_class->put_prev_task(rq, prev);
1845}
1846
1847static inline void set_next_task(struct rq *rq, struct task_struct *next)
1848{
1849 WARN_ON_ONCE(rq->curr != next);
Olivier Deprez0e641232021-09-23 10:07:05 +02001850 next->sched_class->set_next_task(rq, next, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001851}
1852
Olivier Deprez157378f2022-04-04 15:47:50 +02001853/* Defined in include/asm-generic/vmlinux.lds.h */
1854extern struct sched_class __begin_sched_classes[];
1855extern struct sched_class __end_sched_classes[];
1856
1857#define sched_class_highest (__end_sched_classes - 1)
1858#define sched_class_lowest (__begin_sched_classes - 1)
David Brazdil0f672f62019-12-10 10:32:29 +00001859
1860#define for_class_range(class, _from, _to) \
Olivier Deprez157378f2022-04-04 15:47:50 +02001861 for (class = (_from); class != (_to); class--)
David Brazdil0f672f62019-12-10 10:32:29 +00001862
1863#define for_each_class(class) \
Olivier Deprez157378f2022-04-04 15:47:50 +02001864 for_class_range(class, sched_class_highest, sched_class_lowest)
David Brazdil0f672f62019-12-10 10:32:29 +00001865
1866extern const struct sched_class stop_sched_class;
1867extern const struct sched_class dl_sched_class;
1868extern const struct sched_class rt_sched_class;
1869extern const struct sched_class fair_sched_class;
1870extern const struct sched_class idle_sched_class;
1871
1872static inline bool sched_stop_runnable(struct rq *rq)
1873{
1874 return rq->stop && task_on_rq_queued(rq->stop);
1875}
1876
1877static inline bool sched_dl_runnable(struct rq *rq)
1878{
1879 return rq->dl.dl_nr_running > 0;
1880}
1881
1882static inline bool sched_rt_runnable(struct rq *rq)
1883{
1884 return rq->rt.rt_queued > 0;
1885}
1886
1887static inline bool sched_fair_runnable(struct rq *rq)
1888{
1889 return rq->cfs.nr_running > 0;
1890}
1891
Olivier Deprez157378f2022-04-04 15:47:50 +02001892extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
1893extern struct task_struct *pick_next_task_idle(struct rq *rq);
1894
David Brazdil0f672f62019-12-10 10:32:29 +00001895#ifdef CONFIG_SMP
1896
1897extern void update_group_capacity(struct sched_domain *sd, int cpu);
1898
1899extern void trigger_load_balance(struct rq *rq);
1900
1901extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1902
1903#endif
1904
1905#ifdef CONFIG_CPU_IDLE
1906static inline void idle_set_state(struct rq *rq,
1907 struct cpuidle_state *idle_state)
1908{
1909 rq->idle_state = idle_state;
1910}
1911
1912static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1913{
1914 SCHED_WARN_ON(!rcu_read_lock_held());
1915
1916 return rq->idle_state;
1917}
1918#else
1919static inline void idle_set_state(struct rq *rq,
1920 struct cpuidle_state *idle_state)
1921{
1922}
1923
1924static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1925{
1926 return NULL;
1927}
1928#endif
1929
1930extern void schedule_idle(void);
1931
1932extern void sysrq_sched_debug_show(void);
1933extern void sched_init_granularity(void);
1934extern void update_max_interval(void);
1935
1936extern void init_sched_dl_class(void);
1937extern void init_sched_rt_class(void);
1938extern void init_sched_fair_class(void);
1939
1940extern void reweight_task(struct task_struct *p, int prio);
1941
1942extern void resched_curr(struct rq *rq);
1943extern void resched_cpu(int cpu);
1944
1945extern struct rt_bandwidth def_rt_bandwidth;
1946extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1947
1948extern struct dl_bandwidth def_dl_bandwidth;
1949extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1950extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1951extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
David Brazdil0f672f62019-12-10 10:32:29 +00001952
1953#define BW_SHIFT 20
1954#define BW_UNIT (1 << BW_SHIFT)
1955#define RATIO_SHIFT 8
Olivier Deprez0e641232021-09-23 10:07:05 +02001956#define MAX_BW_BITS (64 - BW_SHIFT)
1957#define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
David Brazdil0f672f62019-12-10 10:32:29 +00001958unsigned long to_ratio(u64 period, u64 runtime);
1959
1960extern void init_entity_runnable_average(struct sched_entity *se);
1961extern void post_init_entity_util_avg(struct task_struct *p);
1962
1963#ifdef CONFIG_NO_HZ_FULL
1964extern bool sched_can_stop_tick(struct rq *rq);
1965extern int __init sched_tick_offload_init(void);
1966
1967/*
1968 * Tick may be needed by tasks in the runqueue depending on their policy and
1969 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1970 * nohz mode if necessary.
1971 */
1972static inline void sched_update_tick_dependency(struct rq *rq)
1973{
Olivier Deprez157378f2022-04-04 15:47:50 +02001974 int cpu = cpu_of(rq);
David Brazdil0f672f62019-12-10 10:32:29 +00001975
1976 if (!tick_nohz_full_cpu(cpu))
1977 return;
1978
1979 if (sched_can_stop_tick(rq))
1980 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1981 else
1982 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1983}
1984#else
1985static inline int sched_tick_offload_init(void) { return 0; }
1986static inline void sched_update_tick_dependency(struct rq *rq) { }
1987#endif
1988
1989static inline void add_nr_running(struct rq *rq, unsigned count)
1990{
1991 unsigned prev_nr = rq->nr_running;
1992
1993 rq->nr_running = prev_nr + count;
Olivier Deprez157378f2022-04-04 15:47:50 +02001994 if (trace_sched_update_nr_running_tp_enabled()) {
1995 call_trace_sched_update_nr_running(rq, count);
1996 }
David Brazdil0f672f62019-12-10 10:32:29 +00001997
1998#ifdef CONFIG_SMP
1999 if (prev_nr < 2 && rq->nr_running >= 2) {
2000 if (!READ_ONCE(rq->rd->overload))
2001 WRITE_ONCE(rq->rd->overload, 1);
2002 }
2003#endif
2004
2005 sched_update_tick_dependency(rq);
2006}
2007
2008static inline void sub_nr_running(struct rq *rq, unsigned count)
2009{
2010 rq->nr_running -= count;
Olivier Deprez157378f2022-04-04 15:47:50 +02002011 if (trace_sched_update_nr_running_tp_enabled()) {
2012 call_trace_sched_update_nr_running(rq, -count);
2013 }
2014
David Brazdil0f672f62019-12-10 10:32:29 +00002015 /* Check if we still need preemption */
2016 sched_update_tick_dependency(rq);
2017}
2018
2019extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2020extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2021
2022extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
2023
2024extern const_debug unsigned int sysctl_sched_nr_migrate;
2025extern const_debug unsigned int sysctl_sched_migration_cost;
2026
2027#ifdef CONFIG_SCHED_HRTICK
2028
2029/*
2030 * Use hrtick when:
2031 * - enabled by features
2032 * - hrtimer is actually high res
2033 */
2034static inline int hrtick_enabled(struct rq *rq)
2035{
2036 if (!sched_feat(HRTICK))
2037 return 0;
2038 if (!cpu_active(cpu_of(rq)))
2039 return 0;
2040 return hrtimer_is_hres_active(&rq->hrtick_timer);
2041}
2042
2043void hrtick_start(struct rq *rq, u64 delay);
2044
2045#else
2046
2047static inline int hrtick_enabled(struct rq *rq)
2048{
2049 return 0;
2050}
2051
2052#endif /* CONFIG_SCHED_HRTICK */
2053
Olivier Deprez157378f2022-04-04 15:47:50 +02002054#ifndef arch_scale_freq_tick
2055static __always_inline
2056void arch_scale_freq_tick(void)
2057{
2058}
2059#endif
2060
David Brazdil0f672f62019-12-10 10:32:29 +00002061#ifndef arch_scale_freq_capacity
Olivier Deprez157378f2022-04-04 15:47:50 +02002062/**
2063 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
2064 * @cpu: the CPU in question.
2065 *
2066 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
2067 *
2068 * f_curr
2069 * ------ * SCHED_CAPACITY_SCALE
2070 * f_max
2071 */
David Brazdil0f672f62019-12-10 10:32:29 +00002072static __always_inline
2073unsigned long arch_scale_freq_capacity(int cpu)
2074{
2075 return SCHED_CAPACITY_SCALE;
2076}
2077#endif
2078
2079#ifdef CONFIG_SMP
2080#ifdef CONFIG_PREEMPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002081
2082static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
2083
2084/*
2085 * fair double_lock_balance: Safely acquires both rq->locks in a fair
2086 * way at the expense of forcing extra atomic operations in all
2087 * invocations. This assures that the double_lock is acquired using the
2088 * same underlying policy as the spinlock_t on this architecture, which
2089 * reduces latency compared to the unfair variant below. However, it
2090 * also adds more overhead and therefore may reduce throughput.
2091 */
2092static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2093 __releases(this_rq->lock)
2094 __acquires(busiest->lock)
2095 __acquires(this_rq->lock)
2096{
2097 raw_spin_unlock(&this_rq->lock);
2098 double_rq_lock(this_rq, busiest);
2099
2100 return 1;
2101}
2102
2103#else
2104/*
2105 * Unfair double_lock_balance: Optimizes throughput at the expense of
2106 * latency by eliminating extra atomic operations when the locks are
2107 * already in proper order on entry. This favors lower CPU-ids and will
2108 * grant the double lock to lower CPUs over higher ids under contention,
2109 * regardless of entry order into the function.
2110 */
2111static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2112 __releases(this_rq->lock)
2113 __acquires(busiest->lock)
2114 __acquires(this_rq->lock)
2115{
2116 int ret = 0;
2117
2118 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
2119 if (busiest < this_rq) {
2120 raw_spin_unlock(&this_rq->lock);
2121 raw_spin_lock(&busiest->lock);
2122 raw_spin_lock_nested(&this_rq->lock,
2123 SINGLE_DEPTH_NESTING);
2124 ret = 1;
2125 } else
2126 raw_spin_lock_nested(&busiest->lock,
2127 SINGLE_DEPTH_NESTING);
2128 }
2129 return ret;
2130}
2131
David Brazdil0f672f62019-12-10 10:32:29 +00002132#endif /* CONFIG_PREEMPTION */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002133
2134/*
2135 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2136 */
2137static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2138{
2139 if (unlikely(!irqs_disabled())) {
2140 /* printk() doesn't work well under rq->lock */
2141 raw_spin_unlock(&this_rq->lock);
2142 BUG_ON(1);
2143 }
2144
2145 return _double_lock_balance(this_rq, busiest);
2146}
2147
2148static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2149 __releases(busiest->lock)
2150{
2151 raw_spin_unlock(&busiest->lock);
2152 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2153}
2154
2155static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
2156{
2157 if (l1 > l2)
2158 swap(l1, l2);
2159
2160 spin_lock(l1);
2161 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2162}
2163
2164static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
2165{
2166 if (l1 > l2)
2167 swap(l1, l2);
2168
2169 spin_lock_irq(l1);
2170 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2171}
2172
2173static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2174{
2175 if (l1 > l2)
2176 swap(l1, l2);
2177
2178 raw_spin_lock(l1);
2179 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2180}
2181
2182/*
2183 * double_rq_lock - safely lock two runqueues
2184 *
2185 * Note this does not disable interrupts like task_rq_lock,
2186 * you need to do so manually before calling.
2187 */
2188static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2189 __acquires(rq1->lock)
2190 __acquires(rq2->lock)
2191{
2192 BUG_ON(!irqs_disabled());
2193 if (rq1 == rq2) {
2194 raw_spin_lock(&rq1->lock);
2195 __acquire(rq2->lock); /* Fake it out ;) */
2196 } else {
2197 if (rq1 < rq2) {
2198 raw_spin_lock(&rq1->lock);
2199 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2200 } else {
2201 raw_spin_lock(&rq2->lock);
2202 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2203 }
2204 }
2205}
2206
2207/*
2208 * double_rq_unlock - safely unlock two runqueues
2209 *
2210 * Note this does not restore interrupts like task_rq_unlock,
2211 * you need to do so manually after calling.
2212 */
2213static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2214 __releases(rq1->lock)
2215 __releases(rq2->lock)
2216{
2217 raw_spin_unlock(&rq1->lock);
2218 if (rq1 != rq2)
2219 raw_spin_unlock(&rq2->lock);
2220 else
2221 __release(rq2->lock);
2222}
2223
2224extern void set_rq_online (struct rq *rq);
2225extern void set_rq_offline(struct rq *rq);
2226extern bool sched_smp_initialized;
2227
2228#else /* CONFIG_SMP */
2229
2230/*
2231 * double_rq_lock - safely lock two runqueues
2232 *
2233 * Note this does not disable interrupts like task_rq_lock,
2234 * you need to do so manually before calling.
2235 */
2236static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2237 __acquires(rq1->lock)
2238 __acquires(rq2->lock)
2239{
2240 BUG_ON(!irqs_disabled());
2241 BUG_ON(rq1 != rq2);
2242 raw_spin_lock(&rq1->lock);
2243 __acquire(rq2->lock); /* Fake it out ;) */
2244}
2245
2246/*
2247 * double_rq_unlock - safely unlock two runqueues
2248 *
2249 * Note this does not restore interrupts like task_rq_unlock,
2250 * you need to do so manually after calling.
2251 */
2252static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2253 __releases(rq1->lock)
2254 __releases(rq2->lock)
2255{
2256 BUG_ON(rq1 != rq2);
2257 raw_spin_unlock(&rq1->lock);
2258 __release(rq2->lock);
2259}
2260
2261#endif
2262
2263extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2264extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
2265
2266#ifdef CONFIG_SCHED_DEBUG
2267extern bool sched_debug_enabled;
2268
2269extern void print_cfs_stats(struct seq_file *m, int cpu);
2270extern void print_rt_stats(struct seq_file *m, int cpu);
2271extern void print_dl_stats(struct seq_file *m, int cpu);
2272extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
2273extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2274extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2275#ifdef CONFIG_NUMA_BALANCING
2276extern void
2277show_numa_stats(struct task_struct *p, struct seq_file *m);
2278extern void
2279print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2280 unsigned long tpf, unsigned long gsf, unsigned long gpf);
2281#endif /* CONFIG_NUMA_BALANCING */
2282#endif /* CONFIG_SCHED_DEBUG */
2283
2284extern void init_cfs_rq(struct cfs_rq *cfs_rq);
2285extern void init_rt_rq(struct rt_rq *rt_rq);
2286extern void init_dl_rq(struct dl_rq *dl_rq);
2287
2288extern void cfs_bandwidth_usage_inc(void);
2289extern void cfs_bandwidth_usage_dec(void);
2290
2291#ifdef CONFIG_NO_HZ_COMMON
2292#define NOHZ_BALANCE_KICK_BIT 0
2293#define NOHZ_STATS_KICK_BIT 1
2294
2295#define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
2296#define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
2297
2298#define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
2299
2300#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
2301
2302extern void nohz_balance_exit_idle(struct rq *rq);
2303#else
2304static inline void nohz_balance_exit_idle(struct rq *rq) { }
2305#endif
2306
2307
2308#ifdef CONFIG_SMP
2309static inline
2310void __dl_update(struct dl_bw *dl_b, s64 bw)
2311{
2312 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2313 int i;
2314
2315 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2316 "sched RCU must be held");
2317 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2318 struct rq *rq = cpu_rq(i);
2319
2320 rq->dl.extra_bw += bw;
2321 }
2322}
2323#else
2324static inline
2325void __dl_update(struct dl_bw *dl_b, s64 bw)
2326{
2327 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2328
2329 dl->extra_bw += bw;
2330}
2331#endif
2332
2333
2334#ifdef CONFIG_IRQ_TIME_ACCOUNTING
2335struct irqtime {
2336 u64 total;
2337 u64 tick_delta;
2338 u64 irq_start_time;
2339 struct u64_stats_sync sync;
2340};
2341
2342DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
2343
2344/*
2345 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2346 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2347 * and never move forward.
2348 */
2349static inline u64 irq_time_read(int cpu)
2350{
2351 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2352 unsigned int seq;
2353 u64 total;
2354
2355 do {
2356 seq = __u64_stats_fetch_begin(&irqtime->sync);
2357 total = irqtime->total;
2358 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
2359
2360 return total;
2361}
2362#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2363
2364#ifdef CONFIG_CPU_FREQ
David Brazdil0f672f62019-12-10 10:32:29 +00002365DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002366
2367/**
2368 * cpufreq_update_util - Take a note about CPU utilization changes.
2369 * @rq: Runqueue to carry out the update for.
2370 * @flags: Update reason flags.
2371 *
2372 * This function is called by the scheduler on the CPU whose utilization is
2373 * being updated.
2374 *
2375 * It can only be called from RCU-sched read-side critical sections.
2376 *
2377 * The way cpufreq is currently arranged requires it to evaluate the CPU
2378 * performance state (frequency/voltage) on a regular basis to prevent it from
2379 * being stuck in a completely inadequate performance level for too long.
2380 * That is not guaranteed to happen if the updates are only triggered from CFS
2381 * and DL, though, because they may not be coming in if only RT tasks are
2382 * active all the time (or there are RT tasks only).
2383 *
2384 * As a workaround for that issue, this function is called periodically by the
2385 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
2386 * but that really is a band-aid. Going forward it should be replaced with
2387 * solutions targeted more specifically at RT tasks.
2388 */
2389static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2390{
2391 struct update_util_data *data;
2392
2393 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2394 cpu_of(rq)));
2395 if (data)
2396 data->func(data, rq_clock(rq), flags);
2397}
2398#else
2399static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2400#endif /* CONFIG_CPU_FREQ */
2401
David Brazdil0f672f62019-12-10 10:32:29 +00002402#ifdef CONFIG_UCLAMP_TASK
Olivier Deprez157378f2022-04-04 15:47:50 +02002403unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
David Brazdil0f672f62019-12-10 10:32:29 +00002404
Olivier Deprez0e641232021-09-23 10:07:05 +02002405/**
Olivier Deprez157378f2022-04-04 15:47:50 +02002406 * uclamp_rq_util_with - clamp @util with @rq and @p effective uclamp values.
Olivier Deprez0e641232021-09-23 10:07:05 +02002407 * @rq: The rq to clamp against. Must not be NULL.
2408 * @util: The util value to clamp.
2409 * @p: The task to clamp against. Can be NULL if you want to clamp
2410 * against @rq only.
2411 *
2412 * Clamps the passed @util to the max(@rq, @p) effective uclamp values.
2413 *
2414 * If sched_uclamp_used static key is disabled, then just return the util
2415 * without any clamping since uclamp aggregation at the rq level in the fast
2416 * path is disabled, rendering this operation a NOP.
2417 *
2418 * Use uclamp_eff_value() if you don't care about uclamp values at rq level. It
2419 * will return the correct effective uclamp value of the task even if the
2420 * static key is disabled.
2421 */
David Brazdil0f672f62019-12-10 10:32:29 +00002422static __always_inline
Olivier Deprez157378f2022-04-04 15:47:50 +02002423unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2424 struct task_struct *p)
David Brazdil0f672f62019-12-10 10:32:29 +00002425{
Olivier Deprez157378f2022-04-04 15:47:50 +02002426 unsigned long min_util = 0;
2427 unsigned long max_util = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02002428
2429 if (!static_branch_likely(&sched_uclamp_used))
2430 return util;
2431
David Brazdil0f672f62019-12-10 10:32:29 +00002432 if (p) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002433 min_util = uclamp_eff_value(p, UCLAMP_MIN);
2434 max_util = uclamp_eff_value(p, UCLAMP_MAX);
2435
2436 /*
2437 * Ignore last runnable task's max clamp, as this task will
2438 * reset it. Similarly, no need to read the rq's min clamp.
2439 */
2440 if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
2441 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00002442 }
2443
Olivier Deprez157378f2022-04-04 15:47:50 +02002444 min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
2445 max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));
2446out:
David Brazdil0f672f62019-12-10 10:32:29 +00002447 /*
2448 * Since CPU's {min,max}_util clamps are MAX aggregated considering
2449 * RUNNABLE tasks with _different_ clamps, we can end up with an
2450 * inversion. Fix it now when the clamps are applied.
2451 */
2452 if (unlikely(min_util >= max_util))
2453 return min_util;
2454
2455 return clamp(util, min_util, max_util);
2456}
2457
Olivier Deprez0e641232021-09-23 10:07:05 +02002458/*
2459 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
2460 * by default in the fast path and only gets turned on once userspace performs
2461 * an operation that requires it.
2462 *
2463 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
2464 * hence is active.
2465 */
2466static inline bool uclamp_is_used(void)
2467{
2468 return static_branch_likely(&sched_uclamp_used);
2469}
David Brazdil0f672f62019-12-10 10:32:29 +00002470#else /* CONFIG_UCLAMP_TASK */
Olivier Deprez157378f2022-04-04 15:47:50 +02002471static inline
2472unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2473 struct task_struct *p)
David Brazdil0f672f62019-12-10 10:32:29 +00002474{
2475 return util;
2476}
Olivier Deprez0e641232021-09-23 10:07:05 +02002477
2478static inline bool uclamp_is_used(void)
2479{
2480 return false;
2481}
David Brazdil0f672f62019-12-10 10:32:29 +00002482#endif /* CONFIG_UCLAMP_TASK */
2483
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002484#ifdef arch_scale_freq_capacity
2485# ifndef arch_scale_freq_invariant
2486# define arch_scale_freq_invariant() true
2487# endif
2488#else
2489# define arch_scale_freq_invariant() false
2490#endif
2491
David Brazdil0f672f62019-12-10 10:32:29 +00002492#ifdef CONFIG_SMP
2493static inline unsigned long capacity_orig_of(int cpu)
2494{
2495 return cpu_rq(cpu)->cpu_capacity_orig;
2496}
2497#endif
2498
2499/**
2500 * enum schedutil_type - CPU utilization type
2501 * @FREQUENCY_UTIL: Utilization used to select frequency
2502 * @ENERGY_UTIL: Utilization used during energy calculation
2503 *
2504 * The utilization signals of all scheduling classes (CFS/RT/DL) and IRQ time
2505 * need to be aggregated differently depending on the usage made of them. This
2506 * enum is used within schedutil_freq_util() to differentiate the types of
2507 * utilization expected by the callers, and adjust the aggregation accordingly.
2508 */
2509enum schedutil_type {
2510 FREQUENCY_UTIL,
2511 ENERGY_UTIL,
2512};
2513
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002514#ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
David Brazdil0f672f62019-12-10 10:32:29 +00002515
2516unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2517 unsigned long max, enum schedutil_type type,
2518 struct task_struct *p);
2519
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002520static inline unsigned long cpu_bw_dl(struct rq *rq)
2521{
2522 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
2523}
2524
2525static inline unsigned long cpu_util_dl(struct rq *rq)
2526{
2527 return READ_ONCE(rq->avg_dl.util_avg);
2528}
2529
2530static inline unsigned long cpu_util_cfs(struct rq *rq)
2531{
2532 unsigned long util = READ_ONCE(rq->cfs.avg.util_avg);
2533
2534 if (sched_feat(UTIL_EST)) {
2535 util = max_t(unsigned long, util,
2536 READ_ONCE(rq->cfs.avg.util_est.enqueued));
2537 }
2538
2539 return util;
2540}
2541
2542static inline unsigned long cpu_util_rt(struct rq *rq)
2543{
2544 return READ_ONCE(rq->avg_rt.util_avg);
2545}
David Brazdil0f672f62019-12-10 10:32:29 +00002546#else /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2547static inline unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2548 unsigned long max, enum schedutil_type type,
2549 struct task_struct *p)
2550{
2551 return 0;
2552}
2553#endif /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002554
2555#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
2556static inline unsigned long cpu_util_irq(struct rq *rq)
2557{
2558 return rq->avg_irq.util_avg;
2559}
2560
2561static inline
2562unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2563{
2564 util *= (max - irq);
2565 util /= max;
2566
2567 return util;
2568
2569}
2570#else
2571static inline unsigned long cpu_util_irq(struct rq *rq)
2572{
2573 return 0;
2574}
2575
2576static inline
2577unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2578{
2579 return util;
2580}
2581#endif
David Brazdil0f672f62019-12-10 10:32:29 +00002582
2583#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
2584
2585#define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
2586
2587DECLARE_STATIC_KEY_FALSE(sched_energy_present);
2588
2589static inline bool sched_energy_enabled(void)
2590{
2591 return static_branch_unlikely(&sched_energy_present);
2592}
2593
2594#else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
2595
2596#define perf_domain_span(pd) NULL
2597static inline bool sched_energy_enabled(void) { return false; }
2598
2599#endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2600
2601#ifdef CONFIG_MEMBARRIER
2602/*
2603 * The scheduler provides memory barriers required by membarrier between:
2604 * - prior user-space memory accesses and store to rq->membarrier_state,
2605 * - store to rq->membarrier_state and following user-space memory accesses.
2606 * In the same way it provides those guarantees around store to rq->curr.
2607 */
2608static inline void membarrier_switch_mm(struct rq *rq,
2609 struct mm_struct *prev_mm,
2610 struct mm_struct *next_mm)
2611{
2612 int membarrier_state;
2613
2614 if (prev_mm == next_mm)
2615 return;
2616
2617 membarrier_state = atomic_read(&next_mm->membarrier_state);
2618 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
2619 return;
2620
2621 WRITE_ONCE(rq->membarrier_state, membarrier_state);
2622}
2623#else
2624static inline void membarrier_switch_mm(struct rq *rq,
2625 struct mm_struct *prev_mm,
2626 struct mm_struct *next_mm)
2627{
2628}
2629#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02002630
2631#ifdef CONFIG_SMP
2632static inline bool is_per_cpu_kthread(struct task_struct *p)
2633{
2634 if (!(p->flags & PF_KTHREAD))
2635 return false;
2636
2637 if (p->nr_cpus_allowed != 1)
2638 return false;
2639
2640 return true;
2641}
2642#endif
2643
2644void swake_up_all_locked(struct swait_queue_head *q);
2645void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);