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