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