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