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