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