1 /* 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>
48 #include <linux/energy_model.h>
49 #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>
59 #include <linux/psi.h>
60 #include <linux/rcupdate_wait.h>
61 #include <linux/security.h>
62 #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>
70 #include <asm-generic/vmlinux.lds.h>
71
72 #ifdef CONFIG_PARAVIRT
73 # include <asm/paravirt.h>
74 #endif
75
76 #include "cpupri.h"
77 #include "cpudeadline.h"
78
79 #include <trace/events/sched.h>
80
81 #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
87 struct rq;
88 struct cpuidle_state;
89
90 #ifdef CONFIG_SCHED_RT_CAS
91 extern unsigned long uclamp_task_util(struct task_struct *p);
92 #endif
93
94 #ifdef CONFIG_SCHED_WALT
95 extern unsigned int sched_ravg_window;
96 extern unsigned int walt_cpu_util_freq_divisor;
97
98 struct walt_sched_stats {
99 u64 cumulative_runnable_avg_scaled;
100 };
101
102 struct load_subtractions {
103 u64 window_start;
104 u64 subs;
105 u64 new_subs;
106 };
107
108 #define NUM_TRACKED_WINDOWS 2
109
110 struct sched_cluster {
111 raw_spinlock_t load_lock;
112 struct list_head list;
113 struct cpumask cpus;
114 int id;
115 int max_power_cost;
116 int min_power_cost;
117 int max_possible_capacity;
118 int capacity;
119 int efficiency; /* Differentiate cpus with different IPC capability */
120 int load_scale_factor;
121 unsigned int exec_scale_factor;
122 /*
123 * max_freq = user maximum
124 * max_possible_freq = maximum supported by hardware
125 */
126 unsigned int cur_freq, max_freq, min_freq;
127 unsigned int max_possible_freq;
128 bool freq_init_done;
129 };
130
131 extern unsigned int sched_disable_window_stats;
132 #endif /* CONFIG_SCHED_WALT */
133
134
135 /* task_struct::on_rq states: */
136 #define TASK_ON_RQ_QUEUED 1
137 #define TASK_ON_RQ_MIGRATING 2
138
139 extern __read_mostly int scheduler_running;
140
141 extern unsigned long calc_load_update;
142 extern atomic_long_t calc_load_tasks;
143
144 extern void calc_global_load_tick(struct rq *this_rq);
145 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
146
147 #ifdef CONFIG_SMP
148 extern void init_sched_groups_capacity(int cpu, struct sched_domain *sd);
149 #endif
150
151 extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
152 /*
153 * Helpers for converting nanosecond timing to jiffy resolution
154 */
155 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
156
157 /*
158 * Increase resolution of nice-level calculations for 64-bit architectures.
159 * The extra resolution improves shares distribution and load balancing of
160 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
161 * hierarchies, especially on larger systems. This is not a user-visible change
162 * and does not change the user-interface for setting shares/weights.
163 *
164 * We increase resolution only if we have enough bits to allow this increased
165 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
166 * are pretty high and the returns do not justify the increased costs.
167 *
168 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
169 * increase coverage and consistency always enable it on 64-bit platforms.
170 */
171 #ifdef CONFIG_64BIT
172 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
173 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
174 # define scale_load_down(w) \
175 ({ \
176 unsigned long __w = (w); \
177 if (__w) \
178 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
179 __w; \
180 })
181 #else
182 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
183 # define scale_load(w) (w)
184 # define scale_load_down(w) (w)
185 #endif
186
187 /*
188 * Task weight (visible to users) and its load (invisible to users) have
189 * independent resolution, but they should be well calibrated. We use
190 * scale_load() and scale_load_down(w) to convert between them. The
191 * following must be true:
192 *
193 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
194 *
195 */
196 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
197
198 /*
199 * Single value that decides SCHED_DEADLINE internal math precision.
200 * 10 -> just above 1us
201 * 9 -> just above 0.5us
202 */
203 #define DL_SCALE 10
204
205 /*
206 * Single value that denotes runtime == period, ie unlimited time.
207 */
208 #define RUNTIME_INF ((u64)~0ULL)
209
idle_policy(int policy)210 static inline int idle_policy(int policy)
211 {
212 return policy == SCHED_IDLE;
213 }
fair_policy(int policy)214 static inline int fair_policy(int policy)
215 {
216 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
217 }
218
rt_policy(int policy)219 static inline int rt_policy(int policy)
220 {
221 return policy == SCHED_FIFO || policy == SCHED_RR;
222 }
223
dl_policy(int policy)224 static inline int dl_policy(int policy)
225 {
226 return policy == SCHED_DEADLINE;
227 }
valid_policy(int policy)228 static inline bool valid_policy(int policy)
229 {
230 return idle_policy(policy) || fair_policy(policy) ||
231 rt_policy(policy) || dl_policy(policy);
232 }
233
task_has_idle_policy(struct task_struct * p)234 static inline int task_has_idle_policy(struct task_struct *p)
235 {
236 return idle_policy(p->policy);
237 }
238
task_has_rt_policy(struct task_struct * p)239 static inline int task_has_rt_policy(struct task_struct *p)
240 {
241 return rt_policy(p->policy);
242 }
243
task_has_dl_policy(struct task_struct * p)244 static inline int task_has_dl_policy(struct task_struct *p)
245 {
246 return dl_policy(p->policy);
247 }
248
249 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
250
update_avg(u64 * avg,u64 sample)251 static inline void update_avg(u64 *avg, u64 sample)
252 {
253 s64 diff = sample - *avg;
254 *avg += diff / 8;
255 }
256
257 /*
258 * Shifting a value by an exponent greater *or equal* to the size of said value
259 * is UB; cap at size-1.
260 */
261 #define shr_bound(val, shift) \
262 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
263
264 /*
265 * !! For sched_setattr_nocheck() (kernel) only !!
266 *
267 * This is actually gross. :(
268 *
269 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
270 * tasks, but still be able to sleep. We need this on platforms that cannot
271 * atomically change clock frequency. Remove once fast switching will be
272 * available on such platforms.
273 *
274 * SUGOV stands for SchedUtil GOVernor.
275 */
276 #define SCHED_FLAG_SUGOV 0x10000000
277
278 #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
279
dl_entity_is_special(struct sched_dl_entity * dl_se)280 static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se)
281 {
282 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
283 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
284 #else
285 return false;
286 #endif
287 }
288
289 /*
290 * Tells if entity @a should preempt entity @b.
291 */
292 static inline bool
dl_entity_preempt(struct sched_dl_entity * a,struct sched_dl_entity * b)293 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
294 {
295 return dl_entity_is_special(a) ||
296 dl_time_before(a->deadline, b->deadline);
297 }
298
299 /*
300 * This is the priority-queue data structure of the RT scheduling class:
301 */
302 struct rt_prio_array {
303 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
304 struct list_head queue[MAX_RT_PRIO];
305 };
306
307 struct rt_bandwidth {
308 /* nests inside the rq lock: */
309 raw_spinlock_t rt_runtime_lock;
310 ktime_t rt_period;
311 u64 rt_runtime;
312 struct hrtimer rt_period_timer;
313 unsigned int rt_period_active;
314 };
315
316 void __dl_clear_params(struct task_struct *p);
317
318 struct dl_bandwidth {
319 raw_spinlock_t dl_runtime_lock;
320 u64 dl_runtime;
321 u64 dl_period;
322 };
323
dl_bandwidth_enabled(void)324 static inline int dl_bandwidth_enabled(void)
325 {
326 return sysctl_sched_rt_runtime >= 0;
327 }
328
329 /*
330 * To keep the bandwidth of -deadline tasks under control
331 * we need some place where:
332 * - store the maximum -deadline bandwidth of each cpu;
333 * - cache the fraction of bandwidth that is currently allocated in
334 * each root domain;
335 *
336 * This is all done in the data structure below. It is similar to the
337 * one used for RT-throttling (rt_bandwidth), with the main difference
338 * that, since here we are only interested in admission control, we
339 * do not decrease any runtime while the group "executes", neither we
340 * need a timer to replenish it.
341 *
342 * With respect to SMP, bandwidth is given on a per root domain basis,
343 * meaning that:
344 * - bw (< 100%) is the deadline bandwidth of each CPU;
345 * - total_bw is the currently allocated bandwidth in each root domain;
346 */
347 struct dl_bw {
348 raw_spinlock_t lock;
349 u64 bw;
350 u64 total_bw;
351 };
352
353 static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
354
355 static inline
__dl_sub(struct dl_bw * dl_b,u64 tsk_bw,int cpus)356 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
357 {
358 dl_b->total_bw -= tsk_bw;
359 __dl_update(dl_b, (s32)tsk_bw / cpus);
360 }
361
362 static inline
__dl_add(struct dl_bw * dl_b,u64 tsk_bw,int cpus)363 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
364 {
365 dl_b->total_bw += tsk_bw;
366 __dl_update(dl_b, -((s32)tsk_bw / cpus));
367 }
368
__dl_overflow(struct dl_bw * dl_b,unsigned long cap,u64 old_bw,u64 new_bw)369 static inline bool __dl_overflow(struct dl_bw *dl_b, unsigned long cap,
370 u64 old_bw, u64 new_bw)
371 {
372 return dl_b->bw != -1 &&
373 cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
374 }
375
376 /*
377 * Verify the fitness of task @p to run on @cpu taking into account the
378 * CPU original capacity and the runtime/deadline ratio of the task.
379 *
380 * The function will return true if the CPU original capacity of the
381 * @cpu scaled by SCHED_CAPACITY_SCALE >= runtime/deadline ratio of the
382 * task and false otherwise.
383 */
dl_task_fits_capacity(struct task_struct * p,int cpu)384 static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
385 {
386 unsigned long cap = arch_scale_cpu_capacity(cpu);
387
388 return cap_scale(p->dl.dl_deadline, cap) >= p->dl.dl_runtime;
389 }
390
391 extern void init_dl_bw(struct dl_bw *dl_b);
392 extern int sched_dl_global_validate(void);
393 extern void sched_dl_do_global(void);
394 extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
395 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
396 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
397 extern bool __checkparam_dl(const struct sched_attr *attr);
398 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
399 extern int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
400 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
401 extern bool dl_cpu_busy(unsigned int cpu);
402
403 #ifdef CONFIG_CGROUP_SCHED
404
405 #include <linux/cgroup.h>
406 #include <linux/psi.h>
407
408 struct cfs_rq;
409 struct rt_rq;
410
411 extern struct list_head task_groups;
412
413 struct cfs_bandwidth {
414 #ifdef CONFIG_CFS_BANDWIDTH
415 raw_spinlock_t lock;
416 ktime_t period;
417 u64 quota;
418 u64 runtime;
419 s64 hierarchical_quota;
420
421 u8 idle;
422 u8 period_active;
423 u8 slack_started;
424 struct hrtimer period_timer;
425 struct hrtimer slack_timer;
426 struct list_head throttled_cfs_rq;
427
428 /* Statistics: */
429 int nr_periods;
430 int nr_throttled;
431 u64 throttled_time;
432 #endif
433 };
434
435 /* Task group related information */
436 struct task_group {
437 struct cgroup_subsys_state css;
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
446 #ifdef CONFIG_SMP
447 /*
448 * load_avg can be heavily contended at clock tick time, so put
449 * it in its own cacheline separated from the fields above which
450 * will also be accessed at each tick.
451 */
452 atomic_long_t load_avg ____cacheline_aligned;
453 #endif
454 #endif
455
456 #ifdef CONFIG_RT_GROUP_SCHED
457 struct sched_rt_entity **rt_se;
458 struct rt_rq **rt_rq;
459
460 struct rt_bandwidth rt_bandwidth;
461 #endif
462
463 struct rcu_head rcu;
464 struct list_head list;
465
466 struct task_group *parent;
467 struct list_head siblings;
468 struct list_head children;
469
470 #ifdef CONFIG_SCHED_AUTOGROUP
471 struct autogroup *autogroup;
472 #endif
473
474 struct cfs_bandwidth cfs_bandwidth;
475
476 #ifdef CONFIG_UCLAMP_TASK_GROUP
477 /* The two decimal precision [%] value requested from user-space */
478 unsigned int uclamp_pct[UCLAMP_CNT];
479 /* Clamp values requested for a task group */
480 struct uclamp_se uclamp_req[UCLAMP_CNT];
481 /* Effective clamp values used for a task group */
482 struct uclamp_se uclamp[UCLAMP_CNT];
483 #endif
484
485 #ifdef CONFIG_SCHED_RTG_CGROUP
486 /*
487 * Controls whether tasks of this cgroup should be colocated with each
488 * other and tasks of other cgroups that have the same flag turned on.
489 */
490 bool colocate;
491
492 /* Controls whether further updates are allowed to the colocate flag */
493 bool colocate_update_disabled;
494 #endif
495 };
496
497 #ifdef CONFIG_FAIR_GROUP_SCHED
498 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
499
500 /*
501 * A weight of 0 or 1 can cause arithmetics problems.
502 * A weight of a cfs_rq is the sum of weights of which entities
503 * are queued on this cfs_rq, so a weight of a entity should not be
504 * too large, so as the shares value of a task group.
505 * (The default weight is 1024 - so there's no practical
506 * limitation from this.)
507 */
508 #define MIN_SHARES (1UL << 1)
509 #define MAX_SHARES (1UL << 18)
510 #endif
511
512 typedef int (*tg_visitor)(struct task_group *, void *);
513
514 extern int walk_tg_tree_from(struct task_group *from,
515 tg_visitor down, tg_visitor up, void *data);
516
517 /*
518 * Iterate the full tree, calling @down when first entering a node and @up when
519 * leaving it for the final time.
520 *
521 * Caller must hold rcu_lock or sufficient equivalent.
522 */
walk_tg_tree(tg_visitor down,tg_visitor up,void * data)523 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
524 {
525 return walk_tg_tree_from(&root_task_group, down, up, data);
526 }
527
528 extern int tg_nop(struct task_group *tg, void *data);
529
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 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
535 struct sched_entity *se, int cpu,
536 struct sched_entity *parent);
537 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
538
539 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
540 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
541 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
542
543 extern void free_rt_sched_group(struct task_group *tg);
544 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
545 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
546 struct sched_rt_entity *rt_se, int cpu,
547 struct sched_rt_entity *parent);
548 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
549 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
550 extern long sched_group_rt_runtime(struct task_group *tg);
551 extern long sched_group_rt_period(struct task_group *tg);
552 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
553
554 extern struct task_group *sched_create_group(struct task_group *parent);
555 extern void sched_online_group(struct task_group *tg,
556 struct task_group *parent);
557 extern void sched_destroy_group(struct task_group *tg);
558 extern void sched_offline_group(struct task_group *tg);
559
560 extern void sched_move_task(struct task_struct *tsk);
561
562 #ifdef CONFIG_FAIR_GROUP_SCHED
563 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
564
565 #ifdef CONFIG_SMP
566 extern void set_task_rq_fair(struct sched_entity *se,
567 struct cfs_rq *prev, struct cfs_rq *next);
568 #else /* !CONFIG_SMP */
set_task_rq_fair(struct sched_entity * se,struct cfs_rq * prev,struct cfs_rq * next)569 static inline void set_task_rq_fair(struct sched_entity *se,
570 struct cfs_rq *prev, struct cfs_rq *next) { }
571 #endif /* CONFIG_SMP */
572 #endif /* CONFIG_FAIR_GROUP_SCHED */
573
574 #else /* CONFIG_CGROUP_SCHED */
575
576 struct cfs_bandwidth { };
577
578 #endif /* CONFIG_CGROUP_SCHED */
579
580 /* CFS-related fields in a runqueue */
581 struct cfs_rq {
582 struct load_weight load;
583 unsigned int nr_running;
584 unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */
585 unsigned int idle_h_nr_running; /* SCHED_IDLE */
586
587 u64 exec_clock;
588 u64 min_vruntime;
589 #ifndef CONFIG_64BIT
590 u64 min_vruntime_copy;
591 #endif
592
593 struct rb_root_cached tasks_timeline;
594
595 /*
596 * 'curr' points to currently running entity on this cfs_rq.
597 * It is set to NULL otherwise (i.e when none are currently running).
598 */
599 struct sched_entity *curr;
600 struct sched_entity *next;
601 struct sched_entity *last;
602 struct sched_entity *skip;
603
604 #ifdef CONFIG_SCHED_DEBUG
605 unsigned int nr_spread_over;
606 #endif
607
608 #ifdef CONFIG_SMP
609 /*
610 * CFS load tracking
611 */
612 struct sched_avg avg;
613 #ifndef CONFIG_64BIT
614 u64 load_last_update_time_copy;
615 #endif
616 struct {
617 raw_spinlock_t lock ____cacheline_aligned;
618 int nr;
619 unsigned long load_avg;
620 unsigned long util_avg;
621 unsigned long runnable_avg;
622 } removed;
623
624 #ifdef CONFIG_FAIR_GROUP_SCHED
625 unsigned long tg_load_avg_contrib;
626 long propagate;
627 long prop_runnable_sum;
628
629 /*
630 * h_load = weight * f(tg)
631 *
632 * Where f(tg) is the recursive weight fraction assigned to
633 * this group.
634 */
635 unsigned long h_load;
636 u64 last_h_load_update;
637 struct sched_entity *h_load_next;
638 #endif /* CONFIG_FAIR_GROUP_SCHED */
639 #endif /* CONFIG_SMP */
640
641 #ifdef CONFIG_FAIR_GROUP_SCHED
642 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
643
644 /*
645 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
646 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
647 * (like users, containers etc.)
648 *
649 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
650 * This list is used during load balance.
651 */
652 int on_list;
653 struct list_head leaf_cfs_rq_list;
654 struct task_group *tg; /* group that "owns" this runqueue */
655
656 #ifdef CONFIG_SCHED_WALT
657 struct walt_sched_stats walt_stats;
658 #endif
659
660 #ifdef CONFIG_CFS_BANDWIDTH
661 int runtime_enabled;
662 s64 runtime_remaining;
663
664 u64 throttled_clock;
665 u64 throttled_clock_task;
666 u64 throttled_clock_task_time;
667 int throttled;
668 int throttle_count;
669 struct list_head throttled_list;
670 #ifdef CONFIG_SCHED_WALT
671 u64 cumulative_runnable_avg;
672 #endif
673 #endif /* CONFIG_CFS_BANDWIDTH */
674 #endif /* CONFIG_FAIR_GROUP_SCHED */
675 };
676
rt_bandwidth_enabled(void)677 static inline int rt_bandwidth_enabled(void)
678 {
679 return sysctl_sched_rt_runtime >= 0;
680 }
681
682 /* RT IPI pull logic requires IRQ_WORK */
683 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
684 # define HAVE_RT_PUSH_IPI
685 #endif
686
687 /* Real-Time classes' related field in a runqueue: */
688 struct rt_rq {
689 struct rt_prio_array active;
690 unsigned int rt_nr_running;
691 unsigned int rr_nr_running;
692 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
693 struct {
694 int curr; /* highest queued rt task prio */
695 #ifdef CONFIG_SMP
696 int next; /* next highest */
697 #endif
698 } highest_prio;
699 #endif
700 #ifdef CONFIG_SMP
701 unsigned long rt_nr_migratory;
702 unsigned long rt_nr_total;
703 int overloaded;
704 struct plist_head pushable_tasks;
705
706 #endif /* CONFIG_SMP */
707 int rt_queued;
708
709 int rt_throttled;
710 u64 rt_time;
711 u64 rt_runtime;
712 /* Nests inside the rq lock: */
713 raw_spinlock_t rt_runtime_lock;
714
715 #ifdef CONFIG_RT_GROUP_SCHED
716 unsigned long rt_nr_boosted;
717
718 struct rq *rq;
719 struct task_group *tg;
720 #endif
721 };
722
rt_rq_is_runnable(struct rt_rq * rt_rq)723 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
724 {
725 return rt_rq->rt_queued && rt_rq->rt_nr_running;
726 }
727
728 /* Deadline class' related fields in a runqueue */
729 struct dl_rq {
730 /* runqueue is an rbtree, ordered by deadline */
731 struct rb_root_cached root;
732
733 unsigned long dl_nr_running;
734
735 #ifdef CONFIG_SMP
736 /*
737 * Deadline values of the currently executing and the
738 * earliest ready task on this rq. Caching these facilitates
739 * the decision whether or not a ready but not running task
740 * should migrate somewhere else.
741 */
742 struct {
743 u64 curr;
744 u64 next;
745 } earliest_dl;
746
747 unsigned long dl_nr_migratory;
748 int overloaded;
749
750 /*
751 * Tasks on this rq that can be pushed away. They are kept in
752 * an rb-tree, ordered by tasks' deadlines, with caching
753 * of the leftmost (earliest deadline) element.
754 */
755 struct rb_root_cached pushable_dl_tasks_root;
756 #else
757 struct dl_bw dl_bw;
758 #endif
759 /*
760 * "Active utilization" for this runqueue: increased when a
761 * task wakes up (becomes TASK_RUNNING) and decreased when a
762 * task blocks
763 */
764 u64 running_bw;
765
766 /*
767 * Utilization of the tasks "assigned" to this runqueue (including
768 * the tasks that are in runqueue and the tasks that executed on this
769 * CPU and blocked). Increased when a task moves to this runqueue, and
770 * decreased when the task moves away (migrates, changes scheduling
771 * policy, or terminates).
772 * This is needed to compute the "inactive utilization" for the
773 * runqueue (inactive utilization = this_bw - running_bw).
774 */
775 u64 this_bw;
776 u64 extra_bw;
777
778 /*
779 * Inverse of the fraction of CPU utilization that can be reclaimed
780 * by the GRUB algorithm.
781 */
782 u64 bw_ratio;
783 };
784
785 #ifdef CONFIG_FAIR_GROUP_SCHED
786 /* An entity is a task if it doesn't "own" a runqueue */
787 #define entity_is_task(se) (!se->my_q)
788
se_update_runnable(struct sched_entity * se)789 static inline void se_update_runnable(struct sched_entity *se)
790 {
791 if (!entity_is_task(se))
792 se->runnable_weight = se->my_q->h_nr_running;
793 }
794
se_runnable(struct sched_entity * se)795 static inline long se_runnable(struct sched_entity *se)
796 {
797 if (entity_is_task(se))
798 return !!se->on_rq;
799 else
800 return se->runnable_weight;
801 }
802
803 #else
804 #define entity_is_task(se) 1
805
se_update_runnable(struct sched_entity * se)806 static inline void se_update_runnable(struct sched_entity *se) {}
807
se_runnable(struct sched_entity * se)808 static inline long se_runnable(struct sched_entity *se)
809 {
810 return !!se->on_rq;
811 }
812 #endif
813
814 #ifdef CONFIG_SMP
815 /*
816 * XXX we want to get rid of these helpers and use the full load resolution.
817 */
se_weight(struct sched_entity * se)818 static inline long se_weight(struct sched_entity *se)
819 {
820 return scale_load_down(se->load.weight);
821 }
822
823
sched_asym_prefer(int a,int b)824 static inline bool sched_asym_prefer(int a, int b)
825 {
826 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
827 }
828
829 struct perf_domain {
830 struct em_perf_domain *em_pd;
831 struct perf_domain *next;
832 struct rcu_head rcu;
833 };
834
835 /* Scheduling group status flags */
836 #define SG_OVERLOAD 0x1 /* More than one runnable task on a CPU. */
837 #define SG_OVERUTILIZED 0x2 /* One or more CPUs are over-utilized. */
838
839 /*
840 * We add the notion of a root-domain which will be used to define per-domain
841 * variables. Each exclusive cpuset essentially defines an island domain by
842 * fully partitioning the member CPUs from any other cpuset. Whenever a new
843 * exclusive cpuset is created, we also create and attach a new root-domain
844 * object.
845 *
846 */
847 struct root_domain {
848 atomic_t refcount;
849 atomic_t rto_count;
850 struct rcu_head rcu;
851 cpumask_var_t span;
852 cpumask_var_t online;
853
854 /*
855 * Indicate pullable load on at least one CPU, e.g:
856 * - More than one runnable task
857 * - Running task is misfit
858 */
859 int overload;
860
861 /* Indicate one or more cpus over-utilized (tipping point) */
862 int overutilized;
863
864 /*
865 * The bit corresponding to a CPU gets set here if such CPU has more
866 * than one runnable -deadline task (as it is below for RT tasks).
867 */
868 cpumask_var_t dlo_mask;
869 atomic_t dlo_count;
870 struct dl_bw dl_bw;
871 struct cpudl cpudl;
872
873 #ifdef HAVE_RT_PUSH_IPI
874 /*
875 * For IPI pull requests, loop across the rto_mask.
876 */
877 struct irq_work rto_push_work;
878 raw_spinlock_t rto_lock;
879 /* These are only updated and read within rto_lock */
880 int rto_loop;
881 int rto_cpu;
882 /* These atomics are updated outside of a lock */
883 atomic_t rto_loop_next;
884 atomic_t rto_loop_start;
885 #endif
886 /*
887 * The "RT overload" flag: it gets set if a CPU has more than
888 * one runnable RT task.
889 */
890 cpumask_var_t rto_mask;
891 struct cpupri cpupri;
892
893 unsigned long max_cpu_capacity;
894
895 /*
896 * NULL-terminated list of performance domains intersecting with the
897 * CPUs of the rd. Protected by RCU.
898 */
899 struct perf_domain __rcu *pd;
900 #ifdef CONFIG_SCHED_RT_CAS
901 int max_cap_orig_cpu;
902 #endif
903 };
904
905 extern void init_defrootdomain(void);
906 extern int sched_init_domains(const struct cpumask *cpu_map);
907 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
908 extern void sched_get_rd(struct root_domain *rd);
909 extern void sched_put_rd(struct root_domain *rd);
910
911 #ifdef HAVE_RT_PUSH_IPI
912 extern void rto_push_irq_work_func(struct irq_work *work);
913 #endif
914 #endif /* CONFIG_SMP */
915
916 #ifdef CONFIG_UCLAMP_TASK
917 /*
918 * struct uclamp_bucket - Utilization clamp bucket
919 * @value: utilization clamp value for tasks on this clamp bucket
920 * @tasks: number of RUNNABLE tasks on this clamp bucket
921 *
922 * Keep track of how many tasks are RUNNABLE for a given utilization
923 * clamp value.
924 */
925 struct uclamp_bucket {
926 unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
927 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
928 };
929
930 /*
931 * struct uclamp_rq - rq's utilization clamp
932 * @value: currently active clamp values for a rq
933 * @bucket: utilization clamp buckets affecting a rq
934 *
935 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
936 * A clamp value is affecting a rq when there is at least one task RUNNABLE
937 * (or actually running) with that value.
938 *
939 * There are up to UCLAMP_CNT possible different clamp values, currently there
940 * are only two: minimum utilization and maximum utilization.
941 *
942 * All utilization clamping values are MAX aggregated, since:
943 * - for util_min: we want to run the CPU at least at the max of the minimum
944 * utilization required by its currently RUNNABLE tasks.
945 * - for util_max: we want to allow the CPU to run up to the max of the
946 * maximum utilization allowed by its currently RUNNABLE tasks.
947 *
948 * Since on each system we expect only a limited number of different
949 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
950 * the metrics required to compute all the per-rq utilization clamp values.
951 */
952 struct uclamp_rq {
953 unsigned int value;
954 struct uclamp_bucket bucket[UCLAMP_BUCKETS];
955 };
956
957 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
958 #endif /* CONFIG_UCLAMP_TASK */
959
960 /*
961 * This is the main, per-CPU runqueue data structure.
962 *
963 * Locking rule: those places that want to lock multiple runqueues
964 * (such as the load balancing or the thread migration code), lock
965 * acquire operations must be ordered by ascending &runqueue.
966 */
967 struct rq {
968 /* runqueue lock: */
969 raw_spinlock_t lock;
970
971 /*
972 * nr_running and cpu_load should be in the same cacheline because
973 * remote CPUs use both these fields when doing load calculation.
974 */
975 unsigned int nr_running;
976 #ifdef CONFIG_NUMA_BALANCING
977 unsigned int nr_numa_running;
978 unsigned int nr_preferred_running;
979 unsigned int numa_migrate_on;
980 #endif
981 #ifdef CONFIG_NO_HZ_COMMON
982 #ifdef CONFIG_SMP
983 unsigned long last_blocked_load_update_tick;
984 unsigned int has_blocked_load;
985 call_single_data_t nohz_csd;
986 #endif /* CONFIG_SMP */
987 unsigned int nohz_tick_stopped;
988 atomic_t nohz_flags;
989 #endif /* CONFIG_NO_HZ_COMMON */
990
991 #ifdef CONFIG_SMP
992 unsigned int ttwu_pending;
993 #endif
994 u64 nr_switches;
995
996 #ifdef CONFIG_UCLAMP_TASK
997 /* Utilization clamp values based on CPU's RUNNABLE tasks */
998 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
999 unsigned int uclamp_flags;
1000 #define UCLAMP_FLAG_IDLE 0x01
1001 #endif
1002
1003 struct cfs_rq cfs;
1004 struct rt_rq rt;
1005 struct dl_rq dl;
1006
1007 #ifdef CONFIG_FAIR_GROUP_SCHED
1008 /* list of leaf cfs_rq on this CPU: */
1009 struct list_head leaf_cfs_rq_list;
1010 struct list_head *tmp_alone_branch;
1011 #endif /* CONFIG_FAIR_GROUP_SCHED */
1012
1013 /*
1014 * This is part of a global counter where only the total sum
1015 * over all CPUs matters. A task can increase this counter on
1016 * one CPU and if it got migrated afterwards it may decrease
1017 * it on another CPU. Always updated under the runqueue lock:
1018 */
1019 unsigned long nr_uninterruptible;
1020
1021 struct task_struct __rcu *curr;
1022 struct task_struct *idle;
1023 struct task_struct *stop;
1024 unsigned long next_balance;
1025 struct mm_struct *prev_mm;
1026
1027 unsigned int clock_update_flags;
1028 u64 clock;
1029 /* Ensure that all clocks are in the same cache line */
1030 u64 clock_task ____cacheline_aligned;
1031 u64 clock_pelt;
1032 unsigned long lost_idle_time;
1033
1034 atomic_t nr_iowait;
1035
1036 #ifdef CONFIG_MEMBARRIER
1037 int membarrier_state;
1038 #endif
1039
1040 #ifdef CONFIG_SMP
1041 struct root_domain *rd;
1042 struct sched_domain __rcu *sd;
1043
1044 unsigned long cpu_capacity;
1045 unsigned long cpu_capacity_orig;
1046
1047 struct callback_head *balance_callback;
1048
1049 unsigned char nohz_idle_balance;
1050 unsigned char idle_balance;
1051
1052 unsigned long misfit_task_load;
1053
1054 /* For active balancing */
1055 int active_balance;
1056 int push_cpu;
1057 #ifdef CONFIG_SCHED_EAS
1058 struct task_struct *push_task;
1059 #endif
1060 struct cpu_stop_work active_balance_work;
1061
1062 /* For rt active balancing */
1063 #ifdef CONFIG_SCHED_RT_ACTIVE_LB
1064 int rt_active_balance;
1065 struct task_struct *rt_push_task;
1066 struct cpu_stop_work rt_active_balance_work;
1067 #endif
1068
1069 /* CPU of this runqueue: */
1070 int cpu;
1071 int online;
1072
1073 struct list_head cfs_tasks;
1074
1075 struct sched_avg avg_rt;
1076 struct sched_avg avg_dl;
1077 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1078 struct sched_avg avg_irq;
1079 #endif
1080 #ifdef CONFIG_SCHED_THERMAL_PRESSURE
1081 struct sched_avg avg_thermal;
1082 #endif
1083 u64 idle_stamp;
1084 u64 avg_idle;
1085
1086 /* This is used to determine avg_idle's max value */
1087 u64 max_idle_balance_cost;
1088 #endif /* CONFIG_SMP */
1089
1090 #ifdef CONFIG_SCHED_WALT
1091 struct sched_cluster *cluster;
1092 struct cpumask freq_domain_cpumask;
1093 struct walt_sched_stats walt_stats;
1094
1095 u64 window_start;
1096 unsigned long walt_flags;
1097
1098 u64 cur_irqload;
1099 u64 avg_irqload;
1100 u64 irqload_ts;
1101 u64 curr_runnable_sum;
1102 u64 prev_runnable_sum;
1103 u64 nt_curr_runnable_sum;
1104 u64 nt_prev_runnable_sum;
1105 u64 cum_window_demand_scaled;
1106 struct load_subtractions load_subs[NUM_TRACKED_WINDOWS];
1107 #ifdef CONFIG_SCHED_RTG
1108 struct group_cpu_time grp_time;
1109 #endif
1110 #endif /* CONFIG_SCHED_WALT */
1111
1112 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1113 u64 prev_irq_time;
1114 #endif
1115 #ifdef CONFIG_PARAVIRT
1116 u64 prev_steal_time;
1117 #endif
1118 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1119 u64 prev_steal_time_rq;
1120 #endif
1121
1122 /* calc_load related fields */
1123 unsigned long calc_load_update;
1124 long calc_load_active;
1125
1126 #ifdef CONFIG_SCHED_HRTICK
1127 #ifdef CONFIG_SMP
1128 call_single_data_t hrtick_csd;
1129 #endif
1130 struct hrtimer hrtick_timer;
1131 ktime_t hrtick_time;
1132 #endif
1133
1134 #ifdef CONFIG_SCHEDSTATS
1135 /* latency stats */
1136 struct sched_info rq_sched_info;
1137 unsigned long long rq_cpu_time;
1138 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
1139
1140 /* sys_sched_yield() stats */
1141 unsigned int yld_count;
1142
1143 /* schedule() stats */
1144 unsigned int sched_count;
1145 unsigned int sched_goidle;
1146
1147 /* try_to_wake_up() stats */
1148 unsigned int ttwu_count;
1149 unsigned int ttwu_local;
1150 #endif
1151
1152 #ifdef CONFIG_CPU_IDLE
1153 /* Must be inspected within a rcu lock section */
1154 struct cpuidle_state *idle_state;
1155 #endif
1156 };
1157
1158 #ifdef CONFIG_FAIR_GROUP_SCHED
1159
1160 /* CPU runqueue to which this cfs_rq is attached */
rq_of(struct cfs_rq * cfs_rq)1161 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1162 {
1163 return cfs_rq->rq;
1164 }
1165
1166 #else
1167
rq_of(struct cfs_rq * cfs_rq)1168 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1169 {
1170 return container_of(cfs_rq, struct rq, cfs);
1171 }
1172 #endif
1173
cpu_of(struct rq * rq)1174 static inline int cpu_of(struct rq *rq)
1175 {
1176 #ifdef CONFIG_SMP
1177 return rq->cpu;
1178 #else
1179 return 0;
1180 #endif
1181 }
1182
1183
1184 #ifdef CONFIG_SCHED_SMT
1185 extern void __update_idle_core(struct rq *rq);
1186
update_idle_core(struct rq * rq)1187 static inline void update_idle_core(struct rq *rq)
1188 {
1189 if (static_branch_unlikely(&sched_smt_present))
1190 __update_idle_core(rq);
1191 }
1192
1193 #else
update_idle_core(struct rq * rq)1194 static inline void update_idle_core(struct rq *rq) { }
1195 #endif
1196
1197 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1198
1199 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1200 #define this_rq() this_cpu_ptr(&runqueues)
1201 #define task_rq(p) cpu_rq(task_cpu(p))
1202 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1203 #define raw_rq() raw_cpu_ptr(&runqueues)
1204
1205 extern void update_rq_clock(struct rq *rq);
1206
__rq_clock_broken(struct rq * rq)1207 static inline u64 __rq_clock_broken(struct rq *rq)
1208 {
1209 return READ_ONCE(rq->clock);
1210 }
1211
1212 /*
1213 * rq::clock_update_flags bits
1214 *
1215 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1216 * call to __schedule(). This is an optimisation to avoid
1217 * neighbouring rq clock updates.
1218 *
1219 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1220 * in effect and calls to update_rq_clock() are being ignored.
1221 *
1222 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1223 * made to update_rq_clock() since the last time rq::lock was pinned.
1224 *
1225 * If inside of __schedule(), clock_update_flags will have been
1226 * shifted left (a left shift is a cheap operation for the fast path
1227 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1228 *
1229 * if (rq-clock_update_flags >= RQCF_UPDATED)
1230 *
1231 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
1232 * one position though, because the next rq_unpin_lock() will shift it
1233 * back.
1234 */
1235 #define RQCF_REQ_SKIP 0x01
1236 #define RQCF_ACT_SKIP 0x02
1237 #define RQCF_UPDATED 0x04
1238
assert_clock_updated(struct rq * rq)1239 static inline void assert_clock_updated(struct rq *rq)
1240 {
1241 /*
1242 * The only reason for not seeing a clock update since the
1243 * last rq_pin_lock() is if we're currently skipping updates.
1244 */
1245 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
1246 }
1247
rq_clock(struct rq * rq)1248 static inline u64 rq_clock(struct rq *rq)
1249 {
1250 lockdep_assert_held(&rq->lock);
1251 assert_clock_updated(rq);
1252
1253 return rq->clock;
1254 }
1255
rq_clock_task(struct rq * rq)1256 static inline u64 rq_clock_task(struct rq *rq)
1257 {
1258 lockdep_assert_held(&rq->lock);
1259 assert_clock_updated(rq);
1260
1261 return rq->clock_task;
1262 }
1263
1264 /**
1265 * By default the decay is the default pelt decay period.
1266 * The decay shift can change the decay period in
1267 * multiples of 32.
1268 * Decay shift Decay period(ms)
1269 * 0 32
1270 * 1 64
1271 * 2 128
1272 * 3 256
1273 * 4 512
1274 */
1275 extern int sched_thermal_decay_shift;
1276
rq_clock_thermal(struct rq * rq)1277 static inline u64 rq_clock_thermal(struct rq *rq)
1278 {
1279 return rq_clock_task(rq) >> sched_thermal_decay_shift;
1280 }
1281
rq_clock_skip_update(struct rq * rq)1282 static inline void rq_clock_skip_update(struct rq *rq)
1283 {
1284 lockdep_assert_held(&rq->lock);
1285 rq->clock_update_flags |= RQCF_REQ_SKIP;
1286 }
1287
1288 /*
1289 * See rt task throttling, which is the only time a skip
1290 * request is cancelled.
1291 */
rq_clock_cancel_skipupdate(struct rq * rq)1292 static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1293 {
1294 lockdep_assert_held(&rq->lock);
1295 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1296 }
1297
1298 struct rq_flags {
1299 unsigned long flags;
1300 struct pin_cookie cookie;
1301 #ifdef CONFIG_SCHED_DEBUG
1302 /*
1303 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1304 * current pin context is stashed here in case it needs to be
1305 * restored in rq_repin_lock().
1306 */
1307 unsigned int clock_update_flags;
1308 #endif
1309 };
1310
1311 /*
1312 * Lockdep annotation that avoids accidental unlocks; it's like a
1313 * sticky/continuous lockdep_assert_held().
1314 *
1315 * This avoids code that has access to 'struct rq *rq' (basically everything in
1316 * the scheduler) from accidentally unlocking the rq if they do not also have a
1317 * copy of the (on-stack) 'struct rq_flags rf'.
1318 *
1319 * Also see Documentation/locking/lockdep-design.rst.
1320 */
rq_pin_lock(struct rq * rq,struct rq_flags * rf)1321 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1322 {
1323 rf->cookie = lockdep_pin_lock(&rq->lock);
1324
1325 #ifdef CONFIG_SCHED_DEBUG
1326 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1327 rf->clock_update_flags = 0;
1328 #endif
1329 }
1330
rq_unpin_lock(struct rq * rq,struct rq_flags * rf)1331 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1332 {
1333 #ifdef CONFIG_SCHED_DEBUG
1334 if (rq->clock_update_flags > RQCF_ACT_SKIP)
1335 rf->clock_update_flags = RQCF_UPDATED;
1336 #endif
1337
1338 lockdep_unpin_lock(&rq->lock, rf->cookie);
1339 }
1340
rq_repin_lock(struct rq * rq,struct rq_flags * rf)1341 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1342 {
1343 lockdep_repin_lock(&rq->lock, rf->cookie);
1344
1345 #ifdef CONFIG_SCHED_DEBUG
1346 /*
1347 * Restore the value we stashed in @rf for this pin context.
1348 */
1349 rq->clock_update_flags |= rf->clock_update_flags;
1350 #endif
1351 }
1352
1353 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1354 __acquires(rq->lock);
1355
1356 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1357 __acquires(p->pi_lock)
1358 __acquires(rq->lock);
1359
__task_rq_unlock(struct rq * rq,struct rq_flags * rf)1360 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1361 __releases(rq->lock)
1362 {
1363 rq_unpin_lock(rq, rf);
1364 raw_spin_unlock(&rq->lock);
1365 }
1366
1367 static inline void
task_rq_unlock(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1368 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1369 __releases(rq->lock)
1370 __releases(p->pi_lock)
1371 {
1372 rq_unpin_lock(rq, rf);
1373 raw_spin_unlock(&rq->lock);
1374 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1375 }
1376
1377 static inline void
rq_lock_irqsave(struct rq * rq,struct rq_flags * rf)1378 rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1379 __acquires(rq->lock)
1380 {
1381 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1382 rq_pin_lock(rq, rf);
1383 }
1384
1385 static inline void
rq_lock_irq(struct rq * rq,struct rq_flags * rf)1386 rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1387 __acquires(rq->lock)
1388 {
1389 raw_spin_lock_irq(&rq->lock);
1390 rq_pin_lock(rq, rf);
1391 }
1392
1393 static inline void
rq_lock(struct rq * rq,struct rq_flags * rf)1394 rq_lock(struct rq *rq, struct rq_flags *rf)
1395 __acquires(rq->lock)
1396 {
1397 raw_spin_lock(&rq->lock);
1398 rq_pin_lock(rq, rf);
1399 }
1400
1401 static inline void
rq_relock(struct rq * rq,struct rq_flags * rf)1402 rq_relock(struct rq *rq, struct rq_flags *rf)
1403 __acquires(rq->lock)
1404 {
1405 raw_spin_lock(&rq->lock);
1406 rq_repin_lock(rq, rf);
1407 }
1408
1409 static inline void
rq_unlock_irqrestore(struct rq * rq,struct rq_flags * rf)1410 rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1411 __releases(rq->lock)
1412 {
1413 rq_unpin_lock(rq, rf);
1414 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1415 }
1416
1417 static inline void
rq_unlock_irq(struct rq * rq,struct rq_flags * rf)1418 rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1419 __releases(rq->lock)
1420 {
1421 rq_unpin_lock(rq, rf);
1422 raw_spin_unlock_irq(&rq->lock);
1423 }
1424
1425 static inline void
rq_unlock(struct rq * rq,struct rq_flags * rf)1426 rq_unlock(struct rq *rq, struct rq_flags *rf)
1427 __releases(rq->lock)
1428 {
1429 rq_unpin_lock(rq, rf);
1430 raw_spin_unlock(&rq->lock);
1431 }
1432
1433 static inline struct rq *
this_rq_lock_irq(struct rq_flags * rf)1434 this_rq_lock_irq(struct rq_flags *rf)
1435 __acquires(rq->lock)
1436 {
1437 struct rq *rq;
1438
1439 local_irq_disable();
1440 rq = this_rq();
1441 rq_lock(rq, rf);
1442 return rq;
1443 }
1444
1445 #ifdef CONFIG_NUMA
1446 enum numa_topology_type {
1447 NUMA_DIRECT,
1448 NUMA_GLUELESS_MESH,
1449 NUMA_BACKPLANE,
1450 };
1451 extern enum numa_topology_type sched_numa_topology_type;
1452 extern int sched_max_numa_distance;
1453 extern bool find_numa_distance(int distance);
1454 extern void sched_init_numa(void);
1455 extern void sched_domains_numa_masks_set(unsigned int cpu);
1456 extern void sched_domains_numa_masks_clear(unsigned int cpu);
1457 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1458 #else
sched_init_numa(void)1459 static inline void sched_init_numa(void) { }
sched_domains_numa_masks_set(unsigned int cpu)1460 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
sched_domains_numa_masks_clear(unsigned int cpu)1461 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
sched_numa_find_closest(const struct cpumask * cpus,int cpu)1462 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1463 {
1464 return nr_cpu_ids;
1465 }
1466 #endif
1467
1468 #ifdef CONFIG_NUMA_BALANCING
1469 /* The regions in numa_faults array from task_struct */
1470 enum numa_faults_stats {
1471 NUMA_MEM = 0,
1472 NUMA_CPU,
1473 NUMA_MEMBUF,
1474 NUMA_CPUBUF
1475 };
1476 extern void sched_setnuma(struct task_struct *p, int node);
1477 extern int migrate_task_to(struct task_struct *p, int cpu);
1478 extern int migrate_swap(struct task_struct *p, struct task_struct *t,
1479 int cpu, int scpu);
1480 extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
1481 #else
1482 static inline void
init_numa_balancing(unsigned long clone_flags,struct task_struct * p)1483 init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
1484 {
1485 }
1486 #endif /* CONFIG_NUMA_BALANCING */
1487
1488 #ifdef CONFIG_SMP
1489
1490 static inline void
queue_balance_callback(struct rq * rq,struct callback_head * head,void (* func)(struct rq * rq))1491 queue_balance_callback(struct rq *rq,
1492 struct callback_head *head,
1493 void (*func)(struct rq *rq))
1494 {
1495 lockdep_assert_held(&rq->lock);
1496
1497 if (unlikely(head->next))
1498 return;
1499
1500 head->func = (void (*)(struct callback_head *))func;
1501 head->next = rq->balance_callback;
1502 rq->balance_callback = head;
1503 }
1504
1505 #define rcu_dereference_check_sched_domain(p) \
1506 rcu_dereference_check((p), \
1507 lockdep_is_held(&sched_domains_mutex))
1508
1509 /*
1510 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1511 * See destroy_sched_domains: call_rcu for details.
1512 *
1513 * The domain tree of any CPU may only be accessed from within
1514 * preempt-disabled sections.
1515 */
1516 #define for_each_domain(cpu, __sd) \
1517 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1518 __sd; __sd = __sd->parent)
1519
1520 /**
1521 * highest_flag_domain - Return highest sched_domain containing flag.
1522 * @cpu: The CPU whose highest level of sched domain is to
1523 * be returned.
1524 * @flag: The flag to check for the highest sched_domain
1525 * for the given CPU.
1526 *
1527 * Returns the highest sched_domain of a CPU which contains the given flag.
1528 */
highest_flag_domain(int cpu,int flag)1529 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1530 {
1531 struct sched_domain *sd, *hsd = NULL;
1532
1533 for_each_domain(cpu, sd) {
1534 if (!(sd->flags & flag))
1535 break;
1536 hsd = sd;
1537 }
1538
1539 return hsd;
1540 }
1541
lowest_flag_domain(int cpu,int flag)1542 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1543 {
1544 struct sched_domain *sd;
1545
1546 for_each_domain(cpu, sd) {
1547 if (sd->flags & flag)
1548 break;
1549 }
1550
1551 return sd;
1552 }
1553
1554 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
1555 DECLARE_PER_CPU(int, sd_llc_size);
1556 DECLARE_PER_CPU(int, sd_llc_id);
1557 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
1558 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
1559 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
1560 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
1561 extern struct static_key_false sched_asym_cpucapacity;
1562
1563 struct sched_group_capacity {
1564 atomic_t ref;
1565 /*
1566 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
1567 * for a single CPU.
1568 */
1569 unsigned long capacity;
1570 unsigned long min_capacity; /* Min per-CPU capacity in group */
1571 unsigned long max_capacity; /* Max per-CPU capacity in group */
1572 unsigned long next_update;
1573 int imbalance; /* XXX unrelated to capacity but shared group state */
1574
1575 #ifdef CONFIG_SCHED_DEBUG
1576 int id;
1577 #endif
1578
1579 unsigned long cpumask[]; /* Balance mask */
1580 };
1581
1582 struct sched_group {
1583 struct sched_group *next; /* Must be a circular list */
1584 atomic_t ref;
1585
1586 unsigned int group_weight;
1587 struct sched_group_capacity *sgc;
1588 int asym_prefer_cpu; /* CPU of highest priority in group */
1589
1590 /*
1591 * The CPUs this group covers.
1592 *
1593 * NOTE: this field is variable length. (Allocated dynamically
1594 * by attaching extra space to the end of the structure,
1595 * depending on how many CPUs the kernel has booted up with)
1596 */
1597 unsigned long cpumask[];
1598 };
1599
sched_group_span(struct sched_group * sg)1600 static inline struct cpumask *sched_group_span(struct sched_group *sg)
1601 {
1602 return to_cpumask(sg->cpumask);
1603 }
1604
1605 /*
1606 * See build_balance_mask().
1607 */
group_balance_mask(struct sched_group * sg)1608 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
1609 {
1610 return to_cpumask(sg->sgc->cpumask);
1611 }
1612
1613 /**
1614 * group_first_cpu - Returns the first CPU in the cpumask of a sched_group.
1615 * @group: The group whose first CPU is to be returned.
1616 */
group_first_cpu(struct sched_group * group)1617 static inline unsigned int group_first_cpu(struct sched_group *group)
1618 {
1619 return cpumask_first(sched_group_span(group));
1620 }
1621
1622 extern int group_balance_cpu(struct sched_group *sg);
1623
1624 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1625 void register_sched_domain_sysctl(void);
1626 void dirty_sched_domain_sysctl(int cpu);
1627 void unregister_sched_domain_sysctl(void);
1628 #else
register_sched_domain_sysctl(void)1629 static inline void register_sched_domain_sysctl(void)
1630 {
1631 }
dirty_sched_domain_sysctl(int cpu)1632 static inline void dirty_sched_domain_sysctl(int cpu)
1633 {
1634 }
unregister_sched_domain_sysctl(void)1635 static inline void unregister_sched_domain_sysctl(void)
1636 {
1637 }
1638 #endif
1639
1640 extern void flush_smp_call_function_from_idle(void);
1641
1642 #else /* !CONFIG_SMP: */
flush_smp_call_function_from_idle(void)1643 static inline void flush_smp_call_function_from_idle(void) { }
1644 #endif
1645
1646 #include "stats.h"
1647 #include "autogroup.h"
1648
1649 #ifdef CONFIG_CGROUP_SCHED
1650
1651 /*
1652 * Return the group to which this tasks belongs.
1653 *
1654 * We cannot use task_css() and friends because the cgroup subsystem
1655 * changes that value before the cgroup_subsys::attach() method is called,
1656 * therefore we cannot pin it and might observe the wrong value.
1657 *
1658 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1659 * core changes this before calling sched_move_task().
1660 *
1661 * Instead we use a 'copy' which is updated from sched_move_task() while
1662 * holding both task_struct::pi_lock and rq::lock.
1663 */
task_group(struct task_struct * p)1664 static inline struct task_group *task_group(struct task_struct *p)
1665 {
1666 return p->sched_task_group;
1667 }
1668
1669 /* 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)1670 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1671 {
1672 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1673 struct task_group *tg = task_group(p);
1674 #endif
1675
1676 #ifdef CONFIG_FAIR_GROUP_SCHED
1677 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1678 p->se.cfs_rq = tg->cfs_rq[cpu];
1679 p->se.parent = tg->se[cpu];
1680 #endif
1681
1682 #ifdef CONFIG_RT_GROUP_SCHED
1683 p->rt.rt_rq = tg->rt_rq[cpu];
1684 p->rt.parent = tg->rt_se[cpu];
1685 #endif
1686 }
1687
1688 #else /* CONFIG_CGROUP_SCHED */
1689
set_task_rq(struct task_struct * p,unsigned int cpu)1690 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
task_group(struct task_struct * p)1691 static inline struct task_group *task_group(struct task_struct *p)
1692 {
1693 return NULL;
1694 }
1695
1696 #endif /* CONFIG_CGROUP_SCHED */
1697
__set_task_cpu(struct task_struct * p,unsigned int cpu)1698 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1699 {
1700 set_task_rq(p, cpu);
1701 #ifdef CONFIG_SMP
1702 /*
1703 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1704 * successfully executed on another CPU. We must ensure that updates of
1705 * per-task data have been completed by this moment.
1706 */
1707 smp_wmb();
1708 #ifdef CONFIG_THREAD_INFO_IN_TASK
1709 WRITE_ONCE(p->cpu, cpu);
1710 #else
1711 WRITE_ONCE(task_thread_info(p)->cpu, cpu);
1712 #endif
1713 p->wake_cpu = cpu;
1714 #endif
1715 }
1716
1717 /*
1718 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1719 */
1720 #ifdef CONFIG_SCHED_DEBUG
1721 # include <linux/static_key.h>
1722 # define const_debug __read_mostly
1723 #else
1724 # define const_debug const
1725 #endif
1726
1727 #define SCHED_FEAT(name, enabled) \
1728 __SCHED_FEAT_##name ,
1729
1730 enum {
1731 #include "features.h"
1732 __SCHED_FEAT_NR,
1733 };
1734
1735 #undef SCHED_FEAT
1736
1737 #ifdef CONFIG_SCHED_DEBUG
1738
1739 /*
1740 * To support run-time toggling of sched features, all the translation units
1741 * (but core.c) reference the sysctl_sched_features defined in core.c.
1742 */
1743 extern const_debug unsigned int sysctl_sched_features;
1744
1745 #ifdef CONFIG_JUMP_LABEL
1746 #define SCHED_FEAT(name, enabled) \
1747 static __always_inline bool static_branch_##name(struct static_key *key) \
1748 { \
1749 return static_key_##enabled(key); \
1750 }
1751
1752 #include "features.h"
1753 #undef SCHED_FEAT
1754
1755 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1756 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1757
1758 #else /* !CONFIG_JUMP_LABEL */
1759
1760 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1761
1762 #endif /* CONFIG_JUMP_LABEL */
1763
1764 #else /* !SCHED_DEBUG */
1765
1766 /*
1767 * Each translation unit has its own copy of sysctl_sched_features to allow
1768 * constants propagation at compile time and compiler optimization based on
1769 * features default.
1770 */
1771 #define SCHED_FEAT(name, enabled) \
1772 (1UL << __SCHED_FEAT_##name) * enabled |
1773 static const_debug __maybe_unused unsigned int sysctl_sched_features =
1774 #include "features.h"
1775 0;
1776 #undef SCHED_FEAT
1777
1778 #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1779
1780 #endif /* SCHED_DEBUG */
1781
1782 extern struct static_key_false sched_numa_balancing;
1783 extern struct static_key_false sched_schedstats;
1784
global_rt_period(void)1785 static inline u64 global_rt_period(void)
1786 {
1787 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1788 }
1789
global_rt_runtime(void)1790 static inline u64 global_rt_runtime(void)
1791 {
1792 if (sysctl_sched_rt_runtime < 0)
1793 return RUNTIME_INF;
1794
1795 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1796 }
1797
task_current(struct rq * rq,struct task_struct * p)1798 static inline int task_current(struct rq *rq, struct task_struct *p)
1799 {
1800 return rq->curr == p;
1801 }
1802
task_running(struct rq * rq,struct task_struct * p)1803 static inline int task_running(struct rq *rq, struct task_struct *p)
1804 {
1805 #ifdef CONFIG_SMP
1806 return p->on_cpu;
1807 #else
1808 return task_current(rq, p);
1809 #endif
1810 }
1811
task_on_rq_queued(struct task_struct * p)1812 static inline int task_on_rq_queued(struct task_struct *p)
1813 {
1814 return p->on_rq == TASK_ON_RQ_QUEUED;
1815 }
1816
task_on_rq_migrating(struct task_struct * p)1817 static inline int task_on_rq_migrating(struct task_struct *p)
1818 {
1819 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
1820 }
1821
1822 /*
1823 * wake flags
1824 */
1825 #define WF_SYNC 0x01 /* Waker goes to sleep after wakeup */
1826 #define WF_FORK 0x02 /* Child wakeup after fork */
1827 #define WF_MIGRATED 0x04 /* Internal use, task got migrated */
1828 #define WF_ON_CPU 0x08 /* Wakee is on_cpu */
1829
1830 /*
1831 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1832 * of tasks with abnormal "nice" values across CPUs the contribution that
1833 * each task makes to its run queue's load is weighted according to its
1834 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1835 * scaled version of the new time slice allocation that they receive on time
1836 * slice expiry etc.
1837 */
1838
1839 #define WEIGHT_IDLEPRIO 3
1840 #define WMULT_IDLEPRIO 1431655765
1841
1842 extern const int sched_prio_to_weight[40];
1843 extern const u32 sched_prio_to_wmult[40];
1844
1845 /*
1846 * {de,en}queue flags:
1847 *
1848 * DEQUEUE_SLEEP - task is no longer runnable
1849 * ENQUEUE_WAKEUP - task just became runnable
1850 *
1851 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1852 * are in a known state which allows modification. Such pairs
1853 * should preserve as much state as possible.
1854 *
1855 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1856 * in the runqueue.
1857 *
1858 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1859 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1860 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1861 *
1862 */
1863
1864 #define DEQUEUE_SLEEP 0x01
1865 #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
1866 #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
1867 #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
1868
1869 #define ENQUEUE_WAKEUP 0x01
1870 #define ENQUEUE_RESTORE 0x02
1871 #define ENQUEUE_MOVE 0x04
1872 #define ENQUEUE_NOCLOCK 0x08
1873
1874 #define ENQUEUE_HEAD 0x10
1875 #define ENQUEUE_REPLENISH 0x20
1876 #ifdef CONFIG_SMP
1877 #define ENQUEUE_MIGRATED 0x40
1878 #else
1879 #define ENQUEUE_MIGRATED 0x00
1880 #endif
1881
1882 #define RETRY_TASK ((void *)-1UL)
1883
1884 struct sched_class {
1885
1886 #ifdef CONFIG_UCLAMP_TASK
1887 int uclamp_enabled;
1888 #endif
1889
1890 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1891 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1892 void (*yield_task) (struct rq *rq);
1893 bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
1894
1895 void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags);
1896
1897 struct task_struct *(*pick_next_task)(struct rq *rq);
1898
1899 void (*put_prev_task)(struct rq *rq, struct task_struct *p);
1900 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
1901
1902 #ifdef CONFIG_SMP
1903 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
1904 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1905 void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
1906
1907 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
1908
1909 void (*set_cpus_allowed)(struct task_struct *p,
1910 const struct cpumask *newmask);
1911
1912 void (*rq_online)(struct rq *rq);
1913 void (*rq_offline)(struct rq *rq);
1914 #endif
1915
1916 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
1917 void (*task_fork)(struct task_struct *p);
1918 void (*task_dead)(struct task_struct *p);
1919
1920 /*
1921 * The switched_from() call is allowed to drop rq->lock, therefore we
1922 * cannot assume the switched_from/switched_to pair is serliazed by
1923 * rq->lock. They are however serialized by p->pi_lock.
1924 */
1925 void (*switched_from)(struct rq *this_rq, struct task_struct *task);
1926 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1927 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1928 int oldprio);
1929
1930 unsigned int (*get_rr_interval)(struct rq *rq,
1931 struct task_struct *task);
1932
1933 void (*update_curr)(struct rq *rq);
1934
1935 #define TASK_SET_GROUP 0
1936 #define TASK_MOVE_GROUP 1
1937
1938 #ifdef CONFIG_FAIR_GROUP_SCHED
1939 void (*task_change_group)(struct task_struct *p, int type);
1940 #endif
1941 #ifdef CONFIG_SCHED_WALT
1942 void (*fixup_walt_sched_stats)(struct rq *rq, struct task_struct *p,
1943 u16 updated_demand_scaled);
1944 #endif
1945 #ifdef CONFIG_SCHED_EAS
1946 void (*check_for_migration)(struct rq *rq, struct task_struct *p);
1947 #endif
1948 } __aligned(STRUCT_ALIGNMENT); /* STRUCT_ALIGN(), vmlinux.lds.h */
1949
put_prev_task(struct rq * rq,struct task_struct * prev)1950 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1951 {
1952 WARN_ON_ONCE(rq->curr != prev);
1953 prev->sched_class->put_prev_task(rq, prev);
1954 }
1955
set_next_task(struct rq * rq,struct task_struct * next)1956 static inline void set_next_task(struct rq *rq, struct task_struct *next)
1957 {
1958 WARN_ON_ONCE(rq->curr != next);
1959 next->sched_class->set_next_task(rq, next, false);
1960 }
1961
1962 /* Defined in include/asm-generic/vmlinux.lds.h */
1963 extern struct sched_class __begin_sched_classes[];
1964 extern struct sched_class __end_sched_classes[];
1965
1966 #define sched_class_highest (__end_sched_classes - 1)
1967 #define sched_class_lowest (__begin_sched_classes - 1)
1968
1969 #define for_class_range(class, _from, _to) \
1970 for (class = (_from); class != (_to); class--)
1971
1972 #define for_each_class(class) \
1973 for_class_range(class, sched_class_highest, sched_class_lowest)
1974
1975 extern const struct sched_class stop_sched_class;
1976 extern const struct sched_class dl_sched_class;
1977 extern const struct sched_class rt_sched_class;
1978 extern const struct sched_class fair_sched_class;
1979 extern const struct sched_class idle_sched_class;
1980
sched_stop_runnable(struct rq * rq)1981 static inline bool sched_stop_runnable(struct rq *rq)
1982 {
1983 return rq->stop && task_on_rq_queued(rq->stop);
1984 }
1985
sched_dl_runnable(struct rq * rq)1986 static inline bool sched_dl_runnable(struct rq *rq)
1987 {
1988 return rq->dl.dl_nr_running > 0;
1989 }
1990
sched_rt_runnable(struct rq * rq)1991 static inline bool sched_rt_runnable(struct rq *rq)
1992 {
1993 return rq->rt.rt_queued > 0;
1994 }
1995
sched_fair_runnable(struct rq * rq)1996 static inline bool sched_fair_runnable(struct rq *rq)
1997 {
1998 return rq->cfs.nr_running > 0;
1999 }
2000
2001 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2002 extern struct task_struct *pick_next_task_idle(struct rq *rq);
2003
2004 #ifdef CONFIG_SMP
2005
2006 extern void update_group_capacity(struct sched_domain *sd, int cpu);
2007
2008 extern void trigger_load_balance(struct rq *rq);
2009
2010 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
2011
2012 #endif
2013
2014 #ifdef CONFIG_CPU_IDLE
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)2015 static inline void idle_set_state(struct rq *rq,
2016 struct cpuidle_state *idle_state)
2017 {
2018 rq->idle_state = idle_state;
2019 }
2020
idle_get_state(struct rq * rq)2021 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2022 {
2023 SCHED_WARN_ON(!rcu_read_lock_held());
2024
2025 return rq->idle_state;
2026 }
2027 #else
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)2028 static inline void idle_set_state(struct rq *rq,
2029 struct cpuidle_state *idle_state)
2030 {
2031 }
2032
idle_get_state(struct rq * rq)2033 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2034 {
2035 return NULL;
2036 }
2037 #endif
2038
2039 extern void schedule_idle(void);
2040
2041 extern void sysrq_sched_debug_show(void);
2042 extern void sched_init_granularity(void);
2043 extern void update_max_interval(void);
2044
2045 extern void init_sched_dl_class(void);
2046 extern void init_sched_rt_class(void);
2047 extern void init_sched_fair_class(void);
2048
2049 extern void reweight_task(struct task_struct *p, int prio);
2050
2051 extern void resched_curr(struct rq *rq);
2052 extern void resched_cpu(int cpu);
2053
2054 extern struct rt_bandwidth def_rt_bandwidth;
2055 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
2056
2057 extern struct dl_bandwidth def_dl_bandwidth;
2058 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
2059 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
2060 extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
2061
2062 #define BW_SHIFT 20
2063 #define BW_UNIT (1 << BW_SHIFT)
2064 #define RATIO_SHIFT 8
2065 #define MAX_BW_BITS (64 - BW_SHIFT)
2066 #define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
2067 unsigned long to_ratio(u64 period, u64 runtime);
2068
2069 extern void init_entity_runnable_average(struct sched_entity *se);
2070 extern void post_init_entity_util_avg(struct task_struct *p);
2071
2072 #ifdef CONFIG_NO_HZ_FULL
2073 extern bool sched_can_stop_tick(struct rq *rq);
2074 extern int __init sched_tick_offload_init(void);
2075
2076 /*
2077 * Tick may be needed by tasks in the runqueue depending on their policy and
2078 * requirements. If tick is needed, lets send the target an IPI to kick it out of
2079 * nohz mode if necessary.
2080 */
sched_update_tick_dependency(struct rq * rq)2081 static inline void sched_update_tick_dependency(struct rq *rq)
2082 {
2083 int cpu = cpu_of(rq);
2084
2085 if (!tick_nohz_full_cpu(cpu))
2086 return;
2087
2088 if (sched_can_stop_tick(rq))
2089 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
2090 else
2091 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
2092 }
2093 #else
sched_tick_offload_init(void)2094 static inline int sched_tick_offload_init(void) { return 0; }
sched_update_tick_dependency(struct rq * rq)2095 static inline void sched_update_tick_dependency(struct rq *rq) { }
2096 #endif
2097
add_nr_running(struct rq * rq,unsigned count)2098 static inline void add_nr_running(struct rq *rq, unsigned count)
2099 {
2100 unsigned prev_nr = rq->nr_running;
2101
2102 rq->nr_running = prev_nr + count;
2103 if (trace_sched_update_nr_running_tp_enabled()) {
2104 call_trace_sched_update_nr_running(rq, count);
2105 }
2106
2107 #ifdef CONFIG_SMP
2108 if (prev_nr < 2 && rq->nr_running >= 2) {
2109 if (!READ_ONCE(rq->rd->overload))
2110 WRITE_ONCE(rq->rd->overload, 1);
2111 }
2112 #endif
2113
2114 sched_update_tick_dependency(rq);
2115 }
2116
sub_nr_running(struct rq * rq,unsigned count)2117 static inline void sub_nr_running(struct rq *rq, unsigned count)
2118 {
2119 rq->nr_running -= count;
2120 if (trace_sched_update_nr_running_tp_enabled()) {
2121 call_trace_sched_update_nr_running(rq, -count);
2122 }
2123
2124 /* Check if we still need preemption */
2125 sched_update_tick_dependency(rq);
2126 }
2127
2128 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2129 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2130
2131 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
2132
2133 extern const_debug unsigned int sysctl_sched_nr_migrate;
2134 extern const_debug unsigned int sysctl_sched_migration_cost;
2135
2136 #ifdef CONFIG_SCHED_HRTICK
2137
2138 /*
2139 * Use hrtick when:
2140 * - enabled by features
2141 * - hrtimer is actually high res
2142 */
hrtick_enabled(struct rq * rq)2143 static inline int hrtick_enabled(struct rq *rq)
2144 {
2145 if (!sched_feat(HRTICK))
2146 return 0;
2147 if (!cpu_active(cpu_of(rq)))
2148 return 0;
2149 return hrtimer_is_hres_active(&rq->hrtick_timer);
2150 }
2151
2152 void hrtick_start(struct rq *rq, u64 delay);
2153
2154 #else
2155
hrtick_enabled(struct rq * rq)2156 static inline int hrtick_enabled(struct rq *rq)
2157 {
2158 return 0;
2159 }
2160
2161 #endif /* CONFIG_SCHED_HRTICK */
2162
2163 #ifdef CONFIG_SCHED_WALT
2164 u64 sched_ktime_clock(void);
2165 #else
sched_ktime_clock(void)2166 static inline u64 sched_ktime_clock(void)
2167 {
2168 return sched_clock();
2169 }
2170 #endif
2171
2172 #ifndef arch_scale_freq_tick
2173 static __always_inline
arch_scale_freq_tick(void)2174 void arch_scale_freq_tick(void)
2175 {
2176 }
2177 #endif
2178
2179 #ifndef arch_scale_freq_capacity
2180 /**
2181 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
2182 * @cpu: the CPU in question.
2183 *
2184 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
2185 *
2186 * f_curr
2187 * ------ * SCHED_CAPACITY_SCALE
2188 * f_max
2189 */
2190 static __always_inline
arch_scale_freq_capacity(int cpu)2191 unsigned long arch_scale_freq_capacity(int cpu)
2192 {
2193 return SCHED_CAPACITY_SCALE;
2194 }
2195 #endif
2196
2197 unsigned long capacity_curr_of(int cpu);
2198 unsigned long cpu_util(int cpu);
2199
2200 #ifdef CONFIG_SMP
2201 #ifdef CONFIG_SCHED_WALT
2202 extern unsigned int sysctl_sched_use_walt_cpu_util;
2203 extern unsigned int walt_disabled;
2204 #endif
2205 #ifdef CONFIG_PREEMPTION
2206
2207 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
2208
2209 /*
2210 * fair double_lock_balance: Safely acquires both rq->locks in a fair
2211 * way at the expense of forcing extra atomic operations in all
2212 * invocations. This assures that the double_lock is acquired using the
2213 * same underlying policy as the spinlock_t on this architecture, which
2214 * reduces latency compared to the unfair variant below. However, it
2215 * also adds more overhead and therefore may reduce throughput.
2216 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)2217 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2218 __releases(this_rq->lock)
2219 __acquires(busiest->lock)
2220 __acquires(this_rq->lock)
2221 {
2222 raw_spin_unlock(&this_rq->lock);
2223 double_rq_lock(this_rq, busiest);
2224
2225 return 1;
2226 }
2227
2228 #else
2229 /*
2230 * Unfair double_lock_balance: Optimizes throughput at the expense of
2231 * latency by eliminating extra atomic operations when the locks are
2232 * already in proper order on entry. This favors lower CPU-ids and will
2233 * grant the double lock to lower CPUs over higher ids under contention,
2234 * regardless of entry order into the function.
2235 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)2236 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2237 __releases(this_rq->lock)
2238 __acquires(busiest->lock)
2239 __acquires(this_rq->lock)
2240 {
2241 int ret = 0;
2242
2243 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
2244 if (busiest < this_rq) {
2245 raw_spin_unlock(&this_rq->lock);
2246 raw_spin_lock(&busiest->lock);
2247 raw_spin_lock_nested(&this_rq->lock,
2248 SINGLE_DEPTH_NESTING);
2249 ret = 1;
2250 } else
2251 raw_spin_lock_nested(&busiest->lock,
2252 SINGLE_DEPTH_NESTING);
2253 }
2254 return ret;
2255 }
2256
2257 #endif /* CONFIG_PREEMPTION */
2258
2259 /*
2260 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2261 */
double_lock_balance(struct rq * this_rq,struct rq * busiest)2262 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2263 {
2264 if (unlikely(!irqs_disabled())) {
2265 /* printk() doesn't work well under rq->lock */
2266 raw_spin_unlock(&this_rq->lock);
2267 BUG_ON(1);
2268 }
2269
2270 return _double_lock_balance(this_rq, busiest);
2271 }
2272
double_unlock_balance(struct rq * this_rq,struct rq * busiest)2273 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2274 __releases(busiest->lock)
2275 {
2276 raw_spin_unlock(&busiest->lock);
2277 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2278 }
2279
double_lock(spinlock_t * l1,spinlock_t * l2)2280 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
2281 {
2282 if (l1 > l2)
2283 swap(l1, l2);
2284
2285 spin_lock(l1);
2286 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2287 }
2288
double_lock_irq(spinlock_t * l1,spinlock_t * l2)2289 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
2290 {
2291 if (l1 > l2)
2292 swap(l1, l2);
2293
2294 spin_lock_irq(l1);
2295 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2296 }
2297
double_raw_lock(raw_spinlock_t * l1,raw_spinlock_t * l2)2298 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2299 {
2300 if (l1 > l2)
2301 swap(l1, l2);
2302
2303 raw_spin_lock(l1);
2304 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2305 }
2306
2307 /*
2308 * double_rq_lock - safely lock two runqueues
2309 *
2310 * Note this does not disable interrupts like task_rq_lock,
2311 * you need to do so manually before calling.
2312 */
double_rq_lock(struct rq * rq1,struct rq * rq2)2313 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2314 __acquires(rq1->lock)
2315 __acquires(rq2->lock)
2316 {
2317 BUG_ON(!irqs_disabled());
2318 if (rq1 == rq2) {
2319 raw_spin_lock(&rq1->lock);
2320 __acquire(rq2->lock); /* Fake it out ;) */
2321 } else {
2322 if (rq1 < rq2) {
2323 raw_spin_lock(&rq1->lock);
2324 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2325 } else {
2326 raw_spin_lock(&rq2->lock);
2327 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2328 }
2329 }
2330 }
2331
2332 /*
2333 * double_rq_unlock - safely unlock two runqueues
2334 *
2335 * Note this does not restore interrupts like task_rq_unlock,
2336 * you need to do so manually after calling.
2337 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)2338 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2339 __releases(rq1->lock)
2340 __releases(rq2->lock)
2341 {
2342 raw_spin_unlock(&rq1->lock);
2343 if (rq1 != rq2)
2344 raw_spin_unlock(&rq2->lock);
2345 else
2346 __release(rq2->lock);
2347 }
2348
2349 extern void set_rq_online (struct rq *rq);
2350 extern void set_rq_offline(struct rq *rq);
2351 extern bool sched_smp_initialized;
2352
2353 #else /* CONFIG_SMP */
2354
2355 /*
2356 * double_rq_lock - safely lock two runqueues
2357 *
2358 * Note this does not disable interrupts like task_rq_lock,
2359 * you need to do so manually before calling.
2360 */
double_rq_lock(struct rq * rq1,struct rq * rq2)2361 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2362 __acquires(rq1->lock)
2363 __acquires(rq2->lock)
2364 {
2365 BUG_ON(!irqs_disabled());
2366 BUG_ON(rq1 != rq2);
2367 raw_spin_lock(&rq1->lock);
2368 __acquire(rq2->lock); /* Fake it out ;) */
2369 }
2370
2371 /*
2372 * double_rq_unlock - safely unlock two runqueues
2373 *
2374 * Note this does not restore interrupts like task_rq_unlock,
2375 * you need to do so manually after calling.
2376 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)2377 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2378 __releases(rq1->lock)
2379 __releases(rq2->lock)
2380 {
2381 BUG_ON(rq1 != rq2);
2382 raw_spin_unlock(&rq1->lock);
2383 __release(rq2->lock);
2384 }
2385
2386 #endif
2387
2388 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2389 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
2390
2391 #ifdef CONFIG_SCHED_DEBUG
2392 extern bool sched_debug_enabled;
2393
2394 extern void print_cfs_stats(struct seq_file *m, int cpu);
2395 extern void print_rt_stats(struct seq_file *m, int cpu);
2396 extern void print_dl_stats(struct seq_file *m, int cpu);
2397 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
2398 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2399 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2400 #ifdef CONFIG_NUMA_BALANCING
2401 extern void
2402 show_numa_stats(struct task_struct *p, struct seq_file *m);
2403 extern void
2404 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2405 unsigned long tpf, unsigned long gsf, unsigned long gpf);
2406 #endif /* CONFIG_NUMA_BALANCING */
2407 #endif /* CONFIG_SCHED_DEBUG */
2408
2409 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
2410 extern void init_rt_rq(struct rt_rq *rt_rq);
2411 extern void init_dl_rq(struct dl_rq *dl_rq);
2412
2413 extern void cfs_bandwidth_usage_inc(void);
2414 extern void cfs_bandwidth_usage_dec(void);
2415
2416 #ifdef CONFIG_NO_HZ_COMMON
2417 #define NOHZ_BALANCE_KICK_BIT 0
2418 #define NOHZ_STATS_KICK_BIT 1
2419
2420 #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
2421 #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
2422
2423 #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
2424
2425 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
2426
2427 extern void nohz_balance_exit_idle(struct rq *rq);
2428 #else
nohz_balance_exit_idle(struct rq * rq)2429 static inline void nohz_balance_exit_idle(struct rq *rq) { }
2430 #endif
2431
2432
2433 #ifdef CONFIG_SMP
2434 static inline
__dl_update(struct dl_bw * dl_b,s64 bw)2435 void __dl_update(struct dl_bw *dl_b, s64 bw)
2436 {
2437 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2438 int i;
2439
2440 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2441 "sched RCU must be held");
2442 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2443 struct rq *rq = cpu_rq(i);
2444
2445 rq->dl.extra_bw += bw;
2446 }
2447 }
2448 #else
2449 static inline
__dl_update(struct dl_bw * dl_b,s64 bw)2450 void __dl_update(struct dl_bw *dl_b, s64 bw)
2451 {
2452 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2453
2454 dl->extra_bw += bw;
2455 }
2456 #endif
2457
2458
2459 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2460 struct irqtime {
2461 u64 total;
2462 u64 tick_delta;
2463 u64 irq_start_time;
2464 struct u64_stats_sync sync;
2465 };
2466
2467 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
2468
2469 /*
2470 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2471 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2472 * and never move forward.
2473 */
irq_time_read(int cpu)2474 static inline u64 irq_time_read(int cpu)
2475 {
2476 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2477 unsigned int seq;
2478 u64 total;
2479
2480 do {
2481 seq = __u64_stats_fetch_begin(&irqtime->sync);
2482 total = irqtime->total;
2483 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
2484
2485 return total;
2486 }
2487 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2488
2489 #ifdef CONFIG_CPU_FREQ
2490 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
2491
2492 /**
2493 * cpufreq_update_util - Take a note about CPU utilization changes.
2494 * @rq: Runqueue to carry out the update for.
2495 * @flags: Update reason flags.
2496 *
2497 * This function is called by the scheduler on the CPU whose utilization is
2498 * being updated.
2499 *
2500 * It can only be called from RCU-sched read-side critical sections.
2501 *
2502 * The way cpufreq is currently arranged requires it to evaluate the CPU
2503 * performance state (frequency/voltage) on a regular basis to prevent it from
2504 * being stuck in a completely inadequate performance level for too long.
2505 * That is not guaranteed to happen if the updates are only triggered from CFS
2506 * and DL, though, because they may not be coming in if only RT tasks are
2507 * active all the time (or there are RT tasks only).
2508 *
2509 * As a workaround for that issue, this function is called periodically by the
2510 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
2511 * but that really is a band-aid. Going forward it should be replaced with
2512 * solutions targeted more specifically at RT tasks.
2513 */
cpufreq_update_util(struct rq * rq,unsigned int flags)2514 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2515 {
2516 struct update_util_data *data;
2517 u64 clock;
2518
2519 #ifdef CONFIG_SCHED_WALT
2520 if (!(flags & SCHED_CPUFREQ_WALT))
2521 return;
2522
2523 clock = sched_ktime_clock();
2524 #else
2525 clock = rq_clock(rq);
2526 #endif
2527 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2528 cpu_of(rq)));
2529 if (data)
2530 data->func(data, clock, flags);
2531 }
2532 #else
cpufreq_update_util(struct rq * rq,unsigned int flags)2533 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2534 #endif /* CONFIG_CPU_FREQ */
2535
2536 #ifdef CONFIG_UCLAMP_TASK
2537 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
2538
2539 /**
2540 * uclamp_rq_util_with - clamp @util with @rq and @p effective uclamp values.
2541 * @rq: The rq to clamp against. Must not be NULL.
2542 * @util: The util value to clamp.
2543 * @p: The task to clamp against. Can be NULL if you want to clamp
2544 * against @rq only.
2545 *
2546 * Clamps the passed @util to the max(@rq, @p) effective uclamp values.
2547 *
2548 * If sched_uclamp_used static key is disabled, then just return the util
2549 * without any clamping since uclamp aggregation at the rq level in the fast
2550 * path is disabled, rendering this operation a NOP.
2551 *
2552 * Use uclamp_eff_value() if you don't care about uclamp values at rq level. It
2553 * will return the correct effective uclamp value of the task even if the
2554 * static key is disabled.
2555 */
2556 static __always_inline
uclamp_rq_util_with(struct rq * rq,unsigned long util,struct task_struct * p)2557 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2558 struct task_struct *p)
2559 {
2560 unsigned long min_util = 0;
2561 unsigned long max_util = 0;
2562
2563 if (!static_branch_likely(&sched_uclamp_used))
2564 return util;
2565
2566 if (p) {
2567 min_util = uclamp_eff_value(p, UCLAMP_MIN);
2568 max_util = uclamp_eff_value(p, UCLAMP_MAX);
2569
2570 /*
2571 * Ignore last runnable task's max clamp, as this task will
2572 * reset it. Similarly, no need to read the rq's min clamp.
2573 */
2574 if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
2575 goto out;
2576 }
2577
2578 min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
2579 max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));
2580 out:
2581 /*
2582 * Since CPU's {min,max}_util clamps are MAX aggregated considering
2583 * RUNNABLE tasks with _different_ clamps, we can end up with an
2584 * inversion. Fix it now when the clamps are applied.
2585 */
2586 if (unlikely(min_util >= max_util))
2587 return min_util;
2588
2589 return clamp(util, min_util, max_util);
2590 }
2591
uclamp_boosted(struct task_struct * p)2592 static inline bool uclamp_boosted(struct task_struct *p)
2593 {
2594 return uclamp_eff_value(p, UCLAMP_MIN) > 0;
2595 }
2596
2597 /*
2598 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
2599 * by default in the fast path and only gets turned on once userspace performs
2600 * an operation that requires it.
2601 *
2602 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
2603 * hence is active.
2604 */
uclamp_is_used(void)2605 static inline bool uclamp_is_used(void)
2606 {
2607 return static_branch_likely(&sched_uclamp_used);
2608 }
2609 #else /* CONFIG_UCLAMP_TASK */
2610 static inline
uclamp_rq_util_with(struct rq * rq,unsigned long util,struct task_struct * p)2611 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2612 struct task_struct *p)
2613 {
2614 return util;
2615 }
2616
uclamp_boosted(struct task_struct * p)2617 static inline bool uclamp_boosted(struct task_struct *p)
2618 {
2619 return false;
2620 }
2621
uclamp_is_used(void)2622 static inline bool uclamp_is_used(void)
2623 {
2624 return false;
2625 }
2626 #endif /* CONFIG_UCLAMP_TASK */
2627
2628 #ifdef arch_scale_freq_capacity
2629 # ifndef arch_scale_freq_invariant
2630 # define arch_scale_freq_invariant() true
2631 # endif
2632 #else
2633 # define arch_scale_freq_invariant() false
2634 #endif
2635
2636 #ifdef CONFIG_SMP
capacity_of(int cpu)2637 static inline unsigned long capacity_of(int cpu)
2638 {
2639 return cpu_rq(cpu)->cpu_capacity;
2640 }
2641
capacity_orig_of(int cpu)2642 static inline unsigned long capacity_orig_of(int cpu)
2643 {
2644 return cpu_rq(cpu)->cpu_capacity_orig;
2645 }
2646 #endif
2647
2648 /**
2649 * enum schedutil_type - CPU utilization type
2650 * @FREQUENCY_UTIL: Utilization used to select frequency
2651 * @ENERGY_UTIL: Utilization used during energy calculation
2652 *
2653 * The utilization signals of all scheduling classes (CFS/RT/DL) and IRQ time
2654 * need to be aggregated differently depending on the usage made of them. This
2655 * enum is used within schedutil_freq_util() to differentiate the types of
2656 * utilization expected by the callers, and adjust the aggregation accordingly.
2657 */
2658 enum schedutil_type {
2659 FREQUENCY_UTIL,
2660 ENERGY_UTIL,
2661 };
2662
2663 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
2664
2665 unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2666 unsigned long max, enum schedutil_type type,
2667 struct task_struct *p);
2668
cpu_bw_dl(struct rq * rq)2669 static inline unsigned long cpu_bw_dl(struct rq *rq)
2670 {
2671 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
2672 }
2673
cpu_util_dl(struct rq * rq)2674 static inline unsigned long cpu_util_dl(struct rq *rq)
2675 {
2676 return READ_ONCE(rq->avg_dl.util_avg);
2677 }
2678
cpu_util_cfs(struct rq * rq)2679 static inline unsigned long cpu_util_cfs(struct rq *rq)
2680 {
2681 unsigned long util = READ_ONCE(rq->cfs.avg.util_avg);
2682
2683 if (sched_feat(UTIL_EST)) {
2684 util = max_t(unsigned long, util,
2685 READ_ONCE(rq->cfs.avg.util_est.enqueued));
2686 }
2687
2688 return util;
2689 }
2690
cpu_util_rt(struct rq * rq)2691 static inline unsigned long cpu_util_rt(struct rq *rq)
2692 {
2693 return READ_ONCE(rq->avg_rt.util_avg);
2694 }
2695 #else /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
schedutil_cpu_util(int cpu,unsigned long util_cfs,unsigned long max,enum schedutil_type type,struct task_struct * p)2696 static inline unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2697 unsigned long max, enum schedutil_type type,
2698 struct task_struct *p)
2699 {
2700 return 0;
2701 }
2702 #endif /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2703
2704 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
cpu_util_irq(struct rq * rq)2705 static inline unsigned long cpu_util_irq(struct rq *rq)
2706 {
2707 return rq->avg_irq.util_avg;
2708 }
2709
2710 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)2711 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2712 {
2713 util *= (max - irq);
2714 util /= max;
2715
2716 return util;
2717
2718 }
2719 #else
cpu_util_irq(struct rq * rq)2720 static inline unsigned long cpu_util_irq(struct rq *rq)
2721 {
2722 return 0;
2723 }
2724
2725 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)2726 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2727 {
2728 return util;
2729 }
2730 #endif
2731
2732 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
2733
2734 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
2735
2736 DECLARE_STATIC_KEY_FALSE(sched_energy_present);
2737
sched_energy_enabled(void)2738 static inline bool sched_energy_enabled(void)
2739 {
2740 return static_branch_unlikely(&sched_energy_present);
2741 }
2742
2743 #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
2744
2745 #define perf_domain_span(pd) NULL
sched_energy_enabled(void)2746 static inline bool sched_energy_enabled(void) { return false; }
2747
2748 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2749
2750 #ifdef CONFIG_MEMBARRIER
2751 /*
2752 * The scheduler provides memory barriers required by membarrier between:
2753 * - prior user-space memory accesses and store to rq->membarrier_state,
2754 * - store to rq->membarrier_state and following user-space memory accesses.
2755 * In the same way it provides those guarantees around store to rq->curr.
2756 */
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)2757 static inline void membarrier_switch_mm(struct rq *rq,
2758 struct mm_struct *prev_mm,
2759 struct mm_struct *next_mm)
2760 {
2761 int membarrier_state;
2762
2763 if (prev_mm == next_mm)
2764 return;
2765
2766 membarrier_state = atomic_read(&next_mm->membarrier_state);
2767 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
2768 return;
2769
2770 WRITE_ONCE(rq->membarrier_state, membarrier_state);
2771 }
2772 #else
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)2773 static inline void membarrier_switch_mm(struct rq *rq,
2774 struct mm_struct *prev_mm,
2775 struct mm_struct *next_mm)
2776 {
2777 }
2778 #endif
2779
2780 #ifdef CONFIG_SMP
is_per_cpu_kthread(struct task_struct * p)2781 static inline bool is_per_cpu_kthread(struct task_struct *p)
2782 {
2783 if (!(p->flags & PF_KTHREAD))
2784 return false;
2785
2786 if (p->nr_cpus_allowed != 1)
2787 return false;
2788
2789 return true;
2790 }
2791 #endif
2792
2793 void swake_up_all_locked(struct swait_queue_head *q);
2794 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
2795
2796 #ifdef CONFIG_SCHED_RTG
2797 extern bool task_fits_max(struct task_struct *p, int cpu);
2798 extern unsigned long capacity_spare_without(int cpu, struct task_struct *p);
2799 extern int update_preferred_cluster(struct related_thread_group *grp,
2800 struct task_struct *p, u32 old_load, bool from_tick);
2801 extern struct cpumask *find_rtg_target(struct task_struct *p);
2802 #endif
2803
2804 #ifdef CONFIG_SCHED_WALT
cluster_first_cpu(struct sched_cluster * cluster)2805 static inline int cluster_first_cpu(struct sched_cluster *cluster)
2806 {
2807 return cpumask_first(&cluster->cpus);
2808 }
2809
2810 extern struct list_head cluster_head;
2811 extern struct sched_cluster *sched_cluster[NR_CPUS];
2812
2813 #define for_each_sched_cluster(cluster) \
2814 list_for_each_entry_rcu(cluster, &cluster_head, list)
2815
2816 extern struct mutex policy_mutex;
2817 extern unsigned int sched_disable_window_stats;
2818 extern unsigned int max_possible_freq;
2819 extern unsigned int min_max_freq;
2820 extern unsigned int max_possible_efficiency;
2821 extern unsigned int min_possible_efficiency;
2822 extern unsigned int max_capacity;
2823 extern unsigned int min_capacity;
2824 extern unsigned int max_load_scale_factor;
2825 extern unsigned int max_possible_capacity;
2826 extern unsigned int min_max_possible_capacity;
2827 extern unsigned int max_power_cost;
2828 extern unsigned int __read_mostly sched_init_task_load_windows;
2829 extern unsigned int sysctl_sched_restrict_cluster_spill;
2830 extern unsigned int sched_pred_alert_load;
2831 extern struct sched_cluster init_cluster;
2832
walt_fixup_cum_window_demand(struct rq * rq,s64 scaled_delta)2833 static inline void walt_fixup_cum_window_demand(struct rq *rq, s64 scaled_delta)
2834 {
2835 rq->cum_window_demand_scaled += scaled_delta;
2836 if (unlikely((s64)rq->cum_window_demand_scaled < 0))
2837 rq->cum_window_demand_scaled = 0;
2838 }
2839
2840 /* Is frequency of two cpus synchronized with each other? */
same_freq_domain(int src_cpu,int dst_cpu)2841 static inline int same_freq_domain(int src_cpu, int dst_cpu)
2842 {
2843 struct rq *rq = cpu_rq(src_cpu);
2844
2845 if (src_cpu == dst_cpu)
2846 return 1;
2847
2848 return cpumask_test_cpu(dst_cpu, &rq->freq_domain_cpumask);
2849 }
2850
2851 extern void reset_task_stats(struct task_struct *p);
2852
2853 #define CPU_RESERVED 1
is_reserved(int cpu)2854 static inline int is_reserved(int cpu)
2855 {
2856 struct rq *rq = cpu_rq(cpu);
2857
2858 return test_bit(CPU_RESERVED, &rq->walt_flags);
2859 }
2860
mark_reserved(int cpu)2861 static inline int mark_reserved(int cpu)
2862 {
2863 struct rq *rq = cpu_rq(cpu);
2864
2865 return test_and_set_bit(CPU_RESERVED, &rq->walt_flags);
2866 }
2867
clear_reserved(int cpu)2868 static inline void clear_reserved(int cpu)
2869 {
2870 struct rq *rq = cpu_rq(cpu);
2871
2872 clear_bit(CPU_RESERVED, &rq->walt_flags);
2873 }
2874
cpu_capacity(int cpu)2875 static inline int cpu_capacity(int cpu)
2876 {
2877 return cpu_rq(cpu)->cluster->capacity;
2878 }
2879
cpu_max_possible_capacity(int cpu)2880 static inline int cpu_max_possible_capacity(int cpu)
2881 {
2882 return cpu_rq(cpu)->cluster->max_possible_capacity;
2883 }
2884
cpu_load_scale_factor(int cpu)2885 static inline int cpu_load_scale_factor(int cpu)
2886 {
2887 return cpu_rq(cpu)->cluster->load_scale_factor;
2888 }
2889
cluster_max_freq(struct sched_cluster * cluster)2890 static inline unsigned int cluster_max_freq(struct sched_cluster *cluster)
2891 {
2892 /*
2893 * Governor and thermal driver don't know the other party's mitigation
2894 * voting. So struct cluster saves both and return min() for current
2895 * cluster fmax.
2896 */
2897 return cluster->max_freq;
2898 }
2899
2900 /* Keep track of max/min capacity possible across CPUs "currently" */
__update_min_max_capacity(void)2901 static inline void __update_min_max_capacity(void)
2902 {
2903 int i;
2904 int max_cap = 0, min_cap = INT_MAX;
2905
2906 for_each_possible_cpu(i) {
2907 if (!cpu_active(i))
2908 continue;
2909
2910 max_cap = max(max_cap, cpu_capacity(i));
2911 min_cap = min(min_cap, cpu_capacity(i));
2912 }
2913
2914 max_capacity = max_cap;
2915 min_capacity = min_cap;
2916 }
2917
2918 /*
2919 * Return load_scale_factor of a cpu in reference to "most" efficient cpu, so
2920 * that "most" efficient cpu gets a load_scale_factor of 1
2921 */
2922 static inline unsigned long
load_scale_cpu_efficiency(struct sched_cluster * cluster)2923 load_scale_cpu_efficiency(struct sched_cluster *cluster)
2924 {
2925 return DIV_ROUND_UP(1024 * max_possible_efficiency,
2926 cluster->efficiency);
2927 }
2928
2929 /*
2930 * Return load_scale_factor of a cpu in reference to cpu with best max_freq
2931 * (max_possible_freq), so that one with best max_freq gets a load_scale_factor
2932 * of 1.
2933 */
load_scale_cpu_freq(struct sched_cluster * cluster)2934 static inline unsigned long load_scale_cpu_freq(struct sched_cluster *cluster)
2935 {
2936 return DIV_ROUND_UP(1024 * max_possible_freq,
2937 cluster_max_freq(cluster));
2938 }
2939
compute_load_scale_factor(struct sched_cluster * cluster)2940 static inline int compute_load_scale_factor(struct sched_cluster *cluster)
2941 {
2942 int load_scale = 1024;
2943
2944 /*
2945 * load_scale_factor accounts for the fact that task load
2946 * is in reference to "best" performing cpu. Task's load will need to be
2947 * scaled (up) by a factor to determine suitability to be placed on a
2948 * (little) cpu.
2949 */
2950 load_scale *= load_scale_cpu_efficiency(cluster);
2951 load_scale >>= 10;
2952
2953 load_scale *= load_scale_cpu_freq(cluster);
2954 load_scale >>= 10;
2955
2956 return load_scale;
2957 }
2958
is_max_capacity_cpu(int cpu)2959 static inline bool is_max_capacity_cpu(int cpu)
2960 {
2961 return cpu_max_possible_capacity(cpu) == max_possible_capacity;
2962 }
2963
is_min_capacity_cpu(int cpu)2964 static inline bool is_min_capacity_cpu(int cpu)
2965 {
2966 return cpu_max_possible_capacity(cpu) == min_max_possible_capacity;
2967 }
2968
2969 /*
2970 * Return 'capacity' of a cpu in reference to "least" efficient cpu, such that
2971 * least efficient cpu gets capacity of 1024
2972 */
2973 static unsigned long
capacity_scale_cpu_efficiency(struct sched_cluster * cluster)2974 capacity_scale_cpu_efficiency(struct sched_cluster *cluster)
2975 {
2976 return (1024 * cluster->efficiency) / min_possible_efficiency;
2977 }
2978
2979 /*
2980 * Return 'capacity' of a cpu in reference to cpu with lowest max_freq
2981 * (min_max_freq), such that one with lowest max_freq gets capacity of 1024.
2982 */
capacity_scale_cpu_freq(struct sched_cluster * cluster)2983 static unsigned long capacity_scale_cpu_freq(struct sched_cluster *cluster)
2984 {
2985 return (1024 * cluster_max_freq(cluster)) / min_max_freq;
2986 }
2987
compute_capacity(struct sched_cluster * cluster)2988 static inline int compute_capacity(struct sched_cluster *cluster)
2989 {
2990 int capacity = 1024;
2991
2992 capacity *= capacity_scale_cpu_efficiency(cluster);
2993 capacity >>= 10;
2994
2995 capacity *= capacity_scale_cpu_freq(cluster);
2996 capacity >>= 10;
2997
2998 return capacity;
2999 }
3000
power_cost(int cpu,u64 demand)3001 static inline unsigned int power_cost(int cpu, u64 demand)
3002 {
3003 return cpu_max_possible_capacity(cpu);
3004 }
3005
cpu_util_freq_walt(int cpu)3006 static inline unsigned long cpu_util_freq_walt(int cpu)
3007 {
3008 u64 util;
3009 struct rq *rq = cpu_rq(cpu);
3010 unsigned long capacity = capacity_orig_of(cpu);
3011
3012 if (unlikely(walt_disabled || !sysctl_sched_use_walt_cpu_util))
3013 return cpu_util(cpu);
3014
3015 util = rq->prev_runnable_sum << SCHED_CAPACITY_SHIFT;
3016 util = div_u64(util, sched_ravg_window);
3017
3018 return (util >= capacity) ? capacity : util;
3019 }
3020
hmp_capable(void)3021 static inline bool hmp_capable(void)
3022 {
3023 return max_possible_capacity != min_max_possible_capacity;
3024 }
3025 #else /* CONFIG_SCHED_WALT */
walt_fixup_cum_window_demand(struct rq * rq,s64 scaled_delta)3026 static inline void walt_fixup_cum_window_demand(struct rq *rq,
3027 s64 scaled_delta) { }
3028
same_freq_domain(int src_cpu,int dst_cpu)3029 static inline int same_freq_domain(int src_cpu, int dst_cpu)
3030 {
3031 return 1;
3032 }
3033
is_reserved(int cpu)3034 static inline int is_reserved(int cpu)
3035 {
3036 return 0;
3037 }
3038
clear_reserved(int cpu)3039 static inline void clear_reserved(int cpu) { }
3040
hmp_capable(void)3041 static inline bool hmp_capable(void)
3042 {
3043 return false;
3044 }
3045 #endif /* CONFIG_SCHED_WALT */
3046
3047 struct sched_avg_stats {
3048 int nr;
3049 int nr_misfit;
3050 int nr_max;
3051 int nr_scaled;
3052 };
3053 #ifdef CONFIG_SCHED_RUNNING_AVG
3054 extern void sched_get_nr_running_avg(struct sched_avg_stats *stats);
3055 #else
sched_get_nr_running_avg(struct sched_avg_stats * stats)3056 static inline void sched_get_nr_running_avg(struct sched_avg_stats *stats) { }
3057 #endif
3058
3059 #ifdef CONFIG_CPU_ISOLATION_OPT
3060 extern int group_balance_cpu_not_isolated(struct sched_group *sg);
3061 #else
group_balance_cpu_not_isolated(struct sched_group * sg)3062 static inline int group_balance_cpu_not_isolated(struct sched_group *sg)
3063 {
3064 return group_balance_cpu(sg);
3065 }
3066 #endif /* CONFIG_CPU_ISOLATION_OPT */
3067
3068 #ifdef CONFIG_HOTPLUG_CPU
3069 extern void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf,
3070 bool migrate_pinned_tasks);
3071 #endif
3072