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