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