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