sched/deadline: Move CPU frequency selection triggering points
[linux-2.6-block.git] / kernel / sched / sched.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
029632fb
PZ
2
3#include <linux/sched.h>
dfc3401a 4#include <linux/sched/autogroup.h>
cf4aebc2 5#include <linux/sched/sysctl.h>
105ab3d8 6#include <linux/sched/topology.h>
8bd75c77 7#include <linux/sched/rt.h>
ef8bd77f 8#include <linux/sched/deadline.h>
e6017571 9#include <linux/sched/clock.h>
84f001e1 10#include <linux/sched/wake_q.h>
3f07c014 11#include <linux/sched/signal.h>
6a3827d7 12#include <linux/sched/numa_balancing.h>
6e84f315 13#include <linux/sched/mm.h>
55687da1 14#include <linux/sched/cpufreq.h>
03441a34 15#include <linux/sched/stat.h>
370c9135 16#include <linux/sched/nohz.h>
b17b0153 17#include <linux/sched/debug.h>
ef8bd77f 18#include <linux/sched/hotplug.h>
29930025 19#include <linux/sched/task.h>
68db0cf1 20#include <linux/sched/task_stack.h>
32ef5517 21#include <linux/sched/cputime.h>
1777e463 22#include <linux/sched/init.h>
ef8bd77f 23
19d23dbf 24#include <linux/u64_stats_sync.h>
a499a5a1 25#include <linux/kernel_stat.h>
3866e845 26#include <linux/binfmts.h>
029632fb
PZ
27#include <linux/mutex.h>
28#include <linux/spinlock.h>
29#include <linux/stop_machine.h>
b6366f04 30#include <linux/irq_work.h>
9f3660c2 31#include <linux/tick.h>
f809ca9a 32#include <linux/slab.h>
d2cc5ed6 33#include <linux/cgroup.h>
029632fb 34
7fce777c
IM
35#ifdef CONFIG_PARAVIRT
36#include <asm/paravirt.h>
37#endif
38
391e43da 39#include "cpupri.h"
6bfd6d72 40#include "cpudeadline.h"
029632fb 41
9148a3a1 42#ifdef CONFIG_SCHED_DEBUG
6d3aed3d 43# define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
9148a3a1 44#else
6d3aed3d 45# define SCHED_WARN_ON(x) ({ (void)(x), 0; })
9148a3a1
PZ
46#endif
47
45ceebf7 48struct rq;
442bf3aa 49struct cpuidle_state;
45ceebf7 50
da0c1e65
KT
51/* task_struct::on_rq states: */
52#define TASK_ON_RQ_QUEUED 1
cca26e80 53#define TASK_ON_RQ_MIGRATING 2
da0c1e65 54
029632fb
PZ
55extern __read_mostly int scheduler_running;
56
45ceebf7
PG
57extern unsigned long calc_load_update;
58extern atomic_long_t calc_load_tasks;
59
3289bdb4 60extern void calc_global_load_tick(struct rq *this_rq);
d60585c5 61extern long calc_load_fold_active(struct rq *this_rq, long adjust);
3289bdb4
PZ
62
63#ifdef CONFIG_SMP
cee1afce 64extern void cpu_load_update_active(struct rq *this_rq);
3289bdb4 65#else
cee1afce 66static inline void cpu_load_update_active(struct rq *this_rq) { }
3289bdb4 67#endif
45ceebf7 68
029632fb
PZ
69/*
70 * Helpers for converting nanosecond timing to jiffy resolution
71 */
72#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
73
cc1f4b1f
LZ
74/*
75 * Increase resolution of nice-level calculations for 64-bit architectures.
76 * The extra resolution improves shares distribution and load balancing of
77 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
78 * hierarchies, especially on larger systems. This is not a user-visible change
79 * and does not change the user-interface for setting shares/weights.
80 *
81 * We increase resolution only if we have enough bits to allow this increased
2159197d
PZ
82 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
83 * pretty high and the returns do not justify the increased costs.
84 *
85 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
86 * increase coverage and consistency always enable it on 64bit platforms.
cc1f4b1f 87 */
2159197d 88#ifdef CONFIG_64BIT
172895e6 89# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
6ecdd749
YD
90# define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
91# define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
cc1f4b1f 92#else
172895e6 93# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
cc1f4b1f
LZ
94# define scale_load(w) (w)
95# define scale_load_down(w) (w)
96#endif
97
6ecdd749 98/*
172895e6
YD
99 * Task weight (visible to users) and its load (invisible to users) have
100 * independent resolution, but they should be well calibrated. We use
101 * scale_load() and scale_load_down(w) to convert between them. The
102 * following must be true:
103 *
104 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
105 *
6ecdd749 106 */
172895e6 107#define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
029632fb 108
332ac17e
DF
109/*
110 * Single value that decides SCHED_DEADLINE internal math precision.
111 * 10 -> just above 1us
112 * 9 -> just above 0.5us
113 */
114#define DL_SCALE (10)
115
029632fb
PZ
116/*
117 * These are the 'tuning knobs' of the scheduler:
029632fb 118 */
029632fb
PZ
119
120/*
121 * single value that denotes runtime == period, ie unlimited time.
122 */
123#define RUNTIME_INF ((u64)~0ULL)
124
20f9cd2a
HA
125static inline int idle_policy(int policy)
126{
127 return policy == SCHED_IDLE;
128}
d50dde5a
DF
129static inline int fair_policy(int policy)
130{
131 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
132}
133
029632fb
PZ
134static inline int rt_policy(int policy)
135{
d50dde5a 136 return policy == SCHED_FIFO || policy == SCHED_RR;
029632fb
PZ
137}
138
aab03e05
DF
139static inline int dl_policy(int policy)
140{
141 return policy == SCHED_DEADLINE;
142}
20f9cd2a
HA
143static inline bool valid_policy(int policy)
144{
145 return idle_policy(policy) || fair_policy(policy) ||
146 rt_policy(policy) || dl_policy(policy);
147}
aab03e05 148
029632fb
PZ
149static inline int task_has_rt_policy(struct task_struct *p)
150{
151 return rt_policy(p->policy);
152}
153
aab03e05
DF
154static inline int task_has_dl_policy(struct task_struct *p)
155{
156 return dl_policy(p->policy);
157}
158
2d3d891d
DF
159/*
160 * Tells if entity @a should preempt entity @b.
161 */
332ac17e
DF
162static inline bool
163dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
2d3d891d
DF
164{
165 return dl_time_before(a->deadline, b->deadline);
166}
167
029632fb
PZ
168/*
169 * This is the priority-queue data structure of the RT scheduling class:
170 */
171struct rt_prio_array {
172 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
173 struct list_head queue[MAX_RT_PRIO];
174};
175
176struct rt_bandwidth {
177 /* nests inside the rq lock: */
178 raw_spinlock_t rt_runtime_lock;
179 ktime_t rt_period;
180 u64 rt_runtime;
181 struct hrtimer rt_period_timer;
4cfafd30 182 unsigned int rt_period_active;
029632fb 183};
a5e7be3b
JL
184
185void __dl_clear_params(struct task_struct *p);
186
332ac17e
DF
187/*
188 * To keep the bandwidth of -deadline tasks and groups under control
189 * we need some place where:
190 * - store the maximum -deadline bandwidth of the system (the group);
191 * - cache the fraction of that bandwidth that is currently allocated.
192 *
193 * This is all done in the data structure below. It is similar to the
194 * one used for RT-throttling (rt_bandwidth), with the main difference
195 * that, since here we are only interested in admission control, we
196 * do not decrease any runtime while the group "executes", neither we
197 * need a timer to replenish it.
198 *
199 * With respect to SMP, the bandwidth is given on a per-CPU basis,
200 * meaning that:
201 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
202 * - dl_total_bw array contains, in the i-eth element, the currently
203 * allocated bandwidth on the i-eth CPU.
204 * Moreover, groups consume bandwidth on each CPU, while tasks only
205 * consume bandwidth on the CPU they're running on.
206 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
207 * that will be shown the next time the proc or cgroup controls will
208 * be red. It on its turn can be changed by writing on its own
209 * control.
210 */
211struct dl_bandwidth {
212 raw_spinlock_t dl_runtime_lock;
213 u64 dl_runtime;
214 u64 dl_period;
215};
216
217static inline int dl_bandwidth_enabled(void)
218{
1724813d 219 return sysctl_sched_rt_runtime >= 0;
332ac17e
DF
220}
221
332ac17e
DF
222struct dl_bw {
223 raw_spinlock_t lock;
224 u64 bw, total_bw;
225};
226
daec5798
LA
227static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
228
7f51412a 229static inline
8c0944ce 230void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
7f51412a
JL
231{
232 dl_b->total_bw -= tsk_bw;
daec5798 233 __dl_update(dl_b, (s32)tsk_bw / cpus);
7f51412a
JL
234}
235
236static inline
daec5798 237void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
7f51412a
JL
238{
239 dl_b->total_bw += tsk_bw;
daec5798 240 __dl_update(dl_b, -((s32)tsk_bw / cpus));
7f51412a
JL
241}
242
243static inline
244bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
245{
246 return dl_b->bw != -1 &&
247 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
248}
249
209a0cbd 250void dl_change_utilization(struct task_struct *p, u64 new_bw);
f2cb1360 251extern void init_dl_bw(struct dl_bw *dl_b);
06a76fe0
NP
252extern int sched_dl_global_validate(void);
253extern void sched_dl_do_global(void);
254extern int sched_dl_overflow(struct task_struct *p, int policy,
255 const struct sched_attr *attr);
256extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
257extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
258extern bool __checkparam_dl(const struct sched_attr *attr);
06a76fe0
NP
259extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
260extern int dl_task_can_attach(struct task_struct *p,
261 const struct cpumask *cs_cpus_allowed);
262extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
263 const struct cpumask *trial);
264extern bool dl_cpu_busy(unsigned int cpu);
029632fb
PZ
265
266#ifdef CONFIG_CGROUP_SCHED
267
268#include <linux/cgroup.h>
269
270struct cfs_rq;
271struct rt_rq;
272
35cf4e50 273extern struct list_head task_groups;
029632fb
PZ
274
275struct cfs_bandwidth {
276#ifdef CONFIG_CFS_BANDWIDTH
277 raw_spinlock_t lock;
278 ktime_t period;
279 u64 quota, runtime;
9c58c79a 280 s64 hierarchical_quota;
029632fb
PZ
281 u64 runtime_expires;
282
4cfafd30 283 int idle, period_active;
029632fb
PZ
284 struct hrtimer period_timer, slack_timer;
285 struct list_head throttled_cfs_rq;
286
287 /* statistics */
288 int nr_periods, nr_throttled;
289 u64 throttled_time;
290#endif
291};
292
293/* task group related information */
294struct task_group {
295 struct cgroup_subsys_state css;
296
297#ifdef CONFIG_FAIR_GROUP_SCHED
298 /* schedulable entities of this group on each cpu */
299 struct sched_entity **se;
300 /* runqueue "owned" by this group on each cpu */
301 struct cfs_rq **cfs_rq;
302 unsigned long shares;
303
fa6bddeb 304#ifdef CONFIG_SMP
b0367629
WL
305 /*
306 * load_avg can be heavily contended at clock tick time, so put
307 * it in its own cacheline separated from the fields above which
308 * will also be accessed at each tick.
309 */
310 atomic_long_t load_avg ____cacheline_aligned;
029632fb 311#endif
fa6bddeb 312#endif
029632fb
PZ
313
314#ifdef CONFIG_RT_GROUP_SCHED
315 struct sched_rt_entity **rt_se;
316 struct rt_rq **rt_rq;
317
318 struct rt_bandwidth rt_bandwidth;
319#endif
320
321 struct rcu_head rcu;
322 struct list_head list;
323
324 struct task_group *parent;
325 struct list_head siblings;
326 struct list_head children;
327
328#ifdef CONFIG_SCHED_AUTOGROUP
329 struct autogroup *autogroup;
330#endif
331
332 struct cfs_bandwidth cfs_bandwidth;
333};
334
335#ifdef CONFIG_FAIR_GROUP_SCHED
336#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
337
338/*
339 * A weight of 0 or 1 can cause arithmetics problems.
340 * A weight of a cfs_rq is the sum of weights of which entities
341 * are queued on this cfs_rq, so a weight of a entity should not be
342 * too large, so as the shares value of a task group.
343 * (The default weight is 1024 - so there's no practical
344 * limitation from this.)
345 */
346#define MIN_SHARES (1UL << 1)
347#define MAX_SHARES (1UL << 18)
348#endif
349
029632fb
PZ
350typedef int (*tg_visitor)(struct task_group *, void *);
351
352extern int walk_tg_tree_from(struct task_group *from,
353 tg_visitor down, tg_visitor up, void *data);
354
355/*
356 * Iterate the full tree, calling @down when first entering a node and @up when
357 * leaving it for the final time.
358 *
359 * Caller must hold rcu_lock or sufficient equivalent.
360 */
361static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
362{
363 return walk_tg_tree_from(&root_task_group, down, up, data);
364}
365
366extern int tg_nop(struct task_group *tg, void *data);
367
368extern void free_fair_sched_group(struct task_group *tg);
369extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
8663e24d 370extern void online_fair_sched_group(struct task_group *tg);
6fe1f348 371extern void unregister_fair_sched_group(struct task_group *tg);
029632fb
PZ
372extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
373 struct sched_entity *se, int cpu,
374 struct sched_entity *parent);
375extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
029632fb
PZ
376
377extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
77a4d1a1 378extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
029632fb
PZ
379extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
380
381extern void free_rt_sched_group(struct task_group *tg);
382extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
383extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
384 struct sched_rt_entity *rt_se, int cpu,
385 struct sched_rt_entity *parent);
8887cd99
NP
386extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
387extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
388extern long sched_group_rt_runtime(struct task_group *tg);
389extern long sched_group_rt_period(struct task_group *tg);
390extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
029632fb 391
25cc7da7
LZ
392extern struct task_group *sched_create_group(struct task_group *parent);
393extern void sched_online_group(struct task_group *tg,
394 struct task_group *parent);
395extern void sched_destroy_group(struct task_group *tg);
396extern void sched_offline_group(struct task_group *tg);
397
398extern void sched_move_task(struct task_struct *tsk);
399
400#ifdef CONFIG_FAIR_GROUP_SCHED
401extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
ad936d86
BP
402
403#ifdef CONFIG_SMP
404extern void set_task_rq_fair(struct sched_entity *se,
405 struct cfs_rq *prev, struct cfs_rq *next);
406#else /* !CONFIG_SMP */
407static inline void set_task_rq_fair(struct sched_entity *se,
408 struct cfs_rq *prev, struct cfs_rq *next) { }
409#endif /* CONFIG_SMP */
410#endif /* CONFIG_FAIR_GROUP_SCHED */
25cc7da7 411
029632fb
PZ
412#else /* CONFIG_CGROUP_SCHED */
413
414struct cfs_bandwidth { };
415
416#endif /* CONFIG_CGROUP_SCHED */
417
418/* CFS-related fields in a runqueue */
419struct cfs_rq {
420 struct load_weight load;
1ea6c46a 421 unsigned long runnable_weight;
c82513e5 422 unsigned int nr_running, h_nr_running;
029632fb
PZ
423
424 u64 exec_clock;
425 u64 min_vruntime;
426#ifndef CONFIG_64BIT
427 u64 min_vruntime_copy;
428#endif
429
bfb06889 430 struct rb_root_cached tasks_timeline;
029632fb 431
029632fb
PZ
432 /*
433 * 'curr' points to currently running entity on this cfs_rq.
434 * It is set to NULL otherwise (i.e when none are currently running).
435 */
436 struct sched_entity *curr, *next, *last, *skip;
437
438#ifdef CONFIG_SCHED_DEBUG
439 unsigned int nr_spread_over;
440#endif
441
2dac754e
PT
442#ifdef CONFIG_SMP
443 /*
9d89c257 444 * CFS load tracking
2dac754e 445 */
9d89c257 446 struct sched_avg avg;
2a2f5d4e
PZ
447#ifndef CONFIG_64BIT
448 u64 load_last_update_time_copy;
9d89c257 449#endif
2a2f5d4e
PZ
450 struct {
451 raw_spinlock_t lock ____cacheline_aligned;
452 int nr;
453 unsigned long load_avg;
454 unsigned long util_avg;
0e2d2aaa 455 unsigned long runnable_sum;
2a2f5d4e 456 } removed;
82958366 457
9d89c257 458#ifdef CONFIG_FAIR_GROUP_SCHED
0e2d2aaa
PZ
459 unsigned long tg_load_avg_contrib;
460 long propagate;
461 long prop_runnable_sum;
462
82958366
PT
463 /*
464 * h_load = weight * f(tg)
465 *
466 * Where f(tg) is the recursive weight fraction assigned to
467 * this group.
468 */
469 unsigned long h_load;
68520796
VD
470 u64 last_h_load_update;
471 struct sched_entity *h_load_next;
472#endif /* CONFIG_FAIR_GROUP_SCHED */
82958366
PT
473#endif /* CONFIG_SMP */
474
029632fb
PZ
475#ifdef CONFIG_FAIR_GROUP_SCHED
476 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
477
478 /*
479 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
480 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
481 * (like users, containers etc.)
482 *
483 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
484 * list is used during load balance.
485 */
486 int on_list;
487 struct list_head leaf_cfs_rq_list;
488 struct task_group *tg; /* group that "owns" this runqueue */
489
029632fb
PZ
490#ifdef CONFIG_CFS_BANDWIDTH
491 int runtime_enabled;
492 u64 runtime_expires;
493 s64 runtime_remaining;
494
f1b17280
PT
495 u64 throttled_clock, throttled_clock_task;
496 u64 throttled_clock_task_time;
55e16d30 497 int throttled, throttle_count;
029632fb
PZ
498 struct list_head throttled_list;
499#endif /* CONFIG_CFS_BANDWIDTH */
500#endif /* CONFIG_FAIR_GROUP_SCHED */
501};
502
503static inline int rt_bandwidth_enabled(void)
504{
505 return sysctl_sched_rt_runtime >= 0;
506}
507
b6366f04 508/* RT IPI pull logic requires IRQ_WORK */
4bdced5c 509#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
b6366f04
SR
510# define HAVE_RT_PUSH_IPI
511#endif
512
029632fb
PZ
513/* Real-Time classes' related field in a runqueue: */
514struct rt_rq {
515 struct rt_prio_array active;
c82513e5 516 unsigned int rt_nr_running;
01d36d0a 517 unsigned int rr_nr_running;
029632fb
PZ
518#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
519 struct {
520 int curr; /* highest queued rt task prio */
521#ifdef CONFIG_SMP
522 int next; /* next highest */
523#endif
524 } highest_prio;
525#endif
526#ifdef CONFIG_SMP
527 unsigned long rt_nr_migratory;
528 unsigned long rt_nr_total;
529 int overloaded;
530 struct plist_head pushable_tasks;
b6366f04 531#endif /* CONFIG_SMP */
f4ebcbc0
KT
532 int rt_queued;
533
029632fb
PZ
534 int rt_throttled;
535 u64 rt_time;
536 u64 rt_runtime;
537 /* Nests inside the rq lock: */
538 raw_spinlock_t rt_runtime_lock;
539
540#ifdef CONFIG_RT_GROUP_SCHED
541 unsigned long rt_nr_boosted;
542
543 struct rq *rq;
029632fb
PZ
544 struct task_group *tg;
545#endif
546};
547
aab03e05
DF
548/* Deadline class' related fields in a runqueue */
549struct dl_rq {
550 /* runqueue is an rbtree, ordered by deadline */
2161573e 551 struct rb_root_cached root;
aab03e05
DF
552
553 unsigned long dl_nr_running;
1baca4ce
JL
554
555#ifdef CONFIG_SMP
556 /*
557 * Deadline values of the currently executing and the
558 * earliest ready task on this rq. Caching these facilitates
559 * the decision wether or not a ready but not running task
560 * should migrate somewhere else.
561 */
562 struct {
563 u64 curr;
564 u64 next;
565 } earliest_dl;
566
567 unsigned long dl_nr_migratory;
1baca4ce
JL
568 int overloaded;
569
570 /*
571 * Tasks on this rq that can be pushed away. They are kept in
572 * an rb-tree, ordered by tasks' deadlines, with caching
573 * of the leftmost (earliest deadline) element.
574 */
2161573e 575 struct rb_root_cached pushable_dl_tasks_root;
332ac17e
DF
576#else
577 struct dl_bw dl_bw;
1baca4ce 578#endif
e36d8677
LA
579 /*
580 * "Active utilization" for this runqueue: increased when a
581 * task wakes up (becomes TASK_RUNNING) and decreased when a
582 * task blocks
583 */
584 u64 running_bw;
4da3abce 585
8fd27231
LA
586 /*
587 * Utilization of the tasks "assigned" to this runqueue (including
588 * the tasks that are in runqueue and the tasks that executed on this
589 * CPU and blocked). Increased when a task moves to this runqueue, and
590 * decreased when the task moves away (migrates, changes scheduling
591 * policy, or terminates).
592 * This is needed to compute the "inactive utilization" for the
593 * runqueue (inactive utilization = this_bw - running_bw).
594 */
595 u64 this_bw;
daec5798 596 u64 extra_bw;
8fd27231 597
4da3abce
LA
598 /*
599 * Inverse of the fraction of CPU utilization that can be reclaimed
600 * by the GRUB algorithm.
601 */
602 u64 bw_ratio;
aab03e05
DF
603};
604
029632fb
PZ
605#ifdef CONFIG_SMP
606
afe06efd
TC
607static inline bool sched_asym_prefer(int a, int b)
608{
609 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
610}
611
029632fb
PZ
612/*
613 * We add the notion of a root-domain which will be used to define per-domain
614 * variables. Each exclusive cpuset essentially defines an island domain by
615 * fully partitioning the member cpus from any other cpuset. Whenever a new
616 * exclusive cpuset is created, we also create and attach a new root-domain
617 * object.
618 *
619 */
620struct root_domain {
621 atomic_t refcount;
622 atomic_t rto_count;
623 struct rcu_head rcu;
624 cpumask_var_t span;
625 cpumask_var_t online;
626
4486edd1
TC
627 /* Indicate more than one runnable task for any CPU */
628 bool overload;
629
1baca4ce
JL
630 /*
631 * The bit corresponding to a CPU gets set here if such CPU has more
632 * than one runnable -deadline task (as it is below for RT tasks).
633 */
634 cpumask_var_t dlo_mask;
635 atomic_t dlo_count;
332ac17e 636 struct dl_bw dl_bw;
6bfd6d72 637 struct cpudl cpudl;
1baca4ce 638
4bdced5c
SRRH
639#ifdef HAVE_RT_PUSH_IPI
640 /*
641 * For IPI pull requests, loop across the rto_mask.
642 */
643 struct irq_work rto_push_work;
644 raw_spinlock_t rto_lock;
645 /* These are only updated and read within rto_lock */
646 int rto_loop;
647 int rto_cpu;
648 /* These atomics are updated outside of a lock */
649 atomic_t rto_loop_next;
650 atomic_t rto_loop_start;
651#endif
029632fb
PZ
652 /*
653 * The "RT overload" flag: it gets set if a CPU has more than
654 * one runnable RT task.
655 */
656 cpumask_var_t rto_mask;
657 struct cpupri cpupri;
cd92bfd3
DE
658
659 unsigned long max_cpu_capacity;
029632fb
PZ
660};
661
662extern struct root_domain def_root_domain;
f2cb1360 663extern struct mutex sched_domains_mutex;
f2cb1360
IM
664
665extern void init_defrootdomain(void);
8d5dc512 666extern int sched_init_domains(const struct cpumask *cpu_map);
f2cb1360 667extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
029632fb 668
4bdced5c
SRRH
669#ifdef HAVE_RT_PUSH_IPI
670extern void rto_push_irq_work_func(struct irq_work *work);
671#endif
029632fb
PZ
672#endif /* CONFIG_SMP */
673
674/*
675 * This is the main, per-CPU runqueue data structure.
676 *
677 * Locking rule: those places that want to lock multiple runqueues
678 * (such as the load balancing or the thread migration code), lock
679 * acquire operations must be ordered by ascending &runqueue.
680 */
681struct rq {
682 /* runqueue lock: */
683 raw_spinlock_t lock;
684
685 /*
686 * nr_running and cpu_load should be in the same cacheline because
687 * remote CPUs use both these fields when doing load calculation.
688 */
c82513e5 689 unsigned int nr_running;
0ec8aa00
PZ
690#ifdef CONFIG_NUMA_BALANCING
691 unsigned int nr_numa_running;
692 unsigned int nr_preferred_running;
693#endif
029632fb
PZ
694 #define CPU_LOAD_IDX_MAX 5
695 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
3451d024 696#ifdef CONFIG_NO_HZ_COMMON
9fd81dd5
FW
697#ifdef CONFIG_SMP
698 unsigned long last_load_update_tick;
699#endif /* CONFIG_SMP */
1c792db7 700 unsigned long nohz_flags;
9fd81dd5 701#endif /* CONFIG_NO_HZ_COMMON */
265f22a9
FW
702#ifdef CONFIG_NO_HZ_FULL
703 unsigned long last_sched_tick;
029632fb 704#endif
029632fb
PZ
705 /* capture load from *all* tasks on this cpu: */
706 struct load_weight load;
707 unsigned long nr_load_updates;
708 u64 nr_switches;
709
710 struct cfs_rq cfs;
711 struct rt_rq rt;
aab03e05 712 struct dl_rq dl;
029632fb
PZ
713
714#ifdef CONFIG_FAIR_GROUP_SCHED
715 /* list of leaf cfs_rq on this cpu: */
716 struct list_head leaf_cfs_rq_list;
9c2791f9 717 struct list_head *tmp_alone_branch;
a35b6466
PZ
718#endif /* CONFIG_FAIR_GROUP_SCHED */
719
029632fb
PZ
720 /*
721 * This is part of a global counter where only the total sum
722 * over all CPUs matters. A task can increase this counter on
723 * one CPU and if it got migrated afterwards it may decrease
724 * it on another CPU. Always updated under the runqueue lock:
725 */
726 unsigned long nr_uninterruptible;
727
728 struct task_struct *curr, *idle, *stop;
729 unsigned long next_balance;
730 struct mm_struct *prev_mm;
731
cb42c9a3 732 unsigned int clock_update_flags;
029632fb
PZ
733 u64 clock;
734 u64 clock_task;
735
736 atomic_t nr_iowait;
737
738#ifdef CONFIG_SMP
739 struct root_domain *rd;
740 struct sched_domain *sd;
741
ced549fa 742 unsigned long cpu_capacity;
ca6d75e6 743 unsigned long cpu_capacity_orig;
029632fb 744
e3fca9e7
PZ
745 struct callback_head *balance_callback;
746
029632fb
PZ
747 unsigned char idle_balance;
748 /* For active balancing */
029632fb
PZ
749 int active_balance;
750 int push_cpu;
751 struct cpu_stop_work active_balance_work;
752 /* cpu of this runqueue: */
753 int cpu;
754 int online;
755
367456c7
PZ
756 struct list_head cfs_tasks;
757
029632fb
PZ
758 u64 rt_avg;
759 u64 age_stamp;
760 u64 idle_stamp;
761 u64 avg_idle;
9bd721c5
JL
762
763 /* This is used to determine avg_idle's max value */
764 u64 max_idle_balance_cost;
029632fb
PZ
765#endif
766
767#ifdef CONFIG_IRQ_TIME_ACCOUNTING
768 u64 prev_irq_time;
769#endif
770#ifdef CONFIG_PARAVIRT
771 u64 prev_steal_time;
772#endif
773#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
774 u64 prev_steal_time_rq;
775#endif
776
777 /* calc_load related fields */
778 unsigned long calc_load_update;
779 long calc_load_active;
780
781#ifdef CONFIG_SCHED_HRTICK
782#ifdef CONFIG_SMP
783 int hrtick_csd_pending;
966a9671 784 call_single_data_t hrtick_csd;
029632fb
PZ
785#endif
786 struct hrtimer hrtick_timer;
787#endif
788
789#ifdef CONFIG_SCHEDSTATS
790 /* latency stats */
791 struct sched_info rq_sched_info;
792 unsigned long long rq_cpu_time;
793 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
794
795 /* sys_sched_yield() stats */
796 unsigned int yld_count;
797
798 /* schedule() stats */
029632fb
PZ
799 unsigned int sched_count;
800 unsigned int sched_goidle;
801
802 /* try_to_wake_up() stats */
803 unsigned int ttwu_count;
804 unsigned int ttwu_local;
805#endif
806
807#ifdef CONFIG_SMP
808 struct llist_head wake_list;
809#endif
442bf3aa
DL
810
811#ifdef CONFIG_CPU_IDLE
812 /* Must be inspected within a rcu lock section */
813 struct cpuidle_state *idle_state;
814#endif
029632fb
PZ
815};
816
817static inline int cpu_of(struct rq *rq)
818{
819#ifdef CONFIG_SMP
820 return rq->cpu;
821#else
822 return 0;
823#endif
824}
825
1b568f0a
PZ
826
827#ifdef CONFIG_SCHED_SMT
828
829extern struct static_key_false sched_smt_present;
830
831extern void __update_idle_core(struct rq *rq);
832
833static inline void update_idle_core(struct rq *rq)
834{
835 if (static_branch_unlikely(&sched_smt_present))
836 __update_idle_core(rq);
837}
838
839#else
840static inline void update_idle_core(struct rq *rq) { }
841#endif
842
8b06c55b 843DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
029632fb 844
518cd623 845#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
4a32fea9 846#define this_rq() this_cpu_ptr(&runqueues)
518cd623
PZ
847#define task_rq(p) cpu_rq(task_cpu(p))
848#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
4a32fea9 849#define raw_rq() raw_cpu_ptr(&runqueues)
518cd623 850
cebde6d6
PZ
851static inline u64 __rq_clock_broken(struct rq *rq)
852{
316c1608 853 return READ_ONCE(rq->clock);
cebde6d6
PZ
854}
855
cb42c9a3
MF
856/*
857 * rq::clock_update_flags bits
858 *
859 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
860 * call to __schedule(). This is an optimisation to avoid
861 * neighbouring rq clock updates.
862 *
863 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
864 * in effect and calls to update_rq_clock() are being ignored.
865 *
866 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
867 * made to update_rq_clock() since the last time rq::lock was pinned.
868 *
869 * If inside of __schedule(), clock_update_flags will have been
870 * shifted left (a left shift is a cheap operation for the fast path
871 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
872 *
873 * if (rq-clock_update_flags >= RQCF_UPDATED)
874 *
875 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
876 * one position though, because the next rq_unpin_lock() will shift it
877 * back.
878 */
879#define RQCF_REQ_SKIP 0x01
880#define RQCF_ACT_SKIP 0x02
881#define RQCF_UPDATED 0x04
882
883static inline void assert_clock_updated(struct rq *rq)
884{
885 /*
886 * The only reason for not seeing a clock update since the
887 * last rq_pin_lock() is if we're currently skipping updates.
888 */
889 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
890}
891
78becc27
FW
892static inline u64 rq_clock(struct rq *rq)
893{
cebde6d6 894 lockdep_assert_held(&rq->lock);
cb42c9a3
MF
895 assert_clock_updated(rq);
896
78becc27
FW
897 return rq->clock;
898}
899
900static inline u64 rq_clock_task(struct rq *rq)
901{
cebde6d6 902 lockdep_assert_held(&rq->lock);
cb42c9a3
MF
903 assert_clock_updated(rq);
904
78becc27
FW
905 return rq->clock_task;
906}
907
9edfbfed
PZ
908static inline void rq_clock_skip_update(struct rq *rq, bool skip)
909{
910 lockdep_assert_held(&rq->lock);
911 if (skip)
cb42c9a3 912 rq->clock_update_flags |= RQCF_REQ_SKIP;
9edfbfed 913 else
cb42c9a3 914 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
9edfbfed
PZ
915}
916
d8ac8971
MF
917struct rq_flags {
918 unsigned long flags;
919 struct pin_cookie cookie;
cb42c9a3
MF
920#ifdef CONFIG_SCHED_DEBUG
921 /*
922 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
923 * current pin context is stashed here in case it needs to be
924 * restored in rq_repin_lock().
925 */
926 unsigned int clock_update_flags;
927#endif
d8ac8971
MF
928};
929
930static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
931{
932 rf->cookie = lockdep_pin_lock(&rq->lock);
cb42c9a3
MF
933
934#ifdef CONFIG_SCHED_DEBUG
935 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
936 rf->clock_update_flags = 0;
937#endif
d8ac8971
MF
938}
939
940static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
941{
cb42c9a3
MF
942#ifdef CONFIG_SCHED_DEBUG
943 if (rq->clock_update_flags > RQCF_ACT_SKIP)
944 rf->clock_update_flags = RQCF_UPDATED;
945#endif
946
d8ac8971
MF
947 lockdep_unpin_lock(&rq->lock, rf->cookie);
948}
949
950static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
951{
952 lockdep_repin_lock(&rq->lock, rf->cookie);
cb42c9a3
MF
953
954#ifdef CONFIG_SCHED_DEBUG
955 /*
956 * Restore the value we stashed in @rf for this pin context.
957 */
958 rq->clock_update_flags |= rf->clock_update_flags;
959#endif
d8ac8971
MF
960}
961
9942f79b 962#ifdef CONFIG_NUMA
e3fe70b1
RR
963enum numa_topology_type {
964 NUMA_DIRECT,
965 NUMA_GLUELESS_MESH,
966 NUMA_BACKPLANE,
967};
968extern enum numa_topology_type sched_numa_topology_type;
9942f79b
RR
969extern int sched_max_numa_distance;
970extern bool find_numa_distance(int distance);
971#endif
972
f2cb1360
IM
973#ifdef CONFIG_NUMA
974extern void sched_init_numa(void);
975extern void sched_domains_numa_masks_set(unsigned int cpu);
976extern void sched_domains_numa_masks_clear(unsigned int cpu);
977#else
978static inline void sched_init_numa(void) { }
979static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
980static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
981#endif
982
f809ca9a 983#ifdef CONFIG_NUMA_BALANCING
44dba3d5
IM
984/* The regions in numa_faults array from task_struct */
985enum numa_faults_stats {
986 NUMA_MEM = 0,
987 NUMA_CPU,
988 NUMA_MEMBUF,
989 NUMA_CPUBUF
990};
0ec8aa00 991extern void sched_setnuma(struct task_struct *p, int node);
e6628d5b 992extern int migrate_task_to(struct task_struct *p, int cpu);
ac66f547 993extern int migrate_swap(struct task_struct *, struct task_struct *);
f809ca9a
MG
994#endif /* CONFIG_NUMA_BALANCING */
995
518cd623
PZ
996#ifdef CONFIG_SMP
997
e3fca9e7
PZ
998static inline void
999queue_balance_callback(struct rq *rq,
1000 struct callback_head *head,
1001 void (*func)(struct rq *rq))
1002{
1003 lockdep_assert_held(&rq->lock);
1004
1005 if (unlikely(head->next))
1006 return;
1007
1008 head->func = (void (*)(struct callback_head *))func;
1009 head->next = rq->balance_callback;
1010 rq->balance_callback = head;
1011}
1012
e3baac47
PZ
1013extern void sched_ttwu_pending(void);
1014
029632fb
PZ
1015#define rcu_dereference_check_sched_domain(p) \
1016 rcu_dereference_check((p), \
1017 lockdep_is_held(&sched_domains_mutex))
1018
1019/*
1020 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1021 * See detach_destroy_domains: synchronize_sched for details.
1022 *
1023 * The domain tree of any CPU may only be accessed from within
1024 * preempt-disabled sections.
1025 */
1026#define for_each_domain(cpu, __sd) \
518cd623
PZ
1027 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1028 __sd; __sd = __sd->parent)
029632fb 1029
77e81365
SS
1030#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1031
518cd623
PZ
1032/**
1033 * highest_flag_domain - Return highest sched_domain containing flag.
1034 * @cpu: The cpu whose highest level of sched domain is to
1035 * be returned.
1036 * @flag: The flag to check for the highest sched_domain
1037 * for the given cpu.
1038 *
1039 * Returns the highest sched_domain of a cpu which contains the given flag.
1040 */
1041static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1042{
1043 struct sched_domain *sd, *hsd = NULL;
1044
1045 for_each_domain(cpu, sd) {
1046 if (!(sd->flags & flag))
1047 break;
1048 hsd = sd;
1049 }
1050
1051 return hsd;
1052}
1053
fb13c7ee
MG
1054static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1055{
1056 struct sched_domain *sd;
1057
1058 for_each_domain(cpu, sd) {
1059 if (sd->flags & flag)
1060 break;
1061 }
1062
1063 return sd;
1064}
1065
518cd623 1066DECLARE_PER_CPU(struct sched_domain *, sd_llc);
7d9ffa89 1067DECLARE_PER_CPU(int, sd_llc_size);
518cd623 1068DECLARE_PER_CPU(int, sd_llc_id);
0e369d75 1069DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
fb13c7ee 1070DECLARE_PER_CPU(struct sched_domain *, sd_numa);
37dc6b50 1071DECLARE_PER_CPU(struct sched_domain *, sd_asym);
518cd623 1072
63b2ca30 1073struct sched_group_capacity {
5e6521ea
LZ
1074 atomic_t ref;
1075 /*
172895e6 1076 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
63b2ca30 1077 * for a single CPU.
5e6521ea 1078 */
bf475ce0
MR
1079 unsigned long capacity;
1080 unsigned long min_capacity; /* Min per-CPU capacity in group */
5e6521ea 1081 unsigned long next_update;
63b2ca30 1082 int imbalance; /* XXX unrelated to capacity but shared group state */
5e6521ea 1083
005f874d
PZ
1084#ifdef CONFIG_SCHED_DEBUG
1085 int id;
1086#endif
1087
e5c14b1f 1088 unsigned long cpumask[0]; /* balance mask */
5e6521ea
LZ
1089};
1090
1091struct sched_group {
1092 struct sched_group *next; /* Must be a circular list */
1093 atomic_t ref;
1094
1095 unsigned int group_weight;
63b2ca30 1096 struct sched_group_capacity *sgc;
afe06efd 1097 int asym_prefer_cpu; /* cpu of highest priority in group */
5e6521ea
LZ
1098
1099 /*
1100 * The CPUs this group covers.
1101 *
1102 * NOTE: this field is variable length. (Allocated dynamically
1103 * by attaching extra space to the end of the structure,
1104 * depending on how many CPUs the kernel has booted up with)
1105 */
1106 unsigned long cpumask[0];
1107};
1108
ae4df9d6 1109static inline struct cpumask *sched_group_span(struct sched_group *sg)
5e6521ea
LZ
1110{
1111 return to_cpumask(sg->cpumask);
1112}
1113
1114/*
e5c14b1f 1115 * See build_balance_mask().
5e6521ea 1116 */
e5c14b1f 1117static inline struct cpumask *group_balance_mask(struct sched_group *sg)
5e6521ea 1118{
63b2ca30 1119 return to_cpumask(sg->sgc->cpumask);
5e6521ea
LZ
1120}
1121
1122/**
1123 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1124 * @group: The group whose first cpu is to be returned.
1125 */
1126static inline unsigned int group_first_cpu(struct sched_group *group)
1127{
ae4df9d6 1128 return cpumask_first(sched_group_span(group));
5e6521ea
LZ
1129}
1130
c1174876
PZ
1131extern int group_balance_cpu(struct sched_group *sg);
1132
3866e845
SRRH
1133#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1134void register_sched_domain_sysctl(void);
bbdacdfe 1135void dirty_sched_domain_sysctl(int cpu);
3866e845
SRRH
1136void unregister_sched_domain_sysctl(void);
1137#else
1138static inline void register_sched_domain_sysctl(void)
1139{
1140}
bbdacdfe
PZ
1141static inline void dirty_sched_domain_sysctl(int cpu)
1142{
1143}
3866e845
SRRH
1144static inline void unregister_sched_domain_sysctl(void)
1145{
1146}
1147#endif
1148
e3baac47
PZ
1149#else
1150
1151static inline void sched_ttwu_pending(void) { }
1152
518cd623 1153#endif /* CONFIG_SMP */
029632fb 1154
391e43da 1155#include "stats.h"
1051408f 1156#include "autogroup.h"
029632fb
PZ
1157
1158#ifdef CONFIG_CGROUP_SCHED
1159
1160/*
1161 * Return the group to which this tasks belongs.
1162 *
8af01f56
TH
1163 * We cannot use task_css() and friends because the cgroup subsystem
1164 * changes that value before the cgroup_subsys::attach() method is called,
1165 * therefore we cannot pin it and might observe the wrong value.
8323f26c
PZ
1166 *
1167 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1168 * core changes this before calling sched_move_task().
1169 *
1170 * Instead we use a 'copy' which is updated from sched_move_task() while
1171 * holding both task_struct::pi_lock and rq::lock.
029632fb
PZ
1172 */
1173static inline struct task_group *task_group(struct task_struct *p)
1174{
8323f26c 1175 return p->sched_task_group;
029632fb
PZ
1176}
1177
1178/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1179static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1180{
1181#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1182 struct task_group *tg = task_group(p);
1183#endif
1184
1185#ifdef CONFIG_FAIR_GROUP_SCHED
ad936d86 1186 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
029632fb
PZ
1187 p->se.cfs_rq = tg->cfs_rq[cpu];
1188 p->se.parent = tg->se[cpu];
1189#endif
1190
1191#ifdef CONFIG_RT_GROUP_SCHED
1192 p->rt.rt_rq = tg->rt_rq[cpu];
1193 p->rt.parent = tg->rt_se[cpu];
1194#endif
1195}
1196
1197#else /* CONFIG_CGROUP_SCHED */
1198
1199static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1200static inline struct task_group *task_group(struct task_struct *p)
1201{
1202 return NULL;
1203}
1204
1205#endif /* CONFIG_CGROUP_SCHED */
1206
1207static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1208{
1209 set_task_rq(p, cpu);
1210#ifdef CONFIG_SMP
1211 /*
1212 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1213 * successfuly executed on another CPU. We must ensure that updates of
1214 * per-task data have been completed by this moment.
1215 */
1216 smp_wmb();
c65eacbe
AL
1217#ifdef CONFIG_THREAD_INFO_IN_TASK
1218 p->cpu = cpu;
1219#else
029632fb 1220 task_thread_info(p)->cpu = cpu;
c65eacbe 1221#endif
ac66f547 1222 p->wake_cpu = cpu;
029632fb
PZ
1223#endif
1224}
1225
1226/*
1227 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1228 */
1229#ifdef CONFIG_SCHED_DEBUG
c5905afb 1230# include <linux/static_key.h>
029632fb
PZ
1231# define const_debug __read_mostly
1232#else
1233# define const_debug const
1234#endif
1235
029632fb
PZ
1236#define SCHED_FEAT(name, enabled) \
1237 __SCHED_FEAT_##name ,
1238
1239enum {
391e43da 1240#include "features.h"
f8b6d1cc 1241 __SCHED_FEAT_NR,
029632fb
PZ
1242};
1243
1244#undef SCHED_FEAT
1245
f8b6d1cc 1246#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
765cc3a4
PB
1247
1248/*
1249 * To support run-time toggling of sched features, all the translation units
1250 * (but core.c) reference the sysctl_sched_features defined in core.c.
1251 */
1252extern const_debug unsigned int sysctl_sched_features;
1253
f8b6d1cc 1254#define SCHED_FEAT(name, enabled) \
c5905afb 1255static __always_inline bool static_branch_##name(struct static_key *key) \
f8b6d1cc 1256{ \
6e76ea8a 1257 return static_key_##enabled(key); \
f8b6d1cc
PZ
1258}
1259
1260#include "features.h"
f8b6d1cc
PZ
1261#undef SCHED_FEAT
1262
c5905afb 1263extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
f8b6d1cc 1264#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
765cc3a4 1265
f8b6d1cc 1266#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
765cc3a4
PB
1267
1268/*
1269 * Each translation unit has its own copy of sysctl_sched_features to allow
1270 * constants propagation at compile time and compiler optimization based on
1271 * features default.
1272 */
1273#define SCHED_FEAT(name, enabled) \
1274 (1UL << __SCHED_FEAT_##name) * enabled |
1275static const_debug __maybe_unused unsigned int sysctl_sched_features =
1276#include "features.h"
1277 0;
1278#undef SCHED_FEAT
1279
029632fb 1280#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
765cc3a4 1281
f8b6d1cc 1282#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
029632fb 1283
2a595721 1284extern struct static_key_false sched_numa_balancing;
cb251765 1285extern struct static_key_false sched_schedstats;
cbee9f88 1286
029632fb
PZ
1287static inline u64 global_rt_period(void)
1288{
1289 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1290}
1291
1292static inline u64 global_rt_runtime(void)
1293{
1294 if (sysctl_sched_rt_runtime < 0)
1295 return RUNTIME_INF;
1296
1297 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1298}
1299
029632fb
PZ
1300static inline int task_current(struct rq *rq, struct task_struct *p)
1301{
1302 return rq->curr == p;
1303}
1304
1305static inline int task_running(struct rq *rq, struct task_struct *p)
1306{
1307#ifdef CONFIG_SMP
1308 return p->on_cpu;
1309#else
1310 return task_current(rq, p);
1311#endif
1312}
1313
da0c1e65
KT
1314static inline int task_on_rq_queued(struct task_struct *p)
1315{
1316 return p->on_rq == TASK_ON_RQ_QUEUED;
1317}
029632fb 1318
cca26e80
KT
1319static inline int task_on_rq_migrating(struct task_struct *p)
1320{
1321 return p->on_rq == TASK_ON_RQ_MIGRATING;
1322}
1323
029632fb
PZ
1324#ifndef prepare_arch_switch
1325# define prepare_arch_switch(next) do { } while (0)
1326#endif
01f23e16
CM
1327#ifndef finish_arch_post_lock_switch
1328# define finish_arch_post_lock_switch() do { } while (0)
1329#endif
029632fb 1330
b13095f0
LZ
1331/*
1332 * wake flags
1333 */
1334#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1335#define WF_FORK 0x02 /* child wakeup after fork */
1336#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1337
029632fb
PZ
1338/*
1339 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1340 * of tasks with abnormal "nice" values across CPUs the contribution that
1341 * each task makes to its run queue's load is weighted according to its
1342 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1343 * scaled version of the new time slice allocation that they receive on time
1344 * slice expiry etc.
1345 */
1346
1347#define WEIGHT_IDLEPRIO 3
1348#define WMULT_IDLEPRIO 1431655765
1349
ed82b8a1
AK
1350extern const int sched_prio_to_weight[40];
1351extern const u32 sched_prio_to_wmult[40];
029632fb 1352
ff77e468
PZ
1353/*
1354 * {de,en}queue flags:
1355 *
1356 * DEQUEUE_SLEEP - task is no longer runnable
1357 * ENQUEUE_WAKEUP - task just became runnable
1358 *
1359 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1360 * are in a known state which allows modification. Such pairs
1361 * should preserve as much state as possible.
1362 *
1363 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1364 * in the runqueue.
1365 *
1366 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1367 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
59efa0ba 1368 * ENQUEUE_MIGRATED - the task was migrated during wakeup
ff77e468
PZ
1369 *
1370 */
1371
1372#define DEQUEUE_SLEEP 0x01
1373#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1374#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
0a67d1ee 1375#define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */
ff77e468 1376
1de64443 1377#define ENQUEUE_WAKEUP 0x01
ff77e468
PZ
1378#define ENQUEUE_RESTORE 0x02
1379#define ENQUEUE_MOVE 0x04
0a67d1ee 1380#define ENQUEUE_NOCLOCK 0x08
ff77e468 1381
0a67d1ee
PZ
1382#define ENQUEUE_HEAD 0x10
1383#define ENQUEUE_REPLENISH 0x20
c82ba9fa 1384#ifdef CONFIG_SMP
0a67d1ee 1385#define ENQUEUE_MIGRATED 0x40
c82ba9fa 1386#else
59efa0ba 1387#define ENQUEUE_MIGRATED 0x00
c82ba9fa 1388#endif
c82ba9fa 1389
37e117c0
PZ
1390#define RETRY_TASK ((void *)-1UL)
1391
c82ba9fa
LZ
1392struct sched_class {
1393 const struct sched_class *next;
1394
1395 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1396 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1397 void (*yield_task) (struct rq *rq);
1398 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1399
1400 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1401
606dba2e
PZ
1402 /*
1403 * It is the responsibility of the pick_next_task() method that will
1404 * return the next task to call put_prev_task() on the @prev task or
1405 * something equivalent.
37e117c0
PZ
1406 *
1407 * May return RETRY_TASK when it finds a higher prio class has runnable
1408 * tasks.
606dba2e
PZ
1409 */
1410 struct task_struct * (*pick_next_task) (struct rq *rq,
e7904a28 1411 struct task_struct *prev,
d8ac8971 1412 struct rq_flags *rf);
c82ba9fa
LZ
1413 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1414
1415#ifdef CONFIG_SMP
ac66f547 1416 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
5a4fd036 1417 void (*migrate_task_rq)(struct task_struct *p);
c82ba9fa 1418
c82ba9fa
LZ
1419 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1420
1421 void (*set_cpus_allowed)(struct task_struct *p,
1422 const struct cpumask *newmask);
1423
1424 void (*rq_online)(struct rq *rq);
1425 void (*rq_offline)(struct rq *rq);
1426#endif
1427
1428 void (*set_curr_task) (struct rq *rq);
1429 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1430 void (*task_fork) (struct task_struct *p);
e6c390f2 1431 void (*task_dead) (struct task_struct *p);
c82ba9fa 1432
67dfa1b7
KT
1433 /*
1434 * The switched_from() call is allowed to drop rq->lock, therefore we
1435 * cannot assume the switched_from/switched_to pair is serliazed by
1436 * rq->lock. They are however serialized by p->pi_lock.
1437 */
c82ba9fa
LZ
1438 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1439 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1440 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1441 int oldprio);
1442
1443 unsigned int (*get_rr_interval) (struct rq *rq,
1444 struct task_struct *task);
1445
6e998916
SG
1446 void (*update_curr) (struct rq *rq);
1447
ea86cb4b
VG
1448#define TASK_SET_GROUP 0
1449#define TASK_MOVE_GROUP 1
1450
c82ba9fa 1451#ifdef CONFIG_FAIR_GROUP_SCHED
ea86cb4b 1452 void (*task_change_group) (struct task_struct *p, int type);
c82ba9fa
LZ
1453#endif
1454};
029632fb 1455
3f1d2a31
PZ
1456static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1457{
1458 prev->sched_class->put_prev_task(rq, prev);
1459}
1460
b2bf6c31
PZ
1461static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1462{
1463 curr->sched_class->set_curr_task(rq);
1464}
1465
f5832c19 1466#ifdef CONFIG_SMP
029632fb 1467#define sched_class_highest (&stop_sched_class)
f5832c19
NP
1468#else
1469#define sched_class_highest (&dl_sched_class)
1470#endif
029632fb
PZ
1471#define for_each_class(class) \
1472 for (class = sched_class_highest; class; class = class->next)
1473
1474extern const struct sched_class stop_sched_class;
aab03e05 1475extern const struct sched_class dl_sched_class;
029632fb
PZ
1476extern const struct sched_class rt_sched_class;
1477extern const struct sched_class fair_sched_class;
1478extern const struct sched_class idle_sched_class;
1479
1480
1481#ifdef CONFIG_SMP
1482
63b2ca30 1483extern void update_group_capacity(struct sched_domain *sd, int cpu);
b719203b 1484
7caff66f 1485extern void trigger_load_balance(struct rq *rq);
029632fb 1486
c5b28038
PZ
1487extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1488
029632fb
PZ
1489#endif
1490
442bf3aa
DL
1491#ifdef CONFIG_CPU_IDLE
1492static inline void idle_set_state(struct rq *rq,
1493 struct cpuidle_state *idle_state)
1494{
1495 rq->idle_state = idle_state;
1496}
1497
1498static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1499{
9148a3a1 1500 SCHED_WARN_ON(!rcu_read_lock_held());
442bf3aa
DL
1501 return rq->idle_state;
1502}
1503#else
1504static inline void idle_set_state(struct rq *rq,
1505 struct cpuidle_state *idle_state)
1506{
1507}
1508
1509static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1510{
1511 return NULL;
1512}
1513#endif
1514
8663effb
SRV
1515extern void schedule_idle(void);
1516
029632fb
PZ
1517extern void sysrq_sched_debug_show(void);
1518extern void sched_init_granularity(void);
1519extern void update_max_interval(void);
1baca4ce
JL
1520
1521extern void init_sched_dl_class(void);
029632fb
PZ
1522extern void init_sched_rt_class(void);
1523extern void init_sched_fair_class(void);
1524
9059393e
VG
1525extern void reweight_task(struct task_struct *p, int prio);
1526
8875125e 1527extern void resched_curr(struct rq *rq);
029632fb
PZ
1528extern void resched_cpu(int cpu);
1529
1530extern struct rt_bandwidth def_rt_bandwidth;
1531extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1532
332ac17e
DF
1533extern struct dl_bandwidth def_dl_bandwidth;
1534extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
aab03e05 1535extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
209a0cbd 1536extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
4da3abce 1537extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
aab03e05 1538
c52f14d3
LA
1539#define BW_SHIFT 20
1540#define BW_UNIT (1 << BW_SHIFT)
4da3abce 1541#define RATIO_SHIFT 8
332ac17e
DF
1542unsigned long to_ratio(u64 period, u64 runtime);
1543
540247fb 1544extern void init_entity_runnable_average(struct sched_entity *se);
2b8c41da 1545extern void post_init_entity_util_avg(struct sched_entity *se);
a75cdaa9 1546
76d92ac3
FW
1547#ifdef CONFIG_NO_HZ_FULL
1548extern bool sched_can_stop_tick(struct rq *rq);
1549
1550/*
1551 * Tick may be needed by tasks in the runqueue depending on their policy and
1552 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1553 * nohz mode if necessary.
1554 */
1555static inline void sched_update_tick_dependency(struct rq *rq)
1556{
1557 int cpu;
1558
1559 if (!tick_nohz_full_enabled())
1560 return;
1561
1562 cpu = cpu_of(rq);
1563
1564 if (!tick_nohz_full_cpu(cpu))
1565 return;
1566
1567 if (sched_can_stop_tick(rq))
1568 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1569 else
1570 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1571}
1572#else
1573static inline void sched_update_tick_dependency(struct rq *rq) { }
1574#endif
1575
72465447 1576static inline void add_nr_running(struct rq *rq, unsigned count)
029632fb 1577{
72465447
KT
1578 unsigned prev_nr = rq->nr_running;
1579
1580 rq->nr_running = prev_nr + count;
9f3660c2 1581
72465447 1582 if (prev_nr < 2 && rq->nr_running >= 2) {
4486edd1
TC
1583#ifdef CONFIG_SMP
1584 if (!rq->rd->overload)
1585 rq->rd->overload = true;
1586#endif
4486edd1 1587 }
76d92ac3
FW
1588
1589 sched_update_tick_dependency(rq);
029632fb
PZ
1590}
1591
72465447 1592static inline void sub_nr_running(struct rq *rq, unsigned count)
029632fb 1593{
72465447 1594 rq->nr_running -= count;
76d92ac3
FW
1595 /* Check if we still need preemption */
1596 sched_update_tick_dependency(rq);
029632fb
PZ
1597}
1598
265f22a9
FW
1599static inline void rq_last_tick_reset(struct rq *rq)
1600{
1601#ifdef CONFIG_NO_HZ_FULL
1602 rq->last_sched_tick = jiffies;
1603#endif
1604}
1605
029632fb
PZ
1606extern void update_rq_clock(struct rq *rq);
1607
1608extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1609extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1610
1611extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1612
1613extern const_debug unsigned int sysctl_sched_time_avg;
1614extern const_debug unsigned int sysctl_sched_nr_migrate;
1615extern const_debug unsigned int sysctl_sched_migration_cost;
1616
1617static inline u64 sched_avg_period(void)
1618{
1619 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1620}
1621
029632fb
PZ
1622#ifdef CONFIG_SCHED_HRTICK
1623
1624/*
1625 * Use hrtick when:
1626 * - enabled by features
1627 * - hrtimer is actually high res
1628 */
1629static inline int hrtick_enabled(struct rq *rq)
1630{
1631 if (!sched_feat(HRTICK))
1632 return 0;
1633 if (!cpu_active(cpu_of(rq)))
1634 return 0;
1635 return hrtimer_is_hres_active(&rq->hrtick_timer);
1636}
1637
1638void hrtick_start(struct rq *rq, u64 delay);
1639
b39e66ea
MG
1640#else
1641
1642static inline int hrtick_enabled(struct rq *rq)
1643{
1644 return 0;
1645}
1646
029632fb
PZ
1647#endif /* CONFIG_SCHED_HRTICK */
1648
1649#ifdef CONFIG_SMP
1650extern void sched_avg_update(struct rq *rq);
dfbca41f
PZ
1651
1652#ifndef arch_scale_freq_capacity
1653static __always_inline
1654unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1655{
1656 return SCHED_CAPACITY_SCALE;
1657}
1658#endif
b5b4860d 1659
8cd5601c
MR
1660#ifndef arch_scale_cpu_capacity
1661static __always_inline
1662unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1663{
e3279a2e 1664 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
8cd5601c
MR
1665 return sd->smt_gain / sd->span_weight;
1666
1667 return SCHED_CAPACITY_SCALE;
1668}
1669#endif
1670
029632fb
PZ
1671static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1672{
b5b4860d 1673 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
029632fb
PZ
1674 sched_avg_update(rq);
1675}
1676#else
1677static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1678static inline void sched_avg_update(struct rq *rq) { }
1679#endif
1680
eb580751 1681struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
3e71a462 1682 __acquires(rq->lock);
8a8c69c3 1683
eb580751 1684struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
3960c8c0 1685 __acquires(p->pi_lock)
3e71a462 1686 __acquires(rq->lock);
3960c8c0 1687
eb580751 1688static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
3960c8c0
PZ
1689 __releases(rq->lock)
1690{
d8ac8971 1691 rq_unpin_lock(rq, rf);
3960c8c0
PZ
1692 raw_spin_unlock(&rq->lock);
1693}
1694
1695static inline void
eb580751 1696task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
3960c8c0
PZ
1697 __releases(rq->lock)
1698 __releases(p->pi_lock)
1699{
d8ac8971 1700 rq_unpin_lock(rq, rf);
3960c8c0 1701 raw_spin_unlock(&rq->lock);
eb580751 1702 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
3960c8c0
PZ
1703}
1704
8a8c69c3
PZ
1705static inline void
1706rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1707 __acquires(rq->lock)
1708{
1709 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1710 rq_pin_lock(rq, rf);
1711}
1712
1713static inline void
1714rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1715 __acquires(rq->lock)
1716{
1717 raw_spin_lock_irq(&rq->lock);
1718 rq_pin_lock(rq, rf);
1719}
1720
1721static inline void
1722rq_lock(struct rq *rq, struct rq_flags *rf)
1723 __acquires(rq->lock)
1724{
1725 raw_spin_lock(&rq->lock);
1726 rq_pin_lock(rq, rf);
1727}
1728
1729static inline void
1730rq_relock(struct rq *rq, struct rq_flags *rf)
1731 __acquires(rq->lock)
1732{
1733 raw_spin_lock(&rq->lock);
1734 rq_repin_lock(rq, rf);
1735}
1736
1737static inline void
1738rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1739 __releases(rq->lock)
1740{
1741 rq_unpin_lock(rq, rf);
1742 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1743}
1744
1745static inline void
1746rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1747 __releases(rq->lock)
1748{
1749 rq_unpin_lock(rq, rf);
1750 raw_spin_unlock_irq(&rq->lock);
1751}
1752
1753static inline void
1754rq_unlock(struct rq *rq, struct rq_flags *rf)
1755 __releases(rq->lock)
1756{
1757 rq_unpin_lock(rq, rf);
1758 raw_spin_unlock(&rq->lock);
1759}
1760
029632fb
PZ
1761#ifdef CONFIG_SMP
1762#ifdef CONFIG_PREEMPT
1763
1764static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1765
1766/*
1767 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1768 * way at the expense of forcing extra atomic operations in all
1769 * invocations. This assures that the double_lock is acquired using the
1770 * same underlying policy as the spinlock_t on this architecture, which
1771 * reduces latency compared to the unfair variant below. However, it
1772 * also adds more overhead and therefore may reduce throughput.
1773 */
1774static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1775 __releases(this_rq->lock)
1776 __acquires(busiest->lock)
1777 __acquires(this_rq->lock)
1778{
1779 raw_spin_unlock(&this_rq->lock);
1780 double_rq_lock(this_rq, busiest);
1781
1782 return 1;
1783}
1784
1785#else
1786/*
1787 * Unfair double_lock_balance: Optimizes throughput at the expense of
1788 * latency by eliminating extra atomic operations when the locks are
1789 * already in proper order on entry. This favors lower cpu-ids and will
1790 * grant the double lock to lower cpus over higher ids under contention,
1791 * regardless of entry order into the function.
1792 */
1793static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1794 __releases(this_rq->lock)
1795 __acquires(busiest->lock)
1796 __acquires(this_rq->lock)
1797{
1798 int ret = 0;
1799
1800 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1801 if (busiest < this_rq) {
1802 raw_spin_unlock(&this_rq->lock);
1803 raw_spin_lock(&busiest->lock);
1804 raw_spin_lock_nested(&this_rq->lock,
1805 SINGLE_DEPTH_NESTING);
1806 ret = 1;
1807 } else
1808 raw_spin_lock_nested(&busiest->lock,
1809 SINGLE_DEPTH_NESTING);
1810 }
1811 return ret;
1812}
1813
1814#endif /* CONFIG_PREEMPT */
1815
1816/*
1817 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1818 */
1819static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1820{
1821 if (unlikely(!irqs_disabled())) {
1822 /* printk() doesn't work good under rq->lock */
1823 raw_spin_unlock(&this_rq->lock);
1824 BUG_ON(1);
1825 }
1826
1827 return _double_lock_balance(this_rq, busiest);
1828}
1829
1830static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1831 __releases(busiest->lock)
1832{
1833 raw_spin_unlock(&busiest->lock);
1834 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1835}
1836
74602315
PZ
1837static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1838{
1839 if (l1 > l2)
1840 swap(l1, l2);
1841
1842 spin_lock(l1);
1843 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1844}
1845
60e69eed
MG
1846static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1847{
1848 if (l1 > l2)
1849 swap(l1, l2);
1850
1851 spin_lock_irq(l1);
1852 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1853}
1854
74602315
PZ
1855static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1856{
1857 if (l1 > l2)
1858 swap(l1, l2);
1859
1860 raw_spin_lock(l1);
1861 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1862}
1863
029632fb
PZ
1864/*
1865 * double_rq_lock - safely lock two runqueues
1866 *
1867 * Note this does not disable interrupts like task_rq_lock,
1868 * you need to do so manually before calling.
1869 */
1870static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1871 __acquires(rq1->lock)
1872 __acquires(rq2->lock)
1873{
1874 BUG_ON(!irqs_disabled());
1875 if (rq1 == rq2) {
1876 raw_spin_lock(&rq1->lock);
1877 __acquire(rq2->lock); /* Fake it out ;) */
1878 } else {
1879 if (rq1 < rq2) {
1880 raw_spin_lock(&rq1->lock);
1881 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1882 } else {
1883 raw_spin_lock(&rq2->lock);
1884 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1885 }
1886 }
1887}
1888
1889/*
1890 * double_rq_unlock - safely unlock two runqueues
1891 *
1892 * Note this does not restore interrupts like task_rq_unlock,
1893 * you need to do so manually after calling.
1894 */
1895static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1896 __releases(rq1->lock)
1897 __releases(rq2->lock)
1898{
1899 raw_spin_unlock(&rq1->lock);
1900 if (rq1 != rq2)
1901 raw_spin_unlock(&rq2->lock);
1902 else
1903 __release(rq2->lock);
1904}
1905
f2cb1360
IM
1906extern void set_rq_online (struct rq *rq);
1907extern void set_rq_offline(struct rq *rq);
1908extern bool sched_smp_initialized;
1909
029632fb
PZ
1910#else /* CONFIG_SMP */
1911
1912/*
1913 * double_rq_lock - safely lock two runqueues
1914 *
1915 * Note this does not disable interrupts like task_rq_lock,
1916 * you need to do so manually before calling.
1917 */
1918static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1919 __acquires(rq1->lock)
1920 __acquires(rq2->lock)
1921{
1922 BUG_ON(!irqs_disabled());
1923 BUG_ON(rq1 != rq2);
1924 raw_spin_lock(&rq1->lock);
1925 __acquire(rq2->lock); /* Fake it out ;) */
1926}
1927
1928/*
1929 * double_rq_unlock - safely unlock two runqueues
1930 *
1931 * Note this does not restore interrupts like task_rq_unlock,
1932 * you need to do so manually after calling.
1933 */
1934static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1935 __releases(rq1->lock)
1936 __releases(rq2->lock)
1937{
1938 BUG_ON(rq1 != rq2);
1939 raw_spin_unlock(&rq1->lock);
1940 __release(rq2->lock);
1941}
1942
1943#endif
1944
1945extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1946extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
6b55c965
SD
1947
1948#ifdef CONFIG_SCHED_DEBUG
9469eb01
PZ
1949extern bool sched_debug_enabled;
1950
029632fb
PZ
1951extern void print_cfs_stats(struct seq_file *m, int cpu);
1952extern void print_rt_stats(struct seq_file *m, int cpu);
acb32132 1953extern void print_dl_stats(struct seq_file *m, int cpu);
6b55c965
SD
1954extern void
1955print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
397f2378
SD
1956#ifdef CONFIG_NUMA_BALANCING
1957extern void
1958show_numa_stats(struct task_struct *p, struct seq_file *m);
1959extern void
1960print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1961 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1962#endif /* CONFIG_NUMA_BALANCING */
1963#endif /* CONFIG_SCHED_DEBUG */
029632fb
PZ
1964
1965extern void init_cfs_rq(struct cfs_rq *cfs_rq);
07c54f7a
AV
1966extern void init_rt_rq(struct rt_rq *rt_rq);
1967extern void init_dl_rq(struct dl_rq *dl_rq);
029632fb 1968
1ee14e6c
BS
1969extern void cfs_bandwidth_usage_inc(void);
1970extern void cfs_bandwidth_usage_dec(void);
1c792db7 1971
3451d024 1972#ifdef CONFIG_NO_HZ_COMMON
1c792db7
SS
1973enum rq_nohz_flag_bits {
1974 NOHZ_TICK_STOPPED,
1975 NOHZ_BALANCE_KICK,
1976};
1977
1978#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
20a5c8cc
TG
1979
1980extern void nohz_balance_exit_idle(unsigned int cpu);
1981#else
1982static inline void nohz_balance_exit_idle(unsigned int cpu) { }
1c792db7 1983#endif
73fbec60 1984
daec5798
LA
1985
1986#ifdef CONFIG_SMP
1987static inline
1988void __dl_update(struct dl_bw *dl_b, s64 bw)
1989{
1990 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
1991 int i;
1992
1993 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
1994 "sched RCU must be held");
1995 for_each_cpu_and(i, rd->span, cpu_active_mask) {
1996 struct rq *rq = cpu_rq(i);
1997
1998 rq->dl.extra_bw += bw;
1999 }
2000}
2001#else
2002static inline
2003void __dl_update(struct dl_bw *dl_b, s64 bw)
2004{
2005 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2006
2007 dl->extra_bw += bw;
2008}
2009#endif
2010
2011
73fbec60 2012#ifdef CONFIG_IRQ_TIME_ACCOUNTING
19d23dbf 2013struct irqtime {
25e2d8c1 2014 u64 total;
a499a5a1 2015 u64 tick_delta;
19d23dbf
FW
2016 u64 irq_start_time;
2017 struct u64_stats_sync sync;
2018};
73fbec60 2019
19d23dbf 2020DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
73fbec60 2021
25e2d8c1
FW
2022/*
2023 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2024 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2025 * and never move forward.
2026 */
73fbec60
FW
2027static inline u64 irq_time_read(int cpu)
2028{
19d23dbf
FW
2029 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2030 unsigned int seq;
2031 u64 total;
73fbec60
FW
2032
2033 do {
19d23dbf 2034 seq = __u64_stats_fetch_begin(&irqtime->sync);
25e2d8c1 2035 total = irqtime->total;
19d23dbf 2036 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
73fbec60 2037
19d23dbf 2038 return total;
73fbec60 2039}
73fbec60 2040#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
adaf9fcd
RW
2041
2042#ifdef CONFIG_CPU_FREQ
2043DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2044
2045/**
2046 * cpufreq_update_util - Take a note about CPU utilization changes.
12bde33d 2047 * @rq: Runqueue to carry out the update for.
58919e83 2048 * @flags: Update reason flags.
adaf9fcd 2049 *
58919e83
RW
2050 * This function is called by the scheduler on the CPU whose utilization is
2051 * being updated.
adaf9fcd
RW
2052 *
2053 * It can only be called from RCU-sched read-side critical sections.
adaf9fcd
RW
2054 *
2055 * The way cpufreq is currently arranged requires it to evaluate the CPU
2056 * performance state (frequency/voltage) on a regular basis to prevent it from
2057 * being stuck in a completely inadequate performance level for too long.
e0367b12
JL
2058 * That is not guaranteed to happen if the updates are only triggered from CFS
2059 * and DL, though, because they may not be coming in if only RT tasks are
2060 * active all the time (or there are RT tasks only).
adaf9fcd 2061 *
e0367b12
JL
2062 * As a workaround for that issue, this function is called periodically by the
2063 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
adaf9fcd 2064 * but that really is a band-aid. Going forward it should be replaced with
e0367b12 2065 * solutions targeted more specifically at RT tasks.
adaf9fcd 2066 */
12bde33d 2067static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
adaf9fcd 2068{
58919e83
RW
2069 struct update_util_data *data;
2070
674e7541
VK
2071 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2072 cpu_of(rq)));
58919e83 2073 if (data)
12bde33d
RW
2074 data->func(data, rq_clock(rq), flags);
2075}
adaf9fcd 2076#else
12bde33d 2077static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
adaf9fcd 2078#endif /* CONFIG_CPU_FREQ */
be53f58f 2079
9bdcb44e
RW
2080#ifdef arch_scale_freq_capacity
2081#ifndef arch_scale_freq_invariant
2082#define arch_scale_freq_invariant() (true)
2083#endif
2084#else /* arch_scale_freq_capacity */
2085#define arch_scale_freq_invariant() (false)
2086#endif
d4edd662
JL
2087
2088static inline unsigned long cpu_util_dl(struct rq *rq)
2089{
2090 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
2091}
2092
2093static inline unsigned long cpu_util_cfs(struct rq *rq)
2094{
2095 return rq->cfs.avg.util_avg;
2096}