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