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