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