OSDN Git Service

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