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