blob: eb363aa5d83cf02a6f2717a79a56e815de4b1c43 [file] [log] [blame]
Peter Zijlstra029632f2011-10-25 10:00:11 +02001
2#include <linux/sched.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -06003#include <linux/sched/sysctl.h>
Clark Williams8bd75c72013-02-07 09:47:07 -06004#include <linux/sched/rt.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02005#include <linux/mutex.h>
6#include <linux/spinlock.h>
7#include <linux/stop_machine.h>
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02008#include <linux/tick.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02009
Peter Zijlstra391e43d2011-11-15 17:14:39 +010010#include "cpupri.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +020011
12extern __read_mostly int scheduler_running;
13
14/*
15 * Convert user-nice values [ -20 ... 0 ... 19 ]
16 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
17 * and back.
18 */
19#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
20#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
21#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
22
23/*
24 * 'User priority' is the nice value converted to something we
25 * can work with better when scaling various scheduler parameters,
26 * it's a [ 0 ... 39 ] range.
27 */
28#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
29#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
30#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
31
32/*
33 * Helpers for converting nanosecond timing to jiffy resolution
34 */
35#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
36
Li Zefancc1f4b12013-03-05 16:06:09 +080037/*
38 * Increase resolution of nice-level calculations for 64-bit architectures.
39 * The extra resolution improves shares distribution and load balancing of
40 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
41 * hierarchies, especially on larger systems. This is not a user-visible change
42 * and does not change the user-interface for setting shares/weights.
43 *
44 * We increase resolution only if we have enough bits to allow this increased
45 * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
46 * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
47 * increased costs.
48 */
49#if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
50# define SCHED_LOAD_RESOLUTION 10
51# define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
52# define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
53#else
54# define SCHED_LOAD_RESOLUTION 0
55# define scale_load(w) (w)
56# define scale_load_down(w) (w)
57#endif
58
59#define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
60#define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
61
Peter Zijlstra029632f2011-10-25 10:00:11 +020062#define NICE_0_LOAD SCHED_LOAD_SCALE
63#define NICE_0_SHIFT SCHED_LOAD_SHIFT
64
65/*
66 * These are the 'tuning knobs' of the scheduler:
Peter Zijlstra029632f2011-10-25 10:00:11 +020067 */
Peter Zijlstra029632f2011-10-25 10:00:11 +020068
69/*
70 * single value that denotes runtime == period, ie unlimited time.
71 */
72#define RUNTIME_INF ((u64)~0ULL)
73
74static inline int rt_policy(int policy)
75{
76 if (policy == SCHED_FIFO || policy == SCHED_RR)
77 return 1;
78 return 0;
79}
80
81static inline int task_has_rt_policy(struct task_struct *p)
82{
83 return rt_policy(p->policy);
84}
85
86/*
87 * This is the priority-queue data structure of the RT scheduling class:
88 */
89struct rt_prio_array {
90 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
91 struct list_head queue[MAX_RT_PRIO];
92};
93
94struct rt_bandwidth {
95 /* nests inside the rq lock: */
96 raw_spinlock_t rt_runtime_lock;
97 ktime_t rt_period;
98 u64 rt_runtime;
99 struct hrtimer rt_period_timer;
100};
101
102extern struct mutex sched_domains_mutex;
103
104#ifdef CONFIG_CGROUP_SCHED
105
106#include <linux/cgroup.h>
107
108struct cfs_rq;
109struct rt_rq;
110
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200111extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200112
113struct cfs_bandwidth {
114#ifdef CONFIG_CFS_BANDWIDTH
115 raw_spinlock_t lock;
116 ktime_t period;
117 u64 quota, runtime;
118 s64 hierarchal_quota;
119 u64 runtime_expires;
120
121 int idle, timer_active;
122 struct hrtimer period_timer, slack_timer;
123 struct list_head throttled_cfs_rq;
124
125 /* statistics */
126 int nr_periods, nr_throttled;
127 u64 throttled_time;
128#endif
129};
130
131/* task group related information */
132struct task_group {
133 struct cgroup_subsys_state css;
134
135#ifdef CONFIG_FAIR_GROUP_SCHED
136 /* schedulable entities of this group on each cpu */
137 struct sched_entity **se;
138 /* runqueue "owned" by this group on each cpu */
139 struct cfs_rq **cfs_rq;
140 unsigned long shares;
141
142 atomic_t load_weight;
Paul Turnerc566e8e2012-10-04 13:18:30 +0200143 atomic64_t load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +0200144 atomic_t runnable_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200145#endif
146
147#ifdef CONFIG_RT_GROUP_SCHED
148 struct sched_rt_entity **rt_se;
149 struct rt_rq **rt_rq;
150
151 struct rt_bandwidth rt_bandwidth;
152#endif
153
154 struct rcu_head rcu;
155 struct list_head list;
156
157 struct task_group *parent;
158 struct list_head siblings;
159 struct list_head children;
160
161#ifdef CONFIG_SCHED_AUTOGROUP
162 struct autogroup *autogroup;
163#endif
164
165 struct cfs_bandwidth cfs_bandwidth;
166};
167
168#ifdef CONFIG_FAIR_GROUP_SCHED
169#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
170
171/*
172 * A weight of 0 or 1 can cause arithmetics problems.
173 * A weight of a cfs_rq is the sum of weights of which entities
174 * are queued on this cfs_rq, so a weight of a entity should not be
175 * too large, so as the shares value of a task group.
176 * (The default weight is 1024 - so there's no practical
177 * limitation from this.)
178 */
179#define MIN_SHARES (1UL << 1)
180#define MAX_SHARES (1UL << 18)
181#endif
182
Peter Zijlstra029632f2011-10-25 10:00:11 +0200183typedef int (*tg_visitor)(struct task_group *, void *);
184
185extern int walk_tg_tree_from(struct task_group *from,
186 tg_visitor down, tg_visitor up, void *data);
187
188/*
189 * Iterate the full tree, calling @down when first entering a node and @up when
190 * leaving it for the final time.
191 *
192 * Caller must hold rcu_lock or sufficient equivalent.
193 */
194static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
195{
196 return walk_tg_tree_from(&root_task_group, down, up, data);
197}
198
199extern int tg_nop(struct task_group *tg, void *data);
200
201extern void free_fair_sched_group(struct task_group *tg);
202extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
203extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
204extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
205 struct sched_entity *se, int cpu,
206 struct sched_entity *parent);
207extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
208extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
209
210extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
211extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
212extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
213
214extern void free_rt_sched_group(struct task_group *tg);
215extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
216extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
217 struct sched_rt_entity *rt_se, int cpu,
218 struct sched_rt_entity *parent);
219
Li Zefan25cc7da2013-03-05 16:07:33 +0800220extern struct task_group *sched_create_group(struct task_group *parent);
221extern void sched_online_group(struct task_group *tg,
222 struct task_group *parent);
223extern void sched_destroy_group(struct task_group *tg);
224extern void sched_offline_group(struct task_group *tg);
225
226extern void sched_move_task(struct task_struct *tsk);
227
228#ifdef CONFIG_FAIR_GROUP_SCHED
229extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
230#endif
231
Peter Zijlstra029632f2011-10-25 10:00:11 +0200232#else /* CONFIG_CGROUP_SCHED */
233
234struct cfs_bandwidth { };
235
236#endif /* CONFIG_CGROUP_SCHED */
237
238/* CFS-related fields in a runqueue */
239struct cfs_rq {
240 struct load_weight load;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200241 unsigned int nr_running, h_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200242
243 u64 exec_clock;
244 u64 min_vruntime;
245#ifndef CONFIG_64BIT
246 u64 min_vruntime_copy;
247#endif
248
249 struct rb_root tasks_timeline;
250 struct rb_node *rb_leftmost;
251
Peter Zijlstra029632f2011-10-25 10:00:11 +0200252 /*
253 * 'curr' points to currently running entity on this cfs_rq.
254 * It is set to NULL otherwise (i.e when none are currently running).
255 */
256 struct sched_entity *curr, *next, *last, *skip;
257
258#ifdef CONFIG_SCHED_DEBUG
259 unsigned int nr_spread_over;
260#endif
261
Paul Turner2dac7542012-10-04 13:18:30 +0200262#ifdef CONFIG_SMP
Paul Turnerf4e26b12012-10-04 13:18:32 +0200263/*
264 * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
265 * removed when useful for applications beyond shares distribution (e.g.
266 * load-balance).
267 */
268#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner2dac7542012-10-04 13:18:30 +0200269 /*
270 * CFS Load tracking
271 * Under CFS, load is tracked on a per-entity basis and aggregated up.
272 * This allows for the description of both thread and group usage (in
273 * the FAIR_GROUP_SCHED case).
274 */
Paul Turner9ee474f2012-10-04 13:18:30 +0200275 u64 runnable_load_avg, blocked_load_avg;
Paul Turneraff3e492012-10-04 13:18:30 +0200276 atomic64_t decay_counter, removed_load;
Paul Turner9ee474f2012-10-04 13:18:30 +0200277 u64 last_decay;
Paul Turnerf4e26b12012-10-04 13:18:32 +0200278#endif /* CONFIG_FAIR_GROUP_SCHED */
279/* These always depend on CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +0200280#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turnerbb17f652012-10-04 13:18:31 +0200281 u32 tg_runnable_contrib;
Paul Turnerc566e8e2012-10-04 13:18:30 +0200282 u64 tg_load_contrib;
Paul Turner82958362012-10-04 13:18:31 +0200283#endif /* CONFIG_FAIR_GROUP_SCHED */
284
285 /*
286 * h_load = weight * f(tg)
287 *
288 * Where f(tg) is the recursive weight fraction assigned to
289 * this group.
290 */
291 unsigned long h_load;
292#endif /* CONFIG_SMP */
293
Peter Zijlstra029632f2011-10-25 10:00:11 +0200294#ifdef CONFIG_FAIR_GROUP_SCHED
295 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
296
297 /*
298 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
299 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
300 * (like users, containers etc.)
301 *
302 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
303 * list is used during load balance.
304 */
305 int on_list;
306 struct list_head leaf_cfs_rq_list;
307 struct task_group *tg; /* group that "owns" this runqueue */
308
Peter Zijlstra029632f2011-10-25 10:00:11 +0200309#ifdef CONFIG_CFS_BANDWIDTH
310 int runtime_enabled;
311 u64 runtime_expires;
312 s64 runtime_remaining;
313
Paul Turnerf1b17282012-10-04 13:18:31 +0200314 u64 throttled_clock, throttled_clock_task;
315 u64 throttled_clock_task_time;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200316 int throttled, throttle_count;
317 struct list_head throttled_list;
318#endif /* CONFIG_CFS_BANDWIDTH */
319#endif /* CONFIG_FAIR_GROUP_SCHED */
320};
321
322static inline int rt_bandwidth_enabled(void)
323{
324 return sysctl_sched_rt_runtime >= 0;
325}
326
327/* Real-Time classes' related field in a runqueue: */
328struct rt_rq {
329 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200330 unsigned int rt_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200331#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
332 struct {
333 int curr; /* highest queued rt task prio */
334#ifdef CONFIG_SMP
335 int next; /* next highest */
336#endif
337 } highest_prio;
338#endif
339#ifdef CONFIG_SMP
340 unsigned long rt_nr_migratory;
341 unsigned long rt_nr_total;
342 int overloaded;
343 struct plist_head pushable_tasks;
344#endif
345 int rt_throttled;
346 u64 rt_time;
347 u64 rt_runtime;
348 /* Nests inside the rq lock: */
349 raw_spinlock_t rt_runtime_lock;
350
351#ifdef CONFIG_RT_GROUP_SCHED
352 unsigned long rt_nr_boosted;
353
354 struct rq *rq;
355 struct list_head leaf_rt_rq_list;
356 struct task_group *tg;
357#endif
358};
359
360#ifdef CONFIG_SMP
361
362/*
363 * We add the notion of a root-domain which will be used to define per-domain
364 * variables. Each exclusive cpuset essentially defines an island domain by
365 * fully partitioning the member cpus from any other cpuset. Whenever a new
366 * exclusive cpuset is created, we also create and attach a new root-domain
367 * object.
368 *
369 */
370struct root_domain {
371 atomic_t refcount;
372 atomic_t rto_count;
373 struct rcu_head rcu;
374 cpumask_var_t span;
375 cpumask_var_t online;
376
377 /*
378 * The "RT overload" flag: it gets set if a CPU has more than
379 * one runnable RT task.
380 */
381 cpumask_var_t rto_mask;
382 struct cpupri cpupri;
383};
384
385extern struct root_domain def_root_domain;
386
387#endif /* CONFIG_SMP */
388
389/*
390 * This is the main, per-CPU runqueue data structure.
391 *
392 * Locking rule: those places that want to lock multiple runqueues
393 * (such as the load balancing or the thread migration code), lock
394 * acquire operations must be ordered by ascending &runqueue.
395 */
396struct rq {
397 /* runqueue lock: */
398 raw_spinlock_t lock;
399
400 /*
401 * nr_running and cpu_load should be in the same cacheline because
402 * remote CPUs use both these fields when doing load calculation.
403 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200404 unsigned int nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200405 #define CPU_LOAD_IDX_MAX 5
406 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
407 unsigned long last_load_update_tick;
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200408#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra029632f2011-10-25 10:00:11 +0200409 u64 nohz_stamp;
Suresh Siddha1c792db2011-12-01 17:07:32 -0800410 unsigned long nohz_flags;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200411#endif
412 int skip_clock_update;
413
414 /* capture load from *all* tasks on this cpu: */
415 struct load_weight load;
416 unsigned long nr_load_updates;
417 u64 nr_switches;
418
419 struct cfs_rq cfs;
420 struct rt_rq rt;
421
422#ifdef CONFIG_FAIR_GROUP_SCHED
423 /* list of leaf cfs_rq on this cpu: */
424 struct list_head leaf_cfs_rq_list;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200425#ifdef CONFIG_SMP
426 unsigned long h_load_throttle;
427#endif /* CONFIG_SMP */
428#endif /* CONFIG_FAIR_GROUP_SCHED */
429
Peter Zijlstra029632f2011-10-25 10:00:11 +0200430#ifdef CONFIG_RT_GROUP_SCHED
431 struct list_head leaf_rt_rq_list;
432#endif
433
434 /*
435 * This is part of a global counter where only the total sum
436 * over all CPUs matters. A task can increase this counter on
437 * one CPU and if it got migrated afterwards it may decrease
438 * it on another CPU. Always updated under the runqueue lock:
439 */
440 unsigned long nr_uninterruptible;
441
442 struct task_struct *curr, *idle, *stop;
443 unsigned long next_balance;
444 struct mm_struct *prev_mm;
445
446 u64 clock;
447 u64 clock_task;
448
449 atomic_t nr_iowait;
450
451#ifdef CONFIG_SMP
452 struct root_domain *rd;
453 struct sched_domain *sd;
454
455 unsigned long cpu_power;
456
457 unsigned char idle_balance;
458 /* For active balancing */
459 int post_schedule;
460 int active_balance;
461 int push_cpu;
462 struct cpu_stop_work active_balance_work;
463 /* cpu of this runqueue: */
464 int cpu;
465 int online;
466
Peter Zijlstra367456c2012-02-20 21:49:09 +0100467 struct list_head cfs_tasks;
468
Peter Zijlstra029632f2011-10-25 10:00:11 +0200469 u64 rt_avg;
470 u64 age_stamp;
471 u64 idle_stamp;
472 u64 avg_idle;
473#endif
474
475#ifdef CONFIG_IRQ_TIME_ACCOUNTING
476 u64 prev_irq_time;
477#endif
478#ifdef CONFIG_PARAVIRT
479 u64 prev_steal_time;
480#endif
481#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
482 u64 prev_steal_time_rq;
483#endif
484
485 /* calc_load related fields */
486 unsigned long calc_load_update;
487 long calc_load_active;
488
489#ifdef CONFIG_SCHED_HRTICK
490#ifdef CONFIG_SMP
491 int hrtick_csd_pending;
492 struct call_single_data hrtick_csd;
493#endif
494 struct hrtimer hrtick_timer;
495#endif
496
497#ifdef CONFIG_SCHEDSTATS
498 /* latency stats */
499 struct sched_info rq_sched_info;
500 unsigned long long rq_cpu_time;
501 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
502
503 /* sys_sched_yield() stats */
504 unsigned int yld_count;
505
506 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200507 unsigned int sched_count;
508 unsigned int sched_goidle;
509
510 /* try_to_wake_up() stats */
511 unsigned int ttwu_count;
512 unsigned int ttwu_local;
513#endif
514
515#ifdef CONFIG_SMP
516 struct llist_head wake_list;
517#endif
Ben Segall18bf2802012-10-04 12:51:20 +0200518
519 struct sched_avg avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200520};
521
522static inline int cpu_of(struct rq *rq)
523{
524#ifdef CONFIG_SMP
525 return rq->cpu;
526#else
527 return 0;
528#endif
529}
530
531DECLARE_PER_CPU(struct rq, runqueues);
532
Peter Zijlstra518cd622011-12-07 15:07:31 +0100533#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
534#define this_rq() (&__get_cpu_var(runqueues))
535#define task_rq(p) cpu_rq(task_cpu(p))
536#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
537#define raw_rq() (&__raw_get_cpu_var(runqueues))
538
539#ifdef CONFIG_SMP
540
Peter Zijlstra029632f2011-10-25 10:00:11 +0200541#define rcu_dereference_check_sched_domain(p) \
542 rcu_dereference_check((p), \
543 lockdep_is_held(&sched_domains_mutex))
544
545/*
546 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
547 * See detach_destroy_domains: synchronize_sched for details.
548 *
549 * The domain tree of any CPU may only be accessed from within
550 * preempt-disabled sections.
551 */
552#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +0100553 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
554 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200555
Suresh Siddha77e81362011-11-17 11:08:23 -0800556#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
557
Peter Zijlstra518cd622011-12-07 15:07:31 +0100558/**
559 * highest_flag_domain - Return highest sched_domain containing flag.
560 * @cpu: The cpu whose highest level of sched domain is to
561 * be returned.
562 * @flag: The flag to check for the highest sched_domain
563 * for the given cpu.
564 *
565 * Returns the highest sched_domain of a cpu which contains the given flag.
566 */
567static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
568{
569 struct sched_domain *sd, *hsd = NULL;
570
571 for_each_domain(cpu, sd) {
572 if (!(sd->flags & flag))
573 break;
574 hsd = sd;
575 }
576
577 return hsd;
578}
579
580DECLARE_PER_CPU(struct sched_domain *, sd_llc);
581DECLARE_PER_CPU(int, sd_llc_id);
582
Li Zefan5e6521e2013-03-05 16:06:23 +0800583struct sched_group_power {
584 atomic_t ref;
585 /*
586 * CPU power of this group, SCHED_LOAD_SCALE being max power for a
587 * single CPU.
588 */
589 unsigned int power, power_orig;
590 unsigned long next_update;
591 /*
592 * Number of busy cpus in this group.
593 */
594 atomic_t nr_busy_cpus;
595
596 unsigned long cpumask[0]; /* iteration mask */
597};
598
599struct sched_group {
600 struct sched_group *next; /* Must be a circular list */
601 atomic_t ref;
602
603 unsigned int group_weight;
604 struct sched_group_power *sgp;
605
606 /*
607 * The CPUs this group covers.
608 *
609 * NOTE: this field is variable length. (Allocated dynamically
610 * by attaching extra space to the end of the structure,
611 * depending on how many CPUs the kernel has booted up with)
612 */
613 unsigned long cpumask[0];
614};
615
616static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
617{
618 return to_cpumask(sg->cpumask);
619}
620
621/*
622 * cpumask masking which cpus in the group are allowed to iterate up the domain
623 * tree.
624 */
625static inline struct cpumask *sched_group_mask(struct sched_group *sg)
626{
627 return to_cpumask(sg->sgp->cpumask);
628}
629
630/**
631 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
632 * @group: The group whose first cpu is to be returned.
633 */
634static inline unsigned int group_first_cpu(struct sched_group *group)
635{
636 return cpumask_first(sched_group_cpus(group));
637}
638
Peter Zijlstrac1174872012-05-31 14:47:33 +0200639extern int group_balance_cpu(struct sched_group *sg);
640
Peter Zijlstra518cd622011-12-07 15:07:31 +0100641#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200642
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100643#include "stats.h"
644#include "auto_group.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +0200645
646#ifdef CONFIG_CGROUP_SCHED
647
648/*
649 * Return the group to which this tasks belongs.
650 *
Peter Zijlstra8323f262012-06-22 13:36:05 +0200651 * We cannot use task_subsys_state() and friends because the cgroup
652 * subsystem changes that value before the cgroup_subsys::attach() method
653 * is called, therefore we cannot pin it and might observe the wrong value.
654 *
655 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
656 * core changes this before calling sched_move_task().
657 *
658 * Instead we use a 'copy' which is updated from sched_move_task() while
659 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +0200660 */
661static inline struct task_group *task_group(struct task_struct *p)
662{
Peter Zijlstra8323f262012-06-22 13:36:05 +0200663 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200664}
665
666/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
667static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
668{
669#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
670 struct task_group *tg = task_group(p);
671#endif
672
673#ifdef CONFIG_FAIR_GROUP_SCHED
674 p->se.cfs_rq = tg->cfs_rq[cpu];
675 p->se.parent = tg->se[cpu];
676#endif
677
678#ifdef CONFIG_RT_GROUP_SCHED
679 p->rt.rt_rq = tg->rt_rq[cpu];
680 p->rt.parent = tg->rt_se[cpu];
681#endif
682}
683
684#else /* CONFIG_CGROUP_SCHED */
685
686static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
687static inline struct task_group *task_group(struct task_struct *p)
688{
689 return NULL;
690}
691
692#endif /* CONFIG_CGROUP_SCHED */
693
694static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
695{
696 set_task_rq(p, cpu);
697#ifdef CONFIG_SMP
698 /*
699 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
700 * successfuly executed on another CPU. We must ensure that updates of
701 * per-task data have been completed by this moment.
702 */
703 smp_wmb();
704 task_thread_info(p)->cpu = cpu;
705#endif
706}
707
708/*
709 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
710 */
711#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +0100712# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +0200713# define const_debug __read_mostly
714#else
715# define const_debug const
716#endif
717
718extern const_debug unsigned int sysctl_sched_features;
719
720#define SCHED_FEAT(name, enabled) \
721 __SCHED_FEAT_##name ,
722
723enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100724#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200725 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +0200726};
727
728#undef SCHED_FEAT
729
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200730#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100731static __always_inline bool static_branch__true(struct static_key *key)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200732{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100733 return static_key_true(key); /* Not out of line branch. */
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200734}
735
Ingo Molnarc5905af2012-02-24 08:31:31 +0100736static __always_inline bool static_branch__false(struct static_key *key)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200737{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100738 return static_key_false(key); /* Out of line branch. */
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200739}
740
741#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +0100742static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200743{ \
744 return static_branch__##enabled(key); \
745}
746
747#include "features.h"
748
749#undef SCHED_FEAT
750
Ingo Molnarc5905af2012-02-24 08:31:31 +0100751extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200752#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
753#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200754#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200755#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200756
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200757#ifdef CONFIG_NUMA_BALANCING
758#define sched_feat_numa(x) sched_feat(x)
Mel Gorman3105b862012-11-23 11:23:49 +0000759#ifdef CONFIG_SCHED_DEBUG
760#define numabalancing_enabled sched_feat_numa(NUMA)
761#else
762extern bool numabalancing_enabled;
763#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200764#else
765#define sched_feat_numa(x) (0)
Mel Gorman3105b862012-11-23 11:23:49 +0000766#define numabalancing_enabled (0)
767#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200768
Peter Zijlstra029632f2011-10-25 10:00:11 +0200769static inline u64 global_rt_period(void)
770{
771 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
772}
773
774static inline u64 global_rt_runtime(void)
775{
776 if (sysctl_sched_rt_runtime < 0)
777 return RUNTIME_INF;
778
779 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
780}
781
782
783
784static inline int task_current(struct rq *rq, struct task_struct *p)
785{
786 return rq->curr == p;
787}
788
789static inline int task_running(struct rq *rq, struct task_struct *p)
790{
791#ifdef CONFIG_SMP
792 return p->on_cpu;
793#else
794 return task_current(rq, p);
795#endif
796}
797
798
799#ifndef prepare_arch_switch
800# define prepare_arch_switch(next) do { } while (0)
801#endif
802#ifndef finish_arch_switch
803# define finish_arch_switch(prev) do { } while (0)
804#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +0000805#ifndef finish_arch_post_lock_switch
806# define finish_arch_post_lock_switch() do { } while (0)
807#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200808
809#ifndef __ARCH_WANT_UNLOCKED_CTXSW
810static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
811{
812#ifdef CONFIG_SMP
813 /*
814 * We can optimise this out completely for !SMP, because the
815 * SMP rebalancing from interrupt is the only thing that cares
816 * here.
817 */
818 next->on_cpu = 1;
819#endif
820}
821
822static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
823{
824#ifdef CONFIG_SMP
825 /*
826 * After ->on_cpu is cleared, the task can be moved to a different CPU.
827 * We must ensure this doesn't happen until the switch is completely
828 * finished.
829 */
830 smp_wmb();
831 prev->on_cpu = 0;
832#endif
833#ifdef CONFIG_DEBUG_SPINLOCK
834 /* this is a valid case when another task releases the spinlock */
835 rq->lock.owner = current;
836#endif
837 /*
838 * If we are tracking spinlock dependencies then we have to
839 * fix up the runqueue lock - which gets 'carried over' from
840 * prev into current:
841 */
842 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
843
844 raw_spin_unlock_irq(&rq->lock);
845}
846
847#else /* __ARCH_WANT_UNLOCKED_CTXSW */
848static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
849{
850#ifdef CONFIG_SMP
851 /*
852 * We can optimise this out completely for !SMP, because the
853 * SMP rebalancing from interrupt is the only thing that cares
854 * here.
855 */
856 next->on_cpu = 1;
857#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200858 raw_spin_unlock(&rq->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200859}
860
861static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
862{
863#ifdef CONFIG_SMP
864 /*
865 * After ->on_cpu is cleared, the task can be moved to a different CPU.
866 * We must ensure this doesn't happen until the switch is completely
867 * finished.
868 */
869 smp_wmb();
870 prev->on_cpu = 0;
871#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200872 local_irq_enable();
Peter Zijlstra029632f2011-10-25 10:00:11 +0200873}
874#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
875
Li Zefanb13095f2013-03-05 16:06:38 +0800876/*
877 * wake flags
878 */
879#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
880#define WF_FORK 0x02 /* child wakeup after fork */
881#define WF_MIGRATED 0x4 /* internal use, task got migrated */
882
Peter Zijlstra029632f2011-10-25 10:00:11 +0200883static inline void update_load_add(struct load_weight *lw, unsigned long inc)
884{
885 lw->weight += inc;
886 lw->inv_weight = 0;
887}
888
889static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
890{
891 lw->weight -= dec;
892 lw->inv_weight = 0;
893}
894
895static inline void update_load_set(struct load_weight *lw, unsigned long w)
896{
897 lw->weight = w;
898 lw->inv_weight = 0;
899}
900
901/*
902 * To aid in avoiding the subversion of "niceness" due to uneven distribution
903 * of tasks with abnormal "nice" values across CPUs the contribution that
904 * each task makes to its run queue's load is weighted according to its
905 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
906 * scaled version of the new time slice allocation that they receive on time
907 * slice expiry etc.
908 */
909
910#define WEIGHT_IDLEPRIO 3
911#define WMULT_IDLEPRIO 1431655765
912
913/*
914 * Nice levels are multiplicative, with a gentle 10% change for every
915 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
916 * nice 1, it will get ~10% less CPU time than another CPU-bound task
917 * that remained on nice 0.
918 *
919 * The "10% effect" is relative and cumulative: from _any_ nice level,
920 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
921 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
922 * If a task goes up by ~10% and another task goes down by ~10% then
923 * the relative distance between them is ~25%.)
924 */
925static const int prio_to_weight[40] = {
926 /* -20 */ 88761, 71755, 56483, 46273, 36291,
927 /* -15 */ 29154, 23254, 18705, 14949, 11916,
928 /* -10 */ 9548, 7620, 6100, 4904, 3906,
929 /* -5 */ 3121, 2501, 1991, 1586, 1277,
930 /* 0 */ 1024, 820, 655, 526, 423,
931 /* 5 */ 335, 272, 215, 172, 137,
932 /* 10 */ 110, 87, 70, 56, 45,
933 /* 15 */ 36, 29, 23, 18, 15,
934};
935
936/*
937 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
938 *
939 * In cases where the weight does not change often, we can use the
940 * precalculated inverse to speed up arithmetics by turning divisions
941 * into multiplications:
942 */
943static const u32 prio_to_wmult[40] = {
944 /* -20 */ 48388, 59856, 76040, 92818, 118348,
945 /* -15 */ 147320, 184698, 229616, 287308, 360437,
946 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
947 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
948 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
949 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
950 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
951 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
952};
953
954/* Time spent by the tasks of the cpu accounting group executing in ... */
955enum cpuacct_stat_index {
956 CPUACCT_STAT_USER, /* ... user mode */
957 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
958
959 CPUACCT_STAT_NSTATS,
960};
961
Li Zefanc82ba9f2013-03-05 16:06:55 +0800962#define ENQUEUE_WAKEUP 1
963#define ENQUEUE_HEAD 2
964#ifdef CONFIG_SMP
965#define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
966#else
967#define ENQUEUE_WAKING 0
968#endif
969
970#define DEQUEUE_SLEEP 1
971
972struct sched_class {
973 const struct sched_class *next;
974
975 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
976 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
977 void (*yield_task) (struct rq *rq);
978 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
979
980 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
981
982 struct task_struct * (*pick_next_task) (struct rq *rq);
983 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
984
985#ifdef CONFIG_SMP
986 int (*select_task_rq)(struct task_struct *p, int sd_flag, int flags);
987 void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
988
989 void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
990 void (*post_schedule) (struct rq *this_rq);
991 void (*task_waking) (struct task_struct *task);
992 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
993
994 void (*set_cpus_allowed)(struct task_struct *p,
995 const struct cpumask *newmask);
996
997 void (*rq_online)(struct rq *rq);
998 void (*rq_offline)(struct rq *rq);
999#endif
1000
1001 void (*set_curr_task) (struct rq *rq);
1002 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1003 void (*task_fork) (struct task_struct *p);
1004
1005 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1006 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1007 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1008 int oldprio);
1009
1010 unsigned int (*get_rr_interval) (struct rq *rq,
1011 struct task_struct *task);
1012
1013#ifdef CONFIG_FAIR_GROUP_SCHED
1014 void (*task_move_group) (struct task_struct *p, int on_rq);
1015#endif
1016};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001017
1018#define sched_class_highest (&stop_sched_class)
1019#define for_each_class(class) \
1020 for (class = sched_class_highest; class; class = class->next)
1021
1022extern const struct sched_class stop_sched_class;
1023extern const struct sched_class rt_sched_class;
1024extern const struct sched_class fair_sched_class;
1025extern const struct sched_class idle_sched_class;
1026
1027
1028#ifdef CONFIG_SMP
1029
Li Zefanb7192032013-03-07 10:00:26 +08001030extern void update_group_power(struct sched_domain *sd, int cpu);
1031
Peter Zijlstra029632f2011-10-25 10:00:11 +02001032extern void trigger_load_balance(struct rq *rq, int cpu);
1033extern void idle_balance(int this_cpu, struct rq *this_rq);
1034
1035#else /* CONFIG_SMP */
1036
1037static inline void idle_balance(int cpu, struct rq *rq)
1038{
1039}
1040
1041#endif
1042
1043extern void sysrq_sched_debug_show(void);
1044extern void sched_init_granularity(void);
1045extern void update_max_interval(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001046extern int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu);
1047extern void init_sched_rt_class(void);
1048extern void init_sched_fair_class(void);
1049
1050extern void resched_task(struct task_struct *p);
1051extern void resched_cpu(int cpu);
1052
1053extern struct rt_bandwidth def_rt_bandwidth;
1054extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1055
Peter Zijlstra556061b2012-05-11 17:31:26 +02001056extern void update_idle_cpu_load(struct rq *this_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001057
1058#ifdef CONFIG_CGROUP_CPUACCT
Glauber Costa54c707e2011-11-28 14:45:19 -02001059#include <linux/cgroup.h>
1060/* track cpu usage of a group of tasks and its child groups */
1061struct cpuacct {
1062 struct cgroup_subsys_state css;
1063 /* cpuusage holds pointer to a u64-type object on every cpu */
1064 u64 __percpu *cpuusage;
1065 struct kernel_cpustat __percpu *cpustat;
1066};
1067
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001068extern struct cgroup_subsys cpuacct_subsys;
1069extern struct cpuacct root_cpuacct;
1070
Glauber Costa54c707e2011-11-28 14:45:19 -02001071/* return cpu accounting group corresponding to this container */
1072static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
1073{
1074 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
1075 struct cpuacct, css);
1076}
1077
1078/* return cpu accounting group to which this task belongs */
1079static inline struct cpuacct *task_ca(struct task_struct *tsk)
1080{
1081 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
1082 struct cpuacct, css);
1083}
1084
1085static inline struct cpuacct *parent_ca(struct cpuacct *ca)
1086{
1087 if (!ca || !ca->css.cgroup->parent)
1088 return NULL;
1089 return cgroup_ca(ca->css.cgroup->parent);
1090}
1091
Peter Zijlstra029632f2011-10-25 10:00:11 +02001092extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001093#else
1094static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001095#endif
1096
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001097#ifdef CONFIG_PARAVIRT
1098static inline u64 steal_ticks(u64 steal)
1099{
1100 if (unlikely(steal > NSEC_PER_SEC))
1101 return div_u64(steal, TICK_NSEC);
1102
1103 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1104}
1105#endif
1106
Peter Zijlstra029632f2011-10-25 10:00:11 +02001107static inline void inc_nr_running(struct rq *rq)
1108{
1109 rq->nr_running++;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001110
1111#ifdef CONFIG_NO_HZ_FULL
1112 if (rq->nr_running == 2) {
1113 if (tick_nohz_full_cpu(rq->cpu)) {
1114 /* Order rq->nr_running write against the IPI */
1115 smp_wmb();
1116 smp_send_reschedule(rq->cpu);
1117 }
1118 }
1119#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001120}
1121
1122static inline void dec_nr_running(struct rq *rq)
1123{
1124 rq->nr_running--;
1125}
1126
1127extern void update_rq_clock(struct rq *rq);
1128
1129extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1130extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1131
1132extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1133
1134extern const_debug unsigned int sysctl_sched_time_avg;
1135extern const_debug unsigned int sysctl_sched_nr_migrate;
1136extern const_debug unsigned int sysctl_sched_migration_cost;
1137
1138static inline u64 sched_avg_period(void)
1139{
1140 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1141}
1142
Peter Zijlstra029632f2011-10-25 10:00:11 +02001143#ifdef CONFIG_SCHED_HRTICK
1144
1145/*
1146 * Use hrtick when:
1147 * - enabled by features
1148 * - hrtimer is actually high res
1149 */
1150static inline int hrtick_enabled(struct rq *rq)
1151{
1152 if (!sched_feat(HRTICK))
1153 return 0;
1154 if (!cpu_active(cpu_of(rq)))
1155 return 0;
1156 return hrtimer_is_hres_active(&rq->hrtick_timer);
1157}
1158
1159void hrtick_start(struct rq *rq, u64 delay);
1160
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001161#else
1162
1163static inline int hrtick_enabled(struct rq *rq)
1164{
1165 return 0;
1166}
1167
Peter Zijlstra029632f2011-10-25 10:00:11 +02001168#endif /* CONFIG_SCHED_HRTICK */
1169
1170#ifdef CONFIG_SMP
1171extern void sched_avg_update(struct rq *rq);
1172static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1173{
1174 rq->rt_avg += rt_delta;
1175 sched_avg_update(rq);
1176}
1177#else
1178static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1179static inline void sched_avg_update(struct rq *rq) { }
1180#endif
1181
1182extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
1183
1184#ifdef CONFIG_SMP
1185#ifdef CONFIG_PREEMPT
1186
1187static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1188
1189/*
1190 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1191 * way at the expense of forcing extra atomic operations in all
1192 * invocations. This assures that the double_lock is acquired using the
1193 * same underlying policy as the spinlock_t on this architecture, which
1194 * reduces latency compared to the unfair variant below. However, it
1195 * also adds more overhead and therefore may reduce throughput.
1196 */
1197static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1198 __releases(this_rq->lock)
1199 __acquires(busiest->lock)
1200 __acquires(this_rq->lock)
1201{
1202 raw_spin_unlock(&this_rq->lock);
1203 double_rq_lock(this_rq, busiest);
1204
1205 return 1;
1206}
1207
1208#else
1209/*
1210 * Unfair double_lock_balance: Optimizes throughput at the expense of
1211 * latency by eliminating extra atomic operations when the locks are
1212 * already in proper order on entry. This favors lower cpu-ids and will
1213 * grant the double lock to lower cpus over higher ids under contention,
1214 * regardless of entry order into the function.
1215 */
1216static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1217 __releases(this_rq->lock)
1218 __acquires(busiest->lock)
1219 __acquires(this_rq->lock)
1220{
1221 int ret = 0;
1222
1223 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1224 if (busiest < this_rq) {
1225 raw_spin_unlock(&this_rq->lock);
1226 raw_spin_lock(&busiest->lock);
1227 raw_spin_lock_nested(&this_rq->lock,
1228 SINGLE_DEPTH_NESTING);
1229 ret = 1;
1230 } else
1231 raw_spin_lock_nested(&busiest->lock,
1232 SINGLE_DEPTH_NESTING);
1233 }
1234 return ret;
1235}
1236
1237#endif /* CONFIG_PREEMPT */
1238
1239/*
1240 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1241 */
1242static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1243{
1244 if (unlikely(!irqs_disabled())) {
1245 /* printk() doesn't work good under rq->lock */
1246 raw_spin_unlock(&this_rq->lock);
1247 BUG_ON(1);
1248 }
1249
1250 return _double_lock_balance(this_rq, busiest);
1251}
1252
1253static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1254 __releases(busiest->lock)
1255{
1256 raw_spin_unlock(&busiest->lock);
1257 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1258}
1259
1260/*
1261 * double_rq_lock - safely lock two runqueues
1262 *
1263 * Note this does not disable interrupts like task_rq_lock,
1264 * you need to do so manually before calling.
1265 */
1266static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1267 __acquires(rq1->lock)
1268 __acquires(rq2->lock)
1269{
1270 BUG_ON(!irqs_disabled());
1271 if (rq1 == rq2) {
1272 raw_spin_lock(&rq1->lock);
1273 __acquire(rq2->lock); /* Fake it out ;) */
1274 } else {
1275 if (rq1 < rq2) {
1276 raw_spin_lock(&rq1->lock);
1277 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1278 } else {
1279 raw_spin_lock(&rq2->lock);
1280 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1281 }
1282 }
1283}
1284
1285/*
1286 * double_rq_unlock - safely unlock two runqueues
1287 *
1288 * Note this does not restore interrupts like task_rq_unlock,
1289 * you need to do so manually after calling.
1290 */
1291static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1292 __releases(rq1->lock)
1293 __releases(rq2->lock)
1294{
1295 raw_spin_unlock(&rq1->lock);
1296 if (rq1 != rq2)
1297 raw_spin_unlock(&rq2->lock);
1298 else
1299 __release(rq2->lock);
1300}
1301
1302#else /* CONFIG_SMP */
1303
1304/*
1305 * double_rq_lock - safely lock two runqueues
1306 *
1307 * Note this does not disable interrupts like task_rq_lock,
1308 * you need to do so manually before calling.
1309 */
1310static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1311 __acquires(rq1->lock)
1312 __acquires(rq2->lock)
1313{
1314 BUG_ON(!irqs_disabled());
1315 BUG_ON(rq1 != rq2);
1316 raw_spin_lock(&rq1->lock);
1317 __acquire(rq2->lock); /* Fake it out ;) */
1318}
1319
1320/*
1321 * double_rq_unlock - safely unlock two runqueues
1322 *
1323 * Note this does not restore interrupts like task_rq_unlock,
1324 * you need to do so manually after calling.
1325 */
1326static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1327 __releases(rq1->lock)
1328 __releases(rq2->lock)
1329{
1330 BUG_ON(rq1 != rq2);
1331 raw_spin_unlock(&rq1->lock);
1332 __release(rq2->lock);
1333}
1334
1335#endif
1336
1337extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1338extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1339extern void print_cfs_stats(struct seq_file *m, int cpu);
1340extern void print_rt_stats(struct seq_file *m, int cpu);
1341
1342extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1343extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001344
1345extern void account_cfs_bandwidth_used(int enabled, int was_enabled);
Suresh Siddha1c792db2011-12-01 17:07:32 -08001346
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001347#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08001348enum rq_nohz_flag_bits {
1349 NOHZ_TICK_STOPPED,
1350 NOHZ_BALANCE_KICK,
Suresh Siddha69e1e812011-12-01 17:07:33 -08001351 NOHZ_IDLE,
Suresh Siddha1c792db2011-12-01 17:07:32 -08001352};
1353
1354#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1355#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001356
1357#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1358
1359DECLARE_PER_CPU(u64, cpu_hardirq_time);
1360DECLARE_PER_CPU(u64, cpu_softirq_time);
1361
1362#ifndef CONFIG_64BIT
1363DECLARE_PER_CPU(seqcount_t, irq_time_seq);
1364
1365static inline void irq_time_write_begin(void)
1366{
1367 __this_cpu_inc(irq_time_seq.sequence);
1368 smp_wmb();
1369}
1370
1371static inline void irq_time_write_end(void)
1372{
1373 smp_wmb();
1374 __this_cpu_inc(irq_time_seq.sequence);
1375}
1376
1377static inline u64 irq_time_read(int cpu)
1378{
1379 u64 irq_time;
1380 unsigned seq;
1381
1382 do {
1383 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1384 irq_time = per_cpu(cpu_softirq_time, cpu) +
1385 per_cpu(cpu_hardirq_time, cpu);
1386 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1387
1388 return irq_time;
1389}
1390#else /* CONFIG_64BIT */
1391static inline void irq_time_write_begin(void)
1392{
1393}
1394
1395static inline void irq_time_write_end(void)
1396{
1397}
1398
1399static inline u64 irq_time_read(int cpu)
1400{
1401 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1402}
1403#endif /* CONFIG_64BIT */
1404#endif /* CONFIG_IRQ_TIME_ACCOUNTING */