blob: df023db7721c252f20dc358fe56f5c0d618e43e1 [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>
Mel Gormanf809ca92013-10-07 11:28:57 +01009#include <linux/slab.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020010
Peter Zijlstra391e43d2011-11-15 17:14:39 +010011#include "cpupri.h"
Li Zefan60fed782013-03-29 14:36:43 +080012#include "cpuacct.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +020013
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040014struct rq;
15
Peter Zijlstra029632f2011-10-25 10:00:11 +020016extern __read_mostly int scheduler_running;
17
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040018extern unsigned long calc_load_update;
19extern atomic_long_t calc_load_tasks;
20
21extern long calc_load_fold_active(struct rq *this_rq);
22extern void update_cpu_load_active(struct rq *this_rq);
23
Peter Zijlstra029632f2011-10-25 10:00:11 +020024/*
25 * Convert user-nice values [ -20 ... 0 ... 19 ]
26 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
27 * and back.
28 */
29#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
30#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
31#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
32
33/*
34 * 'User priority' is the nice value converted to something we
35 * can work with better when scaling various scheduler parameters,
36 * it's a [ 0 ... 39 ] range.
37 */
38#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
39#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
40#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
41
42/*
43 * Helpers for converting nanosecond timing to jiffy resolution
44 */
45#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
46
Li Zefancc1f4b12013-03-05 16:06:09 +080047/*
48 * Increase resolution of nice-level calculations for 64-bit architectures.
49 * The extra resolution improves shares distribution and load balancing of
50 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
51 * hierarchies, especially on larger systems. This is not a user-visible change
52 * and does not change the user-interface for setting shares/weights.
53 *
54 * We increase resolution only if we have enough bits to allow this increased
55 * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
56 * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
57 * increased costs.
58 */
59#if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
60# define SCHED_LOAD_RESOLUTION 10
61# define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
62# define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
63#else
64# define SCHED_LOAD_RESOLUTION 0
65# define scale_load(w) (w)
66# define scale_load_down(w) (w)
67#endif
68
69#define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
70#define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
71
Peter Zijlstra029632f2011-10-25 10:00:11 +020072#define NICE_0_LOAD SCHED_LOAD_SCALE
73#define NICE_0_SHIFT SCHED_LOAD_SHIFT
74
75/*
76 * These are the 'tuning knobs' of the scheduler:
Peter Zijlstra029632f2011-10-25 10:00:11 +020077 */
Peter Zijlstra029632f2011-10-25 10:00:11 +020078
79/*
80 * single value that denotes runtime == period, ie unlimited time.
81 */
82#define RUNTIME_INF ((u64)~0ULL)
83
Dario Faggiolid50dde52013-11-07 14:43:36 +010084static inline int fair_policy(int policy)
85{
86 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
87}
88
Peter Zijlstra029632f2011-10-25 10:00:11 +020089static inline int rt_policy(int policy)
90{
Dario Faggiolid50dde52013-11-07 14:43:36 +010091 return policy == SCHED_FIFO || policy == SCHED_RR;
Peter Zijlstra029632f2011-10-25 10:00:11 +020092}
93
94static inline int task_has_rt_policy(struct task_struct *p)
95{
96 return rt_policy(p->policy);
97}
98
99/*
100 * This is the priority-queue data structure of the RT scheduling class:
101 */
102struct rt_prio_array {
103 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
104 struct list_head queue[MAX_RT_PRIO];
105};
106
107struct rt_bandwidth {
108 /* nests inside the rq lock: */
109 raw_spinlock_t rt_runtime_lock;
110 ktime_t rt_period;
111 u64 rt_runtime;
112 struct hrtimer rt_period_timer;
113};
114
115extern struct mutex sched_domains_mutex;
116
117#ifdef CONFIG_CGROUP_SCHED
118
119#include <linux/cgroup.h>
120
121struct cfs_rq;
122struct rt_rq;
123
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200124extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200125
126struct cfs_bandwidth {
127#ifdef CONFIG_CFS_BANDWIDTH
128 raw_spinlock_t lock;
129 ktime_t period;
130 u64 quota, runtime;
131 s64 hierarchal_quota;
132 u64 runtime_expires;
133
134 int idle, timer_active;
135 struct hrtimer period_timer, slack_timer;
136 struct list_head throttled_cfs_rq;
137
138 /* statistics */
139 int nr_periods, nr_throttled;
140 u64 throttled_time;
141#endif
142};
143
144/* task group related information */
145struct task_group {
146 struct cgroup_subsys_state css;
147
148#ifdef CONFIG_FAIR_GROUP_SCHED
149 /* schedulable entities of this group on each cpu */
150 struct sched_entity **se;
151 /* runqueue "owned" by this group on each cpu */
152 struct cfs_rq **cfs_rq;
153 unsigned long shares;
154
Alex Shifa6bdde2013-06-20 10:18:46 +0800155#ifdef CONFIG_SMP
Alex Shibf5b9862013-06-20 10:18:54 +0800156 atomic_long_t load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +0200157 atomic_t runnable_avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200158#endif
Alex Shifa6bdde2013-06-20 10:18:46 +0800159#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200160
161#ifdef CONFIG_RT_GROUP_SCHED
162 struct sched_rt_entity **rt_se;
163 struct rt_rq **rt_rq;
164
165 struct rt_bandwidth rt_bandwidth;
166#endif
167
168 struct rcu_head rcu;
169 struct list_head list;
170
171 struct task_group *parent;
172 struct list_head siblings;
173 struct list_head children;
174
175#ifdef CONFIG_SCHED_AUTOGROUP
176 struct autogroup *autogroup;
177#endif
178
179 struct cfs_bandwidth cfs_bandwidth;
180};
181
182#ifdef CONFIG_FAIR_GROUP_SCHED
183#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
184
185/*
186 * A weight of 0 or 1 can cause arithmetics problems.
187 * A weight of a cfs_rq is the sum of weights of which entities
188 * are queued on this cfs_rq, so a weight of a entity should not be
189 * too large, so as the shares value of a task group.
190 * (The default weight is 1024 - so there's no practical
191 * limitation from this.)
192 */
193#define MIN_SHARES (1UL << 1)
194#define MAX_SHARES (1UL << 18)
195#endif
196
Peter Zijlstra029632f2011-10-25 10:00:11 +0200197typedef int (*tg_visitor)(struct task_group *, void *);
198
199extern int walk_tg_tree_from(struct task_group *from,
200 tg_visitor down, tg_visitor up, void *data);
201
202/*
203 * Iterate the full tree, calling @down when first entering a node and @up when
204 * leaving it for the final time.
205 *
206 * Caller must hold rcu_lock or sufficient equivalent.
207 */
208static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
209{
210 return walk_tg_tree_from(&root_task_group, down, up, data);
211}
212
213extern int tg_nop(struct task_group *tg, void *data);
214
215extern void free_fair_sched_group(struct task_group *tg);
216extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
217extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
218extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
219 struct sched_entity *se, int cpu,
220 struct sched_entity *parent);
221extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
222extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
223
224extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
225extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
226extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
227
228extern void free_rt_sched_group(struct task_group *tg);
229extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
230extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
231 struct sched_rt_entity *rt_se, int cpu,
232 struct sched_rt_entity *parent);
233
Li Zefan25cc7da2013-03-05 16:07:33 +0800234extern struct task_group *sched_create_group(struct task_group *parent);
235extern void sched_online_group(struct task_group *tg,
236 struct task_group *parent);
237extern void sched_destroy_group(struct task_group *tg);
238extern void sched_offline_group(struct task_group *tg);
239
240extern void sched_move_task(struct task_struct *tsk);
241
242#ifdef CONFIG_FAIR_GROUP_SCHED
243extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
244#endif
245
Peter Zijlstra029632f2011-10-25 10:00:11 +0200246#else /* CONFIG_CGROUP_SCHED */
247
248struct cfs_bandwidth { };
249
250#endif /* CONFIG_CGROUP_SCHED */
251
252/* CFS-related fields in a runqueue */
253struct cfs_rq {
254 struct load_weight load;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200255 unsigned int nr_running, h_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200256
257 u64 exec_clock;
258 u64 min_vruntime;
259#ifndef CONFIG_64BIT
260 u64 min_vruntime_copy;
261#endif
262
263 struct rb_root tasks_timeline;
264 struct rb_node *rb_leftmost;
265
Peter Zijlstra029632f2011-10-25 10:00:11 +0200266 /*
267 * 'curr' points to currently running entity on this cfs_rq.
268 * It is set to NULL otherwise (i.e when none are currently running).
269 */
270 struct sched_entity *curr, *next, *last, *skip;
271
272#ifdef CONFIG_SCHED_DEBUG
273 unsigned int nr_spread_over;
274#endif
275
Paul Turner2dac7542012-10-04 13:18:30 +0200276#ifdef CONFIG_SMP
277 /*
278 * CFS Load tracking
279 * Under CFS, load is tracked on a per-entity basis and aggregated up.
280 * This allows for the description of both thread and group usage (in
281 * the FAIR_GROUP_SCHED case).
282 */
Alex Shi72a4cf22013-06-20 10:18:53 +0800283 unsigned long runnable_load_avg, blocked_load_avg;
Alex Shi25099402013-06-20 10:18:55 +0800284 atomic64_t decay_counter;
Paul Turner9ee474f2012-10-04 13:18:30 +0200285 u64 last_decay;
Alex Shi25099402013-06-20 10:18:55 +0800286 atomic_long_t removed_load;
Alex Shi141965c2013-06-26 13:05:39 +0800287
Paul Turnerc566e8e2012-10-04 13:18:30 +0200288#ifdef CONFIG_FAIR_GROUP_SCHED
Alex Shi141965c2013-06-26 13:05:39 +0800289 /* Required to track per-cpu representation of a task_group */
Paul Turnerbb17f652012-10-04 13:18:31 +0200290 u32 tg_runnable_contrib;
Alex Shibf5b9862013-06-20 10:18:54 +0800291 unsigned long tg_load_contrib;
Paul Turner82958362012-10-04 13:18:31 +0200292
293 /*
294 * h_load = weight * f(tg)
295 *
296 * Where f(tg) is the recursive weight fraction assigned to
297 * this group.
298 */
299 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400300 u64 last_h_load_update;
301 struct sched_entity *h_load_next;
302#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200303#endif /* CONFIG_SMP */
304
Peter Zijlstra029632f2011-10-25 10:00:11 +0200305#ifdef CONFIG_FAIR_GROUP_SCHED
306 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
307
308 /*
309 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
310 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
311 * (like users, containers etc.)
312 *
313 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
314 * list is used during load balance.
315 */
316 int on_list;
317 struct list_head leaf_cfs_rq_list;
318 struct task_group *tg; /* group that "owns" this runqueue */
319
Peter Zijlstra029632f2011-10-25 10:00:11 +0200320#ifdef CONFIG_CFS_BANDWIDTH
321 int runtime_enabled;
322 u64 runtime_expires;
323 s64 runtime_remaining;
324
Paul Turnerf1b17282012-10-04 13:18:31 +0200325 u64 throttled_clock, throttled_clock_task;
326 u64 throttled_clock_task_time;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200327 int throttled, throttle_count;
328 struct list_head throttled_list;
329#endif /* CONFIG_CFS_BANDWIDTH */
330#endif /* CONFIG_FAIR_GROUP_SCHED */
331};
332
333static inline int rt_bandwidth_enabled(void)
334{
335 return sysctl_sched_rt_runtime >= 0;
336}
337
338/* Real-Time classes' related field in a runqueue: */
339struct rt_rq {
340 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200341 unsigned int rt_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200342#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
343 struct {
344 int curr; /* highest queued rt task prio */
345#ifdef CONFIG_SMP
346 int next; /* next highest */
347#endif
348 } highest_prio;
349#endif
350#ifdef CONFIG_SMP
351 unsigned long rt_nr_migratory;
352 unsigned long rt_nr_total;
353 int overloaded;
354 struct plist_head pushable_tasks;
355#endif
356 int rt_throttled;
357 u64 rt_time;
358 u64 rt_runtime;
359 /* Nests inside the rq lock: */
360 raw_spinlock_t rt_runtime_lock;
361
362#ifdef CONFIG_RT_GROUP_SCHED
363 unsigned long rt_nr_boosted;
364
365 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200366 struct task_group *tg;
367#endif
368};
369
370#ifdef CONFIG_SMP
371
372/*
373 * We add the notion of a root-domain which will be used to define per-domain
374 * variables. Each exclusive cpuset essentially defines an island domain by
375 * fully partitioning the member cpus from any other cpuset. Whenever a new
376 * exclusive cpuset is created, we also create and attach a new root-domain
377 * object.
378 *
379 */
380struct root_domain {
381 atomic_t refcount;
382 atomic_t rto_count;
383 struct rcu_head rcu;
384 cpumask_var_t span;
385 cpumask_var_t online;
386
387 /*
388 * The "RT overload" flag: it gets set if a CPU has more than
389 * one runnable RT task.
390 */
391 cpumask_var_t rto_mask;
392 struct cpupri cpupri;
393};
394
395extern struct root_domain def_root_domain;
396
397#endif /* CONFIG_SMP */
398
399/*
400 * This is the main, per-CPU runqueue data structure.
401 *
402 * Locking rule: those places that want to lock multiple runqueues
403 * (such as the load balancing or the thread migration code), lock
404 * acquire operations must be ordered by ascending &runqueue.
405 */
406struct rq {
407 /* runqueue lock: */
408 raw_spinlock_t lock;
409
410 /*
411 * nr_running and cpu_load should be in the same cacheline because
412 * remote CPUs use both these fields when doing load calculation.
413 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200414 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100415#ifdef CONFIG_NUMA_BALANCING
416 unsigned int nr_numa_running;
417 unsigned int nr_preferred_running;
418#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200419 #define CPU_LOAD_IDX_MAX 5
420 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
421 unsigned long last_load_update_tick;
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200422#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra029632f2011-10-25 10:00:11 +0200423 u64 nohz_stamp;
Suresh Siddha1c792db2011-12-01 17:07:32 -0800424 unsigned long nohz_flags;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200425#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200426#ifdef CONFIG_NO_HZ_FULL
427 unsigned long last_sched_tick;
428#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200429 int skip_clock_update;
430
431 /* capture load from *all* tasks on this cpu: */
432 struct load_weight load;
433 unsigned long nr_load_updates;
434 u64 nr_switches;
435
436 struct cfs_rq cfs;
437 struct rt_rq rt;
438
439#ifdef CONFIG_FAIR_GROUP_SCHED
440 /* list of leaf cfs_rq on this cpu: */
441 struct list_head leaf_cfs_rq_list;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200442#endif /* CONFIG_FAIR_GROUP_SCHED */
443
Peter Zijlstra029632f2011-10-25 10:00:11 +0200444#ifdef CONFIG_RT_GROUP_SCHED
445 struct list_head leaf_rt_rq_list;
446#endif
447
448 /*
449 * This is part of a global counter where only the total sum
450 * over all CPUs matters. A task can increase this counter on
451 * one CPU and if it got migrated afterwards it may decrease
452 * it on another CPU. Always updated under the runqueue lock:
453 */
454 unsigned long nr_uninterruptible;
455
456 struct task_struct *curr, *idle, *stop;
457 unsigned long next_balance;
458 struct mm_struct *prev_mm;
459
460 u64 clock;
461 u64 clock_task;
462
463 atomic_t nr_iowait;
464
465#ifdef CONFIG_SMP
466 struct root_domain *rd;
467 struct sched_domain *sd;
468
469 unsigned long cpu_power;
470
471 unsigned char idle_balance;
472 /* For active balancing */
473 int post_schedule;
474 int active_balance;
475 int push_cpu;
476 struct cpu_stop_work active_balance_work;
477 /* cpu of this runqueue: */
478 int cpu;
479 int online;
480
Peter Zijlstra367456c2012-02-20 21:49:09 +0100481 struct list_head cfs_tasks;
482
Peter Zijlstra029632f2011-10-25 10:00:11 +0200483 u64 rt_avg;
484 u64 age_stamp;
485 u64 idle_stamp;
486 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700487
488 /* This is used to determine avg_idle's max value */
489 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200490#endif
491
492#ifdef CONFIG_IRQ_TIME_ACCOUNTING
493 u64 prev_irq_time;
494#endif
495#ifdef CONFIG_PARAVIRT
496 u64 prev_steal_time;
497#endif
498#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
499 u64 prev_steal_time_rq;
500#endif
501
502 /* calc_load related fields */
503 unsigned long calc_load_update;
504 long calc_load_active;
505
506#ifdef CONFIG_SCHED_HRTICK
507#ifdef CONFIG_SMP
508 int hrtick_csd_pending;
509 struct call_single_data hrtick_csd;
510#endif
511 struct hrtimer hrtick_timer;
512#endif
513
514#ifdef CONFIG_SCHEDSTATS
515 /* latency stats */
516 struct sched_info rq_sched_info;
517 unsigned long long rq_cpu_time;
518 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
519
520 /* sys_sched_yield() stats */
521 unsigned int yld_count;
522
523 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200524 unsigned int sched_count;
525 unsigned int sched_goidle;
526
527 /* try_to_wake_up() stats */
528 unsigned int ttwu_count;
529 unsigned int ttwu_local;
530#endif
531
532#ifdef CONFIG_SMP
533 struct llist_head wake_list;
534#endif
Ben Segall18bf2802012-10-04 12:51:20 +0200535
536 struct sched_avg avg;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200537};
538
539static inline int cpu_of(struct rq *rq)
540{
541#ifdef CONFIG_SMP
542 return rq->cpu;
543#else
544 return 0;
545#endif
546}
547
548DECLARE_PER_CPU(struct rq, runqueues);
549
Peter Zijlstra518cd622011-12-07 15:07:31 +0100550#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
551#define this_rq() (&__get_cpu_var(runqueues))
552#define task_rq(p) cpu_rq(task_cpu(p))
553#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
554#define raw_rq() (&__raw_get_cpu_var(runqueues))
555
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200556static inline u64 rq_clock(struct rq *rq)
557{
558 return rq->clock;
559}
560
561static inline u64 rq_clock_task(struct rq *rq)
562{
563 return rq->clock_task;
564}
565
Mel Gormanf809ca92013-10-07 11:28:57 +0100566#ifdef CONFIG_NUMA_BALANCING
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100567extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +0100568extern int migrate_task_to(struct task_struct *p, int cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +0100569extern int migrate_swap(struct task_struct *, struct task_struct *);
Mel Gormanf809ca92013-10-07 11:28:57 +0100570#endif /* CONFIG_NUMA_BALANCING */
571
Peter Zijlstra518cd622011-12-07 15:07:31 +0100572#ifdef CONFIG_SMP
573
Peter Zijlstra029632f2011-10-25 10:00:11 +0200574#define rcu_dereference_check_sched_domain(p) \
575 rcu_dereference_check((p), \
576 lockdep_is_held(&sched_domains_mutex))
577
578/*
579 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
580 * See detach_destroy_domains: synchronize_sched for details.
581 *
582 * The domain tree of any CPU may only be accessed from within
583 * preempt-disabled sections.
584 */
585#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +0100586 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
587 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200588
Suresh Siddha77e81362011-11-17 11:08:23 -0800589#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
590
Peter Zijlstra518cd622011-12-07 15:07:31 +0100591/**
592 * highest_flag_domain - Return highest sched_domain containing flag.
593 * @cpu: The cpu whose highest level of sched domain is to
594 * be returned.
595 * @flag: The flag to check for the highest sched_domain
596 * for the given cpu.
597 *
598 * Returns the highest sched_domain of a cpu which contains the given flag.
599 */
600static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
601{
602 struct sched_domain *sd, *hsd = NULL;
603
604 for_each_domain(cpu, sd) {
605 if (!(sd->flags & flag))
606 break;
607 hsd = sd;
608 }
609
610 return hsd;
611}
612
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100613static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
614{
615 struct sched_domain *sd;
616
617 for_each_domain(cpu, sd) {
618 if (sd->flags & flag)
619 break;
620 }
621
622 return sd;
623}
624
Peter Zijlstra518cd622011-12-07 15:07:31 +0100625DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +0800626DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +0100627DECLARE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100628DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +0530629DECLARE_PER_CPU(struct sched_domain *, sd_busy);
630DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +0100631
Li Zefan5e6521e2013-03-05 16:06:23 +0800632struct sched_group_power {
633 atomic_t ref;
634 /*
635 * CPU power of this group, SCHED_LOAD_SCALE being max power for a
636 * single CPU.
637 */
638 unsigned int power, power_orig;
639 unsigned long next_update;
Peter Zijlstra62633222013-08-19 12:41:09 +0200640 int imbalance; /* XXX unrelated to power but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +0800641 /*
642 * Number of busy cpus in this group.
643 */
644 atomic_t nr_busy_cpus;
645
646 unsigned long cpumask[0]; /* iteration mask */
647};
648
649struct sched_group {
650 struct sched_group *next; /* Must be a circular list */
651 atomic_t ref;
652
653 unsigned int group_weight;
654 struct sched_group_power *sgp;
655
656 /*
657 * The CPUs this group covers.
658 *
659 * NOTE: this field is variable length. (Allocated dynamically
660 * by attaching extra space to the end of the structure,
661 * depending on how many CPUs the kernel has booted up with)
662 */
663 unsigned long cpumask[0];
664};
665
666static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
667{
668 return to_cpumask(sg->cpumask);
669}
670
671/*
672 * cpumask masking which cpus in the group are allowed to iterate up the domain
673 * tree.
674 */
675static inline struct cpumask *sched_group_mask(struct sched_group *sg)
676{
677 return to_cpumask(sg->sgp->cpumask);
678}
679
680/**
681 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
682 * @group: The group whose first cpu is to be returned.
683 */
684static inline unsigned int group_first_cpu(struct sched_group *group)
685{
686 return cpumask_first(sched_group_cpus(group));
687}
688
Peter Zijlstrac1174872012-05-31 14:47:33 +0200689extern int group_balance_cpu(struct sched_group *sg);
690
Peter Zijlstra518cd622011-12-07 15:07:31 +0100691#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200692
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100693#include "stats.h"
694#include "auto_group.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +0200695
696#ifdef CONFIG_CGROUP_SCHED
697
698/*
699 * Return the group to which this tasks belongs.
700 *
Tejun Heo8af01f52013-08-08 20:11:22 -0400701 * We cannot use task_css() and friends because the cgroup subsystem
702 * changes that value before the cgroup_subsys::attach() method is called,
703 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +0200704 *
705 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
706 * core changes this before calling sched_move_task().
707 *
708 * Instead we use a 'copy' which is updated from sched_move_task() while
709 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +0200710 */
711static inline struct task_group *task_group(struct task_struct *p)
712{
Peter Zijlstra8323f262012-06-22 13:36:05 +0200713 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200714}
715
716/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
717static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
718{
719#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
720 struct task_group *tg = task_group(p);
721#endif
722
723#ifdef CONFIG_FAIR_GROUP_SCHED
724 p->se.cfs_rq = tg->cfs_rq[cpu];
725 p->se.parent = tg->se[cpu];
726#endif
727
728#ifdef CONFIG_RT_GROUP_SCHED
729 p->rt.rt_rq = tg->rt_rq[cpu];
730 p->rt.parent = tg->rt_se[cpu];
731#endif
732}
733
734#else /* CONFIG_CGROUP_SCHED */
735
736static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
737static inline struct task_group *task_group(struct task_struct *p)
738{
739 return NULL;
740}
741
742#endif /* CONFIG_CGROUP_SCHED */
743
744static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
745{
746 set_task_rq(p, cpu);
747#ifdef CONFIG_SMP
748 /*
749 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
750 * successfuly executed on another CPU. We must ensure that updates of
751 * per-task data have been completed by this moment.
752 */
753 smp_wmb();
754 task_thread_info(p)->cpu = cpu;
Peter Zijlstraac66f542013-10-07 11:29:16 +0100755 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200756#endif
757}
758
759/*
760 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
761 */
762#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +0100763# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +0200764# define const_debug __read_mostly
765#else
766# define const_debug const
767#endif
768
769extern const_debug unsigned int sysctl_sched_features;
770
771#define SCHED_FEAT(name, enabled) \
772 __SCHED_FEAT_##name ,
773
774enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100775#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200776 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +0200777};
778
779#undef SCHED_FEAT
780
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200781#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100782static __always_inline bool static_branch__true(struct static_key *key)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200783{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100784 return static_key_true(key); /* Not out of line branch. */
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200785}
786
Ingo Molnarc5905af2012-02-24 08:31:31 +0100787static __always_inline bool static_branch__false(struct static_key *key)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200788{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100789 return static_key_false(key); /* Out of line branch. */
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200790}
791
792#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +0100793static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200794{ \
795 return static_branch__##enabled(key); \
796}
797
798#include "features.h"
799
800#undef SCHED_FEAT
801
Ingo Molnarc5905af2012-02-24 08:31:31 +0100802extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200803#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
804#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200805#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200806#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200807
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200808#ifdef CONFIG_NUMA_BALANCING
809#define sched_feat_numa(x) sched_feat(x)
Mel Gorman3105b862012-11-23 11:23:49 +0000810#ifdef CONFIG_SCHED_DEBUG
811#define numabalancing_enabled sched_feat_numa(NUMA)
812#else
813extern bool numabalancing_enabled;
814#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200815#else
816#define sched_feat_numa(x) (0)
Mel Gorman3105b862012-11-23 11:23:49 +0000817#define numabalancing_enabled (0)
818#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstracbee9f82012-10-25 14:16:43 +0200819
Peter Zijlstra029632f2011-10-25 10:00:11 +0200820static inline u64 global_rt_period(void)
821{
822 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
823}
824
825static inline u64 global_rt_runtime(void)
826{
827 if (sysctl_sched_rt_runtime < 0)
828 return RUNTIME_INF;
829
830 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
831}
832
833
834
835static inline int task_current(struct rq *rq, struct task_struct *p)
836{
837 return rq->curr == p;
838}
839
840static inline int task_running(struct rq *rq, struct task_struct *p)
841{
842#ifdef CONFIG_SMP
843 return p->on_cpu;
844#else
845 return task_current(rq, p);
846#endif
847}
848
849
850#ifndef prepare_arch_switch
851# define prepare_arch_switch(next) do { } while (0)
852#endif
853#ifndef finish_arch_switch
854# define finish_arch_switch(prev) do { } while (0)
855#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +0000856#ifndef finish_arch_post_lock_switch
857# define finish_arch_post_lock_switch() do { } while (0)
858#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200859
860#ifndef __ARCH_WANT_UNLOCKED_CTXSW
861static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
862{
863#ifdef CONFIG_SMP
864 /*
865 * We can optimise this out completely for !SMP, because the
866 * SMP rebalancing from interrupt is the only thing that cares
867 * here.
868 */
869 next->on_cpu = 1;
870#endif
871}
872
873static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
874{
875#ifdef CONFIG_SMP
876 /*
877 * After ->on_cpu is cleared, the task can be moved to a different CPU.
878 * We must ensure this doesn't happen until the switch is completely
879 * finished.
880 */
881 smp_wmb();
882 prev->on_cpu = 0;
883#endif
884#ifdef CONFIG_DEBUG_SPINLOCK
885 /* this is a valid case when another task releases the spinlock */
886 rq->lock.owner = current;
887#endif
888 /*
889 * If we are tracking spinlock dependencies then we have to
890 * fix up the runqueue lock - which gets 'carried over' from
891 * prev into current:
892 */
893 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
894
895 raw_spin_unlock_irq(&rq->lock);
896}
897
898#else /* __ARCH_WANT_UNLOCKED_CTXSW */
899static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
900{
901#ifdef CONFIG_SMP
902 /*
903 * We can optimise this out completely for !SMP, because the
904 * SMP rebalancing from interrupt is the only thing that cares
905 * here.
906 */
907 next->on_cpu = 1;
908#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200909 raw_spin_unlock(&rq->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200910}
911
912static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
913{
914#ifdef CONFIG_SMP
915 /*
916 * After ->on_cpu is cleared, the task can be moved to a different CPU.
917 * We must ensure this doesn't happen until the switch is completely
918 * finished.
919 */
920 smp_wmb();
921 prev->on_cpu = 0;
922#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200923 local_irq_enable();
Peter Zijlstra029632f2011-10-25 10:00:11 +0200924}
925#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
926
Li Zefanb13095f2013-03-05 16:06:38 +0800927/*
928 * wake flags
929 */
930#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
931#define WF_FORK 0x02 /* child wakeup after fork */
932#define WF_MIGRATED 0x4 /* internal use, task got migrated */
933
Peter Zijlstra029632f2011-10-25 10:00:11 +0200934/*
935 * To aid in avoiding the subversion of "niceness" due to uneven distribution
936 * of tasks with abnormal "nice" values across CPUs the contribution that
937 * each task makes to its run queue's load is weighted according to its
938 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
939 * scaled version of the new time slice allocation that they receive on time
940 * slice expiry etc.
941 */
942
943#define WEIGHT_IDLEPRIO 3
944#define WMULT_IDLEPRIO 1431655765
945
946/*
947 * Nice levels are multiplicative, with a gentle 10% change for every
948 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
949 * nice 1, it will get ~10% less CPU time than another CPU-bound task
950 * that remained on nice 0.
951 *
952 * The "10% effect" is relative and cumulative: from _any_ nice level,
953 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
954 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
955 * If a task goes up by ~10% and another task goes down by ~10% then
956 * the relative distance between them is ~25%.)
957 */
958static const int prio_to_weight[40] = {
959 /* -20 */ 88761, 71755, 56483, 46273, 36291,
960 /* -15 */ 29154, 23254, 18705, 14949, 11916,
961 /* -10 */ 9548, 7620, 6100, 4904, 3906,
962 /* -5 */ 3121, 2501, 1991, 1586, 1277,
963 /* 0 */ 1024, 820, 655, 526, 423,
964 /* 5 */ 335, 272, 215, 172, 137,
965 /* 10 */ 110, 87, 70, 56, 45,
966 /* 15 */ 36, 29, 23, 18, 15,
967};
968
969/*
970 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
971 *
972 * In cases where the weight does not change often, we can use the
973 * precalculated inverse to speed up arithmetics by turning divisions
974 * into multiplications:
975 */
976static const u32 prio_to_wmult[40] = {
977 /* -20 */ 48388, 59856, 76040, 92818, 118348,
978 /* -15 */ 147320, 184698, 229616, 287308, 360437,
979 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
980 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
981 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
982 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
983 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
984 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
985};
986
Li Zefanc82ba9f2013-03-05 16:06:55 +0800987#define ENQUEUE_WAKEUP 1
988#define ENQUEUE_HEAD 2
989#ifdef CONFIG_SMP
990#define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
991#else
992#define ENQUEUE_WAKING 0
993#endif
994
995#define DEQUEUE_SLEEP 1
996
997struct sched_class {
998 const struct sched_class *next;
999
1000 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1001 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1002 void (*yield_task) (struct rq *rq);
1003 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1004
1005 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1006
1007 struct task_struct * (*pick_next_task) (struct rq *rq);
1008 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1009
1010#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001011 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001012 void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
1013
1014 void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
1015 void (*post_schedule) (struct rq *this_rq);
1016 void (*task_waking) (struct task_struct *task);
1017 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1018
1019 void (*set_cpus_allowed)(struct task_struct *p,
1020 const struct cpumask *newmask);
1021
1022 void (*rq_online)(struct rq *rq);
1023 void (*rq_offline)(struct rq *rq);
1024#endif
1025
1026 void (*set_curr_task) (struct rq *rq);
1027 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1028 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001029 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001030
1031 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1032 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1033 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1034 int oldprio);
1035
1036 unsigned int (*get_rr_interval) (struct rq *rq,
1037 struct task_struct *task);
1038
1039#ifdef CONFIG_FAIR_GROUP_SCHED
1040 void (*task_move_group) (struct task_struct *p, int on_rq);
1041#endif
1042};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001043
1044#define sched_class_highest (&stop_sched_class)
1045#define for_each_class(class) \
1046 for (class = sched_class_highest; class; class = class->next)
1047
1048extern const struct sched_class stop_sched_class;
1049extern const struct sched_class rt_sched_class;
1050extern const struct sched_class fair_sched_class;
1051extern const struct sched_class idle_sched_class;
1052
1053
1054#ifdef CONFIG_SMP
1055
Li Zefanb7192032013-03-07 10:00:26 +08001056extern void update_group_power(struct sched_domain *sd, int cpu);
1057
Peter Zijlstra029632f2011-10-25 10:00:11 +02001058extern void trigger_load_balance(struct rq *rq, int cpu);
1059extern void idle_balance(int this_cpu, struct rq *this_rq);
1060
Vincent Guittot642dbc32013-04-18 18:34:26 +02001061extern void idle_enter_fair(struct rq *this_rq);
1062extern void idle_exit_fair(struct rq *this_rq);
Vincent Guittot642dbc32013-04-18 18:34:26 +02001063
Peter Zijlstra029632f2011-10-25 10:00:11 +02001064#else /* CONFIG_SMP */
1065
1066static inline void idle_balance(int cpu, struct rq *rq)
1067{
1068}
1069
1070#endif
1071
1072extern void sysrq_sched_debug_show(void);
1073extern void sched_init_granularity(void);
1074extern void update_max_interval(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001075extern void init_sched_rt_class(void);
1076extern void init_sched_fair_class(void);
1077
1078extern void resched_task(struct task_struct *p);
1079extern void resched_cpu(int cpu);
1080
1081extern struct rt_bandwidth def_rt_bandwidth;
1082extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1083
Peter Zijlstra556061b2012-05-11 17:31:26 +02001084extern void update_idle_cpu_load(struct rq *this_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001085
Alex Shia75cdaa2013-06-20 10:18:47 +08001086extern void init_task_runnable_average(struct task_struct *p);
1087
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001088#ifdef CONFIG_PARAVIRT
1089static inline u64 steal_ticks(u64 steal)
1090{
1091 if (unlikely(steal > NSEC_PER_SEC))
1092 return div_u64(steal, TICK_NSEC);
1093
1094 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1095}
1096#endif
1097
Peter Zijlstra029632f2011-10-25 10:00:11 +02001098static inline void inc_nr_running(struct rq *rq)
1099{
1100 rq->nr_running++;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001101
1102#ifdef CONFIG_NO_HZ_FULL
1103 if (rq->nr_running == 2) {
1104 if (tick_nohz_full_cpu(rq->cpu)) {
1105 /* Order rq->nr_running write against the IPI */
1106 smp_wmb();
1107 smp_send_reschedule(rq->cpu);
1108 }
1109 }
1110#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001111}
1112
1113static inline void dec_nr_running(struct rq *rq)
1114{
1115 rq->nr_running--;
1116}
1117
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001118static inline void rq_last_tick_reset(struct rq *rq)
1119{
1120#ifdef CONFIG_NO_HZ_FULL
1121 rq->last_sched_tick = jiffies;
1122#endif
1123}
1124
Peter Zijlstra029632f2011-10-25 10:00:11 +02001125extern void update_rq_clock(struct rq *rq);
1126
1127extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1128extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1129
1130extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1131
1132extern const_debug unsigned int sysctl_sched_time_avg;
1133extern const_debug unsigned int sysctl_sched_nr_migrate;
1134extern const_debug unsigned int sysctl_sched_migration_cost;
1135
1136static inline u64 sched_avg_period(void)
1137{
1138 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1139}
1140
Peter Zijlstra029632f2011-10-25 10:00:11 +02001141#ifdef CONFIG_SCHED_HRTICK
1142
1143/*
1144 * Use hrtick when:
1145 * - enabled by features
1146 * - hrtimer is actually high res
1147 */
1148static inline int hrtick_enabled(struct rq *rq)
1149{
1150 if (!sched_feat(HRTICK))
1151 return 0;
1152 if (!cpu_active(cpu_of(rq)))
1153 return 0;
1154 return hrtimer_is_hres_active(&rq->hrtick_timer);
1155}
1156
1157void hrtick_start(struct rq *rq, u64 delay);
1158
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001159#else
1160
1161static inline int hrtick_enabled(struct rq *rq)
1162{
1163 return 0;
1164}
1165
Peter Zijlstra029632f2011-10-25 10:00:11 +02001166#endif /* CONFIG_SCHED_HRTICK */
1167
1168#ifdef CONFIG_SMP
1169extern void sched_avg_update(struct rq *rq);
1170static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1171{
1172 rq->rt_avg += rt_delta;
1173 sched_avg_update(rq);
1174}
1175#else
1176static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1177static inline void sched_avg_update(struct rq *rq) { }
1178#endif
1179
1180extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
1181
1182#ifdef CONFIG_SMP
1183#ifdef CONFIG_PREEMPT
1184
1185static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1186
1187/*
1188 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1189 * way at the expense of forcing extra atomic operations in all
1190 * invocations. This assures that the double_lock is acquired using the
1191 * same underlying policy as the spinlock_t on this architecture, which
1192 * reduces latency compared to the unfair variant below. However, it
1193 * also adds more overhead and therefore may reduce throughput.
1194 */
1195static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1196 __releases(this_rq->lock)
1197 __acquires(busiest->lock)
1198 __acquires(this_rq->lock)
1199{
1200 raw_spin_unlock(&this_rq->lock);
1201 double_rq_lock(this_rq, busiest);
1202
1203 return 1;
1204}
1205
1206#else
1207/*
1208 * Unfair double_lock_balance: Optimizes throughput at the expense of
1209 * latency by eliminating extra atomic operations when the locks are
1210 * already in proper order on entry. This favors lower cpu-ids and will
1211 * grant the double lock to lower cpus over higher ids under contention,
1212 * regardless of entry order into the function.
1213 */
1214static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1215 __releases(this_rq->lock)
1216 __acquires(busiest->lock)
1217 __acquires(this_rq->lock)
1218{
1219 int ret = 0;
1220
1221 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1222 if (busiest < this_rq) {
1223 raw_spin_unlock(&this_rq->lock);
1224 raw_spin_lock(&busiest->lock);
1225 raw_spin_lock_nested(&this_rq->lock,
1226 SINGLE_DEPTH_NESTING);
1227 ret = 1;
1228 } else
1229 raw_spin_lock_nested(&busiest->lock,
1230 SINGLE_DEPTH_NESTING);
1231 }
1232 return ret;
1233}
1234
1235#endif /* CONFIG_PREEMPT */
1236
1237/*
1238 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1239 */
1240static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1241{
1242 if (unlikely(!irqs_disabled())) {
1243 /* printk() doesn't work good under rq->lock */
1244 raw_spin_unlock(&this_rq->lock);
1245 BUG_ON(1);
1246 }
1247
1248 return _double_lock_balance(this_rq, busiest);
1249}
1250
1251static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1252 __releases(busiest->lock)
1253{
1254 raw_spin_unlock(&busiest->lock);
1255 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1256}
1257
Peter Zijlstra74602312013-10-10 20:17:22 +02001258static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1259{
1260 if (l1 > l2)
1261 swap(l1, l2);
1262
1263 spin_lock(l1);
1264 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1265}
1266
1267static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1268{
1269 if (l1 > l2)
1270 swap(l1, l2);
1271
1272 raw_spin_lock(l1);
1273 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1274}
1275
Peter Zijlstra029632f2011-10-25 10:00:11 +02001276/*
1277 * double_rq_lock - safely lock two runqueues
1278 *
1279 * Note this does not disable interrupts like task_rq_lock,
1280 * you need to do so manually before calling.
1281 */
1282static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1283 __acquires(rq1->lock)
1284 __acquires(rq2->lock)
1285{
1286 BUG_ON(!irqs_disabled());
1287 if (rq1 == rq2) {
1288 raw_spin_lock(&rq1->lock);
1289 __acquire(rq2->lock); /* Fake it out ;) */
1290 } else {
1291 if (rq1 < rq2) {
1292 raw_spin_lock(&rq1->lock);
1293 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1294 } else {
1295 raw_spin_lock(&rq2->lock);
1296 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1297 }
1298 }
1299}
1300
1301/*
1302 * double_rq_unlock - safely unlock two runqueues
1303 *
1304 * Note this does not restore interrupts like task_rq_unlock,
1305 * you need to do so manually after calling.
1306 */
1307static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1308 __releases(rq1->lock)
1309 __releases(rq2->lock)
1310{
1311 raw_spin_unlock(&rq1->lock);
1312 if (rq1 != rq2)
1313 raw_spin_unlock(&rq2->lock);
1314 else
1315 __release(rq2->lock);
1316}
1317
1318#else /* CONFIG_SMP */
1319
1320/*
1321 * double_rq_lock - safely lock two runqueues
1322 *
1323 * Note this does not disable interrupts like task_rq_lock,
1324 * you need to do so manually before calling.
1325 */
1326static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1327 __acquires(rq1->lock)
1328 __acquires(rq2->lock)
1329{
1330 BUG_ON(!irqs_disabled());
1331 BUG_ON(rq1 != rq2);
1332 raw_spin_lock(&rq1->lock);
1333 __acquire(rq2->lock); /* Fake it out ;) */
1334}
1335
1336/*
1337 * double_rq_unlock - safely unlock two runqueues
1338 *
1339 * Note this does not restore interrupts like task_rq_unlock,
1340 * you need to do so manually after calling.
1341 */
1342static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1343 __releases(rq1->lock)
1344 __releases(rq2->lock)
1345{
1346 BUG_ON(rq1 != rq2);
1347 raw_spin_unlock(&rq1->lock);
1348 __release(rq2->lock);
1349}
1350
1351#endif
1352
1353extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1354extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1355extern void print_cfs_stats(struct seq_file *m, int cpu);
1356extern void print_rt_stats(struct seq_file *m, int cpu);
1357
1358extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1359extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001360
Ben Segall1ee14e62013-10-16 11:16:12 -07001361extern void cfs_bandwidth_usage_inc(void);
1362extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08001363
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001364#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08001365enum rq_nohz_flag_bits {
1366 NOHZ_TICK_STOPPED,
1367 NOHZ_BALANCE_KICK,
1368};
1369
1370#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1371#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001372
1373#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1374
1375DECLARE_PER_CPU(u64, cpu_hardirq_time);
1376DECLARE_PER_CPU(u64, cpu_softirq_time);
1377
1378#ifndef CONFIG_64BIT
1379DECLARE_PER_CPU(seqcount_t, irq_time_seq);
1380
1381static inline void irq_time_write_begin(void)
1382{
1383 __this_cpu_inc(irq_time_seq.sequence);
1384 smp_wmb();
1385}
1386
1387static inline void irq_time_write_end(void)
1388{
1389 smp_wmb();
1390 __this_cpu_inc(irq_time_seq.sequence);
1391}
1392
1393static inline u64 irq_time_read(int cpu)
1394{
1395 u64 irq_time;
1396 unsigned seq;
1397
1398 do {
1399 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1400 irq_time = per_cpu(cpu_softirq_time, cpu) +
1401 per_cpu(cpu_hardirq_time, cpu);
1402 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1403
1404 return irq_time;
1405}
1406#else /* CONFIG_64BIT */
1407static inline void irq_time_write_begin(void)
1408{
1409}
1410
1411static inline void irq_time_write_end(void)
1412{
1413}
1414
1415static inline u64 irq_time_read(int cpu)
1416{
1417 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1418}
1419#endif /* CONFIG_64BIT */
1420#endif /* CONFIG_IRQ_TIME_ACCOUNTING */