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