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