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