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