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