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