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