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