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