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