sched/deadline: speed up SCHED_DEADLINE pushes with a push-heap
[deliverable/linux.git] / kernel / sched / sched.h
CommitLineData
029632fb
PZ
1
2#include <linux/sched.h>
cf4aebc2 3#include <linux/sched/sysctl.h>
8bd75c77 4#include <linux/sched/rt.h>
aab03e05 5#include <linux/sched/deadline.h>
029632fb
PZ
6#include <linux/mutex.h>
7#include <linux/spinlock.h>
8#include <linux/stop_machine.h>
9f3660c2 9#include <linux/tick.h>
f809ca9a 10#include <linux/slab.h>
029632fb 11
391e43da 12#include "cpupri.h"
6bfd6d72 13#include "cpudeadline.h"
60fed789 14#include "cpuacct.h"
029632fb 15
45ceebf7
PG
16struct rq;
17
029632fb
PZ
18extern __read_mostly int scheduler_running;
19
45ceebf7
PG
20extern unsigned long calc_load_update;
21extern atomic_long_t calc_load_tasks;
22
23extern long calc_load_fold_active(struct rq *this_rq);
24extern void update_cpu_load_active(struct rq *this_rq);
25
029632fb
PZ
26/*
27 * Convert user-nice values [ -20 ... 0 ... 19 ]
28 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
29 * and back.
30 */
31#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
32#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
33#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
34
35/*
36 * 'User priority' is the nice value converted to something we
37 * can work with better when scaling various scheduler parameters,
38 * it's a [ 0 ... 39 ] range.
39 */
40#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
41#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
42#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
43
44/*
45 * Helpers for converting nanosecond timing to jiffy resolution
46 */
47#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
48
cc1f4b1f
LZ
49/*
50 * Increase resolution of nice-level calculations for 64-bit architectures.
51 * The extra resolution improves shares distribution and load balancing of
52 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
53 * hierarchies, especially on larger systems. This is not a user-visible change
54 * and does not change the user-interface for setting shares/weights.
55 *
56 * We increase resolution only if we have enough bits to allow this increased
57 * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
58 * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
59 * increased costs.
60 */
61#if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
62# define SCHED_LOAD_RESOLUTION 10
63# define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
64# define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
65#else
66# define SCHED_LOAD_RESOLUTION 0
67# define scale_load(w) (w)
68# define scale_load_down(w) (w)
69#endif
70
71#define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
72#define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
73
029632fb
PZ
74#define NICE_0_LOAD SCHED_LOAD_SCALE
75#define NICE_0_SHIFT SCHED_LOAD_SHIFT
76
332ac17e
DF
77/*
78 * Single value that decides SCHED_DEADLINE internal math precision.
79 * 10 -> just above 1us
80 * 9 -> just above 0.5us
81 */
82#define DL_SCALE (10)
83
029632fb
PZ
84/*
85 * These are the 'tuning knobs' of the scheduler:
029632fb 86 */
029632fb
PZ
87
88/*
89 * single value that denotes runtime == period, ie unlimited time.
90 */
91#define RUNTIME_INF ((u64)~0ULL)
92
d50dde5a
DF
93static inline int fair_policy(int policy)
94{
95 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
96}
97
029632fb
PZ
98static inline int rt_policy(int policy)
99{
d50dde5a 100 return policy == SCHED_FIFO || policy == SCHED_RR;
029632fb
PZ
101}
102
aab03e05
DF
103static inline int dl_policy(int policy)
104{
105 return policy == SCHED_DEADLINE;
106}
107
029632fb
PZ
108static inline int task_has_rt_policy(struct task_struct *p)
109{
110 return rt_policy(p->policy);
111}
112
aab03e05
DF
113static inline int task_has_dl_policy(struct task_struct *p)
114{
115 return dl_policy(p->policy);
116}
117
332ac17e 118static inline bool dl_time_before(u64 a, u64 b)
2d3d891d
DF
119{
120 return (s64)(a - b) < 0;
121}
122
123/*
124 * Tells if entity @a should preempt entity @b.
125 */
332ac17e
DF
126static inline bool
127dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
2d3d891d
DF
128{
129 return dl_time_before(a->deadline, b->deadline);
130}
131
029632fb
PZ
132/*
133 * This is the priority-queue data structure of the RT scheduling class:
134 */
135struct rt_prio_array {
136 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
137 struct list_head queue[MAX_RT_PRIO];
138};
139
140struct rt_bandwidth {
141 /* nests inside the rq lock: */
142 raw_spinlock_t rt_runtime_lock;
143 ktime_t rt_period;
144 u64 rt_runtime;
145 struct hrtimer rt_period_timer;
146};
332ac17e
DF
147/*
148 * To keep the bandwidth of -deadline tasks and groups under control
149 * we need some place where:
150 * - store the maximum -deadline bandwidth of the system (the group);
151 * - cache the fraction of that bandwidth that is currently allocated.
152 *
153 * This is all done in the data structure below. It is similar to the
154 * one used for RT-throttling (rt_bandwidth), with the main difference
155 * that, since here we are only interested in admission control, we
156 * do not decrease any runtime while the group "executes", neither we
157 * need a timer to replenish it.
158 *
159 * With respect to SMP, the bandwidth is given on a per-CPU basis,
160 * meaning that:
161 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
162 * - dl_total_bw array contains, in the i-eth element, the currently
163 * allocated bandwidth on the i-eth CPU.
164 * Moreover, groups consume bandwidth on each CPU, while tasks only
165 * consume bandwidth on the CPU they're running on.
166 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
167 * that will be shown the next time the proc or cgroup controls will
168 * be red. It on its turn can be changed by writing on its own
169 * control.
170 */
171struct dl_bandwidth {
172 raw_spinlock_t dl_runtime_lock;
173 u64 dl_runtime;
174 u64 dl_period;
175};
176
177static inline int dl_bandwidth_enabled(void)
178{
179 return sysctl_sched_dl_runtime >= 0;
180}
181
182extern struct dl_bw *dl_bw_of(int i);
183
184struct dl_bw {
185 raw_spinlock_t lock;
186 u64 bw, total_bw;
187};
188
189static inline u64 global_dl_period(void);
190static inline u64 global_dl_runtime(void);
029632fb
PZ
191
192extern struct mutex sched_domains_mutex;
193
194#ifdef CONFIG_CGROUP_SCHED
195
196#include <linux/cgroup.h>
197
198struct cfs_rq;
199struct rt_rq;
200
35cf4e50 201extern struct list_head task_groups;
029632fb
PZ
202
203struct cfs_bandwidth {
204#ifdef CONFIG_CFS_BANDWIDTH
205 raw_spinlock_t lock;
206 ktime_t period;
207 u64 quota, runtime;
208 s64 hierarchal_quota;
209 u64 runtime_expires;
210
211 int idle, timer_active;
212 struct hrtimer period_timer, slack_timer;
213 struct list_head throttled_cfs_rq;
214
215 /* statistics */
216 int nr_periods, nr_throttled;
217 u64 throttled_time;
218#endif
219};
220
221/* task group related information */
222struct task_group {
223 struct cgroup_subsys_state css;
224
225#ifdef CONFIG_FAIR_GROUP_SCHED
226 /* schedulable entities of this group on each cpu */
227 struct sched_entity **se;
228 /* runqueue "owned" by this group on each cpu */
229 struct cfs_rq **cfs_rq;
230 unsigned long shares;
231
fa6bddeb 232#ifdef CONFIG_SMP
bf5b986e 233 atomic_long_t load_avg;
bb17f655 234 atomic_t runnable_avg;
029632fb 235#endif
fa6bddeb 236#endif
029632fb
PZ
237
238#ifdef CONFIG_RT_GROUP_SCHED
239 struct sched_rt_entity **rt_se;
240 struct rt_rq **rt_rq;
241
242 struct rt_bandwidth rt_bandwidth;
243#endif
244
245 struct rcu_head rcu;
246 struct list_head list;
247
248 struct task_group *parent;
249 struct list_head siblings;
250 struct list_head children;
251
252#ifdef CONFIG_SCHED_AUTOGROUP
253 struct autogroup *autogroup;
254#endif
255
256 struct cfs_bandwidth cfs_bandwidth;
257};
258
259#ifdef CONFIG_FAIR_GROUP_SCHED
260#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
261
262/*
263 * A weight of 0 or 1 can cause arithmetics problems.
264 * A weight of a cfs_rq is the sum of weights of which entities
265 * are queued on this cfs_rq, so a weight of a entity should not be
266 * too large, so as the shares value of a task group.
267 * (The default weight is 1024 - so there's no practical
268 * limitation from this.)
269 */
270#define MIN_SHARES (1UL << 1)
271#define MAX_SHARES (1UL << 18)
272#endif
273
029632fb
PZ
274typedef int (*tg_visitor)(struct task_group *, void *);
275
276extern int walk_tg_tree_from(struct task_group *from,
277 tg_visitor down, tg_visitor up, void *data);
278
279/*
280 * Iterate the full tree, calling @down when first entering a node and @up when
281 * leaving it for the final time.
282 *
283 * Caller must hold rcu_lock or sufficient equivalent.
284 */
285static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
286{
287 return walk_tg_tree_from(&root_task_group, down, up, data);
288}
289
290extern int tg_nop(struct task_group *tg, void *data);
291
292extern void free_fair_sched_group(struct task_group *tg);
293extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
294extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
295extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
296 struct sched_entity *se, int cpu,
297 struct sched_entity *parent);
298extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
299extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
300
301extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
302extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
303extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
304
305extern void free_rt_sched_group(struct task_group *tg);
306extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
307extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
308 struct sched_rt_entity *rt_se, int cpu,
309 struct sched_rt_entity *parent);
310
25cc7da7
LZ
311extern struct task_group *sched_create_group(struct task_group *parent);
312extern void sched_online_group(struct task_group *tg,
313 struct task_group *parent);
314extern void sched_destroy_group(struct task_group *tg);
315extern void sched_offline_group(struct task_group *tg);
316
317extern void sched_move_task(struct task_struct *tsk);
318
319#ifdef CONFIG_FAIR_GROUP_SCHED
320extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
321#endif
322
029632fb
PZ
323#else /* CONFIG_CGROUP_SCHED */
324
325struct cfs_bandwidth { };
326
327#endif /* CONFIG_CGROUP_SCHED */
328
329/* CFS-related fields in a runqueue */
330struct cfs_rq {
331 struct load_weight load;
c82513e5 332 unsigned int nr_running, h_nr_running;
029632fb
PZ
333
334 u64 exec_clock;
335 u64 min_vruntime;
336#ifndef CONFIG_64BIT
337 u64 min_vruntime_copy;
338#endif
339
340 struct rb_root tasks_timeline;
341 struct rb_node *rb_leftmost;
342
029632fb
PZ
343 /*
344 * 'curr' points to currently running entity on this cfs_rq.
345 * It is set to NULL otherwise (i.e when none are currently running).
346 */
347 struct sched_entity *curr, *next, *last, *skip;
348
349#ifdef CONFIG_SCHED_DEBUG
350 unsigned int nr_spread_over;
351#endif
352
2dac754e
PT
353#ifdef CONFIG_SMP
354 /*
355 * CFS Load tracking
356 * Under CFS, load is tracked on a per-entity basis and aggregated up.
357 * This allows for the description of both thread and group usage (in
358 * the FAIR_GROUP_SCHED case).
359 */
72a4cf20 360 unsigned long runnable_load_avg, blocked_load_avg;
2509940f 361 atomic64_t decay_counter;
9ee474f5 362 u64 last_decay;
2509940f 363 atomic_long_t removed_load;
141965c7 364
c566e8e9 365#ifdef CONFIG_FAIR_GROUP_SCHED
141965c7 366 /* Required to track per-cpu representation of a task_group */
bb17f655 367 u32 tg_runnable_contrib;
bf5b986e 368 unsigned long tg_load_contrib;
82958366
PT
369
370 /*
371 * h_load = weight * f(tg)
372 *
373 * Where f(tg) is the recursive weight fraction assigned to
374 * this group.
375 */
376 unsigned long h_load;
68520796
VD
377 u64 last_h_load_update;
378 struct sched_entity *h_load_next;
379#endif /* CONFIG_FAIR_GROUP_SCHED */
82958366
PT
380#endif /* CONFIG_SMP */
381
029632fb
PZ
382#ifdef CONFIG_FAIR_GROUP_SCHED
383 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
384
385 /*
386 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
387 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
388 * (like users, containers etc.)
389 *
390 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
391 * list is used during load balance.
392 */
393 int on_list;
394 struct list_head leaf_cfs_rq_list;
395 struct task_group *tg; /* group that "owns" this runqueue */
396
029632fb
PZ
397#ifdef CONFIG_CFS_BANDWIDTH
398 int runtime_enabled;
399 u64 runtime_expires;
400 s64 runtime_remaining;
401
f1b17280
PT
402 u64 throttled_clock, throttled_clock_task;
403 u64 throttled_clock_task_time;
029632fb
PZ
404 int throttled, throttle_count;
405 struct list_head throttled_list;
406#endif /* CONFIG_CFS_BANDWIDTH */
407#endif /* CONFIG_FAIR_GROUP_SCHED */
408};
409
410static inline int rt_bandwidth_enabled(void)
411{
412 return sysctl_sched_rt_runtime >= 0;
413}
414
415/* Real-Time classes' related field in a runqueue: */
416struct rt_rq {
417 struct rt_prio_array active;
c82513e5 418 unsigned int rt_nr_running;
029632fb
PZ
419#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
420 struct {
421 int curr; /* highest queued rt task prio */
422#ifdef CONFIG_SMP
423 int next; /* next highest */
424#endif
425 } highest_prio;
426#endif
427#ifdef CONFIG_SMP
428 unsigned long rt_nr_migratory;
429 unsigned long rt_nr_total;
430 int overloaded;
431 struct plist_head pushable_tasks;
432#endif
433 int rt_throttled;
434 u64 rt_time;
435 u64 rt_runtime;
436 /* Nests inside the rq lock: */
437 raw_spinlock_t rt_runtime_lock;
438
439#ifdef CONFIG_RT_GROUP_SCHED
440 unsigned long rt_nr_boosted;
441
442 struct rq *rq;
029632fb
PZ
443 struct task_group *tg;
444#endif
445};
446
aab03e05
DF
447/* Deadline class' related fields in a runqueue */
448struct dl_rq {
449 /* runqueue is an rbtree, ordered by deadline */
450 struct rb_root rb_root;
451 struct rb_node *rb_leftmost;
452
453 unsigned long dl_nr_running;
1baca4ce
JL
454
455#ifdef CONFIG_SMP
456 /*
457 * Deadline values of the currently executing and the
458 * earliest ready task on this rq. Caching these facilitates
459 * the decision wether or not a ready but not running task
460 * should migrate somewhere else.
461 */
462 struct {
463 u64 curr;
464 u64 next;
465 } earliest_dl;
466
467 unsigned long dl_nr_migratory;
468 unsigned long dl_nr_total;
469 int overloaded;
470
471 /*
472 * Tasks on this rq that can be pushed away. They are kept in
473 * an rb-tree, ordered by tasks' deadlines, with caching
474 * of the leftmost (earliest deadline) element.
475 */
476 struct rb_root pushable_dl_tasks_root;
477 struct rb_node *pushable_dl_tasks_leftmost;
332ac17e
DF
478#else
479 struct dl_bw dl_bw;
1baca4ce 480#endif
aab03e05
DF
481};
482
029632fb
PZ
483#ifdef CONFIG_SMP
484
485/*
486 * We add the notion of a root-domain which will be used to define per-domain
487 * variables. Each exclusive cpuset essentially defines an island domain by
488 * fully partitioning the member cpus from any other cpuset. Whenever a new
489 * exclusive cpuset is created, we also create and attach a new root-domain
490 * object.
491 *
492 */
493struct root_domain {
494 atomic_t refcount;
495 atomic_t rto_count;
496 struct rcu_head rcu;
497 cpumask_var_t span;
498 cpumask_var_t online;
499
1baca4ce
JL
500 /*
501 * The bit corresponding to a CPU gets set here if such CPU has more
502 * than one runnable -deadline task (as it is below for RT tasks).
503 */
504 cpumask_var_t dlo_mask;
505 atomic_t dlo_count;
332ac17e 506 struct dl_bw dl_bw;
6bfd6d72 507 struct cpudl cpudl;
1baca4ce 508
029632fb
PZ
509 /*
510 * The "RT overload" flag: it gets set if a CPU has more than
511 * one runnable RT task.
512 */
513 cpumask_var_t rto_mask;
514 struct cpupri cpupri;
515};
516
517extern struct root_domain def_root_domain;
518
519#endif /* CONFIG_SMP */
520
521/*
522 * This is the main, per-CPU runqueue data structure.
523 *
524 * Locking rule: those places that want to lock multiple runqueues
525 * (such as the load balancing or the thread migration code), lock
526 * acquire operations must be ordered by ascending &runqueue.
527 */
528struct rq {
529 /* runqueue lock: */
530 raw_spinlock_t lock;
531
532 /*
533 * nr_running and cpu_load should be in the same cacheline because
534 * remote CPUs use both these fields when doing load calculation.
535 */
c82513e5 536 unsigned int nr_running;
0ec8aa00
PZ
537#ifdef CONFIG_NUMA_BALANCING
538 unsigned int nr_numa_running;
539 unsigned int nr_preferred_running;
540#endif
029632fb
PZ
541 #define CPU_LOAD_IDX_MAX 5
542 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
543 unsigned long last_load_update_tick;
3451d024 544#ifdef CONFIG_NO_HZ_COMMON
029632fb 545 u64 nohz_stamp;
1c792db7 546 unsigned long nohz_flags;
265f22a9
FW
547#endif
548#ifdef CONFIG_NO_HZ_FULL
549 unsigned long last_sched_tick;
029632fb
PZ
550#endif
551 int skip_clock_update;
552
553 /* capture load from *all* tasks on this cpu: */
554 struct load_weight load;
555 unsigned long nr_load_updates;
556 u64 nr_switches;
557
558 struct cfs_rq cfs;
559 struct rt_rq rt;
aab03e05 560 struct dl_rq dl;
029632fb
PZ
561
562#ifdef CONFIG_FAIR_GROUP_SCHED
563 /* list of leaf cfs_rq on this cpu: */
564 struct list_head leaf_cfs_rq_list;
a35b6466
PZ
565#endif /* CONFIG_FAIR_GROUP_SCHED */
566
029632fb
PZ
567#ifdef CONFIG_RT_GROUP_SCHED
568 struct list_head leaf_rt_rq_list;
569#endif
570
571 /*
572 * This is part of a global counter where only the total sum
573 * over all CPUs matters. A task can increase this counter on
574 * one CPU and if it got migrated afterwards it may decrease
575 * it on another CPU. Always updated under the runqueue lock:
576 */
577 unsigned long nr_uninterruptible;
578
579 struct task_struct *curr, *idle, *stop;
580 unsigned long next_balance;
581 struct mm_struct *prev_mm;
582
583 u64 clock;
584 u64 clock_task;
585
586 atomic_t nr_iowait;
587
588#ifdef CONFIG_SMP
589 struct root_domain *rd;
590 struct sched_domain *sd;
591
592 unsigned long cpu_power;
593
594 unsigned char idle_balance;
595 /* For active balancing */
596 int post_schedule;
597 int active_balance;
598 int push_cpu;
599 struct cpu_stop_work active_balance_work;
600 /* cpu of this runqueue: */
601 int cpu;
602 int online;
603
367456c7
PZ
604 struct list_head cfs_tasks;
605
029632fb
PZ
606 u64 rt_avg;
607 u64 age_stamp;
608 u64 idle_stamp;
609 u64 avg_idle;
9bd721c5
JL
610
611 /* This is used to determine avg_idle's max value */
612 u64 max_idle_balance_cost;
029632fb
PZ
613#endif
614
615#ifdef CONFIG_IRQ_TIME_ACCOUNTING
616 u64 prev_irq_time;
617#endif
618#ifdef CONFIG_PARAVIRT
619 u64 prev_steal_time;
620#endif
621#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
622 u64 prev_steal_time_rq;
623#endif
624
625 /* calc_load related fields */
626 unsigned long calc_load_update;
627 long calc_load_active;
628
629#ifdef CONFIG_SCHED_HRTICK
630#ifdef CONFIG_SMP
631 int hrtick_csd_pending;
632 struct call_single_data hrtick_csd;
633#endif
634 struct hrtimer hrtick_timer;
635#endif
636
637#ifdef CONFIG_SCHEDSTATS
638 /* latency stats */
639 struct sched_info rq_sched_info;
640 unsigned long long rq_cpu_time;
641 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
642
643 /* sys_sched_yield() stats */
644 unsigned int yld_count;
645
646 /* schedule() stats */
029632fb
PZ
647 unsigned int sched_count;
648 unsigned int sched_goidle;
649
650 /* try_to_wake_up() stats */
651 unsigned int ttwu_count;
652 unsigned int ttwu_local;
653#endif
654
655#ifdef CONFIG_SMP
656 struct llist_head wake_list;
657#endif
18bf2805
BS
658
659 struct sched_avg avg;
029632fb
PZ
660};
661
662static inline int cpu_of(struct rq *rq)
663{
664#ifdef CONFIG_SMP
665 return rq->cpu;
666#else
667 return 0;
668#endif
669}
670
671DECLARE_PER_CPU(struct rq, runqueues);
672
518cd623
PZ
673#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
674#define this_rq() (&__get_cpu_var(runqueues))
675#define task_rq(p) cpu_rq(task_cpu(p))
676#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
677#define raw_rq() (&__raw_get_cpu_var(runqueues))
678
78becc27
FW
679static inline u64 rq_clock(struct rq *rq)
680{
681 return rq->clock;
682}
683
684static inline u64 rq_clock_task(struct rq *rq)
685{
686 return rq->clock_task;
687}
688
f809ca9a 689#ifdef CONFIG_NUMA_BALANCING
0ec8aa00 690extern void sched_setnuma(struct task_struct *p, int node);
e6628d5b 691extern int migrate_task_to(struct task_struct *p, int cpu);
ac66f547 692extern int migrate_swap(struct task_struct *, struct task_struct *);
f809ca9a
MG
693#endif /* CONFIG_NUMA_BALANCING */
694
518cd623
PZ
695#ifdef CONFIG_SMP
696
029632fb
PZ
697#define rcu_dereference_check_sched_domain(p) \
698 rcu_dereference_check((p), \
699 lockdep_is_held(&sched_domains_mutex))
700
701/*
702 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
703 * See detach_destroy_domains: synchronize_sched for details.
704 *
705 * The domain tree of any CPU may only be accessed from within
706 * preempt-disabled sections.
707 */
708#define for_each_domain(cpu, __sd) \
518cd623
PZ
709 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
710 __sd; __sd = __sd->parent)
029632fb 711
77e81365
SS
712#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
713
518cd623
PZ
714/**
715 * highest_flag_domain - Return highest sched_domain containing flag.
716 * @cpu: The cpu whose highest level of sched domain is to
717 * be returned.
718 * @flag: The flag to check for the highest sched_domain
719 * for the given cpu.
720 *
721 * Returns the highest sched_domain of a cpu which contains the given flag.
722 */
723static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
724{
725 struct sched_domain *sd, *hsd = NULL;
726
727 for_each_domain(cpu, sd) {
728 if (!(sd->flags & flag))
729 break;
730 hsd = sd;
731 }
732
733 return hsd;
734}
735
fb13c7ee
MG
736static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
737{
738 struct sched_domain *sd;
739
740 for_each_domain(cpu, sd) {
741 if (sd->flags & flag)
742 break;
743 }
744
745 return sd;
746}
747
518cd623 748DECLARE_PER_CPU(struct sched_domain *, sd_llc);
7d9ffa89 749DECLARE_PER_CPU(int, sd_llc_size);
518cd623 750DECLARE_PER_CPU(int, sd_llc_id);
fb13c7ee 751DECLARE_PER_CPU(struct sched_domain *, sd_numa);
37dc6b50
PM
752DECLARE_PER_CPU(struct sched_domain *, sd_busy);
753DECLARE_PER_CPU(struct sched_domain *, sd_asym);
518cd623 754
5e6521ea
LZ
755struct sched_group_power {
756 atomic_t ref;
757 /*
758 * CPU power of this group, SCHED_LOAD_SCALE being max power for a
759 * single CPU.
760 */
761 unsigned int power, power_orig;
762 unsigned long next_update;
6263322c 763 int imbalance; /* XXX unrelated to power but shared group state */
5e6521ea
LZ
764 /*
765 * Number of busy cpus in this group.
766 */
767 atomic_t nr_busy_cpus;
768
769 unsigned long cpumask[0]; /* iteration mask */
770};
771
772struct sched_group {
773 struct sched_group *next; /* Must be a circular list */
774 atomic_t ref;
775
776 unsigned int group_weight;
777 struct sched_group_power *sgp;
778
779 /*
780 * The CPUs this group covers.
781 *
782 * NOTE: this field is variable length. (Allocated dynamically
783 * by attaching extra space to the end of the structure,
784 * depending on how many CPUs the kernel has booted up with)
785 */
786 unsigned long cpumask[0];
787};
788
789static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
790{
791 return to_cpumask(sg->cpumask);
792}
793
794/*
795 * cpumask masking which cpus in the group are allowed to iterate up the domain
796 * tree.
797 */
798static inline struct cpumask *sched_group_mask(struct sched_group *sg)
799{
800 return to_cpumask(sg->sgp->cpumask);
801}
802
803/**
804 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
805 * @group: The group whose first cpu is to be returned.
806 */
807static inline unsigned int group_first_cpu(struct sched_group *group)
808{
809 return cpumask_first(sched_group_cpus(group));
810}
811
c1174876
PZ
812extern int group_balance_cpu(struct sched_group *sg);
813
518cd623 814#endif /* CONFIG_SMP */
029632fb 815
391e43da
PZ
816#include "stats.h"
817#include "auto_group.h"
029632fb
PZ
818
819#ifdef CONFIG_CGROUP_SCHED
820
821/*
822 * Return the group to which this tasks belongs.
823 *
8af01f56
TH
824 * We cannot use task_css() and friends because the cgroup subsystem
825 * changes that value before the cgroup_subsys::attach() method is called,
826 * therefore we cannot pin it and might observe the wrong value.
8323f26c
PZ
827 *
828 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
829 * core changes this before calling sched_move_task().
830 *
831 * Instead we use a 'copy' which is updated from sched_move_task() while
832 * holding both task_struct::pi_lock and rq::lock.
029632fb
PZ
833 */
834static inline struct task_group *task_group(struct task_struct *p)
835{
8323f26c 836 return p->sched_task_group;
029632fb
PZ
837}
838
839/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
840static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
841{
842#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
843 struct task_group *tg = task_group(p);
844#endif
845
846#ifdef CONFIG_FAIR_GROUP_SCHED
847 p->se.cfs_rq = tg->cfs_rq[cpu];
848 p->se.parent = tg->se[cpu];
849#endif
850
851#ifdef CONFIG_RT_GROUP_SCHED
852 p->rt.rt_rq = tg->rt_rq[cpu];
853 p->rt.parent = tg->rt_se[cpu];
854#endif
855}
856
857#else /* CONFIG_CGROUP_SCHED */
858
859static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
860static inline struct task_group *task_group(struct task_struct *p)
861{
862 return NULL;
863}
864
865#endif /* CONFIG_CGROUP_SCHED */
866
867static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
868{
869 set_task_rq(p, cpu);
870#ifdef CONFIG_SMP
871 /*
872 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
873 * successfuly executed on another CPU. We must ensure that updates of
874 * per-task data have been completed by this moment.
875 */
876 smp_wmb();
877 task_thread_info(p)->cpu = cpu;
ac66f547 878 p->wake_cpu = cpu;
029632fb
PZ
879#endif
880}
881
882/*
883 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
884 */
885#ifdef CONFIG_SCHED_DEBUG
c5905afb 886# include <linux/static_key.h>
029632fb
PZ
887# define const_debug __read_mostly
888#else
889# define const_debug const
890#endif
891
892extern const_debug unsigned int sysctl_sched_features;
893
894#define SCHED_FEAT(name, enabled) \
895 __SCHED_FEAT_##name ,
896
897enum {
391e43da 898#include "features.h"
f8b6d1cc 899 __SCHED_FEAT_NR,
029632fb
PZ
900};
901
902#undef SCHED_FEAT
903
f8b6d1cc 904#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
c5905afb 905static __always_inline bool static_branch__true(struct static_key *key)
f8b6d1cc 906{
c5905afb 907 return static_key_true(key); /* Not out of line branch. */
f8b6d1cc
PZ
908}
909
c5905afb 910static __always_inline bool static_branch__false(struct static_key *key)
f8b6d1cc 911{
c5905afb 912 return static_key_false(key); /* Out of line branch. */
f8b6d1cc
PZ
913}
914
915#define SCHED_FEAT(name, enabled) \
c5905afb 916static __always_inline bool static_branch_##name(struct static_key *key) \
f8b6d1cc
PZ
917{ \
918 return static_branch__##enabled(key); \
919}
920
921#include "features.h"
922
923#undef SCHED_FEAT
924
c5905afb 925extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
f8b6d1cc
PZ
926#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
927#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
029632fb 928#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
f8b6d1cc 929#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
029632fb 930
cbee9f88
PZ
931#ifdef CONFIG_NUMA_BALANCING
932#define sched_feat_numa(x) sched_feat(x)
3105b86a
MG
933#ifdef CONFIG_SCHED_DEBUG
934#define numabalancing_enabled sched_feat_numa(NUMA)
935#else
936extern bool numabalancing_enabled;
937#endif /* CONFIG_SCHED_DEBUG */
cbee9f88
PZ
938#else
939#define sched_feat_numa(x) (0)
3105b86a
MG
940#define numabalancing_enabled (0)
941#endif /* CONFIG_NUMA_BALANCING */
cbee9f88 942
029632fb
PZ
943static inline u64 global_rt_period(void)
944{
945 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
946}
947
948static inline u64 global_rt_runtime(void)
949{
950 if (sysctl_sched_rt_runtime < 0)
951 return RUNTIME_INF;
952
953 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
954}
955
332ac17e
DF
956static inline u64 global_dl_period(void)
957{
958 return (u64)sysctl_sched_dl_period * NSEC_PER_USEC;
959}
960
961static inline u64 global_dl_runtime(void)
962{
963 if (sysctl_sched_dl_runtime < 0)
964 return RUNTIME_INF;
029632fb 965
332ac17e
DF
966 return (u64)sysctl_sched_dl_runtime * NSEC_PER_USEC;
967}
029632fb
PZ
968
969static inline int task_current(struct rq *rq, struct task_struct *p)
970{
971 return rq->curr == p;
972}
973
974static inline int task_running(struct rq *rq, struct task_struct *p)
975{
976#ifdef CONFIG_SMP
977 return p->on_cpu;
978#else
979 return task_current(rq, p);
980#endif
981}
982
983
984#ifndef prepare_arch_switch
985# define prepare_arch_switch(next) do { } while (0)
986#endif
987#ifndef finish_arch_switch
988# define finish_arch_switch(prev) do { } while (0)
989#endif
01f23e16
CM
990#ifndef finish_arch_post_lock_switch
991# define finish_arch_post_lock_switch() do { } while (0)
992#endif
029632fb
PZ
993
994#ifndef __ARCH_WANT_UNLOCKED_CTXSW
995static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
996{
997#ifdef CONFIG_SMP
998 /*
999 * We can optimise this out completely for !SMP, because the
1000 * SMP rebalancing from interrupt is the only thing that cares
1001 * here.
1002 */
1003 next->on_cpu = 1;
1004#endif
1005}
1006
1007static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1008{
1009#ifdef CONFIG_SMP
1010 /*
1011 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1012 * We must ensure this doesn't happen until the switch is completely
1013 * finished.
1014 */
1015 smp_wmb();
1016 prev->on_cpu = 0;
1017#endif
1018#ifdef CONFIG_DEBUG_SPINLOCK
1019 /* this is a valid case when another task releases the spinlock */
1020 rq->lock.owner = current;
1021#endif
1022 /*
1023 * If we are tracking spinlock dependencies then we have to
1024 * fix up the runqueue lock - which gets 'carried over' from
1025 * prev into current:
1026 */
1027 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1028
1029 raw_spin_unlock_irq(&rq->lock);
1030}
1031
1032#else /* __ARCH_WANT_UNLOCKED_CTXSW */
1033static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1034{
1035#ifdef CONFIG_SMP
1036 /*
1037 * We can optimise this out completely for !SMP, because the
1038 * SMP rebalancing from interrupt is the only thing that cares
1039 * here.
1040 */
1041 next->on_cpu = 1;
1042#endif
029632fb 1043 raw_spin_unlock(&rq->lock);
029632fb
PZ
1044}
1045
1046static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1047{
1048#ifdef CONFIG_SMP
1049 /*
1050 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1051 * We must ensure this doesn't happen until the switch is completely
1052 * finished.
1053 */
1054 smp_wmb();
1055 prev->on_cpu = 0;
1056#endif
029632fb 1057 local_irq_enable();
029632fb
PZ
1058}
1059#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
1060
b13095f0
LZ
1061/*
1062 * wake flags
1063 */
1064#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1065#define WF_FORK 0x02 /* child wakeup after fork */
1066#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1067
029632fb
PZ
1068/*
1069 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1070 * of tasks with abnormal "nice" values across CPUs the contribution that
1071 * each task makes to its run queue's load is weighted according to its
1072 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1073 * scaled version of the new time slice allocation that they receive on time
1074 * slice expiry etc.
1075 */
1076
1077#define WEIGHT_IDLEPRIO 3
1078#define WMULT_IDLEPRIO 1431655765
1079
1080/*
1081 * Nice levels are multiplicative, with a gentle 10% change for every
1082 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1083 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1084 * that remained on nice 0.
1085 *
1086 * The "10% effect" is relative and cumulative: from _any_ nice level,
1087 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1088 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1089 * If a task goes up by ~10% and another task goes down by ~10% then
1090 * the relative distance between them is ~25%.)
1091 */
1092static const int prio_to_weight[40] = {
1093 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1094 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1095 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1096 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1097 /* 0 */ 1024, 820, 655, 526, 423,
1098 /* 5 */ 335, 272, 215, 172, 137,
1099 /* 10 */ 110, 87, 70, 56, 45,
1100 /* 15 */ 36, 29, 23, 18, 15,
1101};
1102
1103/*
1104 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1105 *
1106 * In cases where the weight does not change often, we can use the
1107 * precalculated inverse to speed up arithmetics by turning divisions
1108 * into multiplications:
1109 */
1110static const u32 prio_to_wmult[40] = {
1111 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1112 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1113 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1114 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1115 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1116 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1117 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1118 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1119};
1120
c82ba9fa
LZ
1121#define ENQUEUE_WAKEUP 1
1122#define ENQUEUE_HEAD 2
1123#ifdef CONFIG_SMP
1124#define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
1125#else
1126#define ENQUEUE_WAKING 0
1127#endif
aab03e05 1128#define ENQUEUE_REPLENISH 8
c82ba9fa
LZ
1129
1130#define DEQUEUE_SLEEP 1
1131
1132struct sched_class {
1133 const struct sched_class *next;
1134
1135 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1136 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1137 void (*yield_task) (struct rq *rq);
1138 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1139
1140 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1141
1142 struct task_struct * (*pick_next_task) (struct rq *rq);
1143 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1144
1145#ifdef CONFIG_SMP
ac66f547 1146 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
c82ba9fa
LZ
1147 void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
1148
1149 void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
1150 void (*post_schedule) (struct rq *this_rq);
1151 void (*task_waking) (struct task_struct *task);
1152 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1153
1154 void (*set_cpus_allowed)(struct task_struct *p,
1155 const struct cpumask *newmask);
1156
1157 void (*rq_online)(struct rq *rq);
1158 void (*rq_offline)(struct rq *rq);
1159#endif
1160
1161 void (*set_curr_task) (struct rq *rq);
1162 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1163 void (*task_fork) (struct task_struct *p);
e6c390f2 1164 void (*task_dead) (struct task_struct *p);
c82ba9fa
LZ
1165
1166 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1167 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1168 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1169 int oldprio);
1170
1171 unsigned int (*get_rr_interval) (struct rq *rq,
1172 struct task_struct *task);
1173
1174#ifdef CONFIG_FAIR_GROUP_SCHED
1175 void (*task_move_group) (struct task_struct *p, int on_rq);
1176#endif
1177};
029632fb
PZ
1178
1179#define sched_class_highest (&stop_sched_class)
1180#define for_each_class(class) \
1181 for (class = sched_class_highest; class; class = class->next)
1182
1183extern const struct sched_class stop_sched_class;
aab03e05 1184extern const struct sched_class dl_sched_class;
029632fb
PZ
1185extern const struct sched_class rt_sched_class;
1186extern const struct sched_class fair_sched_class;
1187extern const struct sched_class idle_sched_class;
1188
1189
1190#ifdef CONFIG_SMP
1191
b719203b
LZ
1192extern void update_group_power(struct sched_domain *sd, int cpu);
1193
029632fb
PZ
1194extern void trigger_load_balance(struct rq *rq, int cpu);
1195extern void idle_balance(int this_cpu, struct rq *this_rq);
1196
642dbc39
VG
1197extern void idle_enter_fair(struct rq *this_rq);
1198extern void idle_exit_fair(struct rq *this_rq);
642dbc39 1199
029632fb
PZ
1200#else /* CONFIG_SMP */
1201
1202static inline void idle_balance(int cpu, struct rq *rq)
1203{
1204}
1205
1206#endif
1207
1208extern void sysrq_sched_debug_show(void);
1209extern void sched_init_granularity(void);
1210extern void update_max_interval(void);
1baca4ce
JL
1211
1212extern void init_sched_dl_class(void);
029632fb
PZ
1213extern void init_sched_rt_class(void);
1214extern void init_sched_fair_class(void);
332ac17e 1215extern void init_sched_dl_class(void);
029632fb
PZ
1216
1217extern void resched_task(struct task_struct *p);
1218extern void resched_cpu(int cpu);
1219
1220extern struct rt_bandwidth def_rt_bandwidth;
1221extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1222
332ac17e
DF
1223extern struct dl_bandwidth def_dl_bandwidth;
1224extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
aab03e05
DF
1225extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1226
332ac17e
DF
1227unsigned long to_ratio(u64 period, u64 runtime);
1228
556061b0 1229extern void update_idle_cpu_load(struct rq *this_rq);
029632fb 1230
a75cdaa9
AS
1231extern void init_task_runnable_average(struct task_struct *p);
1232
73fbec60
FW
1233#ifdef CONFIG_PARAVIRT
1234static inline u64 steal_ticks(u64 steal)
1235{
1236 if (unlikely(steal > NSEC_PER_SEC))
1237 return div_u64(steal, TICK_NSEC);
1238
1239 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1240}
1241#endif
1242
029632fb
PZ
1243static inline void inc_nr_running(struct rq *rq)
1244{
1245 rq->nr_running++;
9f3660c2
FW
1246
1247#ifdef CONFIG_NO_HZ_FULL
1248 if (rq->nr_running == 2) {
1249 if (tick_nohz_full_cpu(rq->cpu)) {
1250 /* Order rq->nr_running write against the IPI */
1251 smp_wmb();
1252 smp_send_reschedule(rq->cpu);
1253 }
1254 }
1255#endif
029632fb
PZ
1256}
1257
1258static inline void dec_nr_running(struct rq *rq)
1259{
1260 rq->nr_running--;
1261}
1262
265f22a9
FW
1263static inline void rq_last_tick_reset(struct rq *rq)
1264{
1265#ifdef CONFIG_NO_HZ_FULL
1266 rq->last_sched_tick = jiffies;
1267#endif
1268}
1269
029632fb
PZ
1270extern void update_rq_clock(struct rq *rq);
1271
1272extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1273extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1274
1275extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1276
1277extern const_debug unsigned int sysctl_sched_time_avg;
1278extern const_debug unsigned int sysctl_sched_nr_migrate;
1279extern const_debug unsigned int sysctl_sched_migration_cost;
1280
1281static inline u64 sched_avg_period(void)
1282{
1283 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1284}
1285
029632fb
PZ
1286#ifdef CONFIG_SCHED_HRTICK
1287
1288/*
1289 * Use hrtick when:
1290 * - enabled by features
1291 * - hrtimer is actually high res
1292 */
1293static inline int hrtick_enabled(struct rq *rq)
1294{
1295 if (!sched_feat(HRTICK))
1296 return 0;
1297 if (!cpu_active(cpu_of(rq)))
1298 return 0;
1299 return hrtimer_is_hres_active(&rq->hrtick_timer);
1300}
1301
1302void hrtick_start(struct rq *rq, u64 delay);
1303
b39e66ea
MG
1304#else
1305
1306static inline int hrtick_enabled(struct rq *rq)
1307{
1308 return 0;
1309}
1310
029632fb
PZ
1311#endif /* CONFIG_SCHED_HRTICK */
1312
1313#ifdef CONFIG_SMP
1314extern void sched_avg_update(struct rq *rq);
1315static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1316{
1317 rq->rt_avg += rt_delta;
1318 sched_avg_update(rq);
1319}
1320#else
1321static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1322static inline void sched_avg_update(struct rq *rq) { }
1323#endif
1324
1325extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
1326
1327#ifdef CONFIG_SMP
1328#ifdef CONFIG_PREEMPT
1329
1330static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1331
1332/*
1333 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1334 * way at the expense of forcing extra atomic operations in all
1335 * invocations. This assures that the double_lock is acquired using the
1336 * same underlying policy as the spinlock_t on this architecture, which
1337 * reduces latency compared to the unfair variant below. However, it
1338 * also adds more overhead and therefore may reduce throughput.
1339 */
1340static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1341 __releases(this_rq->lock)
1342 __acquires(busiest->lock)
1343 __acquires(this_rq->lock)
1344{
1345 raw_spin_unlock(&this_rq->lock);
1346 double_rq_lock(this_rq, busiest);
1347
1348 return 1;
1349}
1350
1351#else
1352/*
1353 * Unfair double_lock_balance: Optimizes throughput at the expense of
1354 * latency by eliminating extra atomic operations when the locks are
1355 * already in proper order on entry. This favors lower cpu-ids and will
1356 * grant the double lock to lower cpus over higher ids under contention,
1357 * regardless of entry order into the function.
1358 */
1359static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1360 __releases(this_rq->lock)
1361 __acquires(busiest->lock)
1362 __acquires(this_rq->lock)
1363{
1364 int ret = 0;
1365
1366 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1367 if (busiest < this_rq) {
1368 raw_spin_unlock(&this_rq->lock);
1369 raw_spin_lock(&busiest->lock);
1370 raw_spin_lock_nested(&this_rq->lock,
1371 SINGLE_DEPTH_NESTING);
1372 ret = 1;
1373 } else
1374 raw_spin_lock_nested(&busiest->lock,
1375 SINGLE_DEPTH_NESTING);
1376 }
1377 return ret;
1378}
1379
1380#endif /* CONFIG_PREEMPT */
1381
1382/*
1383 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1384 */
1385static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1386{
1387 if (unlikely(!irqs_disabled())) {
1388 /* printk() doesn't work good under rq->lock */
1389 raw_spin_unlock(&this_rq->lock);
1390 BUG_ON(1);
1391 }
1392
1393 return _double_lock_balance(this_rq, busiest);
1394}
1395
1396static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1397 __releases(busiest->lock)
1398{
1399 raw_spin_unlock(&busiest->lock);
1400 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1401}
1402
74602315
PZ
1403static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1404{
1405 if (l1 > l2)
1406 swap(l1, l2);
1407
1408 spin_lock(l1);
1409 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1410}
1411
1412static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1413{
1414 if (l1 > l2)
1415 swap(l1, l2);
1416
1417 raw_spin_lock(l1);
1418 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1419}
1420
029632fb
PZ
1421/*
1422 * double_rq_lock - safely lock two runqueues
1423 *
1424 * Note this does not disable interrupts like task_rq_lock,
1425 * you need to do so manually before calling.
1426 */
1427static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1428 __acquires(rq1->lock)
1429 __acquires(rq2->lock)
1430{
1431 BUG_ON(!irqs_disabled());
1432 if (rq1 == rq2) {
1433 raw_spin_lock(&rq1->lock);
1434 __acquire(rq2->lock); /* Fake it out ;) */
1435 } else {
1436 if (rq1 < rq2) {
1437 raw_spin_lock(&rq1->lock);
1438 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1439 } else {
1440 raw_spin_lock(&rq2->lock);
1441 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1442 }
1443 }
1444}
1445
1446/*
1447 * double_rq_unlock - safely unlock two runqueues
1448 *
1449 * Note this does not restore interrupts like task_rq_unlock,
1450 * you need to do so manually after calling.
1451 */
1452static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1453 __releases(rq1->lock)
1454 __releases(rq2->lock)
1455{
1456 raw_spin_unlock(&rq1->lock);
1457 if (rq1 != rq2)
1458 raw_spin_unlock(&rq2->lock);
1459 else
1460 __release(rq2->lock);
1461}
1462
1463#else /* CONFIG_SMP */
1464
1465/*
1466 * double_rq_lock - safely lock two runqueues
1467 *
1468 * Note this does not disable interrupts like task_rq_lock,
1469 * you need to do so manually before calling.
1470 */
1471static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1472 __acquires(rq1->lock)
1473 __acquires(rq2->lock)
1474{
1475 BUG_ON(!irqs_disabled());
1476 BUG_ON(rq1 != rq2);
1477 raw_spin_lock(&rq1->lock);
1478 __acquire(rq2->lock); /* Fake it out ;) */
1479}
1480
1481/*
1482 * double_rq_unlock - safely unlock two runqueues
1483 *
1484 * Note this does not restore interrupts like task_rq_unlock,
1485 * you need to do so manually after calling.
1486 */
1487static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1488 __releases(rq1->lock)
1489 __releases(rq2->lock)
1490{
1491 BUG_ON(rq1 != rq2);
1492 raw_spin_unlock(&rq1->lock);
1493 __release(rq2->lock);
1494}
1495
1496#endif
1497
1498extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1499extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1500extern void print_cfs_stats(struct seq_file *m, int cpu);
1501extern void print_rt_stats(struct seq_file *m, int cpu);
1502
1503extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1504extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
aab03e05 1505extern void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq);
029632fb 1506
1ee14e6c
BS
1507extern void cfs_bandwidth_usage_inc(void);
1508extern void cfs_bandwidth_usage_dec(void);
1c792db7 1509
3451d024 1510#ifdef CONFIG_NO_HZ_COMMON
1c792db7
SS
1511enum rq_nohz_flag_bits {
1512 NOHZ_TICK_STOPPED,
1513 NOHZ_BALANCE_KICK,
1514};
1515
1516#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1517#endif
73fbec60
FW
1518
1519#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1520
1521DECLARE_PER_CPU(u64, cpu_hardirq_time);
1522DECLARE_PER_CPU(u64, cpu_softirq_time);
1523
1524#ifndef CONFIG_64BIT
1525DECLARE_PER_CPU(seqcount_t, irq_time_seq);
1526
1527static inline void irq_time_write_begin(void)
1528{
1529 __this_cpu_inc(irq_time_seq.sequence);
1530 smp_wmb();
1531}
1532
1533static inline void irq_time_write_end(void)
1534{
1535 smp_wmb();
1536 __this_cpu_inc(irq_time_seq.sequence);
1537}
1538
1539static inline u64 irq_time_read(int cpu)
1540{
1541 u64 irq_time;
1542 unsigned seq;
1543
1544 do {
1545 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1546 irq_time = per_cpu(cpu_softirq_time, cpu) +
1547 per_cpu(cpu_hardirq_time, cpu);
1548 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1549
1550 return irq_time;
1551}
1552#else /* CONFIG_64BIT */
1553static inline void irq_time_write_begin(void)
1554{
1555}
1556
1557static inline void irq_time_write_end(void)
1558{
1559}
1560
1561static inline u64 irq_time_read(int cpu)
1562{
1563 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1564}
1565#endif /* CONFIG_64BIT */
1566#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
This page took 0.1555 seconds and 5 git commands to generate.