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