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