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