blob: 7fe334ead4f945b30a86986b0ff4563b0253f25f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Eric Dumazet5517d862007-05-08 00:32:57 -070071#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020072#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080075 * Scheduler clock - returns current time in nanosec units.
76 * This is default implementation.
77 * Architectures and sub-architectures can override this.
78 */
79unsigned long long __attribute__((weak)) sched_clock(void)
80{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010081 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080082}
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Convert user-nice values [ -20 ... 0 ... 19 ]
86 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
87 * and back.
88 */
89#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
90#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
91#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
92
93/*
94 * 'User priority' is the nice value converted to something we
95 * can work with better when scaling various scheduler parameters,
96 * it's a [ 0 ... 39 ] range.
97 */
98#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
99#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
100#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
101
102/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100103 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100105#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200107#define NICE_0_LOAD SCHED_LOAD_SCALE
108#define NICE_0_SHIFT SCHED_LOAD_SHIFT
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * These are the 'tuning knobs' of the scheduler:
112 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200113 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Timeslices get refilled after they expire.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700117
Eric Dumazet5517d862007-05-08 00:32:57 -0700118#ifdef CONFIG_SMP
119/*
120 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
121 * Since cpu_power is a 'constant', we can use a reciprocal divide.
122 */
123static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
124{
125 return reciprocal_divide(load, sg->reciprocal_cpu_power);
126}
127
128/*
129 * Each time a sched group cpu_power is changed,
130 * we must compute its reciprocal value
131 */
132static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
133{
134 sg->__cpu_power += val;
135 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
136}
137#endif
138
Ingo Molnare05606d2007-07-09 18:51:59 +0200139static inline int rt_policy(int policy)
140{
141 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
142 return 1;
143 return 0;
144}
145
146static inline int task_has_rt_policy(struct task_struct *p)
147{
148 return rt_policy(p->policy);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200152 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200154struct rt_prio_array {
155 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
156 struct list_head queue[MAX_RT_PRIO];
157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100159#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200160
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700161#include <linux/cgroup.h>
162
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200163struct cfs_rq;
164
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100165static LIST_HEAD(task_groups);
166
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200167/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200168struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100169#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700170 struct cgroup_subsys_state css;
171#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100172
173#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200174 /* schedulable entities of this group on each cpu */
175 struct sched_entity **se;
176 /* runqueue "owned" by this group on each cpu */
177 struct cfs_rq **cfs_rq;
178 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100179#endif
180
181#ifdef CONFIG_RT_GROUP_SCHED
182 struct sched_rt_entity **rt_se;
183 struct rt_rq **rt_rq;
184
185 u64 rt_runtime;
186#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100187
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100188 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100189 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200190};
191
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100192#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200193/* Default task group's sched entity on each cpu */
194static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
195/* Default task group's cfs_rq on each cpu */
196static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
197
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100198static struct sched_entity *init_sched_entity_p[NR_CPUS];
199static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
200#endif
201
202#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100203static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
204static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
205
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100206static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
207static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100208#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100209
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100210/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100211 * a task group's cpu shares.
212 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100213static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100214
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100215/* doms_cur_mutex serializes access to doms_cur[] array */
216static DEFINE_MUTEX(doms_cur_mutex);
217
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100218#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100219#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100220# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200221#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100222# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200223#endif
224
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100225static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100226#endif
227
228/* Default task group.
229 * Every task in system belong to this group at bootup.
230 */
231struct task_group init_task_group = {
232#ifdef CONFIG_FAIR_GROUP_SCHED
233 .se = init_sched_entity_p,
234 .cfs_rq = init_cfs_rq_p,
235#endif
236
237#ifdef CONFIG_RT_GROUP_SCHED
238 .rt_se = init_sched_rt_entity_p,
239 .rt_rq = init_rt_rq_p,
240#endif
241};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242
243/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200244static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200245{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200246 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200247
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100248#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200249 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100250#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700251 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
252 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200253#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100254 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200255#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200256 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257}
258
259/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100260static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200261{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100263 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
264 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100266
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100267#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100268 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
269 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200271}
272
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100273static inline void lock_doms_cur(void)
274{
275 mutex_lock(&doms_cur_mutex);
276}
277
278static inline void unlock_doms_cur(void)
279{
280 mutex_unlock(&doms_cur_mutex);
281}
282
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200283#else
284
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100285static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100286static inline void lock_doms_cur(void) { }
287static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200288
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200291/* CFS-related fields in a runqueue */
292struct cfs_rq {
293 struct load_weight load;
294 unsigned long nr_running;
295
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200296 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200297 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200298
299 struct rb_root tasks_timeline;
300 struct rb_node *rb_leftmost;
301 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200302 /* 'curr' points to currently running entity on this cfs_rq.
303 * It is set to NULL otherwise (i.e when none are currently running).
304 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100305 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200306
307 unsigned long nr_spread_over;
308
Ingo Molnar62160e32007-10-15 17:00:03 +0200309#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200310 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
311
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100312 /*
313 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200314 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
315 * (like users, containers etc.)
316 *
317 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
318 * list is used during load balance.
319 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100320 struct list_head leaf_cfs_rq_list;
321 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200322#endif
323};
324
325/* Real-Time classes' related field in a runqueue: */
326struct rt_rq {
327 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100328 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100329#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100330 int highest_prio; /* highest queued rt task prio */
331#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100332#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100333 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100334 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100335#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100336 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100337 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100338
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100339#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100340 unsigned long rt_nr_boosted;
341
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100342 struct rq *rq;
343 struct list_head leaf_rt_rq_list;
344 struct task_group *tg;
345 struct sched_rt_entity *rt_se;
346#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200347};
348
Gregory Haskins57d885f2008-01-25 21:08:18 +0100349#ifdef CONFIG_SMP
350
351/*
352 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100353 * variables. Each exclusive cpuset essentially defines an island domain by
354 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100355 * exclusive cpuset is created, we also create and attach a new root-domain
356 * object.
357 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100358 */
359struct root_domain {
360 atomic_t refcount;
361 cpumask_t span;
362 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100363
Ingo Molnar0eab9142008-01-25 21:08:19 +0100364 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100365 * The "RT overload" flag: it gets set if a CPU has more than
366 * one runnable RT task.
367 */
368 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100369 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100370};
371
Gregory Haskinsdc938522008-01-25 21:08:26 +0100372/*
373 * By default the system creates a single root-domain with all cpus as
374 * members (mimicking the global state we have today).
375 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100376static struct root_domain def_root_domain;
377
378#endif
379
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * This is the main, per-CPU runqueue data structure.
382 *
383 * Locking rule: those places that want to lock multiple runqueues
384 * (such as the load balancing or the thread migration code), lock
385 * acquire operations must be ordered by ascending &runqueue.
386 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700387struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200388 /* runqueue lock: */
389 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /*
392 * nr_running and cpu_load should be in the same cacheline because
393 * remote CPUs use both these fields when doing load calculation.
394 */
395 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200396 #define CPU_LOAD_IDX_MAX 5
397 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700398 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700399#ifdef CONFIG_NO_HZ
400 unsigned char in_nohz_recently;
401#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200402 /* capture load from *all* tasks on this cpu: */
403 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200404 unsigned long nr_load_updates;
405 u64 nr_switches;
406
407 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100408 struct rt_rq rt;
409 u64 rt_period_expire;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100410 int rt_throttled;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100411
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200412#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200413 /* list of leaf cfs_rq on this cpu: */
414 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100415#endif
416#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100417 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /*
421 * This is part of a global counter where only the total sum
422 * over all CPUs matters. A task can increase this counter on
423 * one CPU and if it got migrated afterwards it may decrease
424 * it on another CPU. Always updated under the runqueue lock:
425 */
426 unsigned long nr_uninterruptible;
427
Ingo Molnar36c8b582006-07-03 00:25:41 -0700428 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800429 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200431
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200432 u64 clock, prev_clock_raw;
433 s64 clock_max_delta;
434
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100435 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200436 u64 idle_clock;
437 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200438 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 atomic_t nr_iowait;
441
442#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100443 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct sched_domain *sd;
445
446 /* For active balancing */
447 int active_balance;
448 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200449 /* cpu of this runqueue: */
450 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Ingo Molnar36c8b582006-07-03 00:25:41 -0700452 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct list_head migration_queue;
454#endif
455
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100456#ifdef CONFIG_SCHED_HRTICK
457 unsigned long hrtick_flags;
458 ktime_t hrtick_expire;
459 struct hrtimer hrtick_timer;
460#endif
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#ifdef CONFIG_SCHEDSTATS
463 /* latency stats */
464 struct sched_info rq_sched_info;
465
466 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200467 unsigned int yld_exp_empty;
468 unsigned int yld_act_empty;
469 unsigned int yld_both_empty;
470 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200473 unsigned int sched_switch;
474 unsigned int sched_count;
475 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200478 unsigned int ttwu_count;
479 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200480
481 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200482 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700484 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485};
486
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700487static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ingo Molnardd41f592007-07-09 18:51:59 +0200489static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
490{
491 rq->curr->sched_class->check_preempt_curr(rq, p);
492}
493
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700494static inline int cpu_of(struct rq *rq)
495{
496#ifdef CONFIG_SMP
497 return rq->cpu;
498#else
499 return 0;
500#endif
501}
502
Nick Piggin674311d2005-06-25 14:57:27 -0700503/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200504 * Update the per-runqueue clock, as finegrained as the platform can give
505 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200506 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200507static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200508{
509 u64 prev_raw = rq->prev_clock_raw;
510 u64 now = sched_clock();
511 s64 delta = now - prev_raw;
512 u64 clock = rq->clock;
513
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200514#ifdef CONFIG_SCHED_DEBUG
515 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
516#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200517 /*
518 * Protect against sched_clock() occasionally going backwards:
519 */
520 if (unlikely(delta < 0)) {
521 clock++;
522 rq->clock_warps++;
523 } else {
524 /*
525 * Catch too large forward jumps too:
526 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200527 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
528 if (clock < rq->tick_timestamp + TICK_NSEC)
529 clock = rq->tick_timestamp + TICK_NSEC;
530 else
531 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200532 rq->clock_overflows++;
533 } else {
534 if (unlikely(delta > rq->clock_max_delta))
535 rq->clock_max_delta = delta;
536 clock += delta;
537 }
538 }
539
540 rq->prev_clock_raw = now;
541 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200542}
543
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200544static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200545{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200546 if (likely(smp_processor_id() == cpu_of(rq)))
547 __update_rq_clock(rq);
548}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200549
Ingo Molnar20d315d2007-07-09 18:51:58 +0200550/*
Nick Piggin674311d2005-06-25 14:57:27 -0700551 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700552 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700553 *
554 * The domain tree of any CPU may only be accessed from within
555 * preempt-disabled sections.
556 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700557#define for_each_domain(cpu, __sd) \
558 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
561#define this_rq() (&__get_cpu_var(runqueues))
562#define task_rq(p) cpu_rq(task_cpu(p))
563#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
564
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100565unsigned long rt_needs_cpu(int cpu)
566{
567 struct rq *rq = cpu_rq(cpu);
568 u64 delta;
569
570 if (!rq->rt_throttled)
571 return 0;
572
573 if (rq->clock > rq->rt_period_expire)
574 return 1;
575
576 delta = rq->rt_period_expire - rq->clock;
577 do_div(delta, NSEC_PER_SEC / HZ);
578
579 return (unsigned long)delta;
580}
581
Ingo Molnare436d802007-07-19 21:28:35 +0200582/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200583 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
584 */
585#ifdef CONFIG_SCHED_DEBUG
586# define const_debug __read_mostly
587#else
588# define const_debug static const
589#endif
590
591/*
592 * Debugging: various feature bits
593 */
594enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200595 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100596 SCHED_FEAT_WAKEUP_PREEMPT = 2,
597 SCHED_FEAT_START_DEBIT = 4,
Peter Zijlstra2070ee02008-03-21 16:43:47 +0100598 SCHED_FEAT_HRTICK = 8,
599 SCHED_FEAT_DOUBLE_TICK = 16,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200600};
601
602const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200603 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100604 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200605 SCHED_FEAT_START_DEBIT * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100606 SCHED_FEAT_HRTICK * 1 |
607 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200608
609#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
610
611/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100612 * Number of tasks to iterate in a single balance run.
613 * Limited because this is done with IRQs disabled.
614 */
615const_debug unsigned int sysctl_sched_nr_migrate = 32;
616
617/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100618 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100619 * default: 1s
620 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100621unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100622
Ingo Molnar6892b752008-02-13 14:02:36 +0100623static __read_mostly int scheduler_running;
624
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100625/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100626 * part of the period that we allow rt tasks to run in us.
627 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100628 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100629int sysctl_sched_rt_runtime = 950000;
630
631/*
632 * single value that denotes runtime == period, ie unlimited time.
633 */
634#define RUNTIME_INF ((u64)~0ULL)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100635
Ingo Molnar27ec4402008-02-28 21:00:21 +0100636static const unsigned long long time_sync_thresh = 100000;
637
638static DEFINE_PER_CPU(unsigned long long, time_offset);
639static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
640
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100641/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100642 * Global lock which we take every now and then to synchronize
643 * the CPUs time. This method is not warp-safe, but it's good
644 * enough to synchronize slowly diverging time sources and thus
645 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200646 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100647static DEFINE_SPINLOCK(time_sync_lock);
648static unsigned long long prev_global_time;
649
650static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
651{
652 unsigned long flags;
653
654 spin_lock_irqsave(&time_sync_lock, flags);
655
656 if (time < prev_global_time) {
657 per_cpu(time_offset, cpu) += prev_global_time - time;
658 time = prev_global_time;
659 } else {
660 prev_global_time = time;
661 }
662
663 spin_unlock_irqrestore(&time_sync_lock, flags);
664
665 return time;
666}
667
668static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200669{
Ingo Molnare436d802007-07-19 21:28:35 +0200670 unsigned long long now;
671 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200672 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200673
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100674 /*
675 * Only call sched_clock() if the scheduler has already been
676 * initialized (some code might call cpu_clock() very early):
677 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100678 if (unlikely(!scheduler_running))
679 return 0;
680
681 local_irq_save(flags);
682 rq = cpu_rq(cpu);
683 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200684 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200685 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200686
687 return now;
688}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100689
690/*
691 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
692 * clock constructed from sched_clock():
693 */
694unsigned long long cpu_clock(int cpu)
695{
696 unsigned long long prev_cpu_time, time, delta_time;
697
698 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
699 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
700 delta_time = time-prev_cpu_time;
701
702 if (unlikely(delta_time > time_sync_thresh))
703 time = __sync_cpu_clock(time, cpu);
704
705 return time;
706}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200707EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700710# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700712#ifndef finish_arch_switch
713# define finish_arch_switch(prev) do { } while (0)
714#endif
715
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100716static inline int task_current(struct rq *rq, struct task_struct *p)
717{
718 return rq->curr == p;
719}
720
Nick Piggin4866cde2005-06-25 14:57:23 -0700721#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700722static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700723{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100724 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700725}
726
Ingo Molnar70b97a72006-07-03 00:25:42 -0700727static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700728{
729}
730
Ingo Molnar70b97a72006-07-03 00:25:42 -0700731static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700732{
Ingo Molnarda04c032005-09-13 11:17:59 +0200733#ifdef CONFIG_DEBUG_SPINLOCK
734 /* this is a valid case when another task releases the spinlock */
735 rq->lock.owner = current;
736#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700737 /*
738 * If we are tracking spinlock dependencies then we have to
739 * fix up the runqueue lock - which gets 'carried over' from
740 * prev into current:
741 */
742 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
743
Nick Piggin4866cde2005-06-25 14:57:23 -0700744 spin_unlock_irq(&rq->lock);
745}
746
747#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700748static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700749{
750#ifdef CONFIG_SMP
751 return p->oncpu;
752#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100753 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700754#endif
755}
756
Ingo Molnar70b97a72006-07-03 00:25:42 -0700757static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700758{
759#ifdef CONFIG_SMP
760 /*
761 * We can optimise this out completely for !SMP, because the
762 * SMP rebalancing from interrupt is the only thing that cares
763 * here.
764 */
765 next->oncpu = 1;
766#endif
767#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
768 spin_unlock_irq(&rq->lock);
769#else
770 spin_unlock(&rq->lock);
771#endif
772}
773
Ingo Molnar70b97a72006-07-03 00:25:42 -0700774static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700775{
776#ifdef CONFIG_SMP
777 /*
778 * After ->oncpu is cleared, the task can be moved to a different CPU.
779 * We must ensure this doesn't happen until the switch is completely
780 * finished.
781 */
782 smp_wmb();
783 prev->oncpu = 0;
784#endif
785#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
786 local_irq_enable();
787#endif
788}
789#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700792 * __task_rq_lock - lock the runqueue a given task resides on.
793 * Must be called interrupts disabled.
794 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700795static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700796 __acquires(rq->lock)
797{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200798 for (;;) {
799 struct rq *rq = task_rq(p);
800 spin_lock(&rq->lock);
801 if (likely(rq == task_rq(p)))
802 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700803 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700804 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700805}
806
807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100809 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * explicitly disabling preemption.
811 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700812static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 __acquires(rq->lock)
814{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700815 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Andi Kleen3a5c3592007-10-15 17:00:14 +0200817 for (;;) {
818 local_irq_save(*flags);
819 rq = task_rq(p);
820 spin_lock(&rq->lock);
821 if (likely(rq == task_rq(p)))
822 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Alexey Dobriyana9957442007-10-15 17:00:13 +0200827static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700828 __releases(rq->lock)
829{
830 spin_unlock(&rq->lock);
831}
832
Ingo Molnar70b97a72006-07-03 00:25:42 -0700833static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 __releases(rq->lock)
835{
836 spin_unlock_irqrestore(&rq->lock, *flags);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800840 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200842static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 __acquires(rq->lock)
844{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700845 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 local_irq_disable();
848 rq = this_rq();
849 spin_lock(&rq->lock);
850
851 return rq;
852}
853
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200854/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200855 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200856 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200857void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200858{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200859 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200860
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200861 spin_lock(&rq->lock);
862 __update_rq_clock(rq);
863 spin_unlock(&rq->lock);
864 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200865}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200866EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
867
868/*
869 * We just idled delta nanoseconds (called with irqs disabled):
870 */
871void sched_clock_idle_wakeup_event(u64 delta_ns)
872{
873 struct rq *rq = cpu_rq(smp_processor_id());
874 u64 now = sched_clock();
875
876 rq->idle_clock += delta_ns;
877 /*
878 * Override the previous timestamp and ignore all
879 * sched_clock() deltas that occured while we idled,
880 * and use the PM-provided delta_ns to advance the
881 * rq clock:
882 */
883 spin_lock(&rq->lock);
884 rq->prev_clock_raw = now;
885 rq->clock += delta_ns;
886 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100887 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200888}
889EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200890
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100891static void __resched_task(struct task_struct *p, int tif_bit);
892
893static inline void resched_task(struct task_struct *p)
894{
895 __resched_task(p, TIF_NEED_RESCHED);
896}
897
898#ifdef CONFIG_SCHED_HRTICK
899/*
900 * Use HR-timers to deliver accurate preemption points.
901 *
902 * Its all a bit involved since we cannot program an hrt while holding the
903 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
904 * reschedule event.
905 *
906 * When we get rescheduled we reprogram the hrtick_timer outside of the
907 * rq->lock.
908 */
909static inline void resched_hrt(struct task_struct *p)
910{
911 __resched_task(p, TIF_HRTICK_RESCHED);
912}
913
914static inline void resched_rq(struct rq *rq)
915{
916 unsigned long flags;
917
918 spin_lock_irqsave(&rq->lock, flags);
919 resched_task(rq->curr);
920 spin_unlock_irqrestore(&rq->lock, flags);
921}
922
923enum {
924 HRTICK_SET, /* re-programm hrtick_timer */
925 HRTICK_RESET, /* not a new slice */
926};
927
928/*
929 * Use hrtick when:
930 * - enabled by features
931 * - hrtimer is actually high res
932 */
933static inline int hrtick_enabled(struct rq *rq)
934{
935 if (!sched_feat(HRTICK))
936 return 0;
937 return hrtimer_is_hres_active(&rq->hrtick_timer);
938}
939
940/*
941 * Called to set the hrtick timer state.
942 *
943 * called with rq->lock held and irqs disabled
944 */
945static void hrtick_start(struct rq *rq, u64 delay, int reset)
946{
947 assert_spin_locked(&rq->lock);
948
949 /*
950 * preempt at: now + delay
951 */
952 rq->hrtick_expire =
953 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
954 /*
955 * indicate we need to program the timer
956 */
957 __set_bit(HRTICK_SET, &rq->hrtick_flags);
958 if (reset)
959 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
960
961 /*
962 * New slices are called from the schedule path and don't need a
963 * forced reschedule.
964 */
965 if (reset)
966 resched_hrt(rq->curr);
967}
968
969static void hrtick_clear(struct rq *rq)
970{
971 if (hrtimer_active(&rq->hrtick_timer))
972 hrtimer_cancel(&rq->hrtick_timer);
973}
974
975/*
976 * Update the timer from the possible pending state.
977 */
978static void hrtick_set(struct rq *rq)
979{
980 ktime_t time;
981 int set, reset;
982 unsigned long flags;
983
984 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
985
986 spin_lock_irqsave(&rq->lock, flags);
987 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
988 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
989 time = rq->hrtick_expire;
990 clear_thread_flag(TIF_HRTICK_RESCHED);
991 spin_unlock_irqrestore(&rq->lock, flags);
992
993 if (set) {
994 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
995 if (reset && !hrtimer_active(&rq->hrtick_timer))
996 resched_rq(rq);
997 } else
998 hrtick_clear(rq);
999}
1000
1001/*
1002 * High-resolution timer tick.
1003 * Runs from hardirq context with interrupts disabled.
1004 */
1005static enum hrtimer_restart hrtick(struct hrtimer *timer)
1006{
1007 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1008
1009 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1010
1011 spin_lock(&rq->lock);
1012 __update_rq_clock(rq);
1013 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1014 spin_unlock(&rq->lock);
1015
1016 return HRTIMER_NORESTART;
1017}
1018
1019static inline void init_rq_hrtick(struct rq *rq)
1020{
1021 rq->hrtick_flags = 0;
1022 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1023 rq->hrtick_timer.function = hrtick;
1024 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1025}
1026
1027void hrtick_resched(void)
1028{
1029 struct rq *rq;
1030 unsigned long flags;
1031
1032 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1033 return;
1034
1035 local_irq_save(flags);
1036 rq = cpu_rq(smp_processor_id());
1037 hrtick_set(rq);
1038 local_irq_restore(flags);
1039}
1040#else
1041static inline void hrtick_clear(struct rq *rq)
1042{
1043}
1044
1045static inline void hrtick_set(struct rq *rq)
1046{
1047}
1048
1049static inline void init_rq_hrtick(struct rq *rq)
1050{
1051}
1052
1053void hrtick_resched(void)
1054{
1055}
1056#endif
1057
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001058/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001059 * resched_task - mark a task 'to be rescheduled now'.
1060 *
1061 * On UP this means the setting of the need_resched flag, on SMP it
1062 * might also involve a cross-CPU call to trigger the scheduler on
1063 * the target CPU.
1064 */
1065#ifdef CONFIG_SMP
1066
1067#ifndef tsk_is_polling
1068#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1069#endif
1070
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001071static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001072{
1073 int cpu;
1074
1075 assert_spin_locked(&task_rq(p)->lock);
1076
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001077 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001078 return;
1079
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001080 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001081
1082 cpu = task_cpu(p);
1083 if (cpu == smp_processor_id())
1084 return;
1085
1086 /* NEED_RESCHED must be visible before we test polling */
1087 smp_mb();
1088 if (!tsk_is_polling(p))
1089 smp_send_reschedule(cpu);
1090}
1091
1092static void resched_cpu(int cpu)
1093{
1094 struct rq *rq = cpu_rq(cpu);
1095 unsigned long flags;
1096
1097 if (!spin_trylock_irqsave(&rq->lock, flags))
1098 return;
1099 resched_task(cpu_curr(cpu));
1100 spin_unlock_irqrestore(&rq->lock, flags);
1101}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001102
1103#ifdef CONFIG_NO_HZ
1104/*
1105 * When add_timer_on() enqueues a timer into the timer wheel of an
1106 * idle CPU then this timer might expire before the next timer event
1107 * which is scheduled to wake up that CPU. In case of a completely
1108 * idle system the next event might even be infinite time into the
1109 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1110 * leaves the inner idle loop so the newly added timer is taken into
1111 * account when the CPU goes back to idle and evaluates the timer
1112 * wheel for the next timer event.
1113 */
1114void wake_up_idle_cpu(int cpu)
1115{
1116 struct rq *rq = cpu_rq(cpu);
1117
1118 if (cpu == smp_processor_id())
1119 return;
1120
1121 /*
1122 * This is safe, as this function is called with the timer
1123 * wheel base lock of (cpu) held. When the CPU is on the way
1124 * to idle and has not yet set rq->curr to idle then it will
1125 * be serialized on the timer wheel base lock and take the new
1126 * timer into account automatically.
1127 */
1128 if (rq->curr != rq->idle)
1129 return;
1130
1131 /*
1132 * We can set TIF_RESCHED on the idle task of the other CPU
1133 * lockless. The worst case is that the other CPU runs the
1134 * idle task through an additional NOOP schedule()
1135 */
1136 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1137
1138 /* NEED_RESCHED must be visible before we test polling */
1139 smp_mb();
1140 if (!tsk_is_polling(rq->idle))
1141 smp_send_reschedule(cpu);
1142}
1143#endif
1144
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001145#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001146static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001147{
1148 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001149 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001150}
1151#endif
1152
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001153#if BITS_PER_LONG == 32
1154# define WMULT_CONST (~0UL)
1155#else
1156# define WMULT_CONST (1UL << 32)
1157#endif
1158
1159#define WMULT_SHIFT 32
1160
Ingo Molnar194081e2007-08-09 11:16:51 +02001161/*
1162 * Shift right and round:
1163 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001164#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001165
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001166static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001167calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1168 struct load_weight *lw)
1169{
1170 u64 tmp;
1171
1172 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001173 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001174
1175 tmp = (u64)delta_exec * weight;
1176 /*
1177 * Check whether we'd overflow the 64-bit multiplication:
1178 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001179 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001180 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001181 WMULT_SHIFT/2);
1182 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001183 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001184
Ingo Molnarecf691d2007-08-02 17:41:40 +02001185 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001186}
1187
1188static inline unsigned long
1189calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1190{
1191 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1192}
1193
Ingo Molnar10919852007-10-15 17:00:04 +02001194static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001195{
1196 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001197 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001198}
1199
Ingo Molnar10919852007-10-15 17:00:04 +02001200static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001201{
1202 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001203 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001207 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1208 * of tasks with abnormal "nice" values across CPUs the contribution that
1209 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001210 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001211 * scaled version of the new time slice allocation that they receive on time
1212 * slice expiry etc.
1213 */
1214
Ingo Molnardd41f592007-07-09 18:51:59 +02001215#define WEIGHT_IDLEPRIO 2
1216#define WMULT_IDLEPRIO (1 << 31)
1217
1218/*
1219 * Nice levels are multiplicative, with a gentle 10% change for every
1220 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1221 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1222 * that remained on nice 0.
1223 *
1224 * The "10% effect" is relative and cumulative: from _any_ nice level,
1225 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001226 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1227 * If a task goes up by ~10% and another task goes down by ~10% then
1228 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001229 */
1230static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001231 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1232 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1233 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1234 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1235 /* 0 */ 1024, 820, 655, 526, 423,
1236 /* 5 */ 335, 272, 215, 172, 137,
1237 /* 10 */ 110, 87, 70, 56, 45,
1238 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001239};
1240
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001241/*
1242 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1243 *
1244 * In cases where the weight does not change often, we can use the
1245 * precalculated inverse to speed up arithmetics by turning divisions
1246 * into multiplications:
1247 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001248static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001249 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1250 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1251 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1252 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1253 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1254 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1255 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1256 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001257};
Peter Williams2dd73a42006-06-27 02:54:34 -07001258
Ingo Molnardd41f592007-07-09 18:51:59 +02001259static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1260
1261/*
1262 * runqueue iterator, to support SMP load-balancing between different
1263 * scheduling classes, without having to expose their internal data
1264 * structures to the load-balancing proper:
1265 */
1266struct rq_iterator {
1267 void *arg;
1268 struct task_struct *(*start)(void *);
1269 struct task_struct *(*next)(void *);
1270};
1271
Peter Williamse1d14842007-10-24 18:23:51 +02001272#ifdef CONFIG_SMP
1273static unsigned long
1274balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1275 unsigned long max_load_move, struct sched_domain *sd,
1276 enum cpu_idle_type idle, int *all_pinned,
1277 int *this_best_prio, struct rq_iterator *iterator);
1278
1279static int
1280iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1281 struct sched_domain *sd, enum cpu_idle_type idle,
1282 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001283#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001284
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001285#ifdef CONFIG_CGROUP_CPUACCT
1286static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1287#else
1288static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1289#endif
1290
Gregory Haskinse7693a32008-01-25 21:08:09 +01001291#ifdef CONFIG_SMP
1292static unsigned long source_load(int cpu, int type);
1293static unsigned long target_load(int cpu, int type);
1294static unsigned long cpu_avg_load_per_task(int cpu);
1295static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1296#endif /* CONFIG_SMP */
1297
Ingo Molnardd41f592007-07-09 18:51:59 +02001298#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001299#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001300#include "sched_fair.c"
1301#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001302#ifdef CONFIG_SCHED_DEBUG
1303# include "sched_debug.c"
1304#endif
1305
1306#define sched_class_highest (&rt_sched_class)
1307
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001308static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001309{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001310 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001311}
1312
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001313static inline void dec_load(struct rq *rq, const struct task_struct *p)
1314{
1315 update_load_sub(&rq->load, p->se.load.weight);
1316}
1317
1318static void inc_nr_running(struct task_struct *p, struct rq *rq)
1319{
1320 rq->nr_running++;
1321 inc_load(rq, p);
1322}
1323
1324static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001325{
1326 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001327 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001328}
1329
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001330static void set_load_weight(struct task_struct *p)
1331{
1332 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001333 p->se.load.weight = prio_to_weight[0] * 2;
1334 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1335 return;
1336 }
1337
1338 /*
1339 * SCHED_IDLE tasks get minimal weight:
1340 */
1341 if (p->policy == SCHED_IDLE) {
1342 p->se.load.weight = WEIGHT_IDLEPRIO;
1343 p->se.load.inv_weight = WMULT_IDLEPRIO;
1344 return;
1345 }
1346
1347 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1348 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001349}
1350
Ingo Molnar8159f872007-08-09 11:16:49 +02001351static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001352{
1353 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001354 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001355 p->se.on_rq = 1;
1356}
1357
Ingo Molnar69be72c2007-08-09 11:16:49 +02001358static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001359{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001360 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001361 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001362}
1363
1364/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001365 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001366 */
Ingo Molnar14531182007-07-09 18:51:59 +02001367static inline int __normal_prio(struct task_struct *p)
1368{
Ingo Molnardd41f592007-07-09 18:51:59 +02001369 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001370}
1371
1372/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001373 * Calculate the expected normal priority: i.e. priority
1374 * without taking RT-inheritance into account. Might be
1375 * boosted by interactivity modifiers. Changes upon fork,
1376 * setprio syscalls, and whenever the interactivity
1377 * estimator recalculates.
1378 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001379static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001380{
1381 int prio;
1382
Ingo Molnare05606d2007-07-09 18:51:59 +02001383 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001384 prio = MAX_RT_PRIO-1 - p->rt_priority;
1385 else
1386 prio = __normal_prio(p);
1387 return prio;
1388}
1389
1390/*
1391 * Calculate the current priority, i.e. the priority
1392 * taken into account by the scheduler. This value might
1393 * be boosted by RT tasks, or might be boosted by
1394 * interactivity modifiers. Will be RT if the task got
1395 * RT-boosted. If not then it returns p->normal_prio.
1396 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001397static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001398{
1399 p->normal_prio = normal_prio(p);
1400 /*
1401 * If we are RT tasks or we were boosted to RT priority,
1402 * keep the priority unchanged. Otherwise, update priority
1403 * to the normal priority:
1404 */
1405 if (!rt_prio(p->prio))
1406 return p->normal_prio;
1407 return p->prio;
1408}
1409
1410/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001411 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001413static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001415 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001416 rq->nr_uninterruptible--;
1417
Ingo Molnar8159f872007-08-09 11:16:49 +02001418 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001419 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * deactivate_task - remove a task from the runqueue.
1424 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001425static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001427 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001428 rq->nr_uninterruptible++;
1429
Ingo Molnar69be72c2007-08-09 11:16:49 +02001430 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001431 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434/**
1435 * task_curr - is this task currently executing on a CPU?
1436 * @p: the task in question.
1437 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001438inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 return cpu_curr(task_cpu(p)) == p;
1441}
1442
Peter Williams2dd73a42006-06-27 02:54:34 -07001443/* Used instead of source_load when we know the type == 0 */
1444unsigned long weighted_cpuload(const int cpu)
1445{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001446 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001447}
1448
1449static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1450{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001451 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001452#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001453 /*
1454 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1455 * successfuly executed on another CPU. We must ensure that updates of
1456 * per-task data have been completed by this moment.
1457 */
1458 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001459 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001460#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001461}
1462
Steven Rostedtcb469842008-01-25 21:08:22 +01001463static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1464 const struct sched_class *prev_class,
1465 int oldprio, int running)
1466{
1467 if (prev_class != p->sched_class) {
1468 if (prev_class->switched_from)
1469 prev_class->switched_from(rq, p, running);
1470 p->sched_class->switched_to(rq, p, running);
1471 } else
1472 p->sched_class->prio_changed(rq, p, oldprio, running);
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001476
Ingo Molnarcc367732007-10-15 17:00:18 +02001477/*
1478 * Is this task likely cache-hot:
1479 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001480static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001481task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1482{
1483 s64 delta;
1484
Ingo Molnarf540a602008-03-15 17:10:34 +01001485 /*
1486 * Buddy candidates are cache hot:
1487 */
1488 if (&p->se == cfs_rq_of(&p->se)->next)
1489 return 1;
1490
Ingo Molnarcc367732007-10-15 17:00:18 +02001491 if (p->sched_class != &fair_sched_class)
1492 return 0;
1493
Ingo Molnar6bc16652007-10-15 17:00:18 +02001494 if (sysctl_sched_migration_cost == -1)
1495 return 1;
1496 if (sysctl_sched_migration_cost == 0)
1497 return 0;
1498
Ingo Molnarcc367732007-10-15 17:00:18 +02001499 delta = now - p->se.exec_start;
1500
1501 return delta < (s64)sysctl_sched_migration_cost;
1502}
1503
1504
Ingo Molnardd41f592007-07-09 18:51:59 +02001505void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001506{
Ingo Molnardd41f592007-07-09 18:51:59 +02001507 int old_cpu = task_cpu(p);
1508 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001509 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1510 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001511 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001512
1513 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001514
1515#ifdef CONFIG_SCHEDSTATS
1516 if (p->se.wait_start)
1517 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001518 if (p->se.sleep_start)
1519 p->se.sleep_start -= clock_offset;
1520 if (p->se.block_start)
1521 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001522 if (old_cpu != new_cpu) {
1523 schedstat_inc(p, se.nr_migrations);
1524 if (task_hot(p, old_rq->clock, NULL))
1525 schedstat_inc(p, se.nr_forced2_migrations);
1526 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001527#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001528 p->se.vruntime -= old_cfsrq->min_vruntime -
1529 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001530
1531 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001532}
1533
Ingo Molnar70b97a72006-07-03 00:25:42 -07001534struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Ingo Molnar36c8b582006-07-03 00:25:41 -07001537 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int dest_cpu;
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001541};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543/*
1544 * The task's runqueue lock must be held.
1545 * Returns true if you have to wait for migration thread.
1546 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001547static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001548migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001550 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /*
1553 * If the task is not on a runqueue (and not running), then
1554 * it is sufficient to simply update the task's cpu field.
1555 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001556 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 set_task_cpu(p, dest_cpu);
1558 return 0;
1559 }
1560
1561 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 req->task = p;
1563 req->dest_cpu = dest_cpu;
1564 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 1;
1567}
1568
1569/*
1570 * wait_task_inactive - wait for a thread to unschedule.
1571 *
1572 * The caller must ensure that the task *will* unschedule sometime soon,
1573 * else this function might spin for a *long* time. This function can't
1574 * be called with interrupts off, or it may introduce deadlock with
1575 * smp_call_function() if an IPI is sent by the same process we are
1576 * waiting to become inactive.
1577 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001578void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001581 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001582 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Andi Kleen3a5c3592007-10-15 17:00:14 +02001584 for (;;) {
1585 /*
1586 * We do the initial early heuristics without holding
1587 * any task-queue locks at all. We'll only try to get
1588 * the runqueue lock when things look like they will
1589 * work out!
1590 */
1591 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001592
Andi Kleen3a5c3592007-10-15 17:00:14 +02001593 /*
1594 * If the task is actively running on another CPU
1595 * still, just relax and busy-wait without holding
1596 * any locks.
1597 *
1598 * NOTE! Since we don't hold any locks, it's not
1599 * even sure that "rq" stays as the right runqueue!
1600 * But we don't care, since "task_running()" will
1601 * return false if the runqueue has changed and p
1602 * is actually now running somewhere else!
1603 */
1604 while (task_running(rq, p))
1605 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001606
Andi Kleen3a5c3592007-10-15 17:00:14 +02001607 /*
1608 * Ok, time to look more closely! We need the rq
1609 * lock now, to be *sure*. If we're wrong, we'll
1610 * just go back and repeat.
1611 */
1612 rq = task_rq_lock(p, &flags);
1613 running = task_running(rq, p);
1614 on_rq = p->se.on_rq;
1615 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001616
Andi Kleen3a5c3592007-10-15 17:00:14 +02001617 /*
1618 * Was it really running after all now that we
1619 * checked with the proper locks actually held?
1620 *
1621 * Oops. Go back and try again..
1622 */
1623 if (unlikely(running)) {
1624 cpu_relax();
1625 continue;
1626 }
1627
1628 /*
1629 * It's not enough that it's not actively running,
1630 * it must be off the runqueue _entirely_, and not
1631 * preempted!
1632 *
1633 * So if it wa still runnable (but just not actively
1634 * running right now), it's preempted, and we should
1635 * yield - it could be a while.
1636 */
1637 if (unlikely(on_rq)) {
1638 schedule_timeout_uninterruptible(1);
1639 continue;
1640 }
1641
1642 /*
1643 * Ahh, all good. It wasn't running, and it wasn't
1644 * runnable, which means that it will never become
1645 * running in the future either. We're all done!
1646 */
1647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/***
1652 * kick_process - kick a running thread to enter/exit the kernel
1653 * @p: the to-be-kicked thread
1654 *
1655 * Cause a process which is running on another CPU to enter
1656 * kernel-mode, without any delay. (to get signals handled.)
1657 *
1658 * NOTE: this function doesnt have to take the runqueue lock,
1659 * because all it wants to ensure is that the remote task enters
1660 * the kernel. If the IPI races and the task has been migrated
1661 * to another CPU then no harm is done and the purpose has been
1662 * achieved as well.
1663 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001664void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 int cpu;
1667
1668 preempt_disable();
1669 cpu = task_cpu(p);
1670 if ((cpu != smp_processor_id()) && task_curr(p))
1671 smp_send_reschedule(cpu);
1672 preempt_enable();
1673}
1674
1675/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001676 * Return a low guess at the load of a migration-source cpu weighted
1677 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
1679 * We want to under-estimate the load of migration sources, to
1680 * balance conservatively.
1681 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001682static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001683{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001684 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001685 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001686
Peter Williams2dd73a42006-06-27 02:54:34 -07001687 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001688 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001689
Ingo Molnardd41f592007-07-09 18:51:59 +02001690 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001694 * Return a high guess at the load of a migration-target cpu weighted
1695 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001697static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001698{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001699 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001700 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001701
Peter Williams2dd73a42006-06-27 02:54:34 -07001702 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001703 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001704
Ingo Molnardd41f592007-07-09 18:51:59 +02001705 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001706}
1707
1708/*
1709 * Return the average load per task on the cpu's run queue
1710 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001711static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001712{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001713 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001714 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001715 unsigned long n = rq->nr_running;
1716
Ingo Molnardd41f592007-07-09 18:51:59 +02001717 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
Nick Piggin147cbb42005-06-25 14:57:19 -07001720/*
1721 * find_idlest_group finds and returns the least busy CPU group within the
1722 * domain.
1723 */
1724static struct sched_group *
1725find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1726{
1727 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1728 unsigned long min_load = ULONG_MAX, this_load = 0;
1729 int load_idx = sd->forkexec_idx;
1730 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1731
1732 do {
1733 unsigned long load, avg_load;
1734 int local_group;
1735 int i;
1736
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001737 /* Skip over this group if it has no CPUs allowed */
1738 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001739 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001740
Nick Piggin147cbb42005-06-25 14:57:19 -07001741 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001742
1743 /* Tally up the load of all CPUs in the group */
1744 avg_load = 0;
1745
1746 for_each_cpu_mask(i, group->cpumask) {
1747 /* Bias balancing toward cpus of our domain */
1748 if (local_group)
1749 load = source_load(i, load_idx);
1750 else
1751 load = target_load(i, load_idx);
1752
1753 avg_load += load;
1754 }
1755
1756 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001757 avg_load = sg_div_cpu_power(group,
1758 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001759
1760 if (local_group) {
1761 this_load = avg_load;
1762 this = group;
1763 } else if (avg_load < min_load) {
1764 min_load = avg_load;
1765 idlest = group;
1766 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001767 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001768
1769 if (!idlest || 100*this_load < imbalance*min_load)
1770 return NULL;
1771 return idlest;
1772}
1773
1774/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001775 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001776 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001777static int
1778find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001779{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001780 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001781 unsigned long load, min_load = ULONG_MAX;
1782 int idlest = -1;
1783 int i;
1784
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001785 /* Traverse only the allowed CPUs */
1786 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1787
1788 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001789 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001790
1791 if (load < min_load || (load == min_load && i == this_cpu)) {
1792 min_load = load;
1793 idlest = i;
1794 }
1795 }
1796
1797 return idlest;
1798}
1799
Nick Piggin476d1392005-06-25 14:57:29 -07001800/*
1801 * sched_balance_self: balance the current task (running on cpu) in domains
1802 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1803 * SD_BALANCE_EXEC.
1804 *
1805 * Balance, ie. select the least loaded group.
1806 *
1807 * Returns the target CPU number, or the same CPU if no balancing is needed.
1808 *
1809 * preempt must be disabled.
1810 */
1811static int sched_balance_self(int cpu, int flag)
1812{
1813 struct task_struct *t = current;
1814 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001815
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001816 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001817 /*
1818 * If power savings logic is enabled for a domain, stop there.
1819 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001820 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1821 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001822 if (tmp->flags & flag)
1823 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001824 }
Nick Piggin476d1392005-06-25 14:57:29 -07001825
1826 while (sd) {
1827 cpumask_t span;
1828 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001829 int new_cpu, weight;
1830
1831 if (!(sd->flags & flag)) {
1832 sd = sd->child;
1833 continue;
1834 }
Nick Piggin476d1392005-06-25 14:57:29 -07001835
1836 span = sd->span;
1837 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001838 if (!group) {
1839 sd = sd->child;
1840 continue;
1841 }
Nick Piggin476d1392005-06-25 14:57:29 -07001842
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001843 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001844 if (new_cpu == -1 || new_cpu == cpu) {
1845 /* Now try balancing at a lower domain level of cpu */
1846 sd = sd->child;
1847 continue;
1848 }
Nick Piggin476d1392005-06-25 14:57:29 -07001849
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001850 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001851 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001852 sd = NULL;
1853 weight = cpus_weight(span);
1854 for_each_domain(cpu, tmp) {
1855 if (weight <= cpus_weight(tmp->span))
1856 break;
1857 if (tmp->flags & flag)
1858 sd = tmp;
1859 }
1860 /* while loop will break here if sd == NULL */
1861 }
1862
1863 return cpu;
1864}
1865
1866#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/***
1869 * try_to_wake_up - wake up a thread
1870 * @p: the to-be-woken-up thread
1871 * @state: the mask of task states that can be woken
1872 * @sync: do a synchronous wakeup?
1873 *
1874 * Put it on the run-queue if it's not already there. The "current"
1875 * thread is always on the run-queue (except when the actual
1876 * re-schedule is in progress), and as such you're allowed to do
1877 * the simpler "current->state = TASK_RUNNING" to mark yourself
1878 * runnable without the overhead of this.
1879 *
1880 * returns failure only if the task is already active.
1881 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001882static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Ingo Molnarcc367732007-10-15 17:00:18 +02001884 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned long flags;
1886 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001887 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Linus Torvalds04e2f172008-02-23 18:05:03 -08001889 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rq = task_rq_lock(p, &flags);
1891 old_state = p->state;
1892 if (!(old_state & state))
1893 goto out;
1894
Ingo Molnardd41f592007-07-09 18:51:59 +02001895 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto out_running;
1897
1898 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001899 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 this_cpu = smp_processor_id();
1901
1902#ifdef CONFIG_SMP
1903 if (unlikely(task_running(rq, p)))
1904 goto out_activate;
1905
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001906 cpu = p->sched_class->select_task_rq(p, sync);
1907 if (cpu != orig_cpu) {
1908 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 task_rq_unlock(rq, &flags);
1910 /* might preempt at this point */
1911 rq = task_rq_lock(p, &flags);
1912 old_state = p->state;
1913 if (!(old_state & state))
1914 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001915 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 goto out_running;
1917
1918 this_cpu = smp_processor_id();
1919 cpu = task_cpu(p);
1920 }
1921
Gregory Haskinse7693a32008-01-25 21:08:09 +01001922#ifdef CONFIG_SCHEDSTATS
1923 schedstat_inc(rq, ttwu_count);
1924 if (cpu == this_cpu)
1925 schedstat_inc(rq, ttwu_local);
1926 else {
1927 struct sched_domain *sd;
1928 for_each_domain(this_cpu, sd) {
1929 if (cpu_isset(cpu, sd->span)) {
1930 schedstat_inc(sd, ttwu_wake_remote);
1931 break;
1932 }
1933 }
1934 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001935#endif
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937out_activate:
1938#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001939 schedstat_inc(p, se.nr_wakeups);
1940 if (sync)
1941 schedstat_inc(p, se.nr_wakeups_sync);
1942 if (orig_cpu != cpu)
1943 schedstat_inc(p, se.nr_wakeups_migrate);
1944 if (cpu == this_cpu)
1945 schedstat_inc(p, se.nr_wakeups_local);
1946 else
1947 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001948 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001949 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 success = 1;
1951
1952out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001953 check_preempt_curr(rq, p);
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001956#ifdef CONFIG_SMP
1957 if (p->sched_class->task_wake_up)
1958 p->sched_class->task_wake_up(rq, p);
1959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960out:
1961 task_rq_unlock(rq, &flags);
1962
1963 return success;
1964}
1965
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001966int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001968 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970EXPORT_SYMBOL(wake_up_process);
1971
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001972int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 return try_to_wake_up(p, state, 0);
1975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/*
1978 * Perform scheduler related setup for a newly forked process p.
1979 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001980 *
1981 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001983static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Ingo Molnardd41f592007-07-09 18:51:59 +02001985 p->se.exec_start = 0;
1986 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001987 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001988 p->se.last_wakeup = 0;
1989 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001990
1991#ifdef CONFIG_SCHEDSTATS
1992 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001993 p->se.sum_sleep_runtime = 0;
1994 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001995 p->se.block_start = 0;
1996 p->se.sleep_max = 0;
1997 p->se.block_max = 0;
1998 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001999 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002000 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002001#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002002
Peter Zijlstrafa717062008-01-25 21:08:27 +01002003 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002004 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002005
Avi Kivitye107be32007-07-26 13:40:43 +02002006#ifdef CONFIG_PREEMPT_NOTIFIERS
2007 INIT_HLIST_HEAD(&p->preempt_notifiers);
2008#endif
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /*
2011 * We mark the process as running here, but have not actually
2012 * inserted it onto the runqueue yet. This guarantees that
2013 * nobody will actually run it, and a signal or other external
2014 * event cannot wake it up and insert it on the runqueue either.
2015 */
2016 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002017}
2018
2019/*
2020 * fork()/clone()-time setup:
2021 */
2022void sched_fork(struct task_struct *p, int clone_flags)
2023{
2024 int cpu = get_cpu();
2025
2026 __sched_fork(p);
2027
2028#ifdef CONFIG_SMP
2029 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2030#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002031 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002032
2033 /*
2034 * Make sure we do not leak PI boosting priority to the child:
2035 */
2036 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002037 if (!rt_prio(p->prio))
2038 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002039
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002040#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002041 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002042 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002044#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002045 p->oncpu = 0;
2046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002048 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002049 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002051 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054/*
2055 * wake_up_new_task - wake up a newly created task for the first time.
2056 *
2057 * This function will do some initial scheduler statistics housekeeping
2058 * that must be done for every newly created context, then puts the task
2059 * on the runqueue and wakes it.
2060 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002061void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002064 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002068 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 p->prio = effective_prio(p);
2071
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002072 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002073 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002076 * Let the scheduling class do new task startup
2077 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002079 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002080 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002082 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002083#ifdef CONFIG_SMP
2084 if (p->sched_class->task_wake_up)
2085 p->sched_class->task_wake_up(rq, p);
2086#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002087 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Avi Kivitye107be32007-07-26 13:40:43 +02002090#ifdef CONFIG_PREEMPT_NOTIFIERS
2091
2092/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002093 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2094 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002095 */
2096void preempt_notifier_register(struct preempt_notifier *notifier)
2097{
2098 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2099}
2100EXPORT_SYMBOL_GPL(preempt_notifier_register);
2101
2102/**
2103 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002104 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002105 *
2106 * This is safe to call from within a preemption notifier.
2107 */
2108void preempt_notifier_unregister(struct preempt_notifier *notifier)
2109{
2110 hlist_del(&notifier->link);
2111}
2112EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2113
2114static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2115{
2116 struct preempt_notifier *notifier;
2117 struct hlist_node *node;
2118
2119 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2120 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2121}
2122
2123static void
2124fire_sched_out_preempt_notifiers(struct task_struct *curr,
2125 struct task_struct *next)
2126{
2127 struct preempt_notifier *notifier;
2128 struct hlist_node *node;
2129
2130 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2131 notifier->ops->sched_out(notifier, next);
2132}
2133
2134#else
2135
2136static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2137{
2138}
2139
2140static void
2141fire_sched_out_preempt_notifiers(struct task_struct *curr,
2142 struct task_struct *next)
2143{
2144}
2145
2146#endif
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002149 * prepare_task_switch - prepare to switch tasks
2150 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002151 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002152 * @next: the task we are going to switch to.
2153 *
2154 * This is called with the rq lock held and interrupts off. It must
2155 * be paired with a subsequent finish_task_switch after the context
2156 * switch.
2157 *
2158 * prepare_task_switch sets up locking and calls architecture specific
2159 * hooks.
2160 */
Avi Kivitye107be32007-07-26 13:40:43 +02002161static inline void
2162prepare_task_switch(struct rq *rq, struct task_struct *prev,
2163 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002164{
Avi Kivitye107be32007-07-26 13:40:43 +02002165 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002166 prepare_lock_switch(rq, next);
2167 prepare_arch_switch(next);
2168}
2169
2170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002172 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * @prev: the thread we just switched away from.
2174 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002175 * finish_task_switch must be called after the context switch, paired
2176 * with a prepare_task_switch call before the context switch.
2177 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2178 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 *
2180 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002181 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * with the lock held can cause deadlocks; see schedule() for
2183 * details.)
2184 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002185static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 __releases(rq->lock)
2187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002189 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 rq->prev_mm = NULL;
2192
2193 /*
2194 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002195 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002196 * schedule one last time. The schedule call will never return, and
2197 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002198 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 * still held, otherwise prev could be scheduled on another cpu, die
2200 * there before we look at prev->state, and then the reference would
2201 * be dropped twice.
2202 * Manfred Spraul <manfred@colorfullife.com>
2203 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002204 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002205 finish_arch_switch(prev);
2206 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002207#ifdef CONFIG_SMP
2208 if (current->sched_class->post_schedule)
2209 current->sched_class->post_schedule(rq);
2210#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002211
Avi Kivitye107be32007-07-26 13:40:43 +02002212 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (mm)
2214 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002215 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002216 /*
2217 * Remove function-return probe instances associated with this
2218 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002219 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002220 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
2225/**
2226 * schedule_tail - first thing a freshly forked thread must call.
2227 * @prev: the thread we just switched away from.
2228 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002229asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 __releases(rq->lock)
2231{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002232 struct rq *rq = this_rq();
2233
Nick Piggin4866cde2005-06-25 14:57:23 -07002234 finish_task_switch(rq, prev);
2235#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2236 /* In this case, finish_task_switch does not reenable preemption */
2237 preempt_enable();
2238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002240 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243/*
2244 * context_switch - switch to the new MM and the new
2245 * thread's register state.
2246 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002247static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002248context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002249 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Ingo Molnardd41f592007-07-09 18:51:59 +02002251 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Avi Kivitye107be32007-07-26 13:40:43 +02002253 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002254 mm = next->mm;
2255 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002256 /*
2257 * For paravirt, this is coupled with an exit in switch_to to
2258 * combine the page table reload and the switch backend into
2259 * one hypercall.
2260 */
2261 arch_enter_lazy_cpu_mode();
2262
Ingo Molnardd41f592007-07-09 18:51:59 +02002263 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 next->active_mm = oldmm;
2265 atomic_inc(&oldmm->mm_count);
2266 enter_lazy_tlb(oldmm, next);
2267 } else
2268 switch_mm(oldmm, mm, next);
2269
Ingo Molnardd41f592007-07-09 18:51:59 +02002270 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 rq->prev_mm = oldmm;
2273 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002274 /*
2275 * Since the runqueue lock will be released by the next
2276 * task (which is an invalid locking op but in the case
2277 * of the scheduler it's an obvious special-case), so we
2278 * do an early lockdep release here:
2279 */
2280#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002281 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 /* Here we just switch the register state and the stack. */
2285 switch_to(prev, next, prev);
2286
Ingo Molnardd41f592007-07-09 18:51:59 +02002287 barrier();
2288 /*
2289 * this_rq must be evaluated again because prev may have moved
2290 * CPUs since it called schedule(), thus the 'rq' on its stack
2291 * frame will be invalid.
2292 */
2293 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296/*
2297 * nr_running, nr_uninterruptible and nr_context_switches:
2298 *
2299 * externally visible scheduler statistics: current number of runnable
2300 * threads, current number of uninterruptible-sleeping threads, total
2301 * number of context switches performed since bootup.
2302 */
2303unsigned long nr_running(void)
2304{
2305 unsigned long i, sum = 0;
2306
2307 for_each_online_cpu(i)
2308 sum += cpu_rq(i)->nr_running;
2309
2310 return sum;
2311}
2312
2313unsigned long nr_uninterruptible(void)
2314{
2315 unsigned long i, sum = 0;
2316
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002317 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 sum += cpu_rq(i)->nr_uninterruptible;
2319
2320 /*
2321 * Since we read the counters lockless, it might be slightly
2322 * inaccurate. Do not allow it to go below zero though:
2323 */
2324 if (unlikely((long)sum < 0))
2325 sum = 0;
2326
2327 return sum;
2328}
2329
2330unsigned long long nr_context_switches(void)
2331{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002332 int i;
2333 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002335 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 sum += cpu_rq(i)->nr_switches;
2337
2338 return sum;
2339}
2340
2341unsigned long nr_iowait(void)
2342{
2343 unsigned long i, sum = 0;
2344
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002345 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2347
2348 return sum;
2349}
2350
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002351unsigned long nr_active(void)
2352{
2353 unsigned long i, running = 0, uninterruptible = 0;
2354
2355 for_each_online_cpu(i) {
2356 running += cpu_rq(i)->nr_running;
2357 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2358 }
2359
2360 if (unlikely((long)uninterruptible < 0))
2361 uninterruptible = 0;
2362
2363 return running + uninterruptible;
2364}
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002367 * Update rq->cpu_load[] statistics. This function is usually called every
2368 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002369 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002370static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002371{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002372 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002373 int i, scale;
2374
2375 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002376
2377 /* Update our load: */
2378 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2379 unsigned long old_load, new_load;
2380
2381 /* scale is effectively 1 << i now, and >> i divides by scale */
2382
2383 old_load = this_rq->cpu_load[i];
2384 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002385 /*
2386 * Round up the averaging division if load is increasing. This
2387 * prevents us from getting stuck on 9 if the load is 10, for
2388 * example.
2389 */
2390 if (new_load > old_load)
2391 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002392 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2393 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002394}
2395
Ingo Molnardd41f592007-07-09 18:51:59 +02002396#ifdef CONFIG_SMP
2397
Ingo Molnar48f24c42006-07-03 00:25:40 -07002398/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * double_rq_lock - safely lock two runqueues
2400 *
2401 * Note this does not disable interrupts like task_rq_lock,
2402 * you need to do so manually before calling.
2403 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002404static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 __acquires(rq1->lock)
2406 __acquires(rq2->lock)
2407{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002408 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (rq1 == rq2) {
2410 spin_lock(&rq1->lock);
2411 __acquire(rq2->lock); /* Fake it out ;) */
2412 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002413 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 spin_lock(&rq1->lock);
2415 spin_lock(&rq2->lock);
2416 } else {
2417 spin_lock(&rq2->lock);
2418 spin_lock(&rq1->lock);
2419 }
2420 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002421 update_rq_clock(rq1);
2422 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
2425/*
2426 * double_rq_unlock - safely unlock two runqueues
2427 *
2428 * Note this does not restore interrupts like task_rq_unlock,
2429 * you need to do so manually after calling.
2430 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002431static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 __releases(rq1->lock)
2433 __releases(rq2->lock)
2434{
2435 spin_unlock(&rq1->lock);
2436 if (rq1 != rq2)
2437 spin_unlock(&rq2->lock);
2438 else
2439 __release(rq2->lock);
2440}
2441
2442/*
2443 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2444 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002445static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 __releases(this_rq->lock)
2447 __acquires(busiest->lock)
2448 __acquires(this_rq->lock)
2449{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002450 int ret = 0;
2451
Kirill Korotaev054b9102006-12-10 02:20:11 -08002452 if (unlikely(!irqs_disabled())) {
2453 /* printk() doesn't work good under rq->lock */
2454 spin_unlock(&this_rq->lock);
2455 BUG_ON(1);
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002458 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 spin_unlock(&this_rq->lock);
2460 spin_lock(&busiest->lock);
2461 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002462 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 } else
2464 spin_lock(&busiest->lock);
2465 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002466 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
2469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * If dest_cpu is allowed for this process, migrate the task to it.
2471 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002472 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * the cpu_allowed mask is restored.
2474 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002475static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002477 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002479 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 rq = task_rq_lock(p, &flags);
2482 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2483 || unlikely(cpu_is_offline(dest_cpu)))
2484 goto out;
2485
2486 /* force the process onto the specified CPU */
2487 if (migrate_task(p, dest_cpu, &req)) {
2488 /* Need to wait for migration thread (might exit: take ref). */
2489 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 get_task_struct(mt);
2492 task_rq_unlock(rq, &flags);
2493 wake_up_process(mt);
2494 put_task_struct(mt);
2495 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return;
2498 }
2499out:
2500 task_rq_unlock(rq, &flags);
2501}
2502
2503/*
Nick Piggin476d1392005-06-25 14:57:29 -07002504 * sched_exec - execve() is a valuable balancing opportunity, because at
2505 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
2507void sched_exec(void)
2508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002510 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002512 if (new_cpu != this_cpu)
2513 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
2516/*
2517 * pull_task - move a task from a remote runqueue to the local runqueue.
2518 * Both runqueues must be locked.
2519 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002520static void pull_task(struct rq *src_rq, struct task_struct *p,
2521 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002523 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002525 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /*
2527 * Note that idle threads have a prio of MAX_PRIO, for this test
2528 * to be always true for them.
2529 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002530 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533/*
2534 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2535 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002536static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002537int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002538 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002539 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 /*
2542 * We do not migrate tasks that are:
2543 * 1) running (obviously), or
2544 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2545 * 3) are cache-hot on their current CPU.
2546 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002547 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2548 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002550 }
Nick Piggin81026792005-06-25 14:57:07 -07002551 *all_pinned = 0;
2552
Ingo Molnarcc367732007-10-15 17:00:18 +02002553 if (task_running(rq, p)) {
2554 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002555 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Ingo Molnarda84d962007-10-15 17:00:18 +02002558 /*
2559 * Aggressive migration if:
2560 * 1) task is cache cold, or
2561 * 2) too many balance attempts have failed.
2562 */
2563
Ingo Molnar6bc16652007-10-15 17:00:18 +02002564 if (!task_hot(p, rq->clock, sd) ||
2565 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002566#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002567 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002568 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002569 schedstat_inc(p, se.nr_forced_migrations);
2570 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002571#endif
2572 return 1;
2573 }
2574
Ingo Molnarcc367732007-10-15 17:00:18 +02002575 if (task_hot(p, rq->clock, sd)) {
2576 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002577 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return 1;
2580}
2581
Peter Williamse1d14842007-10-24 18:23:51 +02002582static unsigned long
2583balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2584 unsigned long max_load_move, struct sched_domain *sd,
2585 enum cpu_idle_type idle, int *all_pinned,
2586 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002587{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002588 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002589 struct task_struct *p;
2590 long rem_load_move = max_load_move;
2591
Peter Williamse1d14842007-10-24 18:23:51 +02002592 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002593 goto out;
2594
2595 pinned = 1;
2596
2597 /*
2598 * Start the load-balancing iterator:
2599 */
2600 p = iterator->start(iterator->arg);
2601next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002602 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002603 goto out;
2604 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002605 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002606 * skip a task if it will be the highest priority task (i.e. smallest
2607 * prio value) on its new queue regardless of its load weight
2608 */
2609 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2610 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002611 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002612 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002613 p = iterator->next(iterator->arg);
2614 goto next;
2615 }
2616
2617 pull_task(busiest, p, this_rq, this_cpu);
2618 pulled++;
2619 rem_load_move -= p->se.load.weight;
2620
2621 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002622 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002623 */
Peter Williamse1d14842007-10-24 18:23:51 +02002624 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002625 if (p->prio < *this_best_prio)
2626 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002627 p = iterator->next(iterator->arg);
2628 goto next;
2629 }
2630out:
2631 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002632 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002633 * so we can safely collect pull_task() stats here rather than
2634 * inside pull_task().
2635 */
2636 schedstat_add(sd, lb_gained[idle], pulled);
2637
2638 if (all_pinned)
2639 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002640
2641 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002642}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644/*
Peter Williams43010652007-08-09 11:16:46 +02002645 * move_tasks tries to move up to max_load_move weighted load from busiest to
2646 * this_rq, as part of a balancing operation within domain "sd".
2647 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
2649 * Called with both runqueues locked.
2650 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002651static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002652 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002653 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002654 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002656 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002657 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002658 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Ingo Molnardd41f592007-07-09 18:51:59 +02002660 do {
Peter Williams43010652007-08-09 11:16:46 +02002661 total_load_moved +=
2662 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002663 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002664 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002665 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002666 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Peter Williams43010652007-08-09 11:16:46 +02002668 return total_load_moved > 0;
2669}
2670
Peter Williamse1d14842007-10-24 18:23:51 +02002671static int
2672iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2673 struct sched_domain *sd, enum cpu_idle_type idle,
2674 struct rq_iterator *iterator)
2675{
2676 struct task_struct *p = iterator->start(iterator->arg);
2677 int pinned = 0;
2678
2679 while (p) {
2680 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2681 pull_task(busiest, p, this_rq, this_cpu);
2682 /*
2683 * Right now, this is only the second place pull_task()
2684 * is called, so we can safely collect pull_task()
2685 * stats here rather than inside pull_task().
2686 */
2687 schedstat_inc(sd, lb_gained[idle]);
2688
2689 return 1;
2690 }
2691 p = iterator->next(iterator->arg);
2692 }
2693
2694 return 0;
2695}
2696
Peter Williams43010652007-08-09 11:16:46 +02002697/*
2698 * move_one_task tries to move exactly one task from busiest to this_rq, as
2699 * part of active balancing operations within "domain".
2700 * Returns 1 if successful and 0 otherwise.
2701 *
2702 * Called with both runqueues locked.
2703 */
2704static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2705 struct sched_domain *sd, enum cpu_idle_type idle)
2706{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002707 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002708
2709 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002710 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002711 return 1;
2712
2713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
2716/*
2717 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002718 * domain. It calculates and returns the amount of weighted load which
2719 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
2721static struct sched_group *
2722find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002723 unsigned long *imbalance, enum cpu_idle_type idle,
2724 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2727 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002728 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002729 unsigned long busiest_load_per_task, busiest_nr_running;
2730 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002731 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002732#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2733 int power_savings_balance = 1;
2734 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2735 unsigned long min_nr_running = ULONG_MAX;
2736 struct sched_group *group_min = NULL, *group_leader = NULL;
2737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002740 busiest_load_per_task = busiest_nr_running = 0;
2741 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002742 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002743 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002744 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002745 load_idx = sd->newidle_idx;
2746 else
2747 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002750 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 int local_group;
2752 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002753 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002754 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002755 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 local_group = cpu_isset(this_cpu, group->cpumask);
2758
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002759 if (local_group)
2760 balance_cpu = first_cpu(group->cpumask);
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002763 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002764 max_cpu_load = 0;
2765 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002768 struct rq *rq;
2769
2770 if (!cpu_isset(i, *cpus))
2771 continue;
2772
2773 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002774
Suresh Siddha9439aab2007-07-19 21:28:35 +02002775 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002776 *sd_idle = 0;
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002779 if (local_group) {
2780 if (idle_cpu(i) && !first_idle_cpu) {
2781 first_idle_cpu = 1;
2782 balance_cpu = i;
2783 }
2784
Nick Piggina2000572006-02-10 01:51:02 -08002785 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002786 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002787 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002788 if (load > max_cpu_load)
2789 max_cpu_load = load;
2790 if (min_cpu_load > load)
2791 min_cpu_load = load;
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002795 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002796 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002799 /*
2800 * First idle cpu or the first cpu(busiest) in this sched group
2801 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002802 * domains. In the newly idle case, we will allow all the cpu's
2803 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002804 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002805 if (idle != CPU_NEWLY_IDLE && local_group &&
2806 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002807 *balance = 0;
2808 goto ret;
2809 }
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002812 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002815 avg_load = sg_div_cpu_power(group,
2816 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Ken Chen908a7c12007-10-17 16:55:11 +02002818 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2819 __group_imb = 1;
2820
Eric Dumazet5517d862007-05-08 00:32:57 -07002821 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (local_group) {
2824 this_load = avg_load;
2825 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002826 this_nr_running = sum_nr_running;
2827 this_load_per_task = sum_weighted_load;
2828 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002829 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 max_load = avg_load;
2831 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002832 busiest_nr_running = sum_nr_running;
2833 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002834 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002836
2837#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2838 /*
2839 * Busy processors will not participate in power savings
2840 * balance.
2841 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002842 if (idle == CPU_NOT_IDLE ||
2843 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2844 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002845
2846 /*
2847 * If the local group is idle or completely loaded
2848 * no need to do power savings balance at this domain
2849 */
2850 if (local_group && (this_nr_running >= group_capacity ||
2851 !this_nr_running))
2852 power_savings_balance = 0;
2853
Ingo Molnardd41f592007-07-09 18:51:59 +02002854 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002855 * If a group is already running at full capacity or idle,
2856 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002857 */
2858 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002859 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002860 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002861
Ingo Molnardd41f592007-07-09 18:51:59 +02002862 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002863 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002864 * This is the group from where we need to pick up the load
2865 * for saving power
2866 */
2867 if ((sum_nr_running < min_nr_running) ||
2868 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002869 first_cpu(group->cpumask) <
2870 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002871 group_min = group;
2872 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002873 min_load_per_task = sum_weighted_load /
2874 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002875 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002876
Ingo Molnardd41f592007-07-09 18:51:59 +02002877 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002878 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002879 * capacity but still has some space to pick up some load
2880 * from other group and save more power
2881 */
2882 if (sum_nr_running <= group_capacity - 1) {
2883 if (sum_nr_running > leader_nr_running ||
2884 (sum_nr_running == leader_nr_running &&
2885 first_cpu(group->cpumask) >
2886 first_cpu(group_leader->cpumask))) {
2887 group_leader = group;
2888 leader_nr_running = sum_nr_running;
2889 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002890 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002891group_next:
2892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 group = group->next;
2894 } while (group != sd->groups);
2895
Peter Williams2dd73a42006-06-27 02:54:34 -07002896 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 goto out_balanced;
2898
2899 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2900
2901 if (this_load >= avg_load ||
2902 100*max_load <= sd->imbalance_pct*this_load)
2903 goto out_balanced;
2904
Peter Williams2dd73a42006-06-27 02:54:34 -07002905 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002906 if (group_imb)
2907 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 /*
2910 * We're trying to get all the cpus to the average_load, so we don't
2911 * want to push ourselves above the average load, nor do we wish to
2912 * reduce the max loaded cpu below the average load, as either of these
2913 * actions would just result in more rebalancing later, and ping-pong
2914 * tasks around. Thus we look for the minimum possible imbalance.
2915 * Negative imbalances (*we* are more loaded than anyone else) will
2916 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002917 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 * appear as very large values with unsigned longs.
2919 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002920 if (max_load <= busiest_load_per_task)
2921 goto out_balanced;
2922
2923 /*
2924 * In the presence of smp nice balancing, certain scenarios can have
2925 * max load less than avg load(as we skip the groups at or below
2926 * its cpu_power, while calculating max_load..)
2927 */
2928 if (max_load < avg_load) {
2929 *imbalance = 0;
2930 goto small_imbalance;
2931 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002932
2933 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002934 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002937 *imbalance = min(max_pull * busiest->__cpu_power,
2938 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 / SCHED_LOAD_SCALE;
2940
Peter Williams2dd73a42006-06-27 02:54:34 -07002941 /*
2942 * if *imbalance is less than the average load per runnable task
2943 * there is no gaurantee that any tasks will be moved so we'll have
2944 * a think about bumping its value to force at least one task to be
2945 * moved
2946 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002947 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002948 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002949 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Peter Williams2dd73a42006-06-27 02:54:34 -07002951small_imbalance:
2952 pwr_move = pwr_now = 0;
2953 imbn = 2;
2954 if (this_nr_running) {
2955 this_load_per_task /= this_nr_running;
2956 if (busiest_load_per_task > this_load_per_task)
2957 imbn = 1;
2958 } else
2959 this_load_per_task = SCHED_LOAD_SCALE;
2960
Ingo Molnardd41f592007-07-09 18:51:59 +02002961 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2962 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002963 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return busiest;
2965 }
2966
2967 /*
2968 * OK, we don't have enough imbalance to justify moving tasks,
2969 * however we may be able to increase total CPU power used by
2970 * moving them.
2971 */
2972
Eric Dumazet5517d862007-05-08 00:32:57 -07002973 pwr_now += busiest->__cpu_power *
2974 min(busiest_load_per_task, max_load);
2975 pwr_now += this->__cpu_power *
2976 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 pwr_now /= SCHED_LOAD_SCALE;
2978
2979 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002980 tmp = sg_div_cpu_power(busiest,
2981 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002983 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002984 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002987 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002988 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002989 tmp = sg_div_cpu_power(this,
2990 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002992 tmp = sg_div_cpu_power(this,
2993 busiest_load_per_task * SCHED_LOAD_SCALE);
2994 pwr_move += this->__cpu_power *
2995 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 pwr_move /= SCHED_LOAD_SCALE;
2997
2998 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002999 if (pwr_move > pwr_now)
3000 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return busiest;
3004
3005out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003006#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003007 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003008 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003010 if (this == group_leader && group_leader != group_min) {
3011 *imbalance = min_load_per_task;
3012 return group_min;
3013 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003014#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003015ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 *imbalance = 0;
3017 return NULL;
3018}
3019
3020/*
3021 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3022 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003023static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003024find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003025 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003027 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003028 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int i;
3030
3031 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003032 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003033
3034 if (!cpu_isset(i, *cpus))
3035 continue;
3036
Ingo Molnar48f24c42006-07-03 00:25:40 -07003037 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003038 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Ingo Molnardd41f592007-07-09 18:51:59 +02003040 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003041 continue;
3042
Ingo Molnardd41f592007-07-09 18:51:59 +02003043 if (wl > max_load) {
3044 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003045 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 }
3048
3049 return busiest;
3050}
3051
3052/*
Nick Piggin77391d72005-06-25 14:57:30 -07003053 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3054 * so long as it is large enough.
3055 */
3056#define MAX_PINNED_INTERVAL 512
3057
3058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3060 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003062static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003063 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003064 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Peter Williams43010652007-08-09 11:16:46 +02003066 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003069 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003070 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003071 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003072
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003073 /*
3074 * When power savings policy is enabled for the parent domain, idle
3075 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003076 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003077 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003078 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003079 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003080 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003081 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Ingo Molnar2d723762007-10-15 17:00:12 +02003083 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003085redo:
3086 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003087 &cpus, balance);
3088
Chen, Kenneth W06066712006-12-10 02:20:35 -08003089 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003090 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (!group) {
3093 schedstat_inc(sd, lb_nobusyg[idle]);
3094 goto out_balanced;
3095 }
3096
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003097 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (!busiest) {
3099 schedstat_inc(sd, lb_nobusyq[idle]);
3100 goto out_balanced;
3101 }
3102
Nick Piggindb935db2005-06-25 14:57:11 -07003103 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 schedstat_add(sd, lb_imbalance[idle], imbalance);
3106
Peter Williams43010652007-08-09 11:16:46 +02003107 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (busiest->nr_running > 1) {
3109 /*
3110 * Attempt to move tasks. If find_busiest_group has found
3111 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003112 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 * correctly treated as an imbalance.
3114 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003115 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003116 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003117 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003118 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003119 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003120 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003121
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003122 /*
3123 * some other cpu did the load balance for us.
3124 */
Peter Williams43010652007-08-09 11:16:46 +02003125 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003126 resched_cpu(this_cpu);
3127
Nick Piggin81026792005-06-25 14:57:07 -07003128 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003129 if (unlikely(all_pinned)) {
3130 cpu_clear(cpu_of(busiest), cpus);
3131 if (!cpus_empty(cpus))
3132 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003133 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
Nick Piggin81026792005-06-25 14:57:07 -07003136
Peter Williams43010652007-08-09 11:16:46 +02003137 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 schedstat_inc(sd, lb_failed[idle]);
3139 sd->nr_balance_failed++;
3140
3141 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003143 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003144
3145 /* don't kick the migration_thread, if the curr
3146 * task on busiest cpu can't be moved to this_cpu
3147 */
3148 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003149 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003150 all_pinned = 1;
3151 goto out_one_pinned;
3152 }
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (!busiest->active_balance) {
3155 busiest->active_balance = 1;
3156 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003157 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003159 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003160 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 wake_up_process(busiest->migration_thread);
3162
3163 /*
3164 * We've kicked active balancing, reset the failure
3165 * counter.
3166 */
Nick Piggin39507452005-06-25 14:57:09 -07003167 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Nick Piggin81026792005-06-25 14:57:07 -07003169 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 sd->nr_balance_failed = 0;
3171
Nick Piggin81026792005-06-25 14:57:07 -07003172 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* We were unbalanced, so reset the balancing interval */
3174 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003175 } else {
3176 /*
3177 * If we've begun active balancing, start to back off. This
3178 * case may not be covered by the all_pinned logic if there
3179 * is only 1 task on the busy runqueue (because we don't call
3180 * move_tasks).
3181 */
3182 if (sd->balance_interval < sd->max_interval)
3183 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185
Peter Williams43010652007-08-09 11:16:46 +02003186 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003187 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003188 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003189 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 schedstat_inc(sd, lb_balanced[idle]);
3193
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003194 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003195
3196out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003198 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3199 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 sd->balance_interval *= 2;
3201
Ingo Molnar48f24c42006-07-03 00:25:40 -07003202 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003203 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003204 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return 0;
3206}
3207
3208/*
3209 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3210 * tasks if there is an imbalance.
3211 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003212 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 * this_rq is locked.
3214 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003215static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003216load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003219 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003221 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003222 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003223 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003224 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003225
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003226 /*
3227 * When power savings policy is enabled for the parent domain, idle
3228 * sibling can pick up load irrespective of busy siblings. In this case,
3229 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003230 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003231 */
3232 if (sd->flags & SD_SHARE_CPUPOWER &&
3233 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003234 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Ingo Molnar2d723762007-10-15 17:00:12 +02003236 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003237redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003238 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003239 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003241 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003242 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003245 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003246 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003247 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003248 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003249 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
3251
Nick Piggindb935db2005-06-25 14:57:11 -07003252 BUG_ON(busiest == this_rq);
3253
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003254 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003255
Peter Williams43010652007-08-09 11:16:46 +02003256 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003257 if (busiest->nr_running > 1) {
3258 /* Attempt to move tasks */
3259 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003260 /* this_rq->clock is already updated */
3261 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003262 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003263 imbalance, sd, CPU_NEWLY_IDLE,
3264 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003265 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003266
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003267 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003268 cpu_clear(cpu_of(busiest), cpus);
3269 if (!cpus_empty(cpus))
3270 goto redo;
3271 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003272 }
3273
Peter Williams43010652007-08-09 11:16:46 +02003274 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003275 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003276 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3277 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003278 return -1;
3279 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003280 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Peter Williams43010652007-08-09 11:16:46 +02003282 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003283
3284out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003285 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003286 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003287 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003288 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003289 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003290
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294/*
3295 * idle_balance is called by schedule() if this_cpu is about to become
3296 * idle. Attempts to pull tasks from other CPUs.
3297 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003298static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003301 int pulled_task = -1;
3302 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003305 unsigned long interval;
3306
3307 if (!(sd->flags & SD_LOAD_BALANCE))
3308 continue;
3309
3310 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003311 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003312 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003313 this_rq, sd);
3314
3315 interval = msecs_to_jiffies(sd->balance_interval);
3316 if (time_after(next_balance, sd->last_balance + interval))
3317 next_balance = sd->last_balance + interval;
3318 if (pulled_task)
3319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003321 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003322 /*
3323 * We are going idle. next_balance may be set based on
3324 * a busy processor. So reset next_balance.
3325 */
3326 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
3330/*
3331 * active_load_balance is run by migration threads. It pushes running tasks
3332 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3333 * running on each physical CPU where possible, and avoids physical /
3334 * logical imbalances.
3335 *
3336 * Called with busiest_rq locked.
3337 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003338static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Nick Piggin39507452005-06-25 14:57:09 -07003340 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003341 struct sched_domain *sd;
3342 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003343
Ingo Molnar48f24c42006-07-03 00:25:40 -07003344 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003345 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003346 return;
3347
3348 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /*
Nick Piggin39507452005-06-25 14:57:09 -07003351 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003352 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003353 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 */
Nick Piggin39507452005-06-25 14:57:09 -07003355 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Nick Piggin39507452005-06-25 14:57:09 -07003357 /* move a task from busiest_rq to target_rq */
3358 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003359 update_rq_clock(busiest_rq);
3360 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Nick Piggin39507452005-06-25 14:57:09 -07003362 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003363 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003364 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003365 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003366 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Ingo Molnar48f24c42006-07-03 00:25:40 -07003369 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003370 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Peter Williams43010652007-08-09 11:16:46 +02003372 if (move_one_task(target_rq, target_cpu, busiest_rq,
3373 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003374 schedstat_inc(sd, alb_pushed);
3375 else
3376 schedstat_inc(sd, alb_failed);
3377 }
Nick Piggin39507452005-06-25 14:57:09 -07003378 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003381#ifdef CONFIG_NO_HZ
3382static struct {
3383 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003384 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003385} nohz ____cacheline_aligned = {
3386 .load_balancer = ATOMIC_INIT(-1),
3387 .cpu_mask = CPU_MASK_NONE,
3388};
3389
Christoph Lameter7835b982006-12-10 02:20:22 -08003390/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003391 * This routine will try to nominate the ilb (idle load balancing)
3392 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3393 * load balancing on behalf of all those cpus. If all the cpus in the system
3394 * go into this tickless mode, then there will be no ilb owner (as there is
3395 * no need for one) and all the cpus will sleep till the next wakeup event
3396 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003397 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003398 * For the ilb owner, tick is not stopped. And this tick will be used
3399 * for idle load balancing. ilb owner will still be part of
3400 * nohz.cpu_mask..
3401 *
3402 * While stopping the tick, this cpu will become the ilb owner if there
3403 * is no other owner. And will be the owner till that cpu becomes busy
3404 * or if all cpus in the system stop their ticks at which point
3405 * there is no need for ilb owner.
3406 *
3407 * When the ilb owner becomes busy, it nominates another owner, during the
3408 * next busy scheduler_tick()
3409 */
3410int select_nohz_load_balancer(int stop_tick)
3411{
3412 int cpu = smp_processor_id();
3413
3414 if (stop_tick) {
3415 cpu_set(cpu, nohz.cpu_mask);
3416 cpu_rq(cpu)->in_nohz_recently = 1;
3417
3418 /*
3419 * If we are going offline and still the leader, give up!
3420 */
3421 if (cpu_is_offline(cpu) &&
3422 atomic_read(&nohz.load_balancer) == cpu) {
3423 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3424 BUG();
3425 return 0;
3426 }
3427
3428 /* time for ilb owner also to sleep */
3429 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3430 if (atomic_read(&nohz.load_balancer) == cpu)
3431 atomic_set(&nohz.load_balancer, -1);
3432 return 0;
3433 }
3434
3435 if (atomic_read(&nohz.load_balancer) == -1) {
3436 /* make me the ilb owner */
3437 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3438 return 1;
3439 } else if (atomic_read(&nohz.load_balancer) == cpu)
3440 return 1;
3441 } else {
3442 if (!cpu_isset(cpu, nohz.cpu_mask))
3443 return 0;
3444
3445 cpu_clear(cpu, nohz.cpu_mask);
3446
3447 if (atomic_read(&nohz.load_balancer) == cpu)
3448 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3449 BUG();
3450 }
3451 return 0;
3452}
3453#endif
3454
3455static DEFINE_SPINLOCK(balancing);
3456
3457/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003458 * It checks each scheduling domain to see if it is due to be balanced,
3459 * and initiates a balancing operation if so.
3460 *
3461 * Balancing parameters are set up in arch_init_sched_domains.
3462 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003463static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003464{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003465 int balance = 1;
3466 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003467 unsigned long interval;
3468 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003469 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003470 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003471 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003473 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (!(sd->flags & SD_LOAD_BALANCE))
3475 continue;
3476
3477 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003478 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 interval *= sd->busy_factor;
3480
3481 /* scale ms to jiffies */
3482 interval = msecs_to_jiffies(interval);
3483 if (unlikely(!interval))
3484 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003485 if (interval > HZ*NR_CPUS/10)
3486 interval = HZ*NR_CPUS/10;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Christoph Lameter08c183f2006-12-10 02:20:29 -08003489 if (sd->flags & SD_SERIALIZE) {
3490 if (!spin_trylock(&balancing))
3491 goto out;
3492 }
3493
Christoph Lameterc9819f42006-12-10 02:20:25 -08003494 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003495 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003496 /*
3497 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003498 * longer idle, or one of our SMT siblings is
3499 * not idle.
3500 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003501 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003503 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003505 if (sd->flags & SD_SERIALIZE)
3506 spin_unlock(&balancing);
3507out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003508 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003509 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003510 update_next_balance = 1;
3511 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003512
3513 /*
3514 * Stop the load balance at this level. There is another
3515 * CPU in our sched group which is doing load balancing more
3516 * actively.
3517 */
3518 if (!balance)
3519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003521
3522 /*
3523 * next_balance will be updated only when there is a need.
3524 * When the cpu is attached to null domain for ex, it will not be
3525 * updated.
3526 */
3527 if (likely(update_next_balance))
3528 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003529}
3530
3531/*
3532 * run_rebalance_domains is triggered when needed from the scheduler tick.
3533 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3534 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3535 */
3536static void run_rebalance_domains(struct softirq_action *h)
3537{
Ingo Molnardd41f592007-07-09 18:51:59 +02003538 int this_cpu = smp_processor_id();
3539 struct rq *this_rq = cpu_rq(this_cpu);
3540 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3541 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003542
Ingo Molnardd41f592007-07-09 18:51:59 +02003543 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003544
3545#ifdef CONFIG_NO_HZ
3546 /*
3547 * If this cpu is the owner for idle load balancing, then do the
3548 * balancing on behalf of the other idle cpus whose ticks are
3549 * stopped.
3550 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003551 if (this_rq->idle_at_tick &&
3552 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003553 cpumask_t cpus = nohz.cpu_mask;
3554 struct rq *rq;
3555 int balance_cpu;
3556
Ingo Molnardd41f592007-07-09 18:51:59 +02003557 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003558 for_each_cpu_mask(balance_cpu, cpus) {
3559 /*
3560 * If this cpu gets work to do, stop the load balancing
3561 * work being done for other cpus. Next load
3562 * balancing owner will pick it up.
3563 */
3564 if (need_resched())
3565 break;
3566
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003567 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003568
3569 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003570 if (time_after(this_rq->next_balance, rq->next_balance))
3571 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003572 }
3573 }
3574#endif
3575}
3576
3577/*
3578 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3579 *
3580 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3581 * idle load balancing owner or decide to stop the periodic load balancing,
3582 * if the whole system is idle.
3583 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003584static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003585{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003586#ifdef CONFIG_NO_HZ
3587 /*
3588 * If we were in the nohz mode recently and busy at the current
3589 * scheduler tick, then check if we need to nominate new idle
3590 * load balancer.
3591 */
3592 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3593 rq->in_nohz_recently = 0;
3594
3595 if (atomic_read(&nohz.load_balancer) == cpu) {
3596 cpu_clear(cpu, nohz.cpu_mask);
3597 atomic_set(&nohz.load_balancer, -1);
3598 }
3599
3600 if (atomic_read(&nohz.load_balancer) == -1) {
3601 /*
3602 * simple selection for now: Nominate the
3603 * first cpu in the nohz list to be the next
3604 * ilb owner.
3605 *
3606 * TBD: Traverse the sched domains and nominate
3607 * the nearest cpu in the nohz.cpu_mask.
3608 */
3609 int ilb = first_cpu(nohz.cpu_mask);
3610
3611 if (ilb != NR_CPUS)
3612 resched_cpu(ilb);
3613 }
3614 }
3615
3616 /*
3617 * If this cpu is idle and doing idle load balancing for all the
3618 * cpus with ticks stopped, is it time for that to stop?
3619 */
3620 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3621 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3622 resched_cpu(cpu);
3623 return;
3624 }
3625
3626 /*
3627 * If this cpu is idle and the idle load balancing is done by
3628 * someone else, then no need raise the SCHED_SOFTIRQ
3629 */
3630 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3631 cpu_isset(cpu, nohz.cpu_mask))
3632 return;
3633#endif
3634 if (time_after_eq(jiffies, rq->next_balance))
3635 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
Ingo Molnardd41f592007-07-09 18:51:59 +02003637
3638#else /* CONFIG_SMP */
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640/*
3641 * on UP we do not need to balance between CPUs:
3642 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003643static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645}
Ingo Molnardd41f592007-07-09 18:51:59 +02003646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647#endif
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649DEFINE_PER_CPU(struct kernel_stat, kstat);
3650
3651EXPORT_PER_CPU_SYMBOL(kstat);
3652
3653/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003654 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3655 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003657unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003660 u64 ns, delta_exec;
3661 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003662
Ingo Molnar41b86e92007-07-09 18:51:58 +02003663 rq = task_rq_lock(p, &flags);
3664 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003665 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003666 update_rq_clock(rq);
3667 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003668 if ((s64)delta_exec > 0)
3669 ns += delta_exec;
3670 }
3671 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return ns;
3674}
3675
3676/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 * Account user cpu time to a process.
3678 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 * @cputime: the cpu time spent in user space since the last update
3680 */
3681void account_user_time(struct task_struct *p, cputime_t cputime)
3682{
3683 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3684 cputime64_t tmp;
3685
3686 p->utime = cputime_add(p->utime, cputime);
3687
3688 /* Add user time to cpustat. */
3689 tmp = cputime_to_cputime64(cputime);
3690 if (TASK_NICE(p) > 0)
3691 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3692 else
3693 cpustat->user = cputime64_add(cpustat->user, tmp);
3694}
3695
3696/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003697 * Account guest cpu time to a process.
3698 * @p: the process that the cpu time gets accounted to
3699 * @cputime: the cpu time spent in virtual machine since the last update
3700 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003701static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003702{
3703 cputime64_t tmp;
3704 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3705
3706 tmp = cputime_to_cputime64(cputime);
3707
3708 p->utime = cputime_add(p->utime, cputime);
3709 p->gtime = cputime_add(p->gtime, cputime);
3710
3711 cpustat->user = cputime64_add(cpustat->user, tmp);
3712 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3713}
3714
3715/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003716 * Account scaled user cpu time to a process.
3717 * @p: the process that the cpu time gets accounted to
3718 * @cputime: the cpu time spent in user space since the last update
3719 */
3720void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3721{
3722 p->utimescaled = cputime_add(p->utimescaled, cputime);
3723}
3724
3725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 * Account system cpu time to a process.
3727 * @p: the process that the cpu time gets accounted to
3728 * @hardirq_offset: the offset to subtract from hardirq_count()
3729 * @cputime: the cpu time spent in kernel space since the last update
3730 */
3731void account_system_time(struct task_struct *p, int hardirq_offset,
3732 cputime_t cputime)
3733{
3734 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003735 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 cputime64_t tmp;
3737
Christian Borntraeger97783852007-11-15 20:57:39 +01003738 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3739 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 p->stime = cputime_add(p->stime, cputime);
3742
3743 /* Add system time to cpustat. */
3744 tmp = cputime_to_cputime64(cputime);
3745 if (hardirq_count() - hardirq_offset)
3746 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3747 else if (softirq_count())
3748 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003749 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003751 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3753 else
3754 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3755 /* Account for system time used */
3756 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
3759/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003760 * Account scaled system cpu time to a process.
3761 * @p: the process that the cpu time gets accounted to
3762 * @hardirq_offset: the offset to subtract from hardirq_count()
3763 * @cputime: the cpu time spent in kernel space since the last update
3764 */
3765void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3766{
3767 p->stimescaled = cputime_add(p->stimescaled, cputime);
3768}
3769
3770/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 * Account for involuntary wait time.
3772 * @p: the process from which the cpu time has been stolen
3773 * @steal: the cpu time spent in involuntary wait
3774 */
3775void account_steal_time(struct task_struct *p, cputime_t steal)
3776{
3777 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3778 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003779 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 if (p == rq->idle) {
3782 p->stime = cputime_add(p->stime, steal);
3783 if (atomic_read(&rq->nr_iowait) > 0)
3784 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3785 else
3786 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003787 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3789}
3790
Christoph Lameter7835b982006-12-10 02:20:22 -08003791/*
3792 * This function gets called by the timer code, with HZ frequency.
3793 * We call it with interrupts disabled.
3794 *
3795 * It also gets called by the fork code, when changing the parent's
3796 * timeslices.
3797 */
3798void scheduler_tick(void)
3799{
Christoph Lameter7835b982006-12-10 02:20:22 -08003800 int cpu = smp_processor_id();
3801 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003802 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003803 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003804
Ingo Molnardd41f592007-07-09 18:51:59 +02003805 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003806 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003807 /*
3808 * Let rq->clock advance by at least TICK_NSEC:
3809 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003810 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003811 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003812 rq->clock_underflows++;
3813 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003814 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003815 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003816 curr->sched_class->task_tick(rq, curr, 0);
3817 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003818 spin_unlock(&rq->lock);
3819
Christoph Lametere418e1c2006-12-10 02:20:23 -08003820#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003821 rq->idle_at_tick = idle_cpu(cpu);
3822 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3827
Srinivasa Ds43627582008-02-23 15:24:04 -08003828void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
3830 /*
3831 * Underflow?
3832 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003833 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3834 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 preempt_count() += val;
3836 /*
3837 * Spinlock count overflowing soon?
3838 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003839 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3840 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841}
3842EXPORT_SYMBOL(add_preempt_count);
3843
Srinivasa Ds43627582008-02-23 15:24:04 -08003844void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
3846 /*
3847 * Underflow?
3848 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003849 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3850 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 /*
3852 * Is the spinlock portion underflowing?
3853 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003854 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3855 !(preempt_count() & PREEMPT_MASK)))
3856 return;
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 preempt_count() -= val;
3859}
3860EXPORT_SYMBOL(sub_preempt_count);
3861
3862#endif
3863
3864/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003865 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003867static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
Satyam Sharma838225b2007-10-24 18:23:50 +02003869 struct pt_regs *regs = get_irq_regs();
3870
3871 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3872 prev->comm, prev->pid, preempt_count());
3873
Ingo Molnardd41f592007-07-09 18:51:59 +02003874 debug_show_held_locks(prev);
3875 if (irqs_disabled())
3876 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003877
3878 if (regs)
3879 show_regs(regs);
3880 else
3881 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Ingo Molnardd41f592007-07-09 18:51:59 +02003884/*
3885 * Various schedule()-time debugging checks and statistics:
3886 */
3887static inline void schedule_debug(struct task_struct *prev)
3888{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003890 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 * schedule() atomically, we ignore that path for now.
3892 * Otherwise, whine if we are scheduling when we should not be.
3893 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003894 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3895 __schedule_bug(prev);
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3898
Ingo Molnar2d723762007-10-15 17:00:12 +02003899 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003900#ifdef CONFIG_SCHEDSTATS
3901 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003902 schedstat_inc(this_rq(), bkl_count);
3903 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003904 }
3905#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003906}
3907
3908/*
3909 * Pick up the highest-prio task:
3910 */
3911static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003912pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003913{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003914 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003915 struct task_struct *p;
3916
3917 /*
3918 * Optimization: we know that if all tasks are in
3919 * the fair class we can call that function directly:
3920 */
3921 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003922 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003923 if (likely(p))
3924 return p;
3925 }
3926
3927 class = sched_class_highest;
3928 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003929 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003930 if (p)
3931 return p;
3932 /*
3933 * Will never be NULL as the idle class always
3934 * returns a non-NULL p:
3935 */
3936 class = class->next;
3937 }
3938}
3939
3940/*
3941 * schedule() is the main scheduler function.
3942 */
3943asmlinkage void __sched schedule(void)
3944{
3945 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003946 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003947 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003948 int cpu;
3949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950need_resched:
3951 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003952 cpu = smp_processor_id();
3953 rq = cpu_rq(cpu);
3954 rcu_qsctr_inc(cpu);
3955 prev = rq->curr;
3956 switch_count = &prev->nivcsw;
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 release_kernel_lock(prev);
3959need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Ingo Molnardd41f592007-07-09 18:51:59 +02003961 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003963 hrtick_clear(rq);
3964
Ingo Molnar1e819952007-10-15 17:00:13 +02003965 /*
3966 * Do the rq-clock update outside the rq lock:
3967 */
3968 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003969 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02003970 spin_lock(&rq->lock);
3971 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Ingo Molnardd41f592007-07-09 18:51:59 +02003973 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3974 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01003975 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003976 prev->state = TASK_RUNNING;
3977 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003978 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003979 }
3980 switch_count = &prev->nvcsw;
3981 }
3982
Steven Rostedt9a897c52008-01-25 21:08:22 +01003983#ifdef CONFIG_SMP
3984 if (prev->sched_class->pre_schedule)
3985 prev->sched_class->pre_schedule(rq, prev);
3986#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01003987
Ingo Molnardd41f592007-07-09 18:51:59 +02003988 if (unlikely(!rq->nr_running))
3989 idle_balance(cpu, rq);
3990
Ingo Molnar31ee5292007-08-09 11:16:49 +02003991 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003992 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 rq->nr_switches++;
3998 rq->curr = next;
3999 ++*switch_count;
4000
Ingo Molnardd41f592007-07-09 18:51:59 +02004001 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004002 /*
4003 * the context switch might have flipped the stack from under
4004 * us, hence refresh the local variables.
4005 */
4006 cpu = smp_processor_id();
4007 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 } else
4009 spin_unlock_irq(&rq->lock);
4010
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004011 hrtick_set(rq);
4012
4013 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 preempt_enable_no_resched();
4017 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4018 goto need_resched;
4019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020EXPORT_SYMBOL(schedule);
4021
4022#ifdef CONFIG_PREEMPT
4023/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004024 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004025 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * occur there and call schedule directly.
4027 */
4028asmlinkage void __sched preempt_schedule(void)
4029{
4030 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct task_struct *task = current;
4032 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 /*
4035 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004036 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004038 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return;
4040
Andi Kleen3a5c3592007-10-15 17:00:14 +02004041 do {
4042 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Andi Kleen3a5c3592007-10-15 17:00:14 +02004044 /*
4045 * We keep the big kernel semaphore locked, but we
4046 * clear ->lock_depth so that schedule() doesnt
4047 * auto-release the semaphore:
4048 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004049 saved_lock_depth = task->lock_depth;
4050 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004051 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004052 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004053 sub_preempt_count(PREEMPT_ACTIVE);
4054
4055 /*
4056 * Check again in case we missed a preemption opportunity
4057 * between schedule and now.
4058 */
4059 barrier();
4060 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062EXPORT_SYMBOL(preempt_schedule);
4063
4064/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004065 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 * off of irq context.
4067 * Note, that this is called and return with irqs disabled. This will
4068 * protect us against recursive calling from irq.
4069 */
4070asmlinkage void __sched preempt_schedule_irq(void)
4071{
4072 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 struct task_struct *task = current;
4074 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004075
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004076 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 BUG_ON(ti->preempt_count || !irqs_disabled());
4078
Andi Kleen3a5c3592007-10-15 17:00:14 +02004079 do {
4080 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Andi Kleen3a5c3592007-10-15 17:00:14 +02004082 /*
4083 * We keep the big kernel semaphore locked, but we
4084 * clear ->lock_depth so that schedule() doesnt
4085 * auto-release the semaphore:
4086 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004087 saved_lock_depth = task->lock_depth;
4088 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004089 local_irq_enable();
4090 schedule();
4091 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004092 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004093 sub_preempt_count(PREEMPT_ACTIVE);
4094
4095 /*
4096 * Check again in case we missed a preemption opportunity
4097 * between schedule and now.
4098 */
4099 barrier();
4100 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
4103#endif /* CONFIG_PREEMPT */
4104
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004105int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4106 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004108 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110EXPORT_SYMBOL(default_wake_function);
4111
4112/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004113 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4114 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 * number) then we wake all the non-exclusive tasks and one exclusive task.
4116 *
4117 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004118 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4120 */
4121static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4122 int nr_exclusive, int sync, void *key)
4123{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004124 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004126 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004127 unsigned flags = curr->flags;
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004130 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 break;
4132 }
4133}
4134
4135/**
4136 * __wake_up - wake up threads blocked on a waitqueue.
4137 * @q: the waitqueue
4138 * @mode: which threads
4139 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004140 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004142void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004143 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 unsigned long flags;
4146
4147 spin_lock_irqsave(&q->lock, flags);
4148 __wake_up_common(q, mode, nr_exclusive, 0, key);
4149 spin_unlock_irqrestore(&q->lock, flags);
4150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151EXPORT_SYMBOL(__wake_up);
4152
4153/*
4154 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4155 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004156void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
4158 __wake_up_common(q, mode, 1, 0, NULL);
4159}
4160
4161/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004162 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 * @q: the waitqueue
4164 * @mode: which threads
4165 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4166 *
4167 * The sync wakeup differs that the waker knows that it will schedule
4168 * away soon, so while the target thread will be woken up, it will not
4169 * be migrated to another CPU - ie. the two threads are 'synchronized'
4170 * with each other. This can prevent needless bouncing between CPUs.
4171 *
4172 * On UP it can prevent extra preemption.
4173 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004174void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004175__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
4177 unsigned long flags;
4178 int sync = 1;
4179
4180 if (unlikely(!q))
4181 return;
4182
4183 if (unlikely(!nr_exclusive))
4184 sync = 0;
4185
4186 spin_lock_irqsave(&q->lock, flags);
4187 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4188 spin_unlock_irqrestore(&q->lock, flags);
4189}
4190EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4191
Ingo Molnarb15136e2007-10-24 18:23:48 +02004192void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 unsigned long flags;
4195
4196 spin_lock_irqsave(&x->wait.lock, flags);
4197 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004198 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 spin_unlock_irqrestore(&x->wait.lock, flags);
4200}
4201EXPORT_SYMBOL(complete);
4202
Ingo Molnarb15136e2007-10-24 18:23:48 +02004203void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 unsigned long flags;
4206
4207 spin_lock_irqsave(&x->wait.lock, flags);
4208 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004209 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 spin_unlock_irqrestore(&x->wait.lock, flags);
4211}
4212EXPORT_SYMBOL(complete_all);
4213
Andi Kleen8cbbe862007-10-15 17:00:14 +02004214static inline long __sched
4215do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (!x->done) {
4218 DECLARE_WAITQUEUE(wait, current);
4219
4220 wait.flags |= WQ_FLAG_EXCLUSIVE;
4221 __add_wait_queue_tail(&x->wait, &wait);
4222 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004223 if ((state == TASK_INTERRUPTIBLE &&
4224 signal_pending(current)) ||
4225 (state == TASK_KILLABLE &&
4226 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004227 __remove_wait_queue(&x->wait, &wait);
4228 return -ERESTARTSYS;
4229 }
4230 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004232 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004234 if (!timeout) {
4235 __remove_wait_queue(&x->wait, &wait);
4236 return timeout;
4237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 } while (!x->done);
4239 __remove_wait_queue(&x->wait, &wait);
4240 }
4241 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004242 return timeout;
4243}
4244
4245static long __sched
4246wait_for_common(struct completion *x, long timeout, int state)
4247{
4248 might_sleep();
4249
4250 spin_lock_irq(&x->wait.lock);
4251 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004253 return timeout;
4254}
4255
Ingo Molnarb15136e2007-10-24 18:23:48 +02004256void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004257{
4258 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260EXPORT_SYMBOL(wait_for_completion);
4261
Ingo Molnarb15136e2007-10-24 18:23:48 +02004262unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4264{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004265 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267EXPORT_SYMBOL(wait_for_completion_timeout);
4268
Andi Kleen8cbbe862007-10-15 17:00:14 +02004269int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
Andi Kleen51e97992007-10-18 21:32:55 +02004271 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4272 if (t == -ERESTARTSYS)
4273 return t;
4274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276EXPORT_SYMBOL(wait_for_completion_interruptible);
4277
Ingo Molnarb15136e2007-10-24 18:23:48 +02004278unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279wait_for_completion_interruptible_timeout(struct completion *x,
4280 unsigned long timeout)
4281{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004282 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4285
Matthew Wilcox009e5772007-12-06 12:29:54 -05004286int __sched wait_for_completion_killable(struct completion *x)
4287{
4288 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4289 if (t == -ERESTARTSYS)
4290 return t;
4291 return 0;
4292}
4293EXPORT_SYMBOL(wait_for_completion_killable);
4294
Andi Kleen8cbbe862007-10-15 17:00:14 +02004295static long __sched
4296sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004297{
4298 unsigned long flags;
4299 wait_queue_t wait;
4300
4301 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Andi Kleen8cbbe862007-10-15 17:00:14 +02004303 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Andi Kleen8cbbe862007-10-15 17:00:14 +02004305 spin_lock_irqsave(&q->lock, flags);
4306 __add_wait_queue(q, &wait);
4307 spin_unlock(&q->lock);
4308 timeout = schedule_timeout(timeout);
4309 spin_lock_irq(&q->lock);
4310 __remove_wait_queue(q, &wait);
4311 spin_unlock_irqrestore(&q->lock, flags);
4312
4313 return timeout;
4314}
4315
4316void __sched interruptible_sleep_on(wait_queue_head_t *q)
4317{
4318 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320EXPORT_SYMBOL(interruptible_sleep_on);
4321
Ingo Molnar0fec1712007-07-09 18:52:01 +02004322long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004323interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004325 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4328
Ingo Molnar0fec1712007-07-09 18:52:01 +02004329void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004331 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333EXPORT_SYMBOL(sleep_on);
4334
Ingo Molnar0fec1712007-07-09 18:52:01 +02004335long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004337 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339EXPORT_SYMBOL(sleep_on_timeout);
4340
Ingo Molnarb29739f2006-06-27 02:54:51 -07004341#ifdef CONFIG_RT_MUTEXES
4342
4343/*
4344 * rt_mutex_setprio - set the current priority of a task
4345 * @p: task
4346 * @prio: prio value (kernel-internal form)
4347 *
4348 * This function changes the 'effective' priority of a task. It does
4349 * not touch ->normal_prio like __setscheduler().
4350 *
4351 * Used by the rt_mutex code to implement priority inheritance logic.
4352 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004353void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004354{
4355 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004356 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004357 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004358 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004359
4360 BUG_ON(prio < 0 || prio > MAX_PRIO);
4361
4362 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004363 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004364
Andrew Mortond5f9f942007-05-08 20:27:06 -07004365 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004366 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004367 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004368 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004369 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004370 if (running)
4371 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004372
4373 if (rt_prio(prio))
4374 p->sched_class = &rt_sched_class;
4375 else
4376 p->sched_class = &fair_sched_class;
4377
Ingo Molnarb29739f2006-06-27 02:54:51 -07004378 p->prio = prio;
4379
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004380 if (running)
4381 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004382 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004383 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004384
4385 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004386 }
4387 task_rq_unlock(rq, &flags);
4388}
4389
4390#endif
4391
Ingo Molnar36c8b582006-07-03 00:25:41 -07004392void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
Ingo Molnardd41f592007-07-09 18:51:59 +02004394 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004396 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4399 return;
4400 /*
4401 * We have to be careful, if called from sys_setpriority(),
4402 * the task might be in the middle of scheduling on another CPU.
4403 */
4404 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004405 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 /*
4407 * The RT priorities are set via sched_setscheduler(), but we still
4408 * allow the 'normal' nice value to be set - but as expected
4409 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004410 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004412 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 p->static_prio = NICE_TO_PRIO(nice);
4414 goto out_unlock;
4415 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004416 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004417 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004418 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004419 dec_load(rq, p);
4420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004423 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004424 old_prio = p->prio;
4425 p->prio = effective_prio(p);
4426 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Ingo Molnardd41f592007-07-09 18:51:59 +02004428 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004429 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004430 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004432 * If the task increased its priority or is running and
4433 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004435 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 resched_task(rq->curr);
4437 }
4438out_unlock:
4439 task_rq_unlock(rq, &flags);
4440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441EXPORT_SYMBOL(set_user_nice);
4442
Matt Mackalle43379f2005-05-01 08:59:00 -07004443/*
4444 * can_nice - check if a task can reduce its nice value
4445 * @p: task
4446 * @nice: nice value
4447 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004448int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004449{
Matt Mackall024f4742005-08-18 11:24:19 -07004450 /* convert nice value [19,-20] to rlimit style value [1,40] */
4451 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004452
Matt Mackalle43379f2005-05-01 08:59:00 -07004453 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4454 capable(CAP_SYS_NICE));
4455}
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457#ifdef __ARCH_WANT_SYS_NICE
4458
4459/*
4460 * sys_nice - change the priority of the current process.
4461 * @increment: priority increment
4462 *
4463 * sys_setpriority is a more generic, but much slower function that
4464 * does similar things.
4465 */
4466asmlinkage long sys_nice(int increment)
4467{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004468 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 /*
4471 * Setpriority might change our priority at the same moment.
4472 * We don't have to worry. Conceptually one call occurs first
4473 * and we have a single winner.
4474 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004475 if (increment < -40)
4476 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (increment > 40)
4478 increment = 40;
4479
4480 nice = PRIO_TO_NICE(current->static_prio) + increment;
4481 if (nice < -20)
4482 nice = -20;
4483 if (nice > 19)
4484 nice = 19;
4485
Matt Mackalle43379f2005-05-01 08:59:00 -07004486 if (increment < 0 && !can_nice(current, nice))
4487 return -EPERM;
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 retval = security_task_setnice(current, nice);
4490 if (retval)
4491 return retval;
4492
4493 set_user_nice(current, nice);
4494 return 0;
4495}
4496
4497#endif
4498
4499/**
4500 * task_prio - return the priority value of a given task.
4501 * @p: the task in question.
4502 *
4503 * This is the priority value as seen by users in /proc.
4504 * RT tasks are offset by -200. Normal tasks are centered
4505 * around 0, value goes from -16 to +15.
4506 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004507int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
4509 return p->prio - MAX_RT_PRIO;
4510}
4511
4512/**
4513 * task_nice - return the nice value of a given task.
4514 * @p: the task in question.
4515 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004516int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
4518 return TASK_NICE(p);
4519}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004520EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522/**
4523 * idle_cpu - is a given cpu idle currently?
4524 * @cpu: the processor in question.
4525 */
4526int idle_cpu(int cpu)
4527{
4528 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4529}
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531/**
4532 * idle_task - return the idle task for a given cpu.
4533 * @cpu: the processor in question.
4534 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004535struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
4537 return cpu_rq(cpu)->idle;
4538}
4539
4540/**
4541 * find_process_by_pid - find a process with a matching PID value.
4542 * @pid: the pid in question.
4543 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004544static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004546 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547}
4548
4549/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004550static void
4551__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Ingo Molnardd41f592007-07-09 18:51:59 +02004553 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004556 switch (p->policy) {
4557 case SCHED_NORMAL:
4558 case SCHED_BATCH:
4559 case SCHED_IDLE:
4560 p->sched_class = &fair_sched_class;
4561 break;
4562 case SCHED_FIFO:
4563 case SCHED_RR:
4564 p->sched_class = &rt_sched_class;
4565 break;
4566 }
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004569 p->normal_prio = normal_prio(p);
4570 /* we are holding p->pi_lock already */
4571 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004572 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
4575/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004576 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 * @p: the task in question.
4578 * @policy: new policy.
4579 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004580 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004581 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004583int sched_setscheduler(struct task_struct *p, int policy,
4584 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004586 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004588 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004589 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Steven Rostedt66e53932006-06-27 02:54:44 -07004591 /* may grab non-irq protected spin_locks */
4592 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593recheck:
4594 /* double check policy once rq lock held */
4595 if (policy < 0)
4596 policy = oldpolicy = p->policy;
4597 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004598 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4599 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /*
4602 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004603 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4604 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 */
4606 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004607 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004608 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004610 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return -EINVAL;
4612
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004613 /*
4614 * Allow unprivileged RT tasks to decrease priority:
4615 */
4616 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004617 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004618 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004619
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004620 if (!lock_task_sighand(p, &flags))
4621 return -ESRCH;
4622 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4623 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004624
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004625 /* can't set/change the rt policy */
4626 if (policy != p->policy && !rlim_rtprio)
4627 return -EPERM;
4628
4629 /* can't increase priority */
4630 if (param->sched_priority > p->rt_priority &&
4631 param->sched_priority > rlim_rtprio)
4632 return -EPERM;
4633 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004634 /*
4635 * Like positive nice levels, dont allow tasks to
4636 * move out of SCHED_IDLE either:
4637 */
4638 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4639 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004640
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004641 /* can't change other user's priorities */
4642 if ((current->euid != p->euid) &&
4643 (current->euid != p->uid))
4644 return -EPERM;
4645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004647#ifdef CONFIG_RT_GROUP_SCHED
4648 /*
4649 * Do not allow realtime tasks into groups that have no runtime
4650 * assigned.
4651 */
4652 if (rt_policy(policy) && task_group(p)->rt_runtime == 0)
4653 return -EPERM;
4654#endif
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 retval = security_task_setscheduler(p, policy, param);
4657 if (retval)
4658 return retval;
4659 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004660 * make sure no PI-waiters arrive (or leave) while we are
4661 * changing the priority of the task:
4662 */
4663 spin_lock_irqsave(&p->pi_lock, flags);
4664 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 * To be able to change p->policy safely, the apropriate
4666 * runqueue lock must be held.
4667 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004668 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 /* recheck policy now with rq lock held */
4670 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4671 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004672 __task_rq_unlock(rq);
4673 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 goto recheck;
4675 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004676 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004677 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004678 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004679 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004680 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004681 if (running)
4682 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004685 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004686
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004687 if (running)
4688 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004689 if (on_rq) {
4690 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004691
4692 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004694 __task_rq_unlock(rq);
4695 spin_unlock_irqrestore(&p->pi_lock, flags);
4696
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004697 rt_mutex_adjust_pi(p);
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return 0;
4700}
4701EXPORT_SYMBOL_GPL(sched_setscheduler);
4702
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004703static int
4704do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 struct sched_param lparam;
4707 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004708 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 if (!param || pid < 0)
4711 return -EINVAL;
4712 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4713 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004714
4715 rcu_read_lock();
4716 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004718 if (p != NULL)
4719 retval = sched_setscheduler(p, policy, &lparam);
4720 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 return retval;
4723}
4724
4725/**
4726 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4727 * @pid: the pid in question.
4728 * @policy: new policy.
4729 * @param: structure containing the new RT priority.
4730 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004731asmlinkage long
4732sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
Jason Baronc21761f2006-01-18 17:43:03 -08004734 /* negative values for policy are not valid */
4735 if (policy < 0)
4736 return -EINVAL;
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 return do_sched_setscheduler(pid, policy, param);
4739}
4740
4741/**
4742 * sys_sched_setparam - set/change the RT priority of a thread
4743 * @pid: the pid in question.
4744 * @param: structure containing the new RT priority.
4745 */
4746asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4747{
4748 return do_sched_setscheduler(pid, -1, param);
4749}
4750
4751/**
4752 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4753 * @pid: the pid in question.
4754 */
4755asmlinkage long sys_sched_getscheduler(pid_t pid)
4756{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004757 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004758 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
4760 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 retval = -ESRCH;
4764 read_lock(&tasklist_lock);
4765 p = find_process_by_pid(pid);
4766 if (p) {
4767 retval = security_task_getscheduler(p);
4768 if (!retval)
4769 retval = p->policy;
4770 }
4771 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return retval;
4773}
4774
4775/**
4776 * sys_sched_getscheduler - get the RT priority of a thread
4777 * @pid: the pid in question.
4778 * @param: structure containing the RT priority.
4779 */
4780asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4781{
4782 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004783 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004784 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004787 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 read_lock(&tasklist_lock);
4790 p = find_process_by_pid(pid);
4791 retval = -ESRCH;
4792 if (!p)
4793 goto out_unlock;
4794
4795 retval = security_task_getscheduler(p);
4796 if (retval)
4797 goto out_unlock;
4798
4799 lp.sched_priority = p->rt_priority;
4800 read_unlock(&tasklist_lock);
4801
4802 /*
4803 * This one might sleep, we cannot do it with a spinlock held ...
4804 */
4805 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 return retval;
4808
4809out_unlock:
4810 read_unlock(&tasklist_lock);
4811 return retval;
4812}
4813
4814long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4815{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004817 struct task_struct *p;
4818 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004820 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 read_lock(&tasklist_lock);
4822
4823 p = find_process_by_pid(pid);
4824 if (!p) {
4825 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004826 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return -ESRCH;
4828 }
4829
4830 /*
4831 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004832 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 * usage count and then drop tasklist_lock.
4834 */
4835 get_task_struct(p);
4836 read_unlock(&tasklist_lock);
4837
4838 retval = -EPERM;
4839 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4840 !capable(CAP_SYS_NICE))
4841 goto out_unlock;
4842
David Quigleye7834f82006-06-23 02:03:59 -07004843 retval = security_task_setscheduler(p, 0, NULL);
4844 if (retval)
4845 goto out_unlock;
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 cpus_allowed = cpuset_cpus_allowed(p);
4848 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004849 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 retval = set_cpus_allowed(p, new_mask);
4851
Paul Menage8707d8b2007-10-18 23:40:22 -07004852 if (!retval) {
4853 cpus_allowed = cpuset_cpus_allowed(p);
4854 if (!cpus_subset(new_mask, cpus_allowed)) {
4855 /*
4856 * We must have raced with a concurrent cpuset
4857 * update. Just reset the cpus_allowed to the
4858 * cpuset's cpus_allowed
4859 */
4860 new_mask = cpus_allowed;
4861 goto again;
4862 }
4863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864out_unlock:
4865 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004866 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return retval;
4868}
4869
4870static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4871 cpumask_t *new_mask)
4872{
4873 if (len < sizeof(cpumask_t)) {
4874 memset(new_mask, 0, sizeof(cpumask_t));
4875 } else if (len > sizeof(cpumask_t)) {
4876 len = sizeof(cpumask_t);
4877 }
4878 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4879}
4880
4881/**
4882 * sys_sched_setaffinity - set the cpu affinity of a process
4883 * @pid: pid of the process
4884 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4885 * @user_mask_ptr: user-space pointer to the new cpu mask
4886 */
4887asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4888 unsigned long __user *user_mask_ptr)
4889{
4890 cpumask_t new_mask;
4891 int retval;
4892
4893 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4894 if (retval)
4895 return retval;
4896
4897 return sched_setaffinity(pid, new_mask);
4898}
4899
4900/*
4901 * Represents all cpu's present in the system
4902 * In systems capable of hotplug, this map could dynamically grow
4903 * as new cpu's are detected in the system via any platform specific
4904 * method, such as ACPI for e.g.
4905 */
4906
Andi Kleen4cef0c62006-01-11 22:44:57 +01004907cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908EXPORT_SYMBOL(cpu_present_map);
4909
4910#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004911cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004912EXPORT_SYMBOL(cpu_online_map);
4913
Andi Kleen4cef0c62006-01-11 22:44:57 +01004914cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004915EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916#endif
4917
4918long sched_getaffinity(pid_t pid, cpumask_t *mask)
4919{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004920 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004923 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 read_lock(&tasklist_lock);
4925
4926 retval = -ESRCH;
4927 p = find_process_by_pid(pid);
4928 if (!p)
4929 goto out_unlock;
4930
David Quigleye7834f82006-06-23 02:03:59 -07004931 retval = security_task_getscheduler(p);
4932 if (retval)
4933 goto out_unlock;
4934
Jack Steiner2f7016d2006-02-01 03:05:18 -08004935 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937out_unlock:
4938 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004939 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Ulrich Drepper9531b622007-08-09 11:16:46 +02004941 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
4944/**
4945 * sys_sched_getaffinity - get the cpu affinity of a process
4946 * @pid: pid of the process
4947 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4948 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4949 */
4950asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4951 unsigned long __user *user_mask_ptr)
4952{
4953 int ret;
4954 cpumask_t mask;
4955
4956 if (len < sizeof(cpumask_t))
4957 return -EINVAL;
4958
4959 ret = sched_getaffinity(pid, &mask);
4960 if (ret < 0)
4961 return ret;
4962
4963 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4964 return -EFAULT;
4965
4966 return sizeof(cpumask_t);
4967}
4968
4969/**
4970 * sys_sched_yield - yield the current processor to other threads.
4971 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004972 * This function yields the current CPU to other tasks. If there are no
4973 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
4975asmlinkage long sys_sched_yield(void)
4976{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004977 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Ingo Molnar2d723762007-10-15 17:00:12 +02004979 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004980 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 /*
4983 * Since we are going to call schedule() anyway, there's
4984 * no need to preempt or enable interrupts:
4985 */
4986 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004987 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 _raw_spin_unlock(&rq->lock);
4989 preempt_enable_no_resched();
4990
4991 schedule();
4992
4993 return 0;
4994}
4995
Andrew Mortone7b38402006-06-30 01:56:00 -07004996static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004998#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4999 __might_sleep(__FILE__, __LINE__);
5000#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005001 /*
5002 * The BKS might be reacquired before we have dropped
5003 * PREEMPT_ACTIVE, which could trigger a second
5004 * cond_resched() call.
5005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 do {
5007 add_preempt_count(PREEMPT_ACTIVE);
5008 schedule();
5009 sub_preempt_count(PREEMPT_ACTIVE);
5010 } while (need_resched());
5011}
5012
Herbert Xu02b67cc2008-01-25 21:08:28 +01005013#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5014int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
Ingo Molnar94142322006-12-29 16:48:13 -08005016 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5017 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 __cond_resched();
5019 return 1;
5020 }
5021 return 0;
5022}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005023EXPORT_SYMBOL(_cond_resched);
5024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026/*
5027 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5028 * call schedule, and on return reacquire the lock.
5029 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005030 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 * operations here to prevent schedule() from being called twice (once via
5032 * spin_unlock(), once by hand).
5033 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005034int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Nick Piggin95c354f2008-01-30 13:31:20 +01005036 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005037 int ret = 0;
5038
Nick Piggin95c354f2008-01-30 13:31:20 +01005039 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005041 if (resched && need_resched())
5042 __cond_resched();
5043 else
5044 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005045 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050EXPORT_SYMBOL(cond_resched_lock);
5051
5052int __sched cond_resched_softirq(void)
5053{
5054 BUG_ON(!in_softirq());
5055
Ingo Molnar94142322006-12-29 16:48:13 -08005056 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005057 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 __cond_resched();
5059 local_bh_disable();
5060 return 1;
5061 }
5062 return 0;
5063}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064EXPORT_SYMBOL(cond_resched_softirq);
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066/**
5067 * yield - yield the current processor to other threads.
5068 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005069 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 * thread runnable and calls sys_sched_yield().
5071 */
5072void __sched yield(void)
5073{
5074 set_current_state(TASK_RUNNING);
5075 sys_sched_yield();
5076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077EXPORT_SYMBOL(yield);
5078
5079/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005080 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 * that process accounting knows that this is a task in IO wait state.
5082 *
5083 * But don't do that if it is a deliberate, throttling IO wait (this task
5084 * has set its backing_dev_info: the queue against which it should throttle)
5085 */
5086void __sched io_schedule(void)
5087{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005088 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005090 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 atomic_inc(&rq->nr_iowait);
5092 schedule();
5093 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005094 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096EXPORT_SYMBOL(io_schedule);
5097
5098long __sched io_schedule_timeout(long timeout)
5099{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005100 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 long ret;
5102
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005103 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 atomic_inc(&rq->nr_iowait);
5105 ret = schedule_timeout(timeout);
5106 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005107 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 return ret;
5109}
5110
5111/**
5112 * sys_sched_get_priority_max - return maximum RT priority.
5113 * @policy: scheduling class.
5114 *
5115 * this syscall returns the maximum rt_priority that can be used
5116 * by a given scheduling class.
5117 */
5118asmlinkage long sys_sched_get_priority_max(int policy)
5119{
5120 int ret = -EINVAL;
5121
5122 switch (policy) {
5123 case SCHED_FIFO:
5124 case SCHED_RR:
5125 ret = MAX_USER_RT_PRIO-1;
5126 break;
5127 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005128 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005129 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 ret = 0;
5131 break;
5132 }
5133 return ret;
5134}
5135
5136/**
5137 * sys_sched_get_priority_min - return minimum RT priority.
5138 * @policy: scheduling class.
5139 *
5140 * this syscall returns the minimum rt_priority that can be used
5141 * by a given scheduling class.
5142 */
5143asmlinkage long sys_sched_get_priority_min(int policy)
5144{
5145 int ret = -EINVAL;
5146
5147 switch (policy) {
5148 case SCHED_FIFO:
5149 case SCHED_RR:
5150 ret = 1;
5151 break;
5152 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005153 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005154 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 ret = 0;
5156 }
5157 return ret;
5158}
5159
5160/**
5161 * sys_sched_rr_get_interval - return the default timeslice of a process.
5162 * @pid: pid of the process.
5163 * @interval: userspace pointer to the timeslice value.
5164 *
5165 * this syscall writes the default timeslice value of a given process
5166 * into the user-space timespec buffer. A value of '0' means infinity.
5167 */
5168asmlinkage
5169long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5170{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005171 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005172 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005173 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
5176 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005177 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
5179 retval = -ESRCH;
5180 read_lock(&tasklist_lock);
5181 p = find_process_by_pid(pid);
5182 if (!p)
5183 goto out_unlock;
5184
5185 retval = security_task_getscheduler(p);
5186 if (retval)
5187 goto out_unlock;
5188
Ingo Molnar77034932007-12-04 17:04:39 +01005189 /*
5190 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5191 * tasks that are on an otherwise idle runqueue:
5192 */
5193 time_slice = 0;
5194 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005195 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005196 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005197 struct sched_entity *se = &p->se;
5198 unsigned long flags;
5199 struct rq *rq;
5200
5201 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005202 if (rq->cfs.load.weight)
5203 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005204 task_rq_unlock(rq, &flags);
5205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005207 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211out_unlock:
5212 read_unlock(&tasklist_lock);
5213 return retval;
5214}
5215
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005216static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005217
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005218void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005221 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005224 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005225 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005226#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005228 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005230 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231#else
5232 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005233 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005235 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236#endif
5237#ifdef CONFIG_DEBUG_STACK_USAGE
5238 {
Al Viro10ebffd2005-11-13 16:06:56 -08005239 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 while (!*n)
5241 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005242 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005245 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005246 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005248 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005251void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005253 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Ingo Molnar4bd77322007-07-11 21:21:47 +02005255#if BITS_PER_LONG == 32
5256 printk(KERN_INFO
5257 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005259 printk(KERN_INFO
5260 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261#endif
5262 read_lock(&tasklist_lock);
5263 do_each_thread(g, p) {
5264 /*
5265 * reset the NMI-timeout, listing all files on a slow
5266 * console might take alot of time:
5267 */
5268 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005269 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005270 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 } while_each_thread(g, p);
5272
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005273 touch_all_softlockup_watchdogs();
5274
Ingo Molnardd41f592007-07-09 18:51:59 +02005275#ifdef CONFIG_SCHED_DEBUG
5276 sysrq_sched_debug_show();
5277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005279 /*
5280 * Only show locks if all tasks are dumped:
5281 */
5282 if (state_filter == -1)
5283 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284}
5285
Ingo Molnar1df21052007-07-09 18:51:58 +02005286void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5287{
Ingo Molnardd41f592007-07-09 18:51:59 +02005288 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005289}
5290
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005291/**
5292 * init_idle - set up an idle thread for a given CPU
5293 * @idle: task in question
5294 * @cpu: cpu the idle task belongs to
5295 *
5296 * NOTE: this function does not set the idle thread's NEED_RESCHED
5297 * flag, to make booting more robust.
5298 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005299void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005301 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 unsigned long flags;
5303
Ingo Molnardd41f592007-07-09 18:51:59 +02005304 __sched_fork(idle);
5305 idle->se.exec_start = sched_clock();
5306
Ingo Molnarb29739f2006-06-27 02:54:51 -07005307 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005309 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 spin_lock_irqsave(&rq->lock, flags);
5312 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005313#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5314 idle->oncpu = 1;
5315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 spin_unlock_irqrestore(&rq->lock, flags);
5317
5318 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005319 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005320
Ingo Molnardd41f592007-07-09 18:51:59 +02005321 /*
5322 * The idle tasks have their own, simple scheduling class:
5323 */
5324 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325}
5326
5327/*
5328 * In a system that switches off the HZ timer nohz_cpu_mask
5329 * indicates which cpus entered this state. This is used
5330 * in the rcu update to wait only for active cpus. For system
5331 * which do not switch off the HZ timer nohz_cpu_mask should
5332 * always be CPU_MASK_NONE.
5333 */
5334cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5335
Ingo Molnar19978ca2007-11-09 22:39:38 +01005336/*
5337 * Increase the granularity value when there are more CPUs,
5338 * because with more CPUs the 'effective latency' as visible
5339 * to users decreases. But the relationship is not linear,
5340 * so pick a second-best guess by going with the log2 of the
5341 * number of CPUs.
5342 *
5343 * This idea comes from the SD scheduler of Con Kolivas:
5344 */
5345static inline void sched_init_granularity(void)
5346{
5347 unsigned int factor = 1 + ilog2(num_online_cpus());
5348 const unsigned long limit = 200000000;
5349
5350 sysctl_sched_min_granularity *= factor;
5351 if (sysctl_sched_min_granularity > limit)
5352 sysctl_sched_min_granularity = limit;
5353
5354 sysctl_sched_latency *= factor;
5355 if (sysctl_sched_latency > limit)
5356 sysctl_sched_latency = limit;
5357
5358 sysctl_sched_wakeup_granularity *= factor;
5359 sysctl_sched_batch_wakeup_granularity *= factor;
5360}
5361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362#ifdef CONFIG_SMP
5363/*
5364 * This is how migration works:
5365 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005366 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 * runqueue and wake up that CPU's migration thread.
5368 * 2) we down() the locked semaphore => thread blocks.
5369 * 3) migration thread wakes up (implicitly it forces the migrated
5370 * thread off the CPU)
5371 * 4) it gets the migration request and checks whether the migrated
5372 * task is still in the wrong runqueue.
5373 * 5) if it's in the wrong runqueue then the migration thread removes
5374 * it and puts it into the right queue.
5375 * 6) migration thread up()s the semaphore.
5376 * 7) we wake up and the migration is done.
5377 */
5378
5379/*
5380 * Change a given task's CPU affinity. Migrate the thread to a
5381 * proper CPU and schedule it away if the CPU it's executing on
5382 * is removed from the allowed bitmask.
5383 *
5384 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005385 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 * call is not atomic; no spinlocks may be held.
5387 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005388int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005390 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005392 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005393 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395 rq = task_rq_lock(p, &flags);
5396 if (!cpus_intersects(new_mask, cpu_online_map)) {
5397 ret = -EINVAL;
5398 goto out;
5399 }
5400
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005401 if (p->sched_class->set_cpus_allowed)
5402 p->sched_class->set_cpus_allowed(p, &new_mask);
5403 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005404 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005405 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005406 }
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 /* Can the task run on the task's current CPU? If so, we're done */
5409 if (cpu_isset(task_cpu(p), new_mask))
5410 goto out;
5411
5412 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5413 /* Need help from migration thread: drop lock and wait. */
5414 task_rq_unlock(rq, &flags);
5415 wake_up_process(rq->migration_thread);
5416 wait_for_completion(&req.done);
5417 tlb_migrate_finish(p->mm);
5418 return 0;
5419 }
5420out:
5421 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 return ret;
5424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425EXPORT_SYMBOL_GPL(set_cpus_allowed);
5426
5427/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005428 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 * this because either it can't run here any more (set_cpus_allowed()
5430 * away from this CPU, or CPU going down), or because we're
5431 * attempting to rebalance this task on exec (sched_exec).
5432 *
5433 * So we race with normal scheduler movements, but that's OK, as long
5434 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005435 *
5436 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005438static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005440 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005441 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446 rq_src = cpu_rq(src_cpu);
5447 rq_dest = cpu_rq(dest_cpu);
5448
5449 double_rq_lock(rq_src, rq_dest);
5450 /* Already moved. */
5451 if (task_cpu(p) != src_cpu)
5452 goto out;
5453 /* Affinity changed (again). */
5454 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5455 goto out;
5456
Ingo Molnardd41f592007-07-09 18:51:59 +02005457 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005458 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005459 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005462 if (on_rq) {
5463 activate_task(rq_dest, p, 0);
5464 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005466 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467out:
5468 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
5472/*
5473 * migration_thread - this is a highprio system thread that performs
5474 * thread migration by bumping thread off CPU then 'pushing' onto
5475 * another runqueue.
5476 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005477static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005480 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 rq = cpu_rq(cpu);
5483 BUG_ON(rq->migration_thread != current);
5484
5485 set_current_state(TASK_INTERRUPTIBLE);
5486 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005487 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 spin_lock_irq(&rq->lock);
5491
5492 if (cpu_is_offline(cpu)) {
5493 spin_unlock_irq(&rq->lock);
5494 goto wait_to_die;
5495 }
5496
5497 if (rq->active_balance) {
5498 active_load_balance(rq, cpu);
5499 rq->active_balance = 0;
5500 }
5501
5502 head = &rq->migration_queue;
5503
5504 if (list_empty(head)) {
5505 spin_unlock_irq(&rq->lock);
5506 schedule();
5507 set_current_state(TASK_INTERRUPTIBLE);
5508 continue;
5509 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005510 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 list_del_init(head->next);
5512
Nick Piggin674311d2005-06-25 14:57:27 -07005513 spin_unlock(&rq->lock);
5514 __migrate_task(req->task, cpu, req->dest_cpu);
5515 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517 complete(&req->done);
5518 }
5519 __set_current_state(TASK_RUNNING);
5520 return 0;
5521
5522wait_to_die:
5523 /* Wait for kthread_stop */
5524 set_current_state(TASK_INTERRUPTIBLE);
5525 while (!kthread_should_stop()) {
5526 schedule();
5527 set_current_state(TASK_INTERRUPTIBLE);
5528 }
5529 __set_current_state(TASK_RUNNING);
5530 return 0;
5531}
5532
5533#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005534
5535static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5536{
5537 int ret;
5538
5539 local_irq_disable();
5540 ret = __migrate_task(p, src_cpu, dest_cpu);
5541 local_irq_enable();
5542 return ret;
5543}
5544
Kirill Korotaev054b9102006-12-10 02:20:11 -08005545/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005546 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005547 * NOTE: interrupts should be disabled by the caller
5548 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005549static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005551 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005553 struct rq *rq;
5554 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Andi Kleen3a5c3592007-10-15 17:00:14 +02005556 do {
5557 /* On same node? */
5558 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5559 cpus_and(mask, mask, p->cpus_allowed);
5560 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Andi Kleen3a5c3592007-10-15 17:00:14 +02005562 /* On any allowed CPU? */
5563 if (dest_cpu == NR_CPUS)
5564 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Andi Kleen3a5c3592007-10-15 17:00:14 +02005566 /* No more Mr. Nice Guy. */
5567 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005568 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5569 /*
5570 * Try to stay on the same cpuset, where the
5571 * current cpuset may be a subset of all cpus.
5572 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005573 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005574 * called within calls to cpuset_lock/cpuset_unlock.
5575 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005576 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005577 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005578 dest_cpu = any_online_cpu(p->cpus_allowed);
5579 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
Andi Kleen3a5c3592007-10-15 17:00:14 +02005581 /*
5582 * Don't tell them about moving exiting tasks or
5583 * kernel threads (both mm NULL), since they never
5584 * leave kernel.
5585 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005586 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005587 printk(KERN_INFO "process %d (%s) no "
5588 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005589 task_pid_nr(p), p->comm, dead_cpu);
5590 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005591 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005592 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593}
5594
5595/*
5596 * While a dead CPU has no uninterruptible tasks queued at this point,
5597 * it might still have a nonzero ->nr_uninterruptible counter, because
5598 * for performance reasons the counter is not stricly tracking tasks to
5599 * their home CPUs. So we just add the counter to another CPU's counter,
5600 * to keep the global sum constant after CPU-down:
5601 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005602static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005604 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 unsigned long flags;
5606
5607 local_irq_save(flags);
5608 double_rq_lock(rq_src, rq_dest);
5609 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5610 rq_src->nr_uninterruptible = 0;
5611 double_rq_unlock(rq_src, rq_dest);
5612 local_irq_restore(flags);
5613}
5614
5615/* Run through task list and migrate tasks from the dead cpu. */
5616static void migrate_live_tasks(int src_cpu)
5617{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005618 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005620 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Ingo Molnar48f24c42006-07-03 00:25:40 -07005622 do_each_thread(t, p) {
5623 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 continue;
5625
Ingo Molnar48f24c42006-07-03 00:25:40 -07005626 if (task_cpu(p) == src_cpu)
5627 move_task_off_dead_cpu(src_cpu, p);
5628 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005630 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631}
5632
Ingo Molnardd41f592007-07-09 18:51:59 +02005633/*
5634 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005635 * It does so by boosting its priority to highest possible.
5636 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 */
5638void sched_idle_next(void)
5639{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005640 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005641 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 struct task_struct *p = rq->idle;
5643 unsigned long flags;
5644
5645 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005646 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Ingo Molnar48f24c42006-07-03 00:25:40 -07005648 /*
5649 * Strictly not necessary since rest of the CPUs are stopped by now
5650 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 */
5652 spin_lock_irqsave(&rq->lock, flags);
5653
Ingo Molnardd41f592007-07-09 18:51:59 +02005654 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005655
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005656 update_rq_clock(rq);
5657 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 spin_unlock_irqrestore(&rq->lock, flags);
5660}
5661
Ingo Molnar48f24c42006-07-03 00:25:40 -07005662/*
5663 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 * offline.
5665 */
5666void idle_task_exit(void)
5667{
5668 struct mm_struct *mm = current->active_mm;
5669
5670 BUG_ON(cpu_online(smp_processor_id()));
5671
5672 if (mm != &init_mm)
5673 switch_mm(mm, &init_mm, current);
5674 mmdrop(mm);
5675}
5676
Kirill Korotaev054b9102006-12-10 02:20:11 -08005677/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005678static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005680 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005683 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
5685 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005686 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Ingo Molnar48f24c42006-07-03 00:25:40 -07005688 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 /*
5691 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005692 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 * fine.
5694 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005695 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005696 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005697 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Ingo Molnar48f24c42006-07-03 00:25:40 -07005699 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700}
5701
5702/* release_task() removes task from tasklist, so we won't find dead tasks. */
5703static void migrate_dead_tasks(unsigned int dead_cpu)
5704{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005705 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005706 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Ingo Molnardd41f592007-07-09 18:51:59 +02005708 for ( ; ; ) {
5709 if (!rq->nr_running)
5710 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005711 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005712 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005713 if (!next)
5714 break;
5715 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005716
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
5718}
5719#endif /* CONFIG_HOTPLUG_CPU */
5720
Nick Piggine692ab52007-07-26 13:40:43 +02005721#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5722
5723static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005724 {
5725 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005726 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005727 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005728 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005729};
5730
5731static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005732 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005733 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005734 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005735 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005736 .child = sd_ctl_dir,
5737 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005738 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005739};
5740
5741static struct ctl_table *sd_alloc_ctl_entry(int n)
5742{
5743 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005744 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005745
Nick Piggine692ab52007-07-26 13:40:43 +02005746 return entry;
5747}
5748
Milton Miller6382bc92007-10-15 17:00:19 +02005749static void sd_free_ctl_entry(struct ctl_table **tablep)
5750{
Milton Millercd790072007-10-17 16:55:11 +02005751 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005752
Milton Millercd790072007-10-17 16:55:11 +02005753 /*
5754 * In the intermediate directories, both the child directory and
5755 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005756 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005757 * static strings and all have proc handlers.
5758 */
5759 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005760 if (entry->child)
5761 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005762 if (entry->proc_handler == NULL)
5763 kfree(entry->procname);
5764 }
Milton Miller6382bc92007-10-15 17:00:19 +02005765
5766 kfree(*tablep);
5767 *tablep = NULL;
5768}
5769
Nick Piggine692ab52007-07-26 13:40:43 +02005770static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005771set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005772 const char *procname, void *data, int maxlen,
5773 mode_t mode, proc_handler *proc_handler)
5774{
Nick Piggine692ab52007-07-26 13:40:43 +02005775 entry->procname = procname;
5776 entry->data = data;
5777 entry->maxlen = maxlen;
5778 entry->mode = mode;
5779 entry->proc_handler = proc_handler;
5780}
5781
5782static struct ctl_table *
5783sd_alloc_ctl_domain_table(struct sched_domain *sd)
5784{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005785 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005786
Milton Millerad1cdc12007-10-15 17:00:19 +02005787 if (table == NULL)
5788 return NULL;
5789
Alexey Dobriyane0361852007-08-09 11:16:46 +02005790 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005791 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005792 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005793 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005794 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005795 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005796 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005797 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005798 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005799 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005800 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005801 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005802 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005803 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005804 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005805 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005806 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005807 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005808 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005809 &sd->cache_nice_tries,
5810 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005811 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005812 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005813 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005814
5815 return table;
5816}
5817
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005818static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005819{
5820 struct ctl_table *entry, *table;
5821 struct sched_domain *sd;
5822 int domain_num = 0, i;
5823 char buf[32];
5824
5825 for_each_domain(cpu, sd)
5826 domain_num++;
5827 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005828 if (table == NULL)
5829 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005830
5831 i = 0;
5832 for_each_domain(cpu, sd) {
5833 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005834 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005835 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005836 entry->child = sd_alloc_ctl_domain_table(sd);
5837 entry++;
5838 i++;
5839 }
5840 return table;
5841}
5842
5843static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005844static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005845{
5846 int i, cpu_num = num_online_cpus();
5847 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5848 char buf[32];
5849
Milton Miller73785472007-10-24 18:23:48 +02005850 WARN_ON(sd_ctl_dir[0].child);
5851 sd_ctl_dir[0].child = entry;
5852
Milton Millerad1cdc12007-10-15 17:00:19 +02005853 if (entry == NULL)
5854 return;
5855
Milton Miller97b6ea72007-10-15 17:00:19 +02005856 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005857 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005858 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005859 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005860 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005861 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005862 }
Milton Miller73785472007-10-24 18:23:48 +02005863
5864 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005865 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5866}
Milton Miller6382bc92007-10-15 17:00:19 +02005867
Milton Miller73785472007-10-24 18:23:48 +02005868/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005869static void unregister_sched_domain_sysctl(void)
5870{
Milton Miller73785472007-10-24 18:23:48 +02005871 if (sd_sysctl_header)
5872 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005873 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005874 if (sd_ctl_dir[0].child)
5875 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005876}
Nick Piggine692ab52007-07-26 13:40:43 +02005877#else
Milton Miller6382bc92007-10-15 17:00:19 +02005878static void register_sched_domain_sysctl(void)
5879{
5880}
5881static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005882{
5883}
5884#endif
5885
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886/*
5887 * migration_call - callback that gets triggered when a CPU is added.
5888 * Here we can start up the necessary migration thread for the new CPU.
5889 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005890static int __cpuinit
5891migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005894 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005896 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005901 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005902 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (IS_ERR(p))
5904 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 kthread_bind(p, cpu);
5906 /* Must be high prio: stop_machine expects to yield to it. */
5907 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005908 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 task_rq_unlock(rq, &flags);
5910 cpu_rq(cpu)->migration_thread = p;
5911 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005912
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005914 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005915 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005917
5918 /* Update our root-domain */
5919 rq = cpu_rq(cpu);
5920 spin_lock_irqsave(&rq->lock, flags);
5921 if (rq->rd) {
5922 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5923 cpu_set(cpu, rq->rd->online);
5924 }
5925 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928#ifdef CONFIG_HOTPLUG_CPU
5929 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005930 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005931 if (!cpu_rq(cpu)->migration_thread)
5932 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005933 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005934 kthread_bind(cpu_rq(cpu)->migration_thread,
5935 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 kthread_stop(cpu_rq(cpu)->migration_thread);
5937 cpu_rq(cpu)->migration_thread = NULL;
5938 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005941 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005942 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 migrate_live_tasks(cpu);
5944 rq = cpu_rq(cpu);
5945 kthread_stop(rq->migration_thread);
5946 rq->migration_thread = NULL;
5947 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005948 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005949 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005950 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005952 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5953 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005955 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005956 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 migrate_nr_uninterruptible(rq);
5958 BUG_ON(rq->nr_running != 0);
5959
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005960 /*
5961 * No need to migrate the tasks: it was best-effort if
5962 * they didn't take sched_hotcpu_mutex. Just wake up
5963 * the requestors.
5964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 spin_lock_irq(&rq->lock);
5966 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005967 struct migration_req *req;
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005970 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 list_del_init(&req->list);
5972 complete(&req->done);
5973 }
5974 spin_unlock_irq(&rq->lock);
5975 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005976
Gregory Haskins08f503b2008-03-10 17:59:11 -04005977 case CPU_DYING:
5978 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01005979 /* Update our root-domain */
5980 rq = cpu_rq(cpu);
5981 spin_lock_irqsave(&rq->lock, flags);
5982 if (rq->rd) {
5983 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5984 cpu_clear(cpu, rq->rd->online);
5985 }
5986 spin_unlock_irqrestore(&rq->lock, flags);
5987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988#endif
5989 }
5990 return NOTIFY_OK;
5991}
5992
5993/* Register at highest priority so that task migration (migrate_all_tasks)
5994 * happens before everything else.
5995 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005996static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 .notifier_call = migration_call,
5998 .priority = 10
5999};
6000
Adrian Bunke6fe6642007-11-09 22:39:39 +01006001void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002{
6003 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006004 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006005
6006 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006007 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6008 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6010 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011}
6012#endif
6013
6014#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006015
6016/* Number of possible processor ids */
6017int nr_cpu_ids __read_mostly = NR_CPUS;
6018EXPORT_SYMBOL(nr_cpu_ids);
6019
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006020#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006021
6022static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6023{
6024 struct sched_group *group = sd->groups;
6025 cpumask_t groupmask;
6026 char str[NR_CPUS];
6027
6028 cpumask_scnprintf(str, NR_CPUS, sd->span);
6029 cpus_clear(groupmask);
6030
6031 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6032
6033 if (!(sd->flags & SD_LOAD_BALANCE)) {
6034 printk("does not load-balance\n");
6035 if (sd->parent)
6036 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6037 " has parent");
6038 return -1;
6039 }
6040
6041 printk(KERN_CONT "span %s\n", str);
6042
6043 if (!cpu_isset(cpu, sd->span)) {
6044 printk(KERN_ERR "ERROR: domain->span does not contain "
6045 "CPU%d\n", cpu);
6046 }
6047 if (!cpu_isset(cpu, group->cpumask)) {
6048 printk(KERN_ERR "ERROR: domain->groups does not contain"
6049 " CPU%d\n", cpu);
6050 }
6051
6052 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6053 do {
6054 if (!group) {
6055 printk("\n");
6056 printk(KERN_ERR "ERROR: group is NULL\n");
6057 break;
6058 }
6059
6060 if (!group->__cpu_power) {
6061 printk(KERN_CONT "\n");
6062 printk(KERN_ERR "ERROR: domain->cpu_power not "
6063 "set\n");
6064 break;
6065 }
6066
6067 if (!cpus_weight(group->cpumask)) {
6068 printk(KERN_CONT "\n");
6069 printk(KERN_ERR "ERROR: empty group\n");
6070 break;
6071 }
6072
6073 if (cpus_intersects(groupmask, group->cpumask)) {
6074 printk(KERN_CONT "\n");
6075 printk(KERN_ERR "ERROR: repeated CPUs\n");
6076 break;
6077 }
6078
6079 cpus_or(groupmask, groupmask, group->cpumask);
6080
6081 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6082 printk(KERN_CONT " %s", str);
6083
6084 group = group->next;
6085 } while (group != sd->groups);
6086 printk(KERN_CONT "\n");
6087
6088 if (!cpus_equal(sd->span, groupmask))
6089 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6090
6091 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6092 printk(KERN_ERR "ERROR: parent span is not a superset "
6093 "of domain->span\n");
6094 return 0;
6095}
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097static void sched_domain_debug(struct sched_domain *sd, int cpu)
6098{
6099 int level = 0;
6100
Nick Piggin41c7ce92005-06-25 14:57:24 -07006101 if (!sd) {
6102 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6103 return;
6104 }
6105
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6107
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006108 for (;;) {
6109 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 level++;
6112 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006113 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006114 break;
6115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116}
6117#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006118# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119#endif
6120
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006121static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006122{
6123 if (cpus_weight(sd->span) == 1)
6124 return 1;
6125
6126 /* Following flags need at least 2 groups */
6127 if (sd->flags & (SD_LOAD_BALANCE |
6128 SD_BALANCE_NEWIDLE |
6129 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006130 SD_BALANCE_EXEC |
6131 SD_SHARE_CPUPOWER |
6132 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006133 if (sd->groups != sd->groups->next)
6134 return 0;
6135 }
6136
6137 /* Following flags don't use groups */
6138 if (sd->flags & (SD_WAKE_IDLE |
6139 SD_WAKE_AFFINE |
6140 SD_WAKE_BALANCE))
6141 return 0;
6142
6143 return 1;
6144}
6145
Ingo Molnar48f24c42006-07-03 00:25:40 -07006146static int
6147sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006148{
6149 unsigned long cflags = sd->flags, pflags = parent->flags;
6150
6151 if (sd_degenerate(parent))
6152 return 1;
6153
6154 if (!cpus_equal(sd->span, parent->span))
6155 return 0;
6156
6157 /* Does parent contain flags not in child? */
6158 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6159 if (cflags & SD_WAKE_AFFINE)
6160 pflags &= ~SD_WAKE_BALANCE;
6161 /* Flags needing groups don't count if only 1 group in parent */
6162 if (parent->groups == parent->groups->next) {
6163 pflags &= ~(SD_LOAD_BALANCE |
6164 SD_BALANCE_NEWIDLE |
6165 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006166 SD_BALANCE_EXEC |
6167 SD_SHARE_CPUPOWER |
6168 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006169 }
6170 if (~cflags & pflags)
6171 return 0;
6172
6173 return 1;
6174}
6175
Gregory Haskins57d885f2008-01-25 21:08:18 +01006176static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6177{
6178 unsigned long flags;
6179 const struct sched_class *class;
6180
6181 spin_lock_irqsave(&rq->lock, flags);
6182
6183 if (rq->rd) {
6184 struct root_domain *old_rd = rq->rd;
6185
Ingo Molnar0eab9142008-01-25 21:08:19 +01006186 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006187 if (class->leave_domain)
6188 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006189 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006190
Gregory Haskinsdc938522008-01-25 21:08:26 +01006191 cpu_clear(rq->cpu, old_rd->span);
6192 cpu_clear(rq->cpu, old_rd->online);
6193
Gregory Haskins57d885f2008-01-25 21:08:18 +01006194 if (atomic_dec_and_test(&old_rd->refcount))
6195 kfree(old_rd);
6196 }
6197
6198 atomic_inc(&rd->refcount);
6199 rq->rd = rd;
6200
Gregory Haskinsdc938522008-01-25 21:08:26 +01006201 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006202 if (cpu_isset(rq->cpu, cpu_online_map))
6203 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006204
Ingo Molnar0eab9142008-01-25 21:08:19 +01006205 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006206 if (class->join_domain)
6207 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006208 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006209
6210 spin_unlock_irqrestore(&rq->lock, flags);
6211}
6212
Gregory Haskinsdc938522008-01-25 21:08:26 +01006213static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006214{
6215 memset(rd, 0, sizeof(*rd));
6216
Gregory Haskinsdc938522008-01-25 21:08:26 +01006217 cpus_clear(rd->span);
6218 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006219}
6220
6221static void init_defrootdomain(void)
6222{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006223 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006224 atomic_set(&def_root_domain.refcount, 1);
6225}
6226
Gregory Haskinsdc938522008-01-25 21:08:26 +01006227static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006228{
6229 struct root_domain *rd;
6230
6231 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6232 if (!rd)
6233 return NULL;
6234
Gregory Haskinsdc938522008-01-25 21:08:26 +01006235 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006236
6237 return rd;
6238}
6239
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006241 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 * hold the hotplug lock.
6243 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006244static void
6245cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006247 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006248 struct sched_domain *tmp;
6249
6250 /* Remove the sched domains which do not contribute to scheduling. */
6251 for (tmp = sd; tmp; tmp = tmp->parent) {
6252 struct sched_domain *parent = tmp->parent;
6253 if (!parent)
6254 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006255 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006256 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006257 if (parent->parent)
6258 parent->parent->child = tmp;
6259 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006260 }
6261
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006262 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006263 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006264 if (sd)
6265 sd->child = NULL;
6266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
6268 sched_domain_debug(sd, cpu);
6269
Gregory Haskins57d885f2008-01-25 21:08:18 +01006270 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006271 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
6273
6274/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006275static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277/* Setup the mask of cpus configured for isolated domains */
6278static int __init isolated_cpu_setup(char *str)
6279{
6280 int ints[NR_CPUS], i;
6281
6282 str = get_options(str, ARRAY_SIZE(ints), ints);
6283 cpus_clear(cpu_isolated_map);
6284 for (i = 1; i <= ints[0]; i++)
6285 if (ints[i] < NR_CPUS)
6286 cpu_set(ints[i], cpu_isolated_map);
6287 return 1;
6288}
6289
Ingo Molnar8927f492007-10-15 17:00:13 +02006290__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
6292/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006293 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6294 * to a function which identifies what group(along with sched group) a CPU
6295 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6296 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 *
6298 * init_sched_build_groups will build a circular linked list of the groups
6299 * covered by the given span, and will set each group's ->cpumask correctly,
6300 * and ->cpu_power to 0.
6301 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006302static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006303init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6304 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6305 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306{
6307 struct sched_group *first = NULL, *last = NULL;
6308 cpumask_t covered = CPU_MASK_NONE;
6309 int i;
6310
6311 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006312 struct sched_group *sg;
6313 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 int j;
6315
6316 if (cpu_isset(i, covered))
6317 continue;
6318
6319 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006320 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006323 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 continue;
6325
6326 cpu_set(j, covered);
6327 cpu_set(j, sg->cpumask);
6328 }
6329 if (!first)
6330 first = sg;
6331 if (last)
6332 last->next = sg;
6333 last = sg;
6334 }
6335 last->next = first;
6336}
6337
John Hawkes9c1cfda2005-09-06 15:18:14 -07006338#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
John Hawkes9c1cfda2005-09-06 15:18:14 -07006340#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006341
John Hawkes9c1cfda2005-09-06 15:18:14 -07006342/**
6343 * find_next_best_node - find the next node to include in a sched_domain
6344 * @node: node whose sched_domain we're building
6345 * @used_nodes: nodes already in the sched_domain
6346 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006347 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006348 * finds the closest node not already in the @used_nodes map.
6349 *
6350 * Should use nodemask_t.
6351 */
6352static int find_next_best_node(int node, unsigned long *used_nodes)
6353{
6354 int i, n, val, min_val, best_node = 0;
6355
6356 min_val = INT_MAX;
6357
6358 for (i = 0; i < MAX_NUMNODES; i++) {
6359 /* Start at @node */
6360 n = (node + i) % MAX_NUMNODES;
6361
6362 if (!nr_cpus_node(n))
6363 continue;
6364
6365 /* Skip already used nodes */
6366 if (test_bit(n, used_nodes))
6367 continue;
6368
6369 /* Simple min distance search */
6370 val = node_distance(node, n);
6371
6372 if (val < min_val) {
6373 min_val = val;
6374 best_node = n;
6375 }
6376 }
6377
6378 set_bit(best_node, used_nodes);
6379 return best_node;
6380}
6381
6382/**
6383 * sched_domain_node_span - get a cpumask for a node's sched_domain
6384 * @node: node whose cpumask we're constructing
6385 * @size: number of nodes to include in this span
6386 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006387 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006388 * should be one that prevents unnecessary balancing, but also spreads tasks
6389 * out optimally.
6390 */
6391static cpumask_t sched_domain_node_span(int node)
6392{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006393 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006394 cpumask_t span, nodemask;
6395 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006396
6397 cpus_clear(span);
6398 bitmap_zero(used_nodes, MAX_NUMNODES);
6399
6400 nodemask = node_to_cpumask(node);
6401 cpus_or(span, span, nodemask);
6402 set_bit(node, used_nodes);
6403
6404 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6405 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006406
John Hawkes9c1cfda2005-09-06 15:18:14 -07006407 nodemask = node_to_cpumask(next_node);
6408 cpus_or(span, span, nodemask);
6409 }
6410
6411 return span;
6412}
6413#endif
6414
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006415int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006416
John Hawkes9c1cfda2005-09-06 15:18:14 -07006417/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006418 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420#ifdef CONFIG_SCHED_SMT
6421static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006422static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006423
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006424static int
6425cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006427 if (sg)
6428 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 return cpu;
6430}
6431#endif
6432
Ingo Molnar48f24c42006-07-03 00:25:40 -07006433/*
6434 * multi-core sched-domains:
6435 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006436#ifdef CONFIG_SCHED_MC
6437static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006438static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006439#endif
6440
6441#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006442static int
6443cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006444{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006445 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006446 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006447 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006448 group = first_cpu(mask);
6449 if (sg)
6450 *sg = &per_cpu(sched_group_core, group);
6451 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006452}
6453#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006454static int
6455cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006456{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006457 if (sg)
6458 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006459 return cpu;
6460}
6461#endif
6462
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006464static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006465
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006466static int
6467cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006469 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006470#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006471 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006472 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006473 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006474#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006475 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006476 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006477 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006479 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006481 if (sg)
6482 *sg = &per_cpu(sched_group_phys, group);
6483 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484}
6485
6486#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006487/*
6488 * The init_sched_build_groups can't handle what we want to do with node
6489 * groups, so roll our own. Now each node has its own list of groups which
6490 * gets dynamically allocated.
6491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006493static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006494
6495static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006496static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006497
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006498static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6499 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006501 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6502 int group;
6503
6504 cpus_and(nodemask, nodemask, *cpu_map);
6505 group = first_cpu(nodemask);
6506
6507 if (sg)
6508 *sg = &per_cpu(sched_group_allnodes, group);
6509 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006511
Siddha, Suresh B08069032006-03-27 01:15:23 -08006512static void init_numa_sched_groups_power(struct sched_group *group_head)
6513{
6514 struct sched_group *sg = group_head;
6515 int j;
6516
6517 if (!sg)
6518 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006519 do {
6520 for_each_cpu_mask(j, sg->cpumask) {
6521 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006522
Andi Kleen3a5c3592007-10-15 17:00:14 +02006523 sd = &per_cpu(phys_domains, j);
6524 if (j != first_cpu(sd->groups->cpumask)) {
6525 /*
6526 * Only add "power" once for each
6527 * physical package.
6528 */
6529 continue;
6530 }
6531
6532 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006533 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006534 sg = sg->next;
6535 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537#endif
6538
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006539#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006540/* Free memory allocated for various sched_group structures */
6541static void free_sched_groups(const cpumask_t *cpu_map)
6542{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006543 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006544
6545 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006546 struct sched_group **sched_group_nodes
6547 = sched_group_nodes_bycpu[cpu];
6548
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006549 if (!sched_group_nodes)
6550 continue;
6551
6552 for (i = 0; i < MAX_NUMNODES; i++) {
6553 cpumask_t nodemask = node_to_cpumask(i);
6554 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6555
6556 cpus_and(nodemask, nodemask, *cpu_map);
6557 if (cpus_empty(nodemask))
6558 continue;
6559
6560 if (sg == NULL)
6561 continue;
6562 sg = sg->next;
6563next_sg:
6564 oldsg = sg;
6565 sg = sg->next;
6566 kfree(oldsg);
6567 if (oldsg != sched_group_nodes[i])
6568 goto next_sg;
6569 }
6570 kfree(sched_group_nodes);
6571 sched_group_nodes_bycpu[cpu] = NULL;
6572 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006573}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006574#else
6575static void free_sched_groups(const cpumask_t *cpu_map)
6576{
6577}
6578#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006579
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006581 * Initialize sched groups cpu_power.
6582 *
6583 * cpu_power indicates the capacity of sched group, which is used while
6584 * distributing the load between different sched groups in a sched domain.
6585 * Typically cpu_power for all the groups in a sched domain will be same unless
6586 * there are asymmetries in the topology. If there are asymmetries, group
6587 * having more cpu_power will pickup more load compared to the group having
6588 * less cpu_power.
6589 *
6590 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6591 * the maximum number of tasks a group can handle in the presence of other idle
6592 * or lightly loaded groups in the same sched domain.
6593 */
6594static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6595{
6596 struct sched_domain *child;
6597 struct sched_group *group;
6598
6599 WARN_ON(!sd || !sd->groups);
6600
6601 if (cpu != first_cpu(sd->groups->cpumask))
6602 return;
6603
6604 child = sd->child;
6605
Eric Dumazet5517d862007-05-08 00:32:57 -07006606 sd->groups->__cpu_power = 0;
6607
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006608 /*
6609 * For perf policy, if the groups in child domain share resources
6610 * (for example cores sharing some portions of the cache hierarchy
6611 * or SMT), then set this domain groups cpu_power such that each group
6612 * can handle only one task, when there are other idle groups in the
6613 * same sched domain.
6614 */
6615 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6616 (child->flags &
6617 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006618 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006619 return;
6620 }
6621
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006622 /*
6623 * add cpu_power of each child group to this groups cpu_power
6624 */
6625 group = child->groups;
6626 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006627 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006628 group = group->next;
6629 } while (group != child->groups);
6630}
6631
6632/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006633 * Build sched domains for a given set of cpus and attach the sched domains
6634 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006636static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637{
6638 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006639 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006640#ifdef CONFIG_NUMA
6641 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006642 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006643
6644 /*
6645 * Allocate the per-node list of sched groups
6646 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006647 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006648 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006649 if (!sched_group_nodes) {
6650 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006651 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006652 }
6653 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Gregory Haskinsdc938522008-01-25 21:08:26 +01006656 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006657 if (!rd) {
6658 printk(KERN_WARNING "Cannot alloc root domain\n");
6659 return -ENOMEM;
6660 }
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006663 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006665 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 struct sched_domain *sd = NULL, *p;
6667 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6668
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006669 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
6671#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006672 if (cpus_weight(*cpu_map) >
6673 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006674 sd = &per_cpu(allnodes_domains, i);
6675 *sd = SD_ALLNODES_INIT;
6676 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006677 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006678 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006679 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006680 } else
6681 p = NULL;
6682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006685 sd->span = sched_domain_node_span(cpu_to_node(i));
6686 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006687 if (p)
6688 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006689 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690#endif
6691
6692 p = sd;
6693 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 *sd = SD_CPU_INIT;
6695 sd->span = nodemask;
6696 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006697 if (p)
6698 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006699 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006701#ifdef CONFIG_SCHED_MC
6702 p = sd;
6703 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006704 *sd = SD_MC_INIT;
6705 sd->span = cpu_coregroup_map(i);
6706 cpus_and(sd->span, sd->span, *cpu_map);
6707 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006708 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006709 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006710#endif
6711
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712#ifdef CONFIG_SCHED_SMT
6713 p = sd;
6714 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006716 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006717 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006719 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006720 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721#endif
6722 }
6723
6724#ifdef CONFIG_SCHED_SMT
6725 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006726 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006727 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006728 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 if (i != first_cpu(this_sibling_map))
6730 continue;
6731
Ingo Molnardd41f592007-07-09 18:51:59 +02006732 init_sched_build_groups(this_sibling_map, cpu_map,
6733 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 }
6735#endif
6736
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006737#ifdef CONFIG_SCHED_MC
6738 /* Set up multi-core groups */
6739 for_each_cpu_mask(i, *cpu_map) {
6740 cpumask_t this_core_map = cpu_coregroup_map(i);
6741 cpus_and(this_core_map, this_core_map, *cpu_map);
6742 if (i != first_cpu(this_core_map))
6743 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006744 init_sched_build_groups(this_core_map, cpu_map,
6745 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006746 }
6747#endif
6748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 /* Set up physical groups */
6750 for (i = 0; i < MAX_NUMNODES; i++) {
6751 cpumask_t nodemask = node_to_cpumask(i);
6752
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006753 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 if (cpus_empty(nodemask))
6755 continue;
6756
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006757 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 }
6759
6760#ifdef CONFIG_NUMA
6761 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006762 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006763 init_sched_build_groups(*cpu_map, cpu_map,
6764 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006765
6766 for (i = 0; i < MAX_NUMNODES; i++) {
6767 /* Set up node groups */
6768 struct sched_group *sg, *prev;
6769 cpumask_t nodemask = node_to_cpumask(i);
6770 cpumask_t domainspan;
6771 cpumask_t covered = CPU_MASK_NONE;
6772 int j;
6773
6774 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006775 if (cpus_empty(nodemask)) {
6776 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006777 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006778 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006779
6780 domainspan = sched_domain_node_span(i);
6781 cpus_and(domainspan, domainspan, *cpu_map);
6782
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006783 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006784 if (!sg) {
6785 printk(KERN_WARNING "Can not alloc domain group for "
6786 "node %d\n", i);
6787 goto error;
6788 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006789 sched_group_nodes[i] = sg;
6790 for_each_cpu_mask(j, nodemask) {
6791 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006792
John Hawkes9c1cfda2005-09-06 15:18:14 -07006793 sd = &per_cpu(node_domains, j);
6794 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006795 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006796 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006797 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006798 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006799 cpus_or(covered, covered, nodemask);
6800 prev = sg;
6801
6802 for (j = 0; j < MAX_NUMNODES; j++) {
6803 cpumask_t tmp, notcovered;
6804 int n = (i + j) % MAX_NUMNODES;
6805
6806 cpus_complement(notcovered, covered);
6807 cpus_and(tmp, notcovered, *cpu_map);
6808 cpus_and(tmp, tmp, domainspan);
6809 if (cpus_empty(tmp))
6810 break;
6811
6812 nodemask = node_to_cpumask(n);
6813 cpus_and(tmp, tmp, nodemask);
6814 if (cpus_empty(tmp))
6815 continue;
6816
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006817 sg = kmalloc_node(sizeof(struct sched_group),
6818 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006819 if (!sg) {
6820 printk(KERN_WARNING
6821 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006822 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006823 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006824 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006825 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006826 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006827 cpus_or(covered, covered, tmp);
6828 prev->next = sg;
6829 prev = sg;
6830 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832#endif
6833
6834 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006835#ifdef CONFIG_SCHED_SMT
6836 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006837 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6838
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006839 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006840 }
6841#endif
6842#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006843 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006844 struct sched_domain *sd = &per_cpu(core_domains, i);
6845
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006846 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006847 }
6848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006850 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006851 struct sched_domain *sd = &per_cpu(phys_domains, i);
6852
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006853 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 }
6855
John Hawkes9c1cfda2005-09-06 15:18:14 -07006856#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006857 for (i = 0; i < MAX_NUMNODES; i++)
6858 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006859
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006860 if (sd_allnodes) {
6861 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006862
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006863 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006864 init_numa_sched_groups_power(sg);
6865 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006866#endif
6867
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006869 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 struct sched_domain *sd;
6871#ifdef CONFIG_SCHED_SMT
6872 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006873#elif defined(CONFIG_SCHED_MC)
6874 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875#else
6876 sd = &per_cpu(phys_domains, i);
6877#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006878 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006880
6881 return 0;
6882
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006883#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006884error:
6885 free_sched_groups(cpu_map);
6886 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888}
Paul Jackson029190c2007-10-18 23:40:20 -07006889
6890static cpumask_t *doms_cur; /* current sched domains */
6891static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6892
6893/*
6894 * Special case: If a kmalloc of a doms_cur partition (array of
6895 * cpumask_t) fails, then fallback to a single sched domain,
6896 * as determined by the single cpumask_t fallback_doms.
6897 */
6898static cpumask_t fallback_doms;
6899
Heiko Carstens22e52b02008-03-12 18:31:59 +01006900void __attribute__((weak)) arch_update_cpu_topology(void)
6901{
6902}
6903
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006904/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006905 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006906 * For now this just excludes isolated cpus, but could be used to
6907 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006908 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006909static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006910{
Milton Miller73785472007-10-24 18:23:48 +02006911 int err;
6912
Heiko Carstens22e52b02008-03-12 18:31:59 +01006913 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006914 ndoms_cur = 1;
6915 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6916 if (!doms_cur)
6917 doms_cur = &fallback_doms;
6918 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006919 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006920 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006921
6922 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006923}
6924
6925static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006927 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006928}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006930/*
6931 * Detach sched domains from a group of cpus specified in cpu_map
6932 * These cpus will now be attached to the NULL domain
6933 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006934static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006935{
6936 int i;
6937
Milton Miller6382bc92007-10-15 17:00:19 +02006938 unregister_sched_domain_sysctl();
6939
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006940 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006941 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006942 synchronize_sched();
6943 arch_destroy_sched_domains(cpu_map);
6944}
6945
Paul Jackson029190c2007-10-18 23:40:20 -07006946/*
6947 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006948 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006949 * doms_new[] to the current sched domain partitioning, doms_cur[].
6950 * It destroys each deleted domain and builds each new domain.
6951 *
6952 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006953 * The masks don't intersect (don't overlap.) We should setup one
6954 * sched domain for each mask. CPUs not in any of the cpumasks will
6955 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006956 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6957 * it as it is.
6958 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006959 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6960 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07006961 * failed the kmalloc call, then it can pass in doms_new == NULL,
6962 * and partition_sched_domains() will fallback to the single partition
6963 * 'fallback_doms'.
6964 *
6965 * Call with hotplug lock held
6966 */
6967void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6968{
6969 int i, j;
6970
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006971 lock_doms_cur();
6972
Milton Miller73785472007-10-24 18:23:48 +02006973 /* always unregister in case we don't destroy any domains */
6974 unregister_sched_domain_sysctl();
6975
Paul Jackson029190c2007-10-18 23:40:20 -07006976 if (doms_new == NULL) {
6977 ndoms_new = 1;
6978 doms_new = &fallback_doms;
6979 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
6980 }
6981
6982 /* Destroy deleted domains */
6983 for (i = 0; i < ndoms_cur; i++) {
6984 for (j = 0; j < ndoms_new; j++) {
6985 if (cpus_equal(doms_cur[i], doms_new[j]))
6986 goto match1;
6987 }
6988 /* no match - a current sched domain not in new doms_new[] */
6989 detach_destroy_domains(doms_cur + i);
6990match1:
6991 ;
6992 }
6993
6994 /* Build new domains */
6995 for (i = 0; i < ndoms_new; i++) {
6996 for (j = 0; j < ndoms_cur; j++) {
6997 if (cpus_equal(doms_new[i], doms_cur[j]))
6998 goto match2;
6999 }
7000 /* no match - add a new doms_new */
7001 build_sched_domains(doms_new + i);
7002match2:
7003 ;
7004 }
7005
7006 /* Remember the new sched domains */
7007 if (doms_cur != &fallback_doms)
7008 kfree(doms_cur);
7009 doms_cur = doms_new;
7010 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007011
7012 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007013
7014 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007015}
7016
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007017#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007018int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007019{
7020 int err;
7021
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007022 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007023 detach_destroy_domains(&cpu_online_map);
7024 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007025 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007026
7027 return err;
7028}
7029
7030static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7031{
7032 int ret;
7033
7034 if (buf[0] != '0' && buf[0] != '1')
7035 return -EINVAL;
7036
7037 if (smt)
7038 sched_smt_power_savings = (buf[0] == '1');
7039 else
7040 sched_mc_power_savings = (buf[0] == '1');
7041
7042 ret = arch_reinit_sched_domains();
7043
7044 return ret ? ret : count;
7045}
7046
Adrian Bunk6707de002007-08-12 18:08:19 +02007047#ifdef CONFIG_SCHED_MC
7048static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7049{
7050 return sprintf(page, "%u\n", sched_mc_power_savings);
7051}
7052static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7053 const char *buf, size_t count)
7054{
7055 return sched_power_savings_store(buf, count, 0);
7056}
7057static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7058 sched_mc_power_savings_store);
7059#endif
7060
7061#ifdef CONFIG_SCHED_SMT
7062static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7063{
7064 return sprintf(page, "%u\n", sched_smt_power_savings);
7065}
7066static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7067 const char *buf, size_t count)
7068{
7069 return sched_power_savings_store(buf, count, 1);
7070}
7071static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7072 sched_smt_power_savings_store);
7073#endif
7074
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007075int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7076{
7077 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007078
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007079#ifdef CONFIG_SCHED_SMT
7080 if (smt_capable())
7081 err = sysfs_create_file(&cls->kset.kobj,
7082 &attr_sched_smt_power_savings.attr);
7083#endif
7084#ifdef CONFIG_SCHED_MC
7085 if (!err && mc_capable())
7086 err = sysfs_create_file(&cls->kset.kobj,
7087 &attr_sched_mc_power_savings.attr);
7088#endif
7089 return err;
7090}
7091#endif
7092
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007094 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007096 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 * which will prevent rebalancing while the sched domains are recalculated.
7098 */
7099static int update_sched_domains(struct notifier_block *nfb,
7100 unsigned long action, void *hcpu)
7101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 switch (action) {
7103 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007104 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007106 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007107 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 return NOTIFY_OK;
7109
7110 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007111 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007113 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007115 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007117 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 /*
7119 * Fall through and re-initialise the domains.
7120 */
7121 break;
7122 default:
7123 return NOTIFY_DONE;
7124 }
7125
7126 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007127 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
7129 return NOTIFY_OK;
7130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
7132void __init sched_init_smp(void)
7133{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007134 cpumask_t non_isolated_cpus;
7135
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007136 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007137 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007138 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007139 if (cpus_empty(non_isolated_cpus))
7140 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007141 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 /* XXX: Theoretical race here - CPU may be hotplugged now */
7143 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007144
7145 /* Move init over to a non-isolated CPU */
7146 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7147 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007148 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149}
7150#else
7151void __init sched_init_smp(void)
7152{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007153 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154}
7155#endif /* CONFIG_SMP */
7156
7157int in_sched_functions(unsigned long addr)
7158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 return in_lock_functions(addr) ||
7160 (addr >= (unsigned long)__sched_text_start
7161 && addr < (unsigned long)__sched_text_end);
7162}
7163
Alexey Dobriyana9957442007-10-15 17:00:13 +02007164static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007165{
7166 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007167#ifdef CONFIG_FAIR_GROUP_SCHED
7168 cfs_rq->rq = rq;
7169#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007170 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007171}
7172
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007173static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7174{
7175 struct rt_prio_array *array;
7176 int i;
7177
7178 array = &rt_rq->active;
7179 for (i = 0; i < MAX_RT_PRIO; i++) {
7180 INIT_LIST_HEAD(array->queue + i);
7181 __clear_bit(i, array->bitmap);
7182 }
7183 /* delimiter for bitsearch: */
7184 __set_bit(MAX_RT_PRIO, array->bitmap);
7185
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007186#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007187 rt_rq->highest_prio = MAX_RT_PRIO;
7188#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007189#ifdef CONFIG_SMP
7190 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007191 rt_rq->overloaded = 0;
7192#endif
7193
7194 rt_rq->rt_time = 0;
7195 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007196
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007197#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007198 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007199 rt_rq->rq = rq;
7200#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007201}
7202
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007203#ifdef CONFIG_FAIR_GROUP_SCHED
7204static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7205 struct cfs_rq *cfs_rq, struct sched_entity *se,
7206 int cpu, int add)
7207{
7208 tg->cfs_rq[cpu] = cfs_rq;
7209 init_cfs_rq(cfs_rq, rq);
7210 cfs_rq->tg = tg;
7211 if (add)
7212 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7213
7214 tg->se[cpu] = se;
7215 se->cfs_rq = &rq->cfs;
7216 se->my_q = cfs_rq;
7217 se->load.weight = tg->shares;
7218 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7219 se->parent = NULL;
7220}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007221#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007222
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007223#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007224static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7225 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7226 int cpu, int add)
7227{
7228 tg->rt_rq[cpu] = rt_rq;
7229 init_rt_rq(rt_rq, rq);
7230 rt_rq->tg = tg;
7231 rt_rq->rt_se = rt_se;
7232 if (add)
7233 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7234
7235 tg->rt_se[cpu] = rt_se;
7236 rt_se->rt_rq = &rq->rt;
7237 rt_se->my_q = rt_rq;
7238 rt_se->parent = NULL;
7239 INIT_LIST_HEAD(&rt_se->run_list);
7240}
7241#endif
7242
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243void __init sched_init(void)
7244{
Christoph Lameter476f3532007-05-06 14:48:58 -07007245 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007246 int i, j;
7247
Gregory Haskins57d885f2008-01-25 21:08:18 +01007248#ifdef CONFIG_SMP
7249 init_defrootdomain();
7250#endif
7251
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007252#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007253 list_add(&init_task_group.list, &task_groups);
7254#endif
7255
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007256 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007257 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 rq = cpu_rq(i);
7260 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007261 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007262 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007263 rq->clock = 1;
7264 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007265 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007266#ifdef CONFIG_FAIR_GROUP_SCHED
7267 init_task_group.shares = init_task_group_load;
7268 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7269 init_tg_cfs_entry(rq, &init_task_group,
7270 &per_cpu(init_cfs_rq, i),
7271 &per_cpu(init_sched_entity, i), i, 1);
7272
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007273#endif
7274#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007275 init_task_group.rt_runtime =
7276 sysctl_sched_rt_runtime * NSEC_PER_USEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007277 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7278 init_tg_rt_entry(rq, &init_task_group,
7279 &per_cpu(init_rt_rq, i),
7280 &per_cpu(init_sched_rt_entity, i), i, 1);
7281#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007282 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007283 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Ingo Molnardd41f592007-07-09 18:51:59 +02007285 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7286 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007288 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007289 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007291 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007293 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 rq->migration_thread = NULL;
7295 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007296 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007298 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007300 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 }
7302
Peter Williams2dd73a42006-06-27 02:54:34 -07007303 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007304
Avi Kivitye107be32007-07-26 13:40:43 +02007305#ifdef CONFIG_PREEMPT_NOTIFIERS
7306 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7307#endif
7308
Christoph Lameterc9819f42006-12-10 02:20:25 -08007309#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007310 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007311 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7312#endif
7313
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007314#ifdef CONFIG_RT_MUTEXES
7315 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7316#endif
7317
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 /*
7319 * The boot idle thread does lazy MMU switching as well:
7320 */
7321 atomic_inc(&init_mm.mm_count);
7322 enter_lazy_tlb(&init_mm, current);
7323
7324 /*
7325 * Make us the idle thread. Technically, schedule() should not be
7326 * called from this thread, however somewhere below it might be,
7327 * but because we are the idle thread, we just pick up running again
7328 * when this runqueue becomes "idle".
7329 */
7330 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007331 /*
7332 * During early bootup we pretend to be a normal task:
7333 */
7334 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007335
7336 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337}
7338
7339#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7340void __might_sleep(char *file, int line)
7341{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007342#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 static unsigned long prev_jiffy; /* ratelimiting */
7344
7345 if ((in_atomic() || irqs_disabled()) &&
7346 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7347 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7348 return;
7349 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007350 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 " context at %s:%d\n", file, line);
7352 printk("in_atomic():%d, irqs_disabled():%d\n",
7353 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007354 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007355 if (irqs_disabled())
7356 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 dump_stack();
7358 }
7359#endif
7360}
7361EXPORT_SYMBOL(__might_sleep);
7362#endif
7363
7364#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007365static void normalize_task(struct rq *rq, struct task_struct *p)
7366{
7367 int on_rq;
7368 update_rq_clock(rq);
7369 on_rq = p->se.on_rq;
7370 if (on_rq)
7371 deactivate_task(rq, p, 0);
7372 __setscheduler(rq, p, SCHED_NORMAL, 0);
7373 if (on_rq) {
7374 activate_task(rq, p, 0);
7375 resched_task(rq->curr);
7376 }
7377}
7378
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379void normalize_rt_tasks(void)
7380{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007381 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007383 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007385 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007386 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007387 /*
7388 * Only normalize user tasks:
7389 */
7390 if (!p->mm)
7391 continue;
7392
Ingo Molnardd41f592007-07-09 18:51:59 +02007393 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007394#ifdef CONFIG_SCHEDSTATS
7395 p->se.wait_start = 0;
7396 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007397 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007398#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007399 task_rq(p)->clock = 0;
7400
7401 if (!rt_task(p)) {
7402 /*
7403 * Renice negative nice level userspace
7404 * tasks back to 0:
7405 */
7406 if (TASK_NICE(p) < 0 && p->mm)
7407 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007411 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007412 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Ingo Molnar178be792007-10-15 17:00:18 +02007414 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007415
Ingo Molnarb29739f2006-06-27 02:54:51 -07007416 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007417 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007418 } while_each_thread(g, p);
7419
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007420 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421}
7422
7423#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007424
7425#ifdef CONFIG_IA64
7426/*
7427 * These functions are only useful for the IA64 MCA handling.
7428 *
7429 * They can only be called when the whole system has been
7430 * stopped - every CPU needs to be quiescent, and no scheduling
7431 * activity can take place. Using them for anything else would
7432 * be a serious bug, and as a result, they aren't even visible
7433 * under any other configuration.
7434 */
7435
7436/**
7437 * curr_task - return the current task for a given cpu.
7438 * @cpu: the processor in question.
7439 *
7440 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7441 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007442struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007443{
7444 return cpu_curr(cpu);
7445}
7446
7447/**
7448 * set_curr_task - set the current task for a given cpu.
7449 * @cpu: the processor in question.
7450 * @p: the task pointer to set.
7451 *
7452 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007453 * are serviced on a separate stack. It allows the architecture to switch the
7454 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007455 * must be called with all CPU's synchronized, and interrupts disabled, the
7456 * and caller must save the original value of the current task (see
7457 * curr_task() above) and restore that value before reenabling interrupts and
7458 * re-starting the system.
7459 *
7460 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7461 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007462void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007463{
7464 cpu_curr(cpu) = p;
7465}
7466
7467#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007468
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007469#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007470
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007471#ifdef CONFIG_FAIR_GROUP_SCHED
7472static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007473{
7474 int i;
7475
7476 for_each_possible_cpu(i) {
7477 if (tg->cfs_rq)
7478 kfree(tg->cfs_rq[i]);
7479 if (tg->se)
7480 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007481 }
7482
7483 kfree(tg->cfs_rq);
7484 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007485}
7486
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007487static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007488{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007489 struct cfs_rq *cfs_rq;
7490 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007491 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007492 int i;
7493
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007494 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007495 if (!tg->cfs_rq)
7496 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007497 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007498 if (!tg->se)
7499 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007500
7501 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007502
7503 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007504 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007505
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007506 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7507 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007508 if (!cfs_rq)
7509 goto err;
7510
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007511 se = kmalloc_node(sizeof(struct sched_entity),
7512 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007513 if (!se)
7514 goto err;
7515
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007516 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007517 }
7518
7519 return 1;
7520
7521 err:
7522 return 0;
7523}
7524
7525static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7526{
7527 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7528 &cpu_rq(cpu)->leaf_cfs_rq_list);
7529}
7530
7531static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7532{
7533 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7534}
7535#else
7536static inline void free_fair_sched_group(struct task_group *tg)
7537{
7538}
7539
7540static inline int alloc_fair_sched_group(struct task_group *tg)
7541{
7542 return 1;
7543}
7544
7545static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7546{
7547}
7548
7549static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7550{
7551}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007552#endif
7553
7554#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007555static void free_rt_sched_group(struct task_group *tg)
7556{
7557 int i;
7558
7559 for_each_possible_cpu(i) {
7560 if (tg->rt_rq)
7561 kfree(tg->rt_rq[i]);
7562 if (tg->rt_se)
7563 kfree(tg->rt_se[i]);
7564 }
7565
7566 kfree(tg->rt_rq);
7567 kfree(tg->rt_se);
7568}
7569
7570static int alloc_rt_sched_group(struct task_group *tg)
7571{
7572 struct rt_rq *rt_rq;
7573 struct sched_rt_entity *rt_se;
7574 struct rq *rq;
7575 int i;
7576
7577 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7578 if (!tg->rt_rq)
7579 goto err;
7580 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7581 if (!tg->rt_se)
7582 goto err;
7583
7584 tg->rt_runtime = 0;
7585
7586 for_each_possible_cpu(i) {
7587 rq = cpu_rq(i);
7588
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007589 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7590 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7591 if (!rt_rq)
7592 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007593
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007594 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7595 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7596 if (!rt_se)
7597 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007598
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007599 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007600 }
7601
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007602 return 1;
7603
7604 err:
7605 return 0;
7606}
7607
7608static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7609{
7610 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7611 &cpu_rq(cpu)->leaf_rt_rq_list);
7612}
7613
7614static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7615{
7616 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7617}
7618#else
7619static inline void free_rt_sched_group(struct task_group *tg)
7620{
7621}
7622
7623static inline int alloc_rt_sched_group(struct task_group *tg)
7624{
7625 return 1;
7626}
7627
7628static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7629{
7630}
7631
7632static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7633{
7634}
7635#endif
7636
7637static void free_sched_group(struct task_group *tg)
7638{
7639 free_fair_sched_group(tg);
7640 free_rt_sched_group(tg);
7641 kfree(tg);
7642}
7643
7644/* allocate runqueue etc for a new task group */
7645struct task_group *sched_create_group(void)
7646{
7647 struct task_group *tg;
7648 unsigned long flags;
7649 int i;
7650
7651 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7652 if (!tg)
7653 return ERR_PTR(-ENOMEM);
7654
7655 if (!alloc_fair_sched_group(tg))
7656 goto err;
7657
7658 if (!alloc_rt_sched_group(tg))
7659 goto err;
7660
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007661 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007662 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007663 register_fair_sched_group(tg, i);
7664 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007665 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007666 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007667 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007668
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007669 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007670
7671err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007672 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007673 return ERR_PTR(-ENOMEM);
7674}
7675
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007676/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007677static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007678{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007679 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007680 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007681}
7682
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007683/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007684void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007685{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007686 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007687 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007688
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007689 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007690 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007691 unregister_fair_sched_group(tg, i);
7692 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007693 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007694 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007695 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007696
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007697 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007698 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007699}
7700
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007701/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007702 * The caller of this function should have put the task in its new group
7703 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7704 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007705 */
7706void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007707{
7708 int on_rq, running;
7709 unsigned long flags;
7710 struct rq *rq;
7711
7712 rq = task_rq_lock(tsk, &flags);
7713
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007714 update_rq_clock(rq);
7715
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007716 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007717 on_rq = tsk->se.on_rq;
7718
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007719 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007720 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007721 if (unlikely(running))
7722 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007723
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007724 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007725
Peter Zijlstra810b3812008-02-29 15:21:01 -05007726#ifdef CONFIG_FAIR_GROUP_SCHED
7727 if (tsk->sched_class->moved_group)
7728 tsk->sched_class->moved_group(tsk);
7729#endif
7730
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007731 if (unlikely(running))
7732 tsk->sched_class->set_curr_task(rq);
7733 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007734 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007735
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007736 task_rq_unlock(rq, &flags);
7737}
7738
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007739#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007740static void set_se_shares(struct sched_entity *se, unsigned long shares)
7741{
7742 struct cfs_rq *cfs_rq = se->cfs_rq;
7743 struct rq *rq = cfs_rq->rq;
7744 int on_rq;
7745
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007746 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007747
7748 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007749 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007750 dequeue_entity(cfs_rq, se, 0);
7751
7752 se->load.weight = shares;
7753 se->load.inv_weight = div64_64((1ULL<<32), shares);
7754
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007755 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007756 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007757
7758 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007759}
7760
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007761static DEFINE_MUTEX(shares_mutex);
7762
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007763int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007764{
7765 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007766 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007767
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007768 /*
7769 * A weight of 0 or 1 can cause arithmetics problems.
7770 * (The default weight is 1024 - so there's no practical
7771 * limitation from this.)
7772 */
7773 if (shares < 2)
7774 shares = 2;
7775
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007776 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007777 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007778 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007779
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007780 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007781 for_each_possible_cpu(i)
7782 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007783 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007784
7785 /* wait for any ongoing reference to this group to finish */
7786 synchronize_sched();
7787
7788 /*
7789 * Now we are free to modify the group's share on each cpu
7790 * w/o tripping rebalance_share or load_balance_fair.
7791 */
7792 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007793 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007794 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007795
7796 /*
7797 * Enable load balance activity on this group, by inserting it back on
7798 * each cpu's rq->leaf_cfs_rq_list.
7799 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007800 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007801 for_each_possible_cpu(i)
7802 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007803 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007804done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007805 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007806 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007807}
7808
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007809unsigned long sched_group_shares(struct task_group *tg)
7810{
7811 return tg->shares;
7812}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007813#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007814
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007815#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007816/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007817 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007818 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007819static DEFINE_MUTEX(rt_constraints_mutex);
7820
7821static unsigned long to_ratio(u64 period, u64 runtime)
7822{
7823 if (runtime == RUNTIME_INF)
7824 return 1ULL << 16;
7825
Peter Zijlstra2692a242008-02-27 12:00:46 +01007826 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007827}
7828
7829static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007830{
7831 struct task_group *tgi;
7832 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007833 unsigned long global_ratio =
7834 to_ratio(sysctl_sched_rt_period,
7835 sysctl_sched_rt_runtime < 0 ?
7836 RUNTIME_INF : sysctl_sched_rt_runtime);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007837
7838 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007839 list_for_each_entry_rcu(tgi, &task_groups, list) {
7840 if (tgi == tg)
7841 continue;
7842
7843 total += to_ratio(period, tgi->rt_runtime);
7844 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007845 rcu_read_unlock();
7846
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007847 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007848}
7849
Dhaval Giani521f1a242008-02-28 15:21:56 +05307850/* Must be called with tasklist_lock held */
7851static inline int tg_has_rt_tasks(struct task_group *tg)
7852{
7853 struct task_struct *g, *p;
7854 do_each_thread(g, p) {
7855 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7856 return 1;
7857 } while_each_thread(g, p);
7858 return 0;
7859}
7860
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007861int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007862{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007863 u64 rt_runtime, rt_period;
7864 int err = 0;
7865
Peter Zijlstra2692a242008-02-27 12:00:46 +01007866 rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007867 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7868 if (rt_runtime_us == -1)
Peter Zijlstra2692a242008-02-27 12:00:46 +01007869 rt_runtime = RUNTIME_INF;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007870
7871 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307872 read_lock(&tasklist_lock);
7873 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) {
7874 err = -EBUSY;
7875 goto unlock;
7876 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007877 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7878 err = -EINVAL;
7879 goto unlock;
7880 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007881 tg->rt_runtime = rt_runtime;
7882 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307883 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007884 mutex_unlock(&rt_constraints_mutex);
7885
7886 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007887}
7888
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007889long sched_group_rt_runtime(struct task_group *tg)
7890{
7891 u64 rt_runtime_us;
7892
7893 if (tg->rt_runtime == RUNTIME_INF)
7894 return -1;
7895
7896 rt_runtime_us = tg->rt_runtime;
7897 do_div(rt_runtime_us, NSEC_PER_USEC);
7898 return rt_runtime_us;
7899}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007900#endif
7901#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007902
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007903#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007904
7905/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007906static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007907{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007908 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
7909 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007910}
7911
7912static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02007913cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007914{
7915 struct task_group *tg;
7916
Paul Menage2b01dfe2007-10-24 18:23:50 +02007917 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007918 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007919 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007920 return &init_task_group.css;
7921 }
7922
7923 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007924 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007925 return ERR_PTR(-EINVAL);
7926
7927 tg = sched_create_group();
7928 if (IS_ERR(tg))
7929 return ERR_PTR(-ENOMEM);
7930
7931 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007932 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007933
7934 return &tg->css;
7935}
7936
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007937static void
7938cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007939{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007940 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007941
7942 sched_destroy_group(tg);
7943}
7944
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007945static int
7946cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7947 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007948{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007949#ifdef CONFIG_RT_GROUP_SCHED
7950 /* Don't accept realtime tasks when there is no way for them to run */
7951 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0)
7952 return -EINVAL;
7953#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007954 /* We don't support RT-tasks being in separate groups */
7955 if (tsk->sched_class != &fair_sched_class)
7956 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007957#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007958
7959 return 0;
7960}
7961
7962static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02007963cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007964 struct cgroup *old_cont, struct task_struct *tsk)
7965{
7966 sched_move_task(tsk);
7967}
7968
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007969#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02007970static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
7971 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007972{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007973 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007974}
7975
Paul Menage2b01dfe2007-10-24 18:23:50 +02007976static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007977{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007978 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007979
7980 return (u64) tg->shares;
7981}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007982#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007983
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007984#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007985static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
7986 struct file *file,
7987 const char __user *userbuf,
7988 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007989{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007990 char buffer[64];
7991 int retval = 0;
7992 s64 val;
7993 char *end;
7994
7995 if (!nbytes)
7996 return -EINVAL;
7997 if (nbytes >= sizeof(buffer))
7998 return -E2BIG;
7999 if (copy_from_user(buffer, userbuf, nbytes))
8000 return -EFAULT;
8001
8002 buffer[nbytes] = 0; /* nul-terminate */
8003
8004 /* strip newline if necessary */
8005 if (nbytes && (buffer[nbytes-1] == '\n'))
8006 buffer[nbytes-1] = 0;
8007 val = simple_strtoll(buffer, &end, 0);
8008 if (*end)
8009 return -EINVAL;
8010
8011 /* Pass to subsystem */
8012 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8013 if (!retval)
8014 retval = nbytes;
8015 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008016}
8017
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008018static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8019 struct file *file,
8020 char __user *buf, size_t nbytes,
8021 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008022{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008023 char tmp[64];
8024 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8025 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008026
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008027 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008028}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008029#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008030
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008031static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008032#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008033 {
8034 .name = "shares",
8035 .read_uint = cpu_shares_read_uint,
8036 .write_uint = cpu_shares_write_uint,
8037 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008038#endif
8039#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008040 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008041 .name = "rt_runtime_us",
8042 .read = cpu_rt_runtime_read,
8043 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008044 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008045#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008046};
8047
8048static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8049{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008050 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008051}
8052
8053struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008054 .name = "cpu",
8055 .create = cpu_cgroup_create,
8056 .destroy = cpu_cgroup_destroy,
8057 .can_attach = cpu_cgroup_can_attach,
8058 .attach = cpu_cgroup_attach,
8059 .populate = cpu_cgroup_populate,
8060 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008061 .early_init = 1,
8062};
8063
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008064#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008065
8066#ifdef CONFIG_CGROUP_CPUACCT
8067
8068/*
8069 * CPU accounting code for task groups.
8070 *
8071 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8072 * (balbir@in.ibm.com).
8073 */
8074
8075/* track cpu usage of a group of tasks */
8076struct cpuacct {
8077 struct cgroup_subsys_state css;
8078 /* cpuusage holds pointer to a u64-type object on every cpu */
8079 u64 *cpuusage;
8080};
8081
8082struct cgroup_subsys cpuacct_subsys;
8083
8084/* return cpu accounting group corresponding to this container */
8085static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8086{
8087 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8088 struct cpuacct, css);
8089}
8090
8091/* return cpu accounting group to which this task belongs */
8092static inline struct cpuacct *task_ca(struct task_struct *tsk)
8093{
8094 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8095 struct cpuacct, css);
8096}
8097
8098/* create a new cpu accounting group */
8099static struct cgroup_subsys_state *cpuacct_create(
8100 struct cgroup_subsys *ss, struct cgroup *cont)
8101{
8102 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8103
8104 if (!ca)
8105 return ERR_PTR(-ENOMEM);
8106
8107 ca->cpuusage = alloc_percpu(u64);
8108 if (!ca->cpuusage) {
8109 kfree(ca);
8110 return ERR_PTR(-ENOMEM);
8111 }
8112
8113 return &ca->css;
8114}
8115
8116/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008117static void
8118cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008119{
8120 struct cpuacct *ca = cgroup_ca(cont);
8121
8122 free_percpu(ca->cpuusage);
8123 kfree(ca);
8124}
8125
8126/* return total cpu usage (in nanoseconds) of a group */
8127static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8128{
8129 struct cpuacct *ca = cgroup_ca(cont);
8130 u64 totalcpuusage = 0;
8131 int i;
8132
8133 for_each_possible_cpu(i) {
8134 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8135
8136 /*
8137 * Take rq->lock to make 64-bit addition safe on 32-bit
8138 * platforms.
8139 */
8140 spin_lock_irq(&cpu_rq(i)->lock);
8141 totalcpuusage += *cpuusage;
8142 spin_unlock_irq(&cpu_rq(i)->lock);
8143 }
8144
8145 return totalcpuusage;
8146}
8147
8148static struct cftype files[] = {
8149 {
8150 .name = "usage",
8151 .read_uint = cpuusage_read,
8152 },
8153};
8154
8155static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8156{
8157 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8158}
8159
8160/*
8161 * charge this task's execution time to its accounting group.
8162 *
8163 * called with rq->lock held.
8164 */
8165static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8166{
8167 struct cpuacct *ca;
8168
8169 if (!cpuacct_subsys.active)
8170 return;
8171
8172 ca = task_ca(tsk);
8173 if (ca) {
8174 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8175
8176 *cpuusage += cputime;
8177 }
8178}
8179
8180struct cgroup_subsys cpuacct_subsys = {
8181 .name = "cpuacct",
8182 .create = cpuacct_create,
8183 .destroy = cpuacct_destroy,
8184 .populate = cpuacct_populate,
8185 .subsys_id = cpuacct_subsys_id,
8186};
8187#endif /* CONFIG_CGROUP_CPUACCT */