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