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