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