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