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