blob: 2c65bf29d133f3b734965fb809a105fdb0358a12 [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>
Mike Travis434d53b2008-04-04 18:11:04 -070070#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet5517d862007-05-08 00:32:57 -070074#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020075#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Convert user-nice values [ -20 ... 0 ... 19 ]
79 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
80 * and back.
81 */
82#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
83#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
84#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
85
86/*
87 * 'User priority' is the nice value converted to something we
88 * can work with better when scaling various scheduler parameters,
89 * it's a [ 0 ... 39 ] range.
90 */
91#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
92#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
93#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
94
95/*
Ingo Molnard7876a02008-01-25 21:08:19 +010096 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +010098#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200100#define NICE_0_LOAD SCHED_LOAD_SCALE
101#define NICE_0_SHIFT SCHED_LOAD_SHIFT
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * These are the 'tuning knobs' of the scheduler:
105 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200106 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Timeslices get refilled after they expire.
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700110
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200111/*
112 * single value that denotes runtime == period, ie unlimited time.
113 */
114#define RUNTIME_INF ((u64)~0ULL)
115
Eric Dumazet5517d862007-05-08 00:32:57 -0700116#ifdef CONFIG_SMP
117/*
118 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
119 * Since cpu_power is a 'constant', we can use a reciprocal divide.
120 */
121static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
122{
123 return reciprocal_divide(load, sg->reciprocal_cpu_power);
124}
125
126/*
127 * Each time a sched group cpu_power is changed,
128 * we must compute its reciprocal value
129 */
130static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
131{
132 sg->__cpu_power += val;
133 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
134}
135#endif
136
Ingo Molnare05606d2007-07-09 18:51:59 +0200137static inline int rt_policy(int policy)
138{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200139 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200140 return 1;
141 return 0;
142}
143
144static inline int task_has_rt_policy(struct task_struct *p)
145{
146 return rt_policy(p->policy);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200150 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200152struct rt_prio_array {
153 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
154 struct list_head queue[MAX_RT_PRIO];
155};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200157struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100158 /* nests inside the rq lock: */
159 spinlock_t rt_runtime_lock;
160 ktime_t rt_period;
161 u64 rt_runtime;
162 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200163};
164
165static struct rt_bandwidth def_rt_bandwidth;
166
167static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
168
169static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
170{
171 struct rt_bandwidth *rt_b =
172 container_of(timer, struct rt_bandwidth, rt_period_timer);
173 ktime_t now;
174 int overrun;
175 int idle = 0;
176
177 for (;;) {
178 now = hrtimer_cb_get_time(timer);
179 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
180
181 if (!overrun)
182 break;
183
184 idle = do_sched_rt_period_timer(rt_b, overrun);
185 }
186
187 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
188}
189
190static
191void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
192{
193 rt_b->rt_period = ns_to_ktime(period);
194 rt_b->rt_runtime = runtime;
195
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200196 spin_lock_init(&rt_b->rt_runtime_lock);
197
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200198 hrtimer_init(&rt_b->rt_period_timer,
199 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
200 rt_b->rt_period_timer.function = sched_rt_period_timer;
201 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
202}
203
204static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
205{
206 ktime_t now;
207
208 if (rt_b->rt_runtime == RUNTIME_INF)
209 return;
210
211 if (hrtimer_active(&rt_b->rt_period_timer))
212 return;
213
214 spin_lock(&rt_b->rt_runtime_lock);
215 for (;;) {
216 if (hrtimer_active(&rt_b->rt_period_timer))
217 break;
218
219 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
220 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
221 hrtimer_start(&rt_b->rt_period_timer,
222 rt_b->rt_period_timer.expires,
223 HRTIMER_MODE_ABS);
224 }
225 spin_unlock(&rt_b->rt_runtime_lock);
226}
227
228#ifdef CONFIG_RT_GROUP_SCHED
229static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
230{
231 hrtimer_cancel(&rt_b->rt_period_timer);
232}
233#endif
234
Heiko Carstens712555e2008-04-28 11:33:07 +0200235/*
236 * sched_domains_mutex serializes calls to arch_init_sched_domains,
237 * detach_destroy_domains and partition_sched_domains.
238 */
239static DEFINE_MUTEX(sched_domains_mutex);
240
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100241#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700243#include <linux/cgroup.h>
244
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200245struct cfs_rq;
246
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100247static LIST_HEAD(task_groups);
248
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200249/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200250struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100251#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700252 struct cgroup_subsys_state css;
253#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100254
255#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200256 /* schedulable entities of this group on each cpu */
257 struct sched_entity **se;
258 /* runqueue "owned" by this group on each cpu */
259 struct cfs_rq **cfs_rq;
260 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#endif
262
263#ifdef CONFIG_RT_GROUP_SCHED
264 struct sched_rt_entity **rt_se;
265 struct rt_rq **rt_rq;
266
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200267 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100268#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100269
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100270 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100271 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200272
273 struct task_group *parent;
274 struct list_head siblings;
275 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200276};
277
Dhaval Giani354d60c2008-04-19 19:44:59 +0200278#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200279
280/*
281 * Root task group.
282 * Every UID task group (including init_task_group aka UID-0) will
283 * be a child to this group.
284 */
285struct task_group root_task_group;
286
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100287#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200288/* Default task group's sched entity on each cpu */
289static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
290/* Default task group's cfs_rq on each cpu */
291static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100292#endif
293
294#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100295static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
296static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100297#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200298#else
299#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200300#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100301
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100302/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100303 * a task group's cpu shares.
304 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100305static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100306
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100307#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100308#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100309# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200310#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100311# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200312#endif
313
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800314/*
315 * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
316 * (The default weight is 1024 - so there's no practical
317 * limitation from this.)
318 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200319#define MIN_SHARES 2
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800320#define MAX_SHARES (ULONG_MAX - 1)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200321
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100322static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100323#endif
324
325/* Default task group.
326 * Every task in system belong to this group at bootup.
327 */
Mike Travis434d53b2008-04-04 18:11:04 -0700328struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200329
330/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200331static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200332{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200333 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200334
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100335#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200336 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100337#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700338 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
339 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200340#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100341 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200342#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200343 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200344}
345
346/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100347static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200348{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100349#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100350 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
351 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100352#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100353
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100354#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100355 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
356 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100357#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200358}
359
360#else
361
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100362static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200363
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100364#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200365
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200366/* CFS-related fields in a runqueue */
367struct cfs_rq {
368 struct load_weight load;
369 unsigned long nr_running;
370
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200371 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200372 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200373
374 struct rb_root tasks_timeline;
375 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200376
377 struct list_head tasks;
378 struct list_head *balance_iterator;
379
380 /*
381 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200382 * It is set to NULL otherwise (i.e when none are currently running).
383 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100384 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200385
386 unsigned long nr_spread_over;
387
Ingo Molnar62160e32007-10-15 17:00:03 +0200388#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200389 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
390
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100391 /*
392 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200393 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
394 * (like users, containers etc.)
395 *
396 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
397 * list is used during load balance.
398 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100399 struct list_head leaf_cfs_rq_list;
400 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200401#endif
402};
403
404/* Real-Time classes' related field in a runqueue: */
405struct rt_rq {
406 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100407 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100408#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100409 int highest_prio; /* highest queued rt task prio */
410#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100411#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100412 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100413 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100414#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100415 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100416 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200417 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100418 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200419 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100420
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100421#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100422 unsigned long rt_nr_boosted;
423
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100424 struct rq *rq;
425 struct list_head leaf_rt_rq_list;
426 struct task_group *tg;
427 struct sched_rt_entity *rt_se;
428#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200429};
430
Gregory Haskins57d885f2008-01-25 21:08:18 +0100431#ifdef CONFIG_SMP
432
433/*
434 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100435 * variables. Each exclusive cpuset essentially defines an island domain by
436 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100437 * exclusive cpuset is created, we also create and attach a new root-domain
438 * object.
439 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100440 */
441struct root_domain {
442 atomic_t refcount;
443 cpumask_t span;
444 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100445
Ingo Molnar0eab9142008-01-25 21:08:19 +0100446 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100447 * The "RT overload" flag: it gets set if a CPU has more than
448 * one runnable RT task.
449 */
450 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100451 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100452};
453
Gregory Haskinsdc938522008-01-25 21:08:26 +0100454/*
455 * By default the system creates a single root-domain with all cpus as
456 * members (mimicking the global state we have today).
457 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100458static struct root_domain def_root_domain;
459
460#endif
461
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * This is the main, per-CPU runqueue data structure.
464 *
465 * Locking rule: those places that want to lock multiple runqueues
466 * (such as the load balancing or the thread migration code), lock
467 * acquire operations must be ordered by ascending &runqueue.
468 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700469struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200470 /* runqueue lock: */
471 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * nr_running and cpu_load should be in the same cacheline because
475 * remote CPUs use both these fields when doing load calculation.
476 */
477 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200478 #define CPU_LOAD_IDX_MAX 5
479 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700480 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700481#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200482 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700483 unsigned char in_nohz_recently;
484#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200485 /* capture load from *all* tasks on this cpu: */
486 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200487 unsigned long nr_load_updates;
488 u64 nr_switches;
489
490 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100491 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100492
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200493#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200494 /* list of leaf cfs_rq on this cpu: */
495 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100496#endif
497#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100498 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /*
502 * This is part of a global counter where only the total sum
503 * over all CPUs matters. A task can increase this counter on
504 * one CPU and if it got migrated afterwards it may decrease
505 * it on another CPU. Always updated under the runqueue lock:
506 */
507 unsigned long nr_uninterruptible;
508
Ingo Molnar36c8b582006-07-03 00:25:41 -0700509 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800510 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200512
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200513 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 atomic_t nr_iowait;
516
517#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100518 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct sched_domain *sd;
520
521 /* For active balancing */
522 int active_balance;
523 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200524 /* cpu of this runqueue: */
525 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ingo Molnar36c8b582006-07-03 00:25:41 -0700527 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct list_head migration_queue;
529#endif
530
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100531#ifdef CONFIG_SCHED_HRTICK
532 unsigned long hrtick_flags;
533 ktime_t hrtick_expire;
534 struct hrtimer hrtick_timer;
535#endif
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#ifdef CONFIG_SCHEDSTATS
538 /* latency stats */
539 struct sched_info rq_sched_info;
540
541 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200542 unsigned int yld_exp_empty;
543 unsigned int yld_act_empty;
544 unsigned int yld_both_empty;
545 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200548 unsigned int sched_switch;
549 unsigned int sched_count;
550 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200553 unsigned int ttwu_count;
554 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200555
556 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200557 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700559 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560};
561
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700562static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Ingo Molnardd41f592007-07-09 18:51:59 +0200564static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
565{
566 rq->curr->sched_class->check_preempt_curr(rq, p);
567}
568
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700569static inline int cpu_of(struct rq *rq)
570{
571#ifdef CONFIG_SMP
572 return rq->cpu;
573#else
574 return 0;
575#endif
576}
577
Ingo Molnar20d315d2007-07-09 18:51:58 +0200578/*
Nick Piggin674311d2005-06-25 14:57:27 -0700579 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700580 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700581 *
582 * The domain tree of any CPU may only be accessed from within
583 * preempt-disabled sections.
584 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700585#define for_each_domain(cpu, __sd) \
586 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
589#define this_rq() (&__get_cpu_var(runqueues))
590#define task_rq(p) cpu_rq(task_cpu(p))
591#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
592
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200593static inline void update_rq_clock(struct rq *rq)
594{
595 rq->clock = sched_clock_cpu(cpu_of(rq));
596}
597
Ingo Molnare436d802007-07-19 21:28:35 +0200598/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200599 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
600 */
601#ifdef CONFIG_SCHED_DEBUG
602# define const_debug __read_mostly
603#else
604# define const_debug static const
605#endif
606
607/*
608 * Debugging: various feature bits
609 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200610
611#define SCHED_FEAT(name, enabled) \
612 __SCHED_FEAT_##name ,
613
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200614enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200615#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200616};
617
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200618#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200619
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200620#define SCHED_FEAT(name, enabled) \
621 (1UL << __SCHED_FEAT_##name) * enabled |
622
623const_debug unsigned int sysctl_sched_features =
624#include "sched_features.h"
625 0;
626
627#undef SCHED_FEAT
628
629#ifdef CONFIG_SCHED_DEBUG
630#define SCHED_FEAT(name, enabled) \
631 #name ,
632
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700633static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200634#include "sched_features.h"
635 NULL
636};
637
638#undef SCHED_FEAT
639
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700640static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200641{
642 filp->private_data = inode->i_private;
643 return 0;
644}
645
646static ssize_t
647sched_feat_read(struct file *filp, char __user *ubuf,
648 size_t cnt, loff_t *ppos)
649{
650 char *buf;
651 int r = 0;
652 int len = 0;
653 int i;
654
655 for (i = 0; sched_feat_names[i]; i++) {
656 len += strlen(sched_feat_names[i]);
657 len += 4;
658 }
659
660 buf = kmalloc(len + 2, GFP_KERNEL);
661 if (!buf)
662 return -ENOMEM;
663
664 for (i = 0; sched_feat_names[i]; i++) {
665 if (sysctl_sched_features & (1UL << i))
666 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
667 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200668 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200669 }
670
671 r += sprintf(buf + r, "\n");
672 WARN_ON(r >= len + 2);
673
674 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
675
676 kfree(buf);
677
678 return r;
679}
680
681static ssize_t
682sched_feat_write(struct file *filp, const char __user *ubuf,
683 size_t cnt, loff_t *ppos)
684{
685 char buf[64];
686 char *cmp = buf;
687 int neg = 0;
688 int i;
689
690 if (cnt > 63)
691 cnt = 63;
692
693 if (copy_from_user(&buf, ubuf, cnt))
694 return -EFAULT;
695
696 buf[cnt] = 0;
697
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200698 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200699 neg = 1;
700 cmp += 3;
701 }
702
703 for (i = 0; sched_feat_names[i]; i++) {
704 int len = strlen(sched_feat_names[i]);
705
706 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
707 if (neg)
708 sysctl_sched_features &= ~(1UL << i);
709 else
710 sysctl_sched_features |= (1UL << i);
711 break;
712 }
713 }
714
715 if (!sched_feat_names[i])
716 return -EINVAL;
717
718 filp->f_pos += cnt;
719
720 return cnt;
721}
722
723static struct file_operations sched_feat_fops = {
724 .open = sched_feat_open,
725 .read = sched_feat_read,
726 .write = sched_feat_write,
727};
728
729static __init int sched_init_debug(void)
730{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200731 debugfs_create_file("sched_features", 0644, NULL, NULL,
732 &sched_feat_fops);
733
734 return 0;
735}
736late_initcall(sched_init_debug);
737
738#endif
739
740#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200741
742/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100743 * Number of tasks to iterate in a single balance run.
744 * Limited because this is done with IRQs disabled.
745 */
746const_debug unsigned int sysctl_sched_nr_migrate = 32;
747
748/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100749 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100750 * default: 1s
751 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100752unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100753
Ingo Molnar6892b752008-02-13 14:02:36 +0100754static __read_mostly int scheduler_running;
755
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100756/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100757 * part of the period that we allow rt tasks to run in us.
758 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100759 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100760int sysctl_sched_rt_runtime = 950000;
761
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200762static inline u64 global_rt_period(void)
763{
764 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
765}
766
767static inline u64 global_rt_runtime(void)
768{
769 if (sysctl_sched_rt_period < 0)
770 return RUNTIME_INF;
771
772 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
773}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100774
Ingo Molnar690229a2008-04-23 09:31:35 +0200775unsigned long long time_sync_thresh = 100000;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100776
777static DEFINE_PER_CPU(unsigned long long, time_offset);
778static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
779
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100780/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100781 * Global lock which we take every now and then to synchronize
782 * the CPUs time. This method is not warp-safe, but it's good
783 * enough to synchronize slowly diverging time sources and thus
784 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200785 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100786static DEFINE_SPINLOCK(time_sync_lock);
787static unsigned long long prev_global_time;
788
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200789static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
Ingo Molnar27ec4402008-02-28 21:00:21 +0100790{
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200791 /*
792 * We want this inlined, to not get tracer function calls
793 * in this critical section:
794 */
795 spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
796 __raw_spin_lock(&time_sync_lock.raw_lock);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100797
798 if (time < prev_global_time) {
799 per_cpu(time_offset, cpu) += prev_global_time - time;
800 time = prev_global_time;
801 } else {
802 prev_global_time = time;
803 }
804
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200805 __raw_spin_unlock(&time_sync_lock.raw_lock);
806 spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100807
808 return time;
809}
810
811static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200812{
Ingo Molnare436d802007-07-19 21:28:35 +0200813 unsigned long long now;
Ingo Molnare436d802007-07-19 21:28:35 +0200814
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100815 /*
816 * Only call sched_clock() if the scheduler has already been
817 * initialized (some code might call cpu_clock() very early):
818 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100819 if (unlikely(!scheduler_running))
820 return 0;
821
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200822 now = sched_clock_cpu(cpu);
Ingo Molnare436d802007-07-19 21:28:35 +0200823
824 return now;
825}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100826
827/*
828 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
829 * clock constructed from sched_clock():
830 */
831unsigned long long cpu_clock(int cpu)
832{
833 unsigned long long prev_cpu_time, time, delta_time;
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200834 unsigned long flags;
Ingo Molnar27ec4402008-02-28 21:00:21 +0100835
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200836 local_irq_save(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100837 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
838 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
839 delta_time = time-prev_cpu_time;
840
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200841 if (unlikely(delta_time > time_sync_thresh)) {
Ingo Molnar27ec4402008-02-28 21:00:21 +0100842 time = __sync_cpu_clock(time, cpu);
Ingo Molnardfbf4a12008-04-23 09:24:06 +0200843 per_cpu(prev_cpu_time, cpu) = time;
844 }
845 local_irq_restore(flags);
Ingo Molnar27ec4402008-02-28 21:00:21 +0100846
847 return time;
848}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200849EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700852# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700854#ifndef finish_arch_switch
855# define finish_arch_switch(prev) do { } while (0)
856#endif
857
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100858static inline int task_current(struct rq *rq, struct task_struct *p)
859{
860 return rq->curr == p;
861}
862
Nick Piggin4866cde2005-06-25 14:57:23 -0700863#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700864static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700865{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100866 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700867}
868
Ingo Molnar70b97a72006-07-03 00:25:42 -0700869static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700870{
871}
872
Ingo Molnar70b97a72006-07-03 00:25:42 -0700873static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700874{
Ingo Molnarda04c032005-09-13 11:17:59 +0200875#ifdef CONFIG_DEBUG_SPINLOCK
876 /* this is a valid case when another task releases the spinlock */
877 rq->lock.owner = current;
878#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700879 /*
880 * If we are tracking spinlock dependencies then we have to
881 * fix up the runqueue lock - which gets 'carried over' from
882 * prev into current:
883 */
884 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
885
Nick Piggin4866cde2005-06-25 14:57:23 -0700886 spin_unlock_irq(&rq->lock);
887}
888
889#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700890static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700891{
892#ifdef CONFIG_SMP
893 return p->oncpu;
894#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100895 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700896#endif
897}
898
Ingo Molnar70b97a72006-07-03 00:25:42 -0700899static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700900{
901#ifdef CONFIG_SMP
902 /*
903 * We can optimise this out completely for !SMP, because the
904 * SMP rebalancing from interrupt is the only thing that cares
905 * here.
906 */
907 next->oncpu = 1;
908#endif
909#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
910 spin_unlock_irq(&rq->lock);
911#else
912 spin_unlock(&rq->lock);
913#endif
914}
915
Ingo Molnar70b97a72006-07-03 00:25:42 -0700916static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700917{
918#ifdef CONFIG_SMP
919 /*
920 * After ->oncpu is cleared, the task can be moved to a different CPU.
921 * We must ensure this doesn't happen until the switch is completely
922 * finished.
923 */
924 smp_wmb();
925 prev->oncpu = 0;
926#endif
927#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
928 local_irq_enable();
929#endif
930}
931#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700934 * __task_rq_lock - lock the runqueue a given task resides on.
935 * Must be called interrupts disabled.
936 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700937static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700938 __acquires(rq->lock)
939{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200940 for (;;) {
941 struct rq *rq = task_rq(p);
942 spin_lock(&rq->lock);
943 if (likely(rq == task_rq(p)))
944 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700945 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700946 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700947}
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100951 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * explicitly disabling preemption.
953 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700954static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 __acquires(rq->lock)
956{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700957 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Andi Kleen3a5c3592007-10-15 17:00:14 +0200959 for (;;) {
960 local_irq_save(*flags);
961 rq = task_rq(p);
962 spin_lock(&rq->lock);
963 if (likely(rq == task_rq(p)))
964 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Alexey Dobriyana9957442007-10-15 17:00:13 +0200969static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700970 __releases(rq->lock)
971{
972 spin_unlock(&rq->lock);
973}
974
Ingo Molnar70b97a72006-07-03 00:25:42 -0700975static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 __releases(rq->lock)
977{
978 spin_unlock_irqrestore(&rq->lock, *flags);
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800982 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200984static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 __acquires(rq->lock)
986{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700987 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 local_irq_disable();
990 rq = this_rq();
991 spin_lock(&rq->lock);
992
993 return rq;
994}
995
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100996static void __resched_task(struct task_struct *p, int tif_bit);
997
998static inline void resched_task(struct task_struct *p)
999{
1000 __resched_task(p, TIF_NEED_RESCHED);
1001}
1002
1003#ifdef CONFIG_SCHED_HRTICK
1004/*
1005 * Use HR-timers to deliver accurate preemption points.
1006 *
1007 * Its all a bit involved since we cannot program an hrt while holding the
1008 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1009 * reschedule event.
1010 *
1011 * When we get rescheduled we reprogram the hrtick_timer outside of the
1012 * rq->lock.
1013 */
1014static inline void resched_hrt(struct task_struct *p)
1015{
1016 __resched_task(p, TIF_HRTICK_RESCHED);
1017}
1018
1019static inline void resched_rq(struct rq *rq)
1020{
1021 unsigned long flags;
1022
1023 spin_lock_irqsave(&rq->lock, flags);
1024 resched_task(rq->curr);
1025 spin_unlock_irqrestore(&rq->lock, flags);
1026}
1027
1028enum {
1029 HRTICK_SET, /* re-programm hrtick_timer */
1030 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001031 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001032};
1033
1034/*
1035 * Use hrtick when:
1036 * - enabled by features
1037 * - hrtimer is actually high res
1038 */
1039static inline int hrtick_enabled(struct rq *rq)
1040{
1041 if (!sched_feat(HRTICK))
1042 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001043 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1044 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001045 return hrtimer_is_hres_active(&rq->hrtick_timer);
1046}
1047
1048/*
1049 * Called to set the hrtick timer state.
1050 *
1051 * called with rq->lock held and irqs disabled
1052 */
1053static void hrtick_start(struct rq *rq, u64 delay, int reset)
1054{
1055 assert_spin_locked(&rq->lock);
1056
1057 /*
1058 * preempt at: now + delay
1059 */
1060 rq->hrtick_expire =
1061 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1062 /*
1063 * indicate we need to program the timer
1064 */
1065 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1066 if (reset)
1067 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1068
1069 /*
1070 * New slices are called from the schedule path and don't need a
1071 * forced reschedule.
1072 */
1073 if (reset)
1074 resched_hrt(rq->curr);
1075}
1076
1077static void hrtick_clear(struct rq *rq)
1078{
1079 if (hrtimer_active(&rq->hrtick_timer))
1080 hrtimer_cancel(&rq->hrtick_timer);
1081}
1082
1083/*
1084 * Update the timer from the possible pending state.
1085 */
1086static void hrtick_set(struct rq *rq)
1087{
1088 ktime_t time;
1089 int set, reset;
1090 unsigned long flags;
1091
1092 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1093
1094 spin_lock_irqsave(&rq->lock, flags);
1095 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1096 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1097 time = rq->hrtick_expire;
1098 clear_thread_flag(TIF_HRTICK_RESCHED);
1099 spin_unlock_irqrestore(&rq->lock, flags);
1100
1101 if (set) {
1102 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1103 if (reset && !hrtimer_active(&rq->hrtick_timer))
1104 resched_rq(rq);
1105 } else
1106 hrtick_clear(rq);
1107}
1108
1109/*
1110 * High-resolution timer tick.
1111 * Runs from hardirq context with interrupts disabled.
1112 */
1113static enum hrtimer_restart hrtick(struct hrtimer *timer)
1114{
1115 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1116
1117 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1118
1119 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001120 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001121 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1122 spin_unlock(&rq->lock);
1123
1124 return HRTIMER_NORESTART;
1125}
1126
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001127static void hotplug_hrtick_disable(int cpu)
1128{
1129 struct rq *rq = cpu_rq(cpu);
1130 unsigned long flags;
1131
1132 spin_lock_irqsave(&rq->lock, flags);
1133 rq->hrtick_flags = 0;
1134 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1135 spin_unlock_irqrestore(&rq->lock, flags);
1136
1137 hrtick_clear(rq);
1138}
1139
1140static void hotplug_hrtick_enable(int cpu)
1141{
1142 struct rq *rq = cpu_rq(cpu);
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&rq->lock, flags);
1146 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1147 spin_unlock_irqrestore(&rq->lock, flags);
1148}
1149
1150static int
1151hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1152{
1153 int cpu = (int)(long)hcpu;
1154
1155 switch (action) {
1156 case CPU_UP_CANCELED:
1157 case CPU_UP_CANCELED_FROZEN:
1158 case CPU_DOWN_PREPARE:
1159 case CPU_DOWN_PREPARE_FROZEN:
1160 case CPU_DEAD:
1161 case CPU_DEAD_FROZEN:
1162 hotplug_hrtick_disable(cpu);
1163 return NOTIFY_OK;
1164
1165 case CPU_UP_PREPARE:
1166 case CPU_UP_PREPARE_FROZEN:
1167 case CPU_DOWN_FAILED:
1168 case CPU_DOWN_FAILED_FROZEN:
1169 case CPU_ONLINE:
1170 case CPU_ONLINE_FROZEN:
1171 hotplug_hrtick_enable(cpu);
1172 return NOTIFY_OK;
1173 }
1174
1175 return NOTIFY_DONE;
1176}
1177
1178static void init_hrtick(void)
1179{
1180 hotcpu_notifier(hotplug_hrtick, 0);
1181}
1182
1183static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001184{
1185 rq->hrtick_flags = 0;
1186 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1187 rq->hrtick_timer.function = hrtick;
1188 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1189}
1190
1191void hrtick_resched(void)
1192{
1193 struct rq *rq;
1194 unsigned long flags;
1195
1196 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1197 return;
1198
1199 local_irq_save(flags);
1200 rq = cpu_rq(smp_processor_id());
1201 hrtick_set(rq);
1202 local_irq_restore(flags);
1203}
1204#else
1205static inline void hrtick_clear(struct rq *rq)
1206{
1207}
1208
1209static inline void hrtick_set(struct rq *rq)
1210{
1211}
1212
1213static inline void init_rq_hrtick(struct rq *rq)
1214{
1215}
1216
1217void hrtick_resched(void)
1218{
1219}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001220
1221static inline void init_hrtick(void)
1222{
1223}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001224#endif
1225
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001226/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001227 * resched_task - mark a task 'to be rescheduled now'.
1228 *
1229 * On UP this means the setting of the need_resched flag, on SMP it
1230 * might also involve a cross-CPU call to trigger the scheduler on
1231 * the target CPU.
1232 */
1233#ifdef CONFIG_SMP
1234
1235#ifndef tsk_is_polling
1236#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1237#endif
1238
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001239static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001240{
1241 int cpu;
1242
1243 assert_spin_locked(&task_rq(p)->lock);
1244
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001245 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001246 return;
1247
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001248 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001249
1250 cpu = task_cpu(p);
1251 if (cpu == smp_processor_id())
1252 return;
1253
1254 /* NEED_RESCHED must be visible before we test polling */
1255 smp_mb();
1256 if (!tsk_is_polling(p))
1257 smp_send_reschedule(cpu);
1258}
1259
1260static void resched_cpu(int cpu)
1261{
1262 struct rq *rq = cpu_rq(cpu);
1263 unsigned long flags;
1264
1265 if (!spin_trylock_irqsave(&rq->lock, flags))
1266 return;
1267 resched_task(cpu_curr(cpu));
1268 spin_unlock_irqrestore(&rq->lock, flags);
1269}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001270
1271#ifdef CONFIG_NO_HZ
1272/*
1273 * When add_timer_on() enqueues a timer into the timer wheel of an
1274 * idle CPU then this timer might expire before the next timer event
1275 * which is scheduled to wake up that CPU. In case of a completely
1276 * idle system the next event might even be infinite time into the
1277 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1278 * leaves the inner idle loop so the newly added timer is taken into
1279 * account when the CPU goes back to idle and evaluates the timer
1280 * wheel for the next timer event.
1281 */
1282void wake_up_idle_cpu(int cpu)
1283{
1284 struct rq *rq = cpu_rq(cpu);
1285
1286 if (cpu == smp_processor_id())
1287 return;
1288
1289 /*
1290 * This is safe, as this function is called with the timer
1291 * wheel base lock of (cpu) held. When the CPU is on the way
1292 * to idle and has not yet set rq->curr to idle then it will
1293 * be serialized on the timer wheel base lock and take the new
1294 * timer into account automatically.
1295 */
1296 if (rq->curr != rq->idle)
1297 return;
1298
1299 /*
1300 * We can set TIF_RESCHED on the idle task of the other CPU
1301 * lockless. The worst case is that the other CPU runs the
1302 * idle task through an additional NOOP schedule()
1303 */
1304 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1305
1306 /* NEED_RESCHED must be visible before we test polling */
1307 smp_mb();
1308 if (!tsk_is_polling(rq->idle))
1309 smp_send_reschedule(cpu);
1310}
1311#endif
1312
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001313#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001314static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001315{
1316 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001317 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001318}
1319#endif
1320
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001321#if BITS_PER_LONG == 32
1322# define WMULT_CONST (~0UL)
1323#else
1324# define WMULT_CONST (1UL << 32)
1325#endif
1326
1327#define WMULT_SHIFT 32
1328
Ingo Molnar194081e2007-08-09 11:16:51 +02001329/*
1330 * Shift right and round:
1331 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001332#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001333
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001334static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001335calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1336 struct load_weight *lw)
1337{
1338 u64 tmp;
1339
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001340 if (!lw->inv_weight)
1341 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001342
1343 tmp = (u64)delta_exec * weight;
1344 /*
1345 * Check whether we'd overflow the 64-bit multiplication:
1346 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001347 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001348 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001349 WMULT_SHIFT/2);
1350 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001351 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001352
Ingo Molnarecf691d2007-08-02 17:41:40 +02001353 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001354}
1355
Ingo Molnarf9305d42008-05-29 11:23:17 +02001356static inline unsigned long
1357calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1358{
1359 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1360}
1361
Ingo Molnar10919852007-10-15 17:00:04 +02001362static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001363{
1364 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001365 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001366}
1367
Ingo Molnar10919852007-10-15 17:00:04 +02001368static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001369{
1370 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001371 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001375 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1376 * of tasks with abnormal "nice" values across CPUs the contribution that
1377 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001378 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001379 * scaled version of the new time slice allocation that they receive on time
1380 * slice expiry etc.
1381 */
1382
Ingo Molnardd41f592007-07-09 18:51:59 +02001383#define WEIGHT_IDLEPRIO 2
1384#define WMULT_IDLEPRIO (1 << 31)
1385
1386/*
1387 * Nice levels are multiplicative, with a gentle 10% change for every
1388 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1389 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1390 * that remained on nice 0.
1391 *
1392 * The "10% effect" is relative and cumulative: from _any_ nice level,
1393 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001394 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1395 * If a task goes up by ~10% and another task goes down by ~10% then
1396 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001397 */
1398static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001399 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1400 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1401 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1402 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1403 /* 0 */ 1024, 820, 655, 526, 423,
1404 /* 5 */ 335, 272, 215, 172, 137,
1405 /* 10 */ 110, 87, 70, 56, 45,
1406 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001407};
1408
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001409/*
1410 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1411 *
1412 * In cases where the weight does not change often, we can use the
1413 * precalculated inverse to speed up arithmetics by turning divisions
1414 * into multiplications:
1415 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001416static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001417 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1418 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1419 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1420 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1421 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1422 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1423 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1424 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001425};
Peter Williams2dd73a42006-06-27 02:54:34 -07001426
Ingo Molnardd41f592007-07-09 18:51:59 +02001427static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1428
1429/*
1430 * runqueue iterator, to support SMP load-balancing between different
1431 * scheduling classes, without having to expose their internal data
1432 * structures to the load-balancing proper:
1433 */
1434struct rq_iterator {
1435 void *arg;
1436 struct task_struct *(*start)(void *);
1437 struct task_struct *(*next)(void *);
1438};
1439
Peter Williamse1d14842007-10-24 18:23:51 +02001440#ifdef CONFIG_SMP
1441static unsigned long
1442balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1443 unsigned long max_load_move, struct sched_domain *sd,
1444 enum cpu_idle_type idle, int *all_pinned,
1445 int *this_best_prio, struct rq_iterator *iterator);
1446
1447static int
1448iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1449 struct sched_domain *sd, enum cpu_idle_type idle,
1450 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001451#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001452
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001453#ifdef CONFIG_CGROUP_CPUACCT
1454static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1455#else
1456static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1457#endif
1458
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001459static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1460{
1461 update_load_add(&rq->load, load);
1462}
1463
1464static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1465{
1466 update_load_sub(&rq->load, load);
1467}
1468
Gregory Haskinse7693a32008-01-25 21:08:09 +01001469#ifdef CONFIG_SMP
1470static unsigned long source_load(int cpu, int type);
1471static unsigned long target_load(int cpu, int type);
1472static unsigned long cpu_avg_load_per_task(int cpu);
1473static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001474#else /* CONFIG_SMP */
1475
1476#ifdef CONFIG_FAIR_GROUP_SCHED
1477static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1478{
1479}
1480#endif
1481
Gregory Haskinse7693a32008-01-25 21:08:09 +01001482#endif /* CONFIG_SMP */
1483
Ingo Molnardd41f592007-07-09 18:51:59 +02001484#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001485#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001486#include "sched_fair.c"
1487#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001488#ifdef CONFIG_SCHED_DEBUG
1489# include "sched_debug.c"
1490#endif
1491
1492#define sched_class_highest (&rt_sched_class)
1493
Ingo Molnar6363ca52008-05-29 11:28:57 +02001494static inline void inc_load(struct rq *rq, const struct task_struct *p)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001495{
Ingo Molnar6363ca52008-05-29 11:28:57 +02001496 update_load_add(&rq->load, p->se.load.weight);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001497}
1498
Ingo Molnar6363ca52008-05-29 11:28:57 +02001499static inline void dec_load(struct rq *rq, const struct task_struct *p)
1500{
1501 update_load_sub(&rq->load, p->se.load.weight);
1502}
1503
1504static void inc_nr_running(struct task_struct *p, struct rq *rq)
1505{
1506 rq->nr_running++;
1507 inc_load(rq, p);
1508}
1509
1510static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001511{
1512 rq->nr_running--;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001513 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001514}
1515
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001516static void set_load_weight(struct task_struct *p)
1517{
1518 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001519 p->se.load.weight = prio_to_weight[0] * 2;
1520 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1521 return;
1522 }
1523
1524 /*
1525 * SCHED_IDLE tasks get minimal weight:
1526 */
1527 if (p->policy == SCHED_IDLE) {
1528 p->se.load.weight = WEIGHT_IDLEPRIO;
1529 p->se.load.inv_weight = WMULT_IDLEPRIO;
1530 return;
1531 }
1532
1533 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1534 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001535}
1536
Ingo Molnar8159f872007-08-09 11:16:49 +02001537static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001538{
1539 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001540 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001541 p->se.on_rq = 1;
1542}
1543
Ingo Molnar69be72c2007-08-09 11:16:49 +02001544static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001545{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001546 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001547 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001548}
1549
1550/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001551 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001552 */
Ingo Molnar14531182007-07-09 18:51:59 +02001553static inline int __normal_prio(struct task_struct *p)
1554{
Ingo Molnardd41f592007-07-09 18:51:59 +02001555 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001556}
1557
1558/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001559 * Calculate the expected normal priority: i.e. priority
1560 * without taking RT-inheritance into account. Might be
1561 * boosted by interactivity modifiers. Changes upon fork,
1562 * setprio syscalls, and whenever the interactivity
1563 * estimator recalculates.
1564 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001565static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001566{
1567 int prio;
1568
Ingo Molnare05606d2007-07-09 18:51:59 +02001569 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001570 prio = MAX_RT_PRIO-1 - p->rt_priority;
1571 else
1572 prio = __normal_prio(p);
1573 return prio;
1574}
1575
1576/*
1577 * Calculate the current priority, i.e. the priority
1578 * taken into account by the scheduler. This value might
1579 * be boosted by RT tasks, or might be boosted by
1580 * interactivity modifiers. Will be RT if the task got
1581 * RT-boosted. If not then it returns p->normal_prio.
1582 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001583static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001584{
1585 p->normal_prio = normal_prio(p);
1586 /*
1587 * If we are RT tasks or we were boosted to RT priority,
1588 * keep the priority unchanged. Otherwise, update priority
1589 * to the normal priority:
1590 */
1591 if (!rt_prio(p->prio))
1592 return p->normal_prio;
1593 return p->prio;
1594}
1595
1596/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001597 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001599static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001601 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001602 rq->nr_uninterruptible--;
1603
Ingo Molnar8159f872007-08-09 11:16:49 +02001604 enqueue_task(rq, p, wakeup);
Ingo Molnar6363ca52008-05-29 11:28:57 +02001605 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 * deactivate_task - remove a task from the runqueue.
1610 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001611static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001613 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001614 rq->nr_uninterruptible++;
1615
Ingo Molnar69be72c2007-08-09 11:16:49 +02001616 dequeue_task(rq, p, sleep);
Ingo Molnar6363ca52008-05-29 11:28:57 +02001617 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/**
1621 * task_curr - is this task currently executing on a CPU?
1622 * @p: the task in question.
1623 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001624inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 return cpu_curr(task_cpu(p)) == p;
1627}
1628
Peter Williams2dd73a42006-06-27 02:54:34 -07001629/* Used instead of source_load when we know the type == 0 */
1630unsigned long weighted_cpuload(const int cpu)
1631{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001632 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001633}
1634
1635static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1636{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001637 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001638#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001639 /*
1640 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1641 * successfuly executed on another CPU. We must ensure that updates of
1642 * per-task data have been completed by this moment.
1643 */
1644 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001645 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001646#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001647}
1648
Steven Rostedtcb469842008-01-25 21:08:22 +01001649static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1650 const struct sched_class *prev_class,
1651 int oldprio, int running)
1652{
1653 if (prev_class != p->sched_class) {
1654 if (prev_class->switched_from)
1655 prev_class->switched_from(rq, p, running);
1656 p->sched_class->switched_to(rq, p, running);
1657 } else
1658 p->sched_class->prio_changed(rq, p, oldprio, running);
1659}
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001662
Ingo Molnarcc367732007-10-15 17:00:18 +02001663/*
1664 * Is this task likely cache-hot:
1665 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001666static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001667task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1668{
1669 s64 delta;
1670
Ingo Molnarf540a602008-03-15 17:10:34 +01001671 /*
1672 * Buddy candidates are cache hot:
1673 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001674 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001675 return 1;
1676
Ingo Molnarcc367732007-10-15 17:00:18 +02001677 if (p->sched_class != &fair_sched_class)
1678 return 0;
1679
Ingo Molnar6bc16652007-10-15 17:00:18 +02001680 if (sysctl_sched_migration_cost == -1)
1681 return 1;
1682 if (sysctl_sched_migration_cost == 0)
1683 return 0;
1684
Ingo Molnarcc367732007-10-15 17:00:18 +02001685 delta = now - p->se.exec_start;
1686
1687 return delta < (s64)sysctl_sched_migration_cost;
1688}
1689
1690
Ingo Molnardd41f592007-07-09 18:51:59 +02001691void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001692{
Ingo Molnardd41f592007-07-09 18:51:59 +02001693 int old_cpu = task_cpu(p);
1694 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001695 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1696 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001697 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001698
1699 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001700
1701#ifdef CONFIG_SCHEDSTATS
1702 if (p->se.wait_start)
1703 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001704 if (p->se.sleep_start)
1705 p->se.sleep_start -= clock_offset;
1706 if (p->se.block_start)
1707 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001708 if (old_cpu != new_cpu) {
1709 schedstat_inc(p, se.nr_migrations);
1710 if (task_hot(p, old_rq->clock, NULL))
1711 schedstat_inc(p, se.nr_forced2_migrations);
1712 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001713#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001714 p->se.vruntime -= old_cfsrq->min_vruntime -
1715 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001716
1717 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001718}
1719
Ingo Molnar70b97a72006-07-03 00:25:42 -07001720struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Ingo Molnar36c8b582006-07-03 00:25:41 -07001723 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int dest_cpu;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001727};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729/*
1730 * The task's runqueue lock must be held.
1731 * Returns true if you have to wait for migration thread.
1732 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001733static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001734migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001736 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /*
1739 * If the task is not on a runqueue (and not running), then
1740 * it is sufficient to simply update the task's cpu field.
1741 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001742 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 set_task_cpu(p, dest_cpu);
1744 return 0;
1745 }
1746
1747 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 req->task = p;
1749 req->dest_cpu = dest_cpu;
1750 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 1;
1753}
1754
1755/*
1756 * wait_task_inactive - wait for a thread to unschedule.
1757 *
1758 * The caller must ensure that the task *will* unschedule sometime soon,
1759 * else this function might spin for a *long* time. This function can't
1760 * be called with interrupts off, or it may introduce deadlock with
1761 * smp_call_function() if an IPI is sent by the same process we are
1762 * waiting to become inactive.
1763 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001764void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001767 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001768 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Andi Kleen3a5c3592007-10-15 17:00:14 +02001770 for (;;) {
1771 /*
1772 * We do the initial early heuristics without holding
1773 * any task-queue locks at all. We'll only try to get
1774 * the runqueue lock when things look like they will
1775 * work out!
1776 */
1777 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001778
Andi Kleen3a5c3592007-10-15 17:00:14 +02001779 /*
1780 * If the task is actively running on another CPU
1781 * still, just relax and busy-wait without holding
1782 * any locks.
1783 *
1784 * NOTE! Since we don't hold any locks, it's not
1785 * even sure that "rq" stays as the right runqueue!
1786 * But we don't care, since "task_running()" will
1787 * return false if the runqueue has changed and p
1788 * is actually now running somewhere else!
1789 */
1790 while (task_running(rq, p))
1791 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001792
Andi Kleen3a5c3592007-10-15 17:00:14 +02001793 /*
1794 * Ok, time to look more closely! We need the rq
1795 * lock now, to be *sure*. If we're wrong, we'll
1796 * just go back and repeat.
1797 */
1798 rq = task_rq_lock(p, &flags);
1799 running = task_running(rq, p);
1800 on_rq = p->se.on_rq;
1801 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001802
Andi Kleen3a5c3592007-10-15 17:00:14 +02001803 /*
1804 * Was it really running after all now that we
1805 * checked with the proper locks actually held?
1806 *
1807 * Oops. Go back and try again..
1808 */
1809 if (unlikely(running)) {
1810 cpu_relax();
1811 continue;
1812 }
1813
1814 /*
1815 * It's not enough that it's not actively running,
1816 * it must be off the runqueue _entirely_, and not
1817 * preempted!
1818 *
1819 * So if it wa still runnable (but just not actively
1820 * running right now), it's preempted, and we should
1821 * yield - it could be a while.
1822 */
1823 if (unlikely(on_rq)) {
1824 schedule_timeout_uninterruptible(1);
1825 continue;
1826 }
1827
1828 /*
1829 * Ahh, all good. It wasn't running, and it wasn't
1830 * runnable, which means that it will never become
1831 * running in the future either. We're all done!
1832 */
1833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837/***
1838 * kick_process - kick a running thread to enter/exit the kernel
1839 * @p: the to-be-kicked thread
1840 *
1841 * Cause a process which is running on another CPU to enter
1842 * kernel-mode, without any delay. (to get signals handled.)
1843 *
1844 * NOTE: this function doesnt have to take the runqueue lock,
1845 * because all it wants to ensure is that the remote task enters
1846 * the kernel. If the IPI races and the task has been migrated
1847 * to another CPU then no harm is done and the purpose has been
1848 * achieved as well.
1849 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001850void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 int cpu;
1853
1854 preempt_disable();
1855 cpu = task_cpu(p);
1856 if ((cpu != smp_processor_id()) && task_curr(p))
1857 smp_send_reschedule(cpu);
1858 preempt_enable();
1859}
1860
1861/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001862 * Return a low guess at the load of a migration-source cpu weighted
1863 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 *
1865 * We want to under-estimate the load of migration sources, to
1866 * balance conservatively.
1867 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001868static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001869{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001870 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001871 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001872
Peter Williams2dd73a42006-06-27 02:54:34 -07001873 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001874 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001875
Ingo Molnardd41f592007-07-09 18:51:59 +02001876 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001880 * Return a high guess at the load of a migration-target cpu weighted
1881 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001883static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001884{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001885 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001886 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001887
Peter Williams2dd73a42006-06-27 02:54:34 -07001888 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001889 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001890
Ingo Molnardd41f592007-07-09 18:51:59 +02001891 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001892}
1893
1894/*
1895 * Return the average load per task on the cpu's run queue
1896 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001897static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001898{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001899 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001900 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001901 unsigned long n = rq->nr_running;
1902
Ingo Molnardd41f592007-07-09 18:51:59 +02001903 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Nick Piggin147cbb42005-06-25 14:57:19 -07001906/*
1907 * find_idlest_group finds and returns the least busy CPU group within the
1908 * domain.
1909 */
1910static struct sched_group *
1911find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1912{
1913 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1914 unsigned long min_load = ULONG_MAX, this_load = 0;
1915 int load_idx = sd->forkexec_idx;
1916 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1917
1918 do {
1919 unsigned long load, avg_load;
1920 int local_group;
1921 int i;
1922
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001923 /* Skip over this group if it has no CPUs allowed */
1924 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001925 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001926
Nick Piggin147cbb42005-06-25 14:57:19 -07001927 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001928
1929 /* Tally up the load of all CPUs in the group */
1930 avg_load = 0;
1931
1932 for_each_cpu_mask(i, group->cpumask) {
1933 /* Bias balancing toward cpus of our domain */
1934 if (local_group)
1935 load = source_load(i, load_idx);
1936 else
1937 load = target_load(i, load_idx);
1938
1939 avg_load += load;
1940 }
1941
1942 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001943 avg_load = sg_div_cpu_power(group,
1944 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001945
1946 if (local_group) {
1947 this_load = avg_load;
1948 this = group;
1949 } else if (avg_load < min_load) {
1950 min_load = avg_load;
1951 idlest = group;
1952 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001953 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001954
1955 if (!idlest || 100*this_load < imbalance*min_load)
1956 return NULL;
1957 return idlest;
1958}
1959
1960/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001961 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001962 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001963static int
Mike Travis7c16ec52008-04-04 18:11:11 -07001964find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
1965 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07001966{
1967 unsigned long load, min_load = ULONG_MAX;
1968 int idlest = -1;
1969 int i;
1970
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001971 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07001972 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001973
Mike Travis7c16ec52008-04-04 18:11:11 -07001974 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001975 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001976
1977 if (load < min_load || (load == min_load && i == this_cpu)) {
1978 min_load = load;
1979 idlest = i;
1980 }
1981 }
1982
1983 return idlest;
1984}
1985
Nick Piggin476d1392005-06-25 14:57:29 -07001986/*
1987 * sched_balance_self: balance the current task (running on cpu) in domains
1988 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1989 * SD_BALANCE_EXEC.
1990 *
1991 * Balance, ie. select the least loaded group.
1992 *
1993 * Returns the target CPU number, or the same CPU if no balancing is needed.
1994 *
1995 * preempt must be disabled.
1996 */
1997static int sched_balance_self(int cpu, int flag)
1998{
1999 struct task_struct *t = current;
2000 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002001
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002002 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002003 /*
2004 * If power savings logic is enabled for a domain, stop there.
2005 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002006 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2007 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002008 if (tmp->flags & flag)
2009 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002010 }
Nick Piggin476d1392005-06-25 14:57:29 -07002011
2012 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002013 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002014 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002015 int new_cpu, weight;
2016
2017 if (!(sd->flags & flag)) {
2018 sd = sd->child;
2019 continue;
2020 }
Nick Piggin476d1392005-06-25 14:57:29 -07002021
2022 span = sd->span;
2023 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002024 if (!group) {
2025 sd = sd->child;
2026 continue;
2027 }
Nick Piggin476d1392005-06-25 14:57:29 -07002028
Mike Travis7c16ec52008-04-04 18:11:11 -07002029 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002030 if (new_cpu == -1 || new_cpu == cpu) {
2031 /* Now try balancing at a lower domain level of cpu */
2032 sd = sd->child;
2033 continue;
2034 }
Nick Piggin476d1392005-06-25 14:57:29 -07002035
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002036 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002037 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002038 sd = NULL;
2039 weight = cpus_weight(span);
2040 for_each_domain(cpu, tmp) {
2041 if (weight <= cpus_weight(tmp->span))
2042 break;
2043 if (tmp->flags & flag)
2044 sd = tmp;
2045 }
2046 /* while loop will break here if sd == NULL */
2047 }
2048
2049 return cpu;
2050}
2051
2052#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/***
2055 * try_to_wake_up - wake up a thread
2056 * @p: the to-be-woken-up thread
2057 * @state: the mask of task states that can be woken
2058 * @sync: do a synchronous wakeup?
2059 *
2060 * Put it on the run-queue if it's not already there. The "current"
2061 * thread is always on the run-queue (except when the actual
2062 * re-schedule is in progress), and as such you're allowed to do
2063 * the simpler "current->state = TASK_RUNNING" to mark yourself
2064 * runnable without the overhead of this.
2065 *
2066 * returns failure only if the task is already active.
2067 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002068static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Ingo Molnarcc367732007-10-15 17:00:18 +02002070 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 unsigned long flags;
2072 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002073 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ingo Molnarb85d0662008-03-16 20:03:22 +01002075 if (!sched_feat(SYNC_WAKEUPS))
2076 sync = 0;
2077
Linus Torvalds04e2f172008-02-23 18:05:03 -08002078 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 rq = task_rq_lock(p, &flags);
2080 old_state = p->state;
2081 if (!(old_state & state))
2082 goto out;
2083
Ingo Molnardd41f592007-07-09 18:51:59 +02002084 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto out_running;
2086
2087 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002088 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 this_cpu = smp_processor_id();
2090
2091#ifdef CONFIG_SMP
2092 if (unlikely(task_running(rq, p)))
2093 goto out_activate;
2094
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002095 cpu = p->sched_class->select_task_rq(p, sync);
2096 if (cpu != orig_cpu) {
2097 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 task_rq_unlock(rq, &flags);
2099 /* might preempt at this point */
2100 rq = task_rq_lock(p, &flags);
2101 old_state = p->state;
2102 if (!(old_state & state))
2103 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002104 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto out_running;
2106
2107 this_cpu = smp_processor_id();
2108 cpu = task_cpu(p);
2109 }
2110
Gregory Haskinse7693a32008-01-25 21:08:09 +01002111#ifdef CONFIG_SCHEDSTATS
2112 schedstat_inc(rq, ttwu_count);
2113 if (cpu == this_cpu)
2114 schedstat_inc(rq, ttwu_local);
2115 else {
2116 struct sched_domain *sd;
2117 for_each_domain(this_cpu, sd) {
2118 if (cpu_isset(cpu, sd->span)) {
2119 schedstat_inc(sd, ttwu_wake_remote);
2120 break;
2121 }
2122 }
2123 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002124#endif
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126out_activate:
2127#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002128 schedstat_inc(p, se.nr_wakeups);
2129 if (sync)
2130 schedstat_inc(p, se.nr_wakeups_sync);
2131 if (orig_cpu != cpu)
2132 schedstat_inc(p, se.nr_wakeups_migrate);
2133 if (cpu == this_cpu)
2134 schedstat_inc(p, se.nr_wakeups_local);
2135 else
2136 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002137 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002138 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 success = 1;
2140
2141out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002142 check_preempt_curr(rq, p);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002145#ifdef CONFIG_SMP
2146 if (p->sched_class->task_wake_up)
2147 p->sched_class->task_wake_up(rq, p);
2148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149out:
2150 task_rq_unlock(rq, &flags);
2151
2152 return success;
2153}
2154
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002155int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002157 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159EXPORT_SYMBOL(wake_up_process);
2160
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002161int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 return try_to_wake_up(p, state, 0);
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/*
2167 * Perform scheduler related setup for a newly forked process p.
2168 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002169 *
2170 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002172static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Ingo Molnardd41f592007-07-09 18:51:59 +02002174 p->se.exec_start = 0;
2175 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002176 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002177 p->se.last_wakeup = 0;
2178 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002179
2180#ifdef CONFIG_SCHEDSTATS
2181 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002182 p->se.sum_sleep_runtime = 0;
2183 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002184 p->se.block_start = 0;
2185 p->se.sleep_max = 0;
2186 p->se.block_max = 0;
2187 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002188 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002189 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002190#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002191
Peter Zijlstrafa717062008-01-25 21:08:27 +01002192 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002193 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002194 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002195
Avi Kivitye107be32007-07-26 13:40:43 +02002196#ifdef CONFIG_PREEMPT_NOTIFIERS
2197 INIT_HLIST_HEAD(&p->preempt_notifiers);
2198#endif
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /*
2201 * We mark the process as running here, but have not actually
2202 * inserted it onto the runqueue yet. This guarantees that
2203 * nobody will actually run it, and a signal or other external
2204 * event cannot wake it up and insert it on the runqueue either.
2205 */
2206 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002207}
2208
2209/*
2210 * fork()/clone()-time setup:
2211 */
2212void sched_fork(struct task_struct *p, int clone_flags)
2213{
2214 int cpu = get_cpu();
2215
2216 __sched_fork(p);
2217
2218#ifdef CONFIG_SMP
2219 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2220#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002221 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002222
2223 /*
2224 * Make sure we do not leak PI boosting priority to the child:
2225 */
2226 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002227 if (!rt_prio(p->prio))
2228 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002229
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002230#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002231 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002232 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002234#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002235 p->oncpu = 0;
2236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002238 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002239 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002241 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244/*
2245 * wake_up_new_task - wake up a newly created task for the first time.
2246 *
2247 * This function will do some initial scheduler statistics housekeeping
2248 * that must be done for every newly created context, then puts the task
2249 * on the runqueue and wakes it.
2250 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002251void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002254 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002258 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 p->prio = effective_prio(p);
2261
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002262 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002263 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002266 * Let the scheduling class do new task startup
2267 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002269 p->sched_class->task_new(rq, p);
Ingo Molnar6363ca52008-05-29 11:28:57 +02002270 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002272 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002273#ifdef CONFIG_SMP
2274 if (p->sched_class->task_wake_up)
2275 p->sched_class->task_wake_up(rq, p);
2276#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002277 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Avi Kivitye107be32007-07-26 13:40:43 +02002280#ifdef CONFIG_PREEMPT_NOTIFIERS
2281
2282/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002283 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2284 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002285 */
2286void preempt_notifier_register(struct preempt_notifier *notifier)
2287{
2288 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2289}
2290EXPORT_SYMBOL_GPL(preempt_notifier_register);
2291
2292/**
2293 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002294 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002295 *
2296 * This is safe to call from within a preemption notifier.
2297 */
2298void preempt_notifier_unregister(struct preempt_notifier *notifier)
2299{
2300 hlist_del(&notifier->link);
2301}
2302EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2303
2304static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2305{
2306 struct preempt_notifier *notifier;
2307 struct hlist_node *node;
2308
2309 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2310 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2311}
2312
2313static void
2314fire_sched_out_preempt_notifiers(struct task_struct *curr,
2315 struct task_struct *next)
2316{
2317 struct preempt_notifier *notifier;
2318 struct hlist_node *node;
2319
2320 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2321 notifier->ops->sched_out(notifier, next);
2322}
2323
2324#else
2325
2326static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2327{
2328}
2329
2330static void
2331fire_sched_out_preempt_notifiers(struct task_struct *curr,
2332 struct task_struct *next)
2333{
2334}
2335
2336#endif
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002339 * prepare_task_switch - prepare to switch tasks
2340 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002341 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002342 * @next: the task we are going to switch to.
2343 *
2344 * This is called with the rq lock held and interrupts off. It must
2345 * be paired with a subsequent finish_task_switch after the context
2346 * switch.
2347 *
2348 * prepare_task_switch sets up locking and calls architecture specific
2349 * hooks.
2350 */
Avi Kivitye107be32007-07-26 13:40:43 +02002351static inline void
2352prepare_task_switch(struct rq *rq, struct task_struct *prev,
2353 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002354{
Avi Kivitye107be32007-07-26 13:40:43 +02002355 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002356 prepare_lock_switch(rq, next);
2357 prepare_arch_switch(next);
2358}
2359
2360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002362 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 * @prev: the thread we just switched away from.
2364 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002365 * finish_task_switch must be called after the context switch, paired
2366 * with a prepare_task_switch call before the context switch.
2367 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2368 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 *
2370 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002371 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 * with the lock held can cause deadlocks; see schedule() for
2373 * details.)
2374 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002375static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 __releases(rq->lock)
2377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002379 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 rq->prev_mm = NULL;
2382
2383 /*
2384 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002385 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002386 * schedule one last time. The schedule call will never return, and
2387 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002388 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * still held, otherwise prev could be scheduled on another cpu, die
2390 * there before we look at prev->state, and then the reference would
2391 * be dropped twice.
2392 * Manfred Spraul <manfred@colorfullife.com>
2393 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002394 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002395 finish_arch_switch(prev);
2396 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002397#ifdef CONFIG_SMP
2398 if (current->sched_class->post_schedule)
2399 current->sched_class->post_schedule(rq);
2400#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002401
Avi Kivitye107be32007-07-26 13:40:43 +02002402 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (mm)
2404 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002405 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002406 /*
2407 * Remove function-return probe instances associated with this
2408 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002409 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002410 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415/**
2416 * schedule_tail - first thing a freshly forked thread must call.
2417 * @prev: the thread we just switched away from.
2418 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002419asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 __releases(rq->lock)
2421{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002422 struct rq *rq = this_rq();
2423
Nick Piggin4866cde2005-06-25 14:57:23 -07002424 finish_task_switch(rq, prev);
2425#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2426 /* In this case, finish_task_switch does not reenable preemption */
2427 preempt_enable();
2428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002430 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433/*
2434 * context_switch - switch to the new MM and the new
2435 * thread's register state.
2436 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002437static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002438context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002439 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Ingo Molnardd41f592007-07-09 18:51:59 +02002441 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Avi Kivitye107be32007-07-26 13:40:43 +02002443 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002444 mm = next->mm;
2445 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002446 /*
2447 * For paravirt, this is coupled with an exit in switch_to to
2448 * combine the page table reload and the switch backend into
2449 * one hypercall.
2450 */
2451 arch_enter_lazy_cpu_mode();
2452
Ingo Molnardd41f592007-07-09 18:51:59 +02002453 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 next->active_mm = oldmm;
2455 atomic_inc(&oldmm->mm_count);
2456 enter_lazy_tlb(oldmm, next);
2457 } else
2458 switch_mm(oldmm, mm, next);
2459
Ingo Molnardd41f592007-07-09 18:51:59 +02002460 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 rq->prev_mm = oldmm;
2463 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002464 /*
2465 * Since the runqueue lock will be released by the next
2466 * task (which is an invalid locking op but in the case
2467 * of the scheduler it's an obvious special-case), so we
2468 * do an early lockdep release here:
2469 */
2470#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002471 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 /* Here we just switch the register state and the stack. */
2475 switch_to(prev, next, prev);
2476
Ingo Molnardd41f592007-07-09 18:51:59 +02002477 barrier();
2478 /*
2479 * this_rq must be evaluated again because prev may have moved
2480 * CPUs since it called schedule(), thus the 'rq' on its stack
2481 * frame will be invalid.
2482 */
2483 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
2486/*
2487 * nr_running, nr_uninterruptible and nr_context_switches:
2488 *
2489 * externally visible scheduler statistics: current number of runnable
2490 * threads, current number of uninterruptible-sleeping threads, total
2491 * number of context switches performed since bootup.
2492 */
2493unsigned long nr_running(void)
2494{
2495 unsigned long i, sum = 0;
2496
2497 for_each_online_cpu(i)
2498 sum += cpu_rq(i)->nr_running;
2499
2500 return sum;
2501}
2502
2503unsigned long nr_uninterruptible(void)
2504{
2505 unsigned long i, sum = 0;
2506
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002507 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 sum += cpu_rq(i)->nr_uninterruptible;
2509
2510 /*
2511 * Since we read the counters lockless, it might be slightly
2512 * inaccurate. Do not allow it to go below zero though:
2513 */
2514 if (unlikely((long)sum < 0))
2515 sum = 0;
2516
2517 return sum;
2518}
2519
2520unsigned long long nr_context_switches(void)
2521{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002522 int i;
2523 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002525 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 sum += cpu_rq(i)->nr_switches;
2527
2528 return sum;
2529}
2530
2531unsigned long nr_iowait(void)
2532{
2533 unsigned long i, sum = 0;
2534
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002535 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2537
2538 return sum;
2539}
2540
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002541unsigned long nr_active(void)
2542{
2543 unsigned long i, running = 0, uninterruptible = 0;
2544
2545 for_each_online_cpu(i) {
2546 running += cpu_rq(i)->nr_running;
2547 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2548 }
2549
2550 if (unlikely((long)uninterruptible < 0))
2551 uninterruptible = 0;
2552
2553 return running + uninterruptible;
2554}
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002557 * Update rq->cpu_load[] statistics. This function is usually called every
2558 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002559 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002560static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002561{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002562 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002563 int i, scale;
2564
2565 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002566
2567 /* Update our load: */
2568 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2569 unsigned long old_load, new_load;
2570
2571 /* scale is effectively 1 << i now, and >> i divides by scale */
2572
2573 old_load = this_rq->cpu_load[i];
2574 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002575 /*
2576 * Round up the averaging division if load is increasing. This
2577 * prevents us from getting stuck on 9 if the load is 10, for
2578 * example.
2579 */
2580 if (new_load > old_load)
2581 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002582 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2583 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002584}
2585
Ingo Molnardd41f592007-07-09 18:51:59 +02002586#ifdef CONFIG_SMP
2587
Ingo Molnar48f24c42006-07-03 00:25:40 -07002588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 * double_rq_lock - safely lock two runqueues
2590 *
2591 * Note this does not disable interrupts like task_rq_lock,
2592 * you need to do so manually before calling.
2593 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002594static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 __acquires(rq1->lock)
2596 __acquires(rq2->lock)
2597{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002598 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (rq1 == rq2) {
2600 spin_lock(&rq1->lock);
2601 __acquire(rq2->lock); /* Fake it out ;) */
2602 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002603 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 spin_lock(&rq1->lock);
2605 spin_lock(&rq2->lock);
2606 } else {
2607 spin_lock(&rq2->lock);
2608 spin_lock(&rq1->lock);
2609 }
2610 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002611 update_rq_clock(rq1);
2612 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
2615/*
2616 * double_rq_unlock - safely unlock two runqueues
2617 *
2618 * Note this does not restore interrupts like task_rq_unlock,
2619 * you need to do so manually after calling.
2620 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002621static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 __releases(rq1->lock)
2623 __releases(rq2->lock)
2624{
2625 spin_unlock(&rq1->lock);
2626 if (rq1 != rq2)
2627 spin_unlock(&rq2->lock);
2628 else
2629 __release(rq2->lock);
2630}
2631
2632/*
2633 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2634 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002635static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 __releases(this_rq->lock)
2637 __acquires(busiest->lock)
2638 __acquires(this_rq->lock)
2639{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002640 int ret = 0;
2641
Kirill Korotaev054b9102006-12-10 02:20:11 -08002642 if (unlikely(!irqs_disabled())) {
2643 /* printk() doesn't work good under rq->lock */
2644 spin_unlock(&this_rq->lock);
2645 BUG_ON(1);
2646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002648 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 spin_unlock(&this_rq->lock);
2650 spin_lock(&busiest->lock);
2651 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002652 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 } else
2654 spin_lock(&busiest->lock);
2655 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * If dest_cpu is allowed for this process, migrate the task to it.
2661 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002662 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 * the cpu_allowed mask is restored.
2664 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002665static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002667 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002669 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 rq = task_rq_lock(p, &flags);
2672 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2673 || unlikely(cpu_is_offline(dest_cpu)))
2674 goto out;
2675
2676 /* force the process onto the specified CPU */
2677 if (migrate_task(p, dest_cpu, &req)) {
2678 /* Need to wait for migration thread (might exit: take ref). */
2679 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 get_task_struct(mt);
2682 task_rq_unlock(rq, &flags);
2683 wake_up_process(mt);
2684 put_task_struct(mt);
2685 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return;
2688 }
2689out:
2690 task_rq_unlock(rq, &flags);
2691}
2692
2693/*
Nick Piggin476d1392005-06-25 14:57:29 -07002694 * sched_exec - execve() is a valuable balancing opportunity, because at
2695 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 */
2697void sched_exec(void)
2698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002700 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002702 if (new_cpu != this_cpu)
2703 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
2706/*
2707 * pull_task - move a task from a remote runqueue to the local runqueue.
2708 * Both runqueues must be locked.
2709 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002710static void pull_task(struct rq *src_rq, struct task_struct *p,
2711 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002713 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002715 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /*
2717 * Note that idle threads have a prio of MAX_PRIO, for this test
2718 * to be always true for them.
2719 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002720 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/*
2724 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2725 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002726static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002727int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002728 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002729 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 /*
2732 * We do not migrate tasks that are:
2733 * 1) running (obviously), or
2734 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2735 * 3) are cache-hot on their current CPU.
2736 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002737 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2738 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002740 }
Nick Piggin81026792005-06-25 14:57:07 -07002741 *all_pinned = 0;
2742
Ingo Molnarcc367732007-10-15 17:00:18 +02002743 if (task_running(rq, p)) {
2744 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002745 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Ingo Molnarda84d962007-10-15 17:00:18 +02002748 /*
2749 * Aggressive migration if:
2750 * 1) task is cache cold, or
2751 * 2) too many balance attempts have failed.
2752 */
2753
Ingo Molnar6bc16652007-10-15 17:00:18 +02002754 if (!task_hot(p, rq->clock, sd) ||
2755 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002756#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002757 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002758 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002759 schedstat_inc(p, se.nr_forced_migrations);
2760 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002761#endif
2762 return 1;
2763 }
2764
Ingo Molnarcc367732007-10-15 17:00:18 +02002765 if (task_hot(p, rq->clock, sd)) {
2766 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002767 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return 1;
2770}
2771
Peter Williamse1d14842007-10-24 18:23:51 +02002772static unsigned long
2773balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2774 unsigned long max_load_move, struct sched_domain *sd,
2775 enum cpu_idle_type idle, int *all_pinned,
2776 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002777{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002778 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002779 struct task_struct *p;
2780 long rem_load_move = max_load_move;
2781
Peter Williamse1d14842007-10-24 18:23:51 +02002782 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002783 goto out;
2784
2785 pinned = 1;
2786
2787 /*
2788 * Start the load-balancing iterator:
2789 */
2790 p = iterator->start(iterator->arg);
2791next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002792 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002793 goto out;
2794 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002795 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002796 * skip a task if it will be the highest priority task (i.e. smallest
2797 * prio value) on its new queue regardless of its load weight
2798 */
2799 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2800 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002801 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002802 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002803 p = iterator->next(iterator->arg);
2804 goto next;
2805 }
2806
2807 pull_task(busiest, p, this_rq, this_cpu);
2808 pulled++;
2809 rem_load_move -= p->se.load.weight;
2810
2811 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002812 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002813 */
Peter Williamse1d14842007-10-24 18:23:51 +02002814 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002815 if (p->prio < *this_best_prio)
2816 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002817 p = iterator->next(iterator->arg);
2818 goto next;
2819 }
2820out:
2821 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002822 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002823 * so we can safely collect pull_task() stats here rather than
2824 * inside pull_task().
2825 */
2826 schedstat_add(sd, lb_gained[idle], pulled);
2827
2828 if (all_pinned)
2829 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002830
2831 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002832}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834/*
Peter Williams43010652007-08-09 11:16:46 +02002835 * move_tasks tries to move up to max_load_move weighted load from busiest to
2836 * this_rq, as part of a balancing operation within domain "sd".
2837 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *
2839 * Called with both runqueues locked.
2840 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002841static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002842 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002843 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002844 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002846 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002847 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002848 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Ingo Molnardd41f592007-07-09 18:51:59 +02002850 do {
Peter Williams43010652007-08-09 11:16:46 +02002851 total_load_moved +=
2852 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002853 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002854 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002855 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002856 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Peter Williams43010652007-08-09 11:16:46 +02002858 return total_load_moved > 0;
2859}
2860
Peter Williamse1d14842007-10-24 18:23:51 +02002861static int
2862iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2863 struct sched_domain *sd, enum cpu_idle_type idle,
2864 struct rq_iterator *iterator)
2865{
2866 struct task_struct *p = iterator->start(iterator->arg);
2867 int pinned = 0;
2868
2869 while (p) {
2870 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2871 pull_task(busiest, p, this_rq, this_cpu);
2872 /*
2873 * Right now, this is only the second place pull_task()
2874 * is called, so we can safely collect pull_task()
2875 * stats here rather than inside pull_task().
2876 */
2877 schedstat_inc(sd, lb_gained[idle]);
2878
2879 return 1;
2880 }
2881 p = iterator->next(iterator->arg);
2882 }
2883
2884 return 0;
2885}
2886
Peter Williams43010652007-08-09 11:16:46 +02002887/*
2888 * move_one_task tries to move exactly one task from busiest to this_rq, as
2889 * part of active balancing operations within "domain".
2890 * Returns 1 if successful and 0 otherwise.
2891 *
2892 * Called with both runqueues locked.
2893 */
2894static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2895 struct sched_domain *sd, enum cpu_idle_type idle)
2896{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002897 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002898
2899 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002900 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002901 return 1;
2902
2903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
2906/*
2907 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002908 * domain. It calculates and returns the amount of weighted load which
2909 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 */
2911static struct sched_group *
2912find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002913 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07002914 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2917 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002918 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002919 unsigned long busiest_load_per_task, busiest_nr_running;
2920 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002921 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002922#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2923 int power_savings_balance = 1;
2924 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2925 unsigned long min_nr_running = ULONG_MAX;
2926 struct sched_group *group_min = NULL, *group_leader = NULL;
2927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002930 busiest_load_per_task = busiest_nr_running = 0;
2931 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002932 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002933 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002934 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002935 load_idx = sd->newidle_idx;
2936 else
2937 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002940 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 int local_group;
2942 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002943 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002944 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002945 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 local_group = cpu_isset(this_cpu, group->cpumask);
2948
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002949 if (local_group)
2950 balance_cpu = first_cpu(group->cpumask);
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002953 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002954 max_cpu_load = 0;
2955 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002958 struct rq *rq;
2959
2960 if (!cpu_isset(i, *cpus))
2961 continue;
2962
2963 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002964
Suresh Siddha9439aab2007-07-19 21:28:35 +02002965 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002966 *sd_idle = 0;
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002969 if (local_group) {
2970 if (idle_cpu(i) && !first_idle_cpu) {
2971 first_idle_cpu = 1;
2972 balance_cpu = i;
2973 }
2974
Nick Piggina2000572006-02-10 01:51:02 -08002975 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002976 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002977 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002978 if (load > max_cpu_load)
2979 max_cpu_load = load;
2980 if (min_cpu_load > load)
2981 min_cpu_load = load;
2982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002985 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002986 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002989 /*
2990 * First idle cpu or the first cpu(busiest) in this sched group
2991 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002992 * domains. In the newly idle case, we will allow all the cpu's
2993 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002994 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002995 if (idle != CPU_NEWLY_IDLE && local_group &&
2996 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002997 *balance = 0;
2998 goto ret;
2999 }
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003002 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003005 avg_load = sg_div_cpu_power(group,
3006 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Ken Chen908a7c12007-10-17 16:55:11 +02003008 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3009 __group_imb = 1;
3010
Eric Dumazet5517d862007-05-08 00:32:57 -07003011 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (local_group) {
3014 this_load = avg_load;
3015 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003016 this_nr_running = sum_nr_running;
3017 this_load_per_task = sum_weighted_load;
3018 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003019 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 max_load = avg_load;
3021 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003022 busiest_nr_running = sum_nr_running;
3023 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003024 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003026
3027#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3028 /*
3029 * Busy processors will not participate in power savings
3030 * balance.
3031 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003032 if (idle == CPU_NOT_IDLE ||
3033 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3034 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003035
3036 /*
3037 * If the local group is idle or completely loaded
3038 * no need to do power savings balance at this domain
3039 */
3040 if (local_group && (this_nr_running >= group_capacity ||
3041 !this_nr_running))
3042 power_savings_balance = 0;
3043
Ingo Molnardd41f592007-07-09 18:51:59 +02003044 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003045 * If a group is already running at full capacity or idle,
3046 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003047 */
3048 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003049 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003050 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003051
Ingo Molnardd41f592007-07-09 18:51:59 +02003052 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003053 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003054 * This is the group from where we need to pick up the load
3055 * for saving power
3056 */
3057 if ((sum_nr_running < min_nr_running) ||
3058 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003059 first_cpu(group->cpumask) <
3060 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003061 group_min = group;
3062 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003063 min_load_per_task = sum_weighted_load /
3064 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003065 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003066
Ingo Molnardd41f592007-07-09 18:51:59 +02003067 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003068 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003069 * capacity but still has some space to pick up some load
3070 * from other group and save more power
3071 */
3072 if (sum_nr_running <= group_capacity - 1) {
3073 if (sum_nr_running > leader_nr_running ||
3074 (sum_nr_running == leader_nr_running &&
3075 first_cpu(group->cpumask) >
3076 first_cpu(group_leader->cpumask))) {
3077 group_leader = group;
3078 leader_nr_running = sum_nr_running;
3079 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003080 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003081group_next:
3082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 group = group->next;
3084 } while (group != sd->groups);
3085
Peter Williams2dd73a42006-06-27 02:54:34 -07003086 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 goto out_balanced;
3088
3089 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3090
3091 if (this_load >= avg_load ||
3092 100*max_load <= sd->imbalance_pct*this_load)
3093 goto out_balanced;
3094
Peter Williams2dd73a42006-06-27 02:54:34 -07003095 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003096 if (group_imb)
3097 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /*
3100 * We're trying to get all the cpus to the average_load, so we don't
3101 * want to push ourselves above the average load, nor do we wish to
3102 * reduce the max loaded cpu below the average load, as either of these
3103 * actions would just result in more rebalancing later, and ping-pong
3104 * tasks around. Thus we look for the minimum possible imbalance.
3105 * Negative imbalances (*we* are more loaded than anyone else) will
3106 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003107 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * appear as very large values with unsigned longs.
3109 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003110 if (max_load <= busiest_load_per_task)
3111 goto out_balanced;
3112
3113 /*
3114 * In the presence of smp nice balancing, certain scenarios can have
3115 * max load less than avg load(as we skip the groups at or below
3116 * its cpu_power, while calculating max_load..)
3117 */
3118 if (max_load < avg_load) {
3119 *imbalance = 0;
3120 goto small_imbalance;
3121 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003122
3123 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003124 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003127 *imbalance = min(max_pull * busiest->__cpu_power,
3128 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 / SCHED_LOAD_SCALE;
3130
Peter Williams2dd73a42006-06-27 02:54:34 -07003131 /*
3132 * if *imbalance is less than the average load per runnable task
3133 * there is no gaurantee that any tasks will be moved so we'll have
3134 * a think about bumping its value to force at least one task to be
3135 * moved
3136 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003137 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003138 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003139 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Peter Williams2dd73a42006-06-27 02:54:34 -07003141small_imbalance:
3142 pwr_move = pwr_now = 0;
3143 imbn = 2;
3144 if (this_nr_running) {
3145 this_load_per_task /= this_nr_running;
3146 if (busiest_load_per_task > this_load_per_task)
3147 imbn = 1;
3148 } else
3149 this_load_per_task = SCHED_LOAD_SCALE;
3150
Ingo Molnardd41f592007-07-09 18:51:59 +02003151 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3152 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003153 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return busiest;
3155 }
3156
3157 /*
3158 * OK, we don't have enough imbalance to justify moving tasks,
3159 * however we may be able to increase total CPU power used by
3160 * moving them.
3161 */
3162
Eric Dumazet5517d862007-05-08 00:32:57 -07003163 pwr_now += busiest->__cpu_power *
3164 min(busiest_load_per_task, max_load);
3165 pwr_now += this->__cpu_power *
3166 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 pwr_now /= SCHED_LOAD_SCALE;
3168
3169 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003170 tmp = sg_div_cpu_power(busiest,
3171 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003173 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003174 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003177 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003178 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003179 tmp = sg_div_cpu_power(this,
3180 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003182 tmp = sg_div_cpu_power(this,
3183 busiest_load_per_task * SCHED_LOAD_SCALE);
3184 pwr_move += this->__cpu_power *
3185 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 pwr_move /= SCHED_LOAD_SCALE;
3187
3188 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003189 if (pwr_move > pwr_now)
3190 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return busiest;
3194
3195out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003196#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003197 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003198 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003200 if (this == group_leader && group_leader != group_min) {
3201 *imbalance = min_load_per_task;
3202 return group_min;
3203 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003204#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003205ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 *imbalance = 0;
3207 return NULL;
3208}
3209
3210/*
3211 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3212 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003213static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003214find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003215 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003217 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003218 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 int i;
3220
3221 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003222 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003223
3224 if (!cpu_isset(i, *cpus))
3225 continue;
3226
Ingo Molnar48f24c42006-07-03 00:25:40 -07003227 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003228 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Ingo Molnardd41f592007-07-09 18:51:59 +02003230 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003231 continue;
3232
Ingo Molnardd41f592007-07-09 18:51:59 +02003233 if (wl > max_load) {
3234 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003235 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
3237 }
3238
3239 return busiest;
3240}
3241
3242/*
Nick Piggin77391d72005-06-25 14:57:30 -07003243 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3244 * so long as it is large enough.
3245 */
3246#define MAX_PINNED_INTERVAL 512
3247
3248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3250 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003252static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003253 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003254 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Peter Williams43010652007-08-09 11:16:46 +02003256 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003259 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003260 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003261
Mike Travis7c16ec52008-04-04 18:11:11 -07003262 cpus_setall(*cpus);
3263
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003264 /*
3265 * When power savings policy is enabled for the parent domain, idle
3266 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003267 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003268 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003269 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003270 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003271 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003272 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Ingo Molnar2d723762007-10-15 17:00:12 +02003274 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003276redo:
3277 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003278 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003279
Chen, Kenneth W06066712006-12-10 02:20:35 -08003280 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003281 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 if (!group) {
3284 schedstat_inc(sd, lb_nobusyg[idle]);
3285 goto out_balanced;
3286 }
3287
Mike Travis7c16ec52008-04-04 18:11:11 -07003288 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (!busiest) {
3290 schedstat_inc(sd, lb_nobusyq[idle]);
3291 goto out_balanced;
3292 }
3293
Nick Piggindb935db2005-06-25 14:57:11 -07003294 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 schedstat_add(sd, lb_imbalance[idle], imbalance);
3297
Peter Williams43010652007-08-09 11:16:46 +02003298 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (busiest->nr_running > 1) {
3300 /*
3301 * Attempt to move tasks. If find_busiest_group has found
3302 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003303 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 * correctly treated as an imbalance.
3305 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003306 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003307 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003308 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003309 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003310 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003311 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003312
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003313 /*
3314 * some other cpu did the load balance for us.
3315 */
Peter Williams43010652007-08-09 11:16:46 +02003316 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003317 resched_cpu(this_cpu);
3318
Nick Piggin81026792005-06-25 14:57:07 -07003319 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003320 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003321 cpu_clear(cpu_of(busiest), *cpus);
3322 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003323 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003324 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
Nick Piggin81026792005-06-25 14:57:07 -07003327
Peter Williams43010652007-08-09 11:16:46 +02003328 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 schedstat_inc(sd, lb_failed[idle]);
3330 sd->nr_balance_failed++;
3331
3332 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003334 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003335
3336 /* don't kick the migration_thread, if the curr
3337 * task on busiest cpu can't be moved to this_cpu
3338 */
3339 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003340 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003341 all_pinned = 1;
3342 goto out_one_pinned;
3343 }
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (!busiest->active_balance) {
3346 busiest->active_balance = 1;
3347 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003348 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003350 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003351 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 wake_up_process(busiest->migration_thread);
3353
3354 /*
3355 * We've kicked active balancing, reset the failure
3356 * counter.
3357 */
Nick Piggin39507452005-06-25 14:57:09 -07003358 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Nick Piggin81026792005-06-25 14:57:07 -07003360 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 sd->nr_balance_failed = 0;
3362
Nick Piggin81026792005-06-25 14:57:07 -07003363 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* We were unbalanced, so reset the balancing interval */
3365 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003366 } else {
3367 /*
3368 * If we've begun active balancing, start to back off. This
3369 * case may not be covered by the all_pinned logic if there
3370 * is only 1 task on the busy runqueue (because we don't call
3371 * move_tasks).
3372 */
3373 if (sd->balance_interval < sd->max_interval)
3374 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376
Peter Williams43010652007-08-09 11:16:46 +02003377 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003378 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Ingo Molnar6363ca52008-05-29 11:28:57 +02003379 return -1;
3380 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 schedstat_inc(sd, lb_balanced[idle]);
3384
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003385 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003386
3387out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003389 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3390 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 sd->balance_interval *= 2;
3392
Ingo Molnar48f24c42006-07-03 00:25:40 -07003393 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003394 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Ingo Molnar6363ca52008-05-29 11:28:57 +02003395 return -1;
3396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399/*
3400 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3401 * tasks if there is an imbalance.
3402 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003403 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 * this_rq is locked.
3405 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003406static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003407load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3408 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003411 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003413 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003414 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003415 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003416
3417 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003418
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003419 /*
3420 * When power savings policy is enabled for the parent domain, idle
3421 * sibling can pick up load irrespective of busy siblings. In this case,
3422 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003423 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003424 */
3425 if (sd->flags & SD_SHARE_CPUPOWER &&
3426 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003427 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Ingo Molnar2d723762007-10-15 17:00:12 +02003429 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003430redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003431 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003432 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003434 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003435 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437
Mike Travis7c16ec52008-04-04 18:11:11 -07003438 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003439 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003440 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003441 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443
Nick Piggindb935db2005-06-25 14:57:11 -07003444 BUG_ON(busiest == this_rq);
3445
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003446 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003447
Peter Williams43010652007-08-09 11:16:46 +02003448 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003449 if (busiest->nr_running > 1) {
3450 /* Attempt to move tasks */
3451 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003452 /* this_rq->clock is already updated */
3453 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003454 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003455 imbalance, sd, CPU_NEWLY_IDLE,
3456 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003457 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003458
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003459 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003460 cpu_clear(cpu_of(busiest), *cpus);
3461 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003462 goto redo;
3463 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003464 }
3465
Peter Williams43010652007-08-09 11:16:46 +02003466 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003467 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003468 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3469 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003470 return -1;
3471 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003472 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Peter Williams43010652007-08-09 11:16:46 +02003474 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003475
3476out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003477 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003478 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003479 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003480 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003481 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003482
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
3486/*
3487 * idle_balance is called by schedule() if this_cpu is about to become
3488 * idle. Attempts to pull tasks from other CPUs.
3489 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003490static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003493 int pulled_task = -1;
3494 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003495 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003498 unsigned long interval;
3499
3500 if (!(sd->flags & SD_LOAD_BALANCE))
3501 continue;
3502
3503 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003504 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003505 pulled_task = load_balance_newidle(this_cpu, this_rq,
3506 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003507
3508 interval = msecs_to_jiffies(sd->balance_interval);
3509 if (time_after(next_balance, sd->last_balance + interval))
3510 next_balance = sd->last_balance + interval;
3511 if (pulled_task)
3512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003514 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003515 /*
3516 * We are going idle. next_balance may be set based on
3517 * a busy processor. So reset next_balance.
3518 */
3519 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
3523/*
3524 * active_load_balance is run by migration threads. It pushes running tasks
3525 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3526 * running on each physical CPU where possible, and avoids physical /
3527 * logical imbalances.
3528 *
3529 * Called with busiest_rq locked.
3530 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003531static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Nick Piggin39507452005-06-25 14:57:09 -07003533 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003534 struct sched_domain *sd;
3535 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003536
Ingo Molnar48f24c42006-07-03 00:25:40 -07003537 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003538 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003539 return;
3540
3541 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
3543 /*
Nick Piggin39507452005-06-25 14:57:09 -07003544 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003545 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003546 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 */
Nick Piggin39507452005-06-25 14:57:09 -07003548 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Nick Piggin39507452005-06-25 14:57:09 -07003550 /* move a task from busiest_rq to target_rq */
3551 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003552 update_rq_clock(busiest_rq);
3553 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Nick Piggin39507452005-06-25 14:57:09 -07003555 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003556 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003557 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003558 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003559 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Ingo Molnar48f24c42006-07-03 00:25:40 -07003562 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003563 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Peter Williams43010652007-08-09 11:16:46 +02003565 if (move_one_task(target_rq, target_cpu, busiest_rq,
3566 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003567 schedstat_inc(sd, alb_pushed);
3568 else
3569 schedstat_inc(sd, alb_failed);
3570 }
Nick Piggin39507452005-06-25 14:57:09 -07003571 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003574#ifdef CONFIG_NO_HZ
3575static struct {
3576 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003577 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003578} nohz ____cacheline_aligned = {
3579 .load_balancer = ATOMIC_INIT(-1),
3580 .cpu_mask = CPU_MASK_NONE,
3581};
3582
Christoph Lameter7835b982006-12-10 02:20:22 -08003583/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003584 * This routine will try to nominate the ilb (idle load balancing)
3585 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3586 * load balancing on behalf of all those cpus. If all the cpus in the system
3587 * go into this tickless mode, then there will be no ilb owner (as there is
3588 * no need for one) and all the cpus will sleep till the next wakeup event
3589 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003590 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003591 * For the ilb owner, tick is not stopped. And this tick will be used
3592 * for idle load balancing. ilb owner will still be part of
3593 * nohz.cpu_mask..
3594 *
3595 * While stopping the tick, this cpu will become the ilb owner if there
3596 * is no other owner. And will be the owner till that cpu becomes busy
3597 * or if all cpus in the system stop their ticks at which point
3598 * there is no need for ilb owner.
3599 *
3600 * When the ilb owner becomes busy, it nominates another owner, during the
3601 * next busy scheduler_tick()
3602 */
3603int select_nohz_load_balancer(int stop_tick)
3604{
3605 int cpu = smp_processor_id();
3606
3607 if (stop_tick) {
3608 cpu_set(cpu, nohz.cpu_mask);
3609 cpu_rq(cpu)->in_nohz_recently = 1;
3610
3611 /*
3612 * If we are going offline and still the leader, give up!
3613 */
3614 if (cpu_is_offline(cpu) &&
3615 atomic_read(&nohz.load_balancer) == cpu) {
3616 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3617 BUG();
3618 return 0;
3619 }
3620
3621 /* time for ilb owner also to sleep */
3622 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3623 if (atomic_read(&nohz.load_balancer) == cpu)
3624 atomic_set(&nohz.load_balancer, -1);
3625 return 0;
3626 }
3627
3628 if (atomic_read(&nohz.load_balancer) == -1) {
3629 /* make me the ilb owner */
3630 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3631 return 1;
3632 } else if (atomic_read(&nohz.load_balancer) == cpu)
3633 return 1;
3634 } else {
3635 if (!cpu_isset(cpu, nohz.cpu_mask))
3636 return 0;
3637
3638 cpu_clear(cpu, nohz.cpu_mask);
3639
3640 if (atomic_read(&nohz.load_balancer) == cpu)
3641 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3642 BUG();
3643 }
3644 return 0;
3645}
3646#endif
3647
3648static DEFINE_SPINLOCK(balancing);
3649
3650/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003651 * It checks each scheduling domain to see if it is due to be balanced,
3652 * and initiates a balancing operation if so.
3653 *
3654 * Balancing parameters are set up in arch_init_sched_domains.
3655 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003656static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003657{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003658 int balance = 1;
3659 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003660 unsigned long interval;
3661 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003662 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003663 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003664 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003665 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003667 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 if (!(sd->flags & SD_LOAD_BALANCE))
3669 continue;
3670
3671 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003672 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 interval *= sd->busy_factor;
3674
3675 /* scale ms to jiffies */
3676 interval = msecs_to_jiffies(interval);
3677 if (unlikely(!interval))
3678 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003679 if (interval > HZ*NR_CPUS/10)
3680 interval = HZ*NR_CPUS/10;
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Christoph Lameter08c183f2006-12-10 02:20:29 -08003683 if (sd->flags & SD_SERIALIZE) {
3684 if (!spin_trylock(&balancing))
3685 goto out;
3686 }
3687
Christoph Lameterc9819f42006-12-10 02:20:25 -08003688 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003689 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003690 /*
3691 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003692 * longer idle, or one of our SMT siblings is
3693 * not idle.
3694 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003695 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003697 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003699 if (sd->flags & SD_SERIALIZE)
3700 spin_unlock(&balancing);
3701out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003702 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003703 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003704 update_next_balance = 1;
3705 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003706
3707 /*
3708 * Stop the load balance at this level. There is another
3709 * CPU in our sched group which is doing load balancing more
3710 * actively.
3711 */
3712 if (!balance)
3713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003715
3716 /*
3717 * next_balance will be updated only when there is a need.
3718 * When the cpu is attached to null domain for ex, it will not be
3719 * updated.
3720 */
3721 if (likely(update_next_balance))
3722 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003723}
3724
3725/*
3726 * run_rebalance_domains is triggered when needed from the scheduler tick.
3727 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3728 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3729 */
3730static void run_rebalance_domains(struct softirq_action *h)
3731{
Ingo Molnardd41f592007-07-09 18:51:59 +02003732 int this_cpu = smp_processor_id();
3733 struct rq *this_rq = cpu_rq(this_cpu);
3734 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3735 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003736
Ingo Molnardd41f592007-07-09 18:51:59 +02003737 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003738
3739#ifdef CONFIG_NO_HZ
3740 /*
3741 * If this cpu is the owner for idle load balancing, then do the
3742 * balancing on behalf of the other idle cpus whose ticks are
3743 * stopped.
3744 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003745 if (this_rq->idle_at_tick &&
3746 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003747 cpumask_t cpus = nohz.cpu_mask;
3748 struct rq *rq;
3749 int balance_cpu;
3750
Ingo Molnardd41f592007-07-09 18:51:59 +02003751 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003752 for_each_cpu_mask(balance_cpu, cpus) {
3753 /*
3754 * If this cpu gets work to do, stop the load balancing
3755 * work being done for other cpus. Next load
3756 * balancing owner will pick it up.
3757 */
3758 if (need_resched())
3759 break;
3760
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003761 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003762
3763 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003764 if (time_after(this_rq->next_balance, rq->next_balance))
3765 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003766 }
3767 }
3768#endif
3769}
3770
3771/*
3772 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3773 *
3774 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3775 * idle load balancing owner or decide to stop the periodic load balancing,
3776 * if the whole system is idle.
3777 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003778static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003779{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003780#ifdef CONFIG_NO_HZ
3781 /*
3782 * If we were in the nohz mode recently and busy at the current
3783 * scheduler tick, then check if we need to nominate new idle
3784 * load balancer.
3785 */
3786 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3787 rq->in_nohz_recently = 0;
3788
3789 if (atomic_read(&nohz.load_balancer) == cpu) {
3790 cpu_clear(cpu, nohz.cpu_mask);
3791 atomic_set(&nohz.load_balancer, -1);
3792 }
3793
3794 if (atomic_read(&nohz.load_balancer) == -1) {
3795 /*
3796 * simple selection for now: Nominate the
3797 * first cpu in the nohz list to be the next
3798 * ilb owner.
3799 *
3800 * TBD: Traverse the sched domains and nominate
3801 * the nearest cpu in the nohz.cpu_mask.
3802 */
3803 int ilb = first_cpu(nohz.cpu_mask);
3804
Mike Travis434d53b2008-04-04 18:11:04 -07003805 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003806 resched_cpu(ilb);
3807 }
3808 }
3809
3810 /*
3811 * If this cpu is idle and doing idle load balancing for all the
3812 * cpus with ticks stopped, is it time for that to stop?
3813 */
3814 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3815 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3816 resched_cpu(cpu);
3817 return;
3818 }
3819
3820 /*
3821 * If this cpu is idle and the idle load balancing is done by
3822 * someone else, then no need raise the SCHED_SOFTIRQ
3823 */
3824 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3825 cpu_isset(cpu, nohz.cpu_mask))
3826 return;
3827#endif
3828 if (time_after_eq(jiffies, rq->next_balance))
3829 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
Ingo Molnardd41f592007-07-09 18:51:59 +02003831
3832#else /* CONFIG_SMP */
3833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834/*
3835 * on UP we do not need to balance between CPUs:
3836 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003837static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839}
Ingo Molnardd41f592007-07-09 18:51:59 +02003840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841#endif
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843DEFINE_PER_CPU(struct kernel_stat, kstat);
3844
3845EXPORT_PER_CPU_SYMBOL(kstat);
3846
3847/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003848 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3849 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003851unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003854 u64 ns, delta_exec;
3855 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003856
Ingo Molnar41b86e92007-07-09 18:51:58 +02003857 rq = task_rq_lock(p, &flags);
3858 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003859 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003860 update_rq_clock(rq);
3861 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003862 if ((s64)delta_exec > 0)
3863 ns += delta_exec;
3864 }
3865 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return ns;
3868}
3869
3870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 * Account user cpu time to a process.
3872 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 * @cputime: the cpu time spent in user space since the last update
3874 */
3875void account_user_time(struct task_struct *p, cputime_t cputime)
3876{
3877 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3878 cputime64_t tmp;
3879
3880 p->utime = cputime_add(p->utime, cputime);
3881
3882 /* Add user time to cpustat. */
3883 tmp = cputime_to_cputime64(cputime);
3884 if (TASK_NICE(p) > 0)
3885 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3886 else
3887 cpustat->user = cputime64_add(cpustat->user, tmp);
3888}
3889
3890/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003891 * Account guest cpu time to a process.
3892 * @p: the process that the cpu time gets accounted to
3893 * @cputime: the cpu time spent in virtual machine since the last update
3894 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003895static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003896{
3897 cputime64_t tmp;
3898 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3899
3900 tmp = cputime_to_cputime64(cputime);
3901
3902 p->utime = cputime_add(p->utime, cputime);
3903 p->gtime = cputime_add(p->gtime, cputime);
3904
3905 cpustat->user = cputime64_add(cpustat->user, tmp);
3906 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3907}
3908
3909/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003910 * Account scaled user cpu time to a process.
3911 * @p: the process that the cpu time gets accounted to
3912 * @cputime: the cpu time spent in user space since the last update
3913 */
3914void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3915{
3916 p->utimescaled = cputime_add(p->utimescaled, cputime);
3917}
3918
3919/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 * Account system cpu time to a process.
3921 * @p: the process that the cpu time gets accounted to
3922 * @hardirq_offset: the offset to subtract from hardirq_count()
3923 * @cputime: the cpu time spent in kernel space since the last update
3924 */
3925void account_system_time(struct task_struct *p, int hardirq_offset,
3926 cputime_t cputime)
3927{
3928 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003929 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 cputime64_t tmp;
3931
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003932 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
3933 account_guest_time(p, cputime);
3934 return;
3935 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 p->stime = cputime_add(p->stime, cputime);
3938
3939 /* Add system time to cpustat. */
3940 tmp = cputime_to_cputime64(cputime);
3941 if (hardirq_count() - hardirq_offset)
3942 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3943 else if (softirq_count())
3944 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003945 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003947 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3949 else
3950 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3951 /* Account for system time used */
3952 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
3955/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003956 * Account scaled system cpu time to a process.
3957 * @p: the process that the cpu time gets accounted to
3958 * @hardirq_offset: the offset to subtract from hardirq_count()
3959 * @cputime: the cpu time spent in kernel space since the last update
3960 */
3961void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3962{
3963 p->stimescaled = cputime_add(p->stimescaled, cputime);
3964}
3965
3966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 * Account for involuntary wait time.
3968 * @p: the process from which the cpu time has been stolen
3969 * @steal: the cpu time spent in involuntary wait
3970 */
3971void account_steal_time(struct task_struct *p, cputime_t steal)
3972{
3973 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3974 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003975 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 if (p == rq->idle) {
3978 p->stime = cputime_add(p->stime, steal);
3979 if (atomic_read(&rq->nr_iowait) > 0)
3980 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3981 else
3982 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003983 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3985}
3986
Christoph Lameter7835b982006-12-10 02:20:22 -08003987/*
3988 * This function gets called by the timer code, with HZ frequency.
3989 * We call it with interrupts disabled.
3990 *
3991 * It also gets called by the fork code, when changing the parent's
3992 * timeslices.
3993 */
3994void scheduler_tick(void)
3995{
Christoph Lameter7835b982006-12-10 02:20:22 -08003996 int cpu = smp_processor_id();
3997 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003998 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003999
4000 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004001
Ingo Molnardd41f592007-07-09 18:51:59 +02004002 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004003 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004004 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004005 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004006 spin_unlock(&rq->lock);
4007
Christoph Lametere418e1c2006-12-10 02:20:23 -08004008#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004009 rq->idle_at_tick = idle_cpu(cpu);
4010 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4015
Srinivasa Ds43627582008-02-23 15:24:04 -08004016void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
4018 /*
4019 * Underflow?
4020 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004021 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 preempt_count() += val;
4024 /*
4025 * Spinlock count overflowing soon?
4026 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004027 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4028 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
4030EXPORT_SYMBOL(add_preempt_count);
4031
Srinivasa Ds43627582008-02-23 15:24:04 -08004032void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 /*
4035 * Underflow?
4036 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004037 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 /*
4040 * Is the spinlock portion underflowing?
4041 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004042 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4043 !(preempt_count() & PREEMPT_MASK)))
4044 return;
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 preempt_count() -= val;
4047}
4048EXPORT_SYMBOL(sub_preempt_count);
4049
4050#endif
4051
4052/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004053 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004055static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Satyam Sharma838225b2007-10-24 18:23:50 +02004057 struct pt_regs *regs = get_irq_regs();
4058
4059 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4060 prev->comm, prev->pid, preempt_count());
4061
Ingo Molnardd41f592007-07-09 18:51:59 +02004062 debug_show_held_locks(prev);
4063 if (irqs_disabled())
4064 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004065
4066 if (regs)
4067 show_regs(regs);
4068 else
4069 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Ingo Molnardd41f592007-07-09 18:51:59 +02004072/*
4073 * Various schedule()-time debugging checks and statistics:
4074 */
4075static inline void schedule_debug(struct task_struct *prev)
4076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004078 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 * schedule() atomically, we ignore that path for now.
4080 * Otherwise, whine if we are scheduling when we should not be.
4081 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004082 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004083 __schedule_bug(prev);
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4086
Ingo Molnar2d723762007-10-15 17:00:12 +02004087 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004088#ifdef CONFIG_SCHEDSTATS
4089 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004090 schedstat_inc(this_rq(), bkl_count);
4091 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004092 }
4093#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004094}
4095
4096/*
4097 * Pick up the highest-prio task:
4098 */
4099static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004100pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004101{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004102 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004103 struct task_struct *p;
4104
4105 /*
4106 * Optimization: we know that if all tasks are in
4107 * the fair class we can call that function directly:
4108 */
4109 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004110 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004111 if (likely(p))
4112 return p;
4113 }
4114
4115 class = sched_class_highest;
4116 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004117 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004118 if (p)
4119 return p;
4120 /*
4121 * Will never be NULL as the idle class always
4122 * returns a non-NULL p:
4123 */
4124 class = class->next;
4125 }
4126}
4127
4128/*
4129 * schedule() is the main scheduler function.
4130 */
4131asmlinkage void __sched schedule(void)
4132{
4133 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004134 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004135 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004136 int cpu;
4137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138need_resched:
4139 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004140 cpu = smp_processor_id();
4141 rq = cpu_rq(cpu);
4142 rcu_qsctr_inc(cpu);
4143 prev = rq->curr;
4144 switch_count = &prev->nivcsw;
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 release_kernel_lock(prev);
4147need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Ingo Molnardd41f592007-07-09 18:51:59 +02004149 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004151 hrtick_clear(rq);
4152
Ingo Molnar1e819952007-10-15 17:00:13 +02004153 /*
4154 * Do the rq-clock update outside the rq lock:
4155 */
4156 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004157 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004158 spin_lock(&rq->lock);
4159 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Ingo Molnardd41f592007-07-09 18:51:59 +02004161 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004162 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004163 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004164 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004165 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004166 switch_count = &prev->nvcsw;
4167 }
4168
Steven Rostedt9a897c52008-01-25 21:08:22 +01004169#ifdef CONFIG_SMP
4170 if (prev->sched_class->pre_schedule)
4171 prev->sched_class->pre_schedule(rq, prev);
4172#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004173
Ingo Molnardd41f592007-07-09 18:51:59 +02004174 if (unlikely(!rq->nr_running))
4175 idle_balance(cpu, rq);
4176
Ingo Molnar31ee5292007-08-09 11:16:49 +02004177 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004178 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004181 sched_info_switch(prev, next);
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 rq->nr_switches++;
4184 rq->curr = next;
4185 ++*switch_count;
4186
Ingo Molnardd41f592007-07-09 18:51:59 +02004187 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004188 /*
4189 * the context switch might have flipped the stack from under
4190 * us, hence refresh the local variables.
4191 */
4192 cpu = smp_processor_id();
4193 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 } else
4195 spin_unlock_irq(&rq->lock);
4196
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004197 hrtick_set(rq);
4198
4199 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 preempt_enable_no_resched();
4203 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4204 goto need_resched;
4205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206EXPORT_SYMBOL(schedule);
4207
4208#ifdef CONFIG_PREEMPT
4209/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004210 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004211 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 * occur there and call schedule directly.
4213 */
4214asmlinkage void __sched preempt_schedule(void)
4215{
4216 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 /*
4219 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004220 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004222 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return;
4224
Andi Kleen3a5c3592007-10-15 17:00:14 +02004225 do {
4226 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004227 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004228 sub_preempt_count(PREEMPT_ACTIVE);
4229
4230 /*
4231 * Check again in case we missed a preemption opportunity
4232 * between schedule and now.
4233 */
4234 barrier();
4235 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237EXPORT_SYMBOL(preempt_schedule);
4238
4239/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004240 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 * off of irq context.
4242 * Note, that this is called and return with irqs disabled. This will
4243 * protect us against recursive calling from irq.
4244 */
4245asmlinkage void __sched preempt_schedule_irq(void)
4246{
4247 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004248
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004249 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 BUG_ON(ti->preempt_count || !irqs_disabled());
4251
Andi Kleen3a5c3592007-10-15 17:00:14 +02004252 do {
4253 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004254 local_irq_enable();
4255 schedule();
4256 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004257 sub_preempt_count(PREEMPT_ACTIVE);
4258
4259 /*
4260 * Check again in case we missed a preemption opportunity
4261 * between schedule and now.
4262 */
4263 barrier();
4264 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
4267#endif /* CONFIG_PREEMPT */
4268
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004269int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4270 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004272 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274EXPORT_SYMBOL(default_wake_function);
4275
4276/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004277 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4278 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 * number) then we wake all the non-exclusive tasks and one exclusive task.
4280 *
4281 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004282 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4284 */
4285static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4286 int nr_exclusive, int sync, void *key)
4287{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004288 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004290 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004291 unsigned flags = curr->flags;
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004294 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 break;
4296 }
4297}
4298
4299/**
4300 * __wake_up - wake up threads blocked on a waitqueue.
4301 * @q: the waitqueue
4302 * @mode: which threads
4303 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004304 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004306void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004307 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
4309 unsigned long flags;
4310
4311 spin_lock_irqsave(&q->lock, flags);
4312 __wake_up_common(q, mode, nr_exclusive, 0, key);
4313 spin_unlock_irqrestore(&q->lock, flags);
4314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315EXPORT_SYMBOL(__wake_up);
4316
4317/*
4318 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4319 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004320void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
4322 __wake_up_common(q, mode, 1, 0, NULL);
4323}
4324
4325/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004326 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * @q: the waitqueue
4328 * @mode: which threads
4329 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4330 *
4331 * The sync wakeup differs that the waker knows that it will schedule
4332 * away soon, so while the target thread will be woken up, it will not
4333 * be migrated to another CPU - ie. the two threads are 'synchronized'
4334 * with each other. This can prevent needless bouncing between CPUs.
4335 *
4336 * On UP it can prevent extra preemption.
4337 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004338void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004339__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
4341 unsigned long flags;
4342 int sync = 1;
4343
4344 if (unlikely(!q))
4345 return;
4346
4347 if (unlikely(!nr_exclusive))
4348 sync = 0;
4349
4350 spin_lock_irqsave(&q->lock, flags);
4351 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4352 spin_unlock_irqrestore(&q->lock, flags);
4353}
4354EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4355
Ingo Molnarb15136e2007-10-24 18:23:48 +02004356void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
4358 unsigned long flags;
4359
4360 spin_lock_irqsave(&x->wait.lock, flags);
4361 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004362 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 spin_unlock_irqrestore(&x->wait.lock, flags);
4364}
4365EXPORT_SYMBOL(complete);
4366
Ingo Molnarb15136e2007-10-24 18:23:48 +02004367void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 unsigned long flags;
4370
4371 spin_lock_irqsave(&x->wait.lock, flags);
4372 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004373 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 spin_unlock_irqrestore(&x->wait.lock, flags);
4375}
4376EXPORT_SYMBOL(complete_all);
4377
Andi Kleen8cbbe862007-10-15 17:00:14 +02004378static inline long __sched
4379do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (!x->done) {
4382 DECLARE_WAITQUEUE(wait, current);
4383
4384 wait.flags |= WQ_FLAG_EXCLUSIVE;
4385 __add_wait_queue_tail(&x->wait, &wait);
4386 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004387 if ((state == TASK_INTERRUPTIBLE &&
4388 signal_pending(current)) ||
4389 (state == TASK_KILLABLE &&
4390 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004391 __remove_wait_queue(&x->wait, &wait);
4392 return -ERESTARTSYS;
4393 }
4394 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004396 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004398 if (!timeout) {
4399 __remove_wait_queue(&x->wait, &wait);
4400 return timeout;
4401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 } while (!x->done);
4403 __remove_wait_queue(&x->wait, &wait);
4404 }
4405 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004406 return timeout;
4407}
4408
4409static long __sched
4410wait_for_common(struct completion *x, long timeout, int state)
4411{
4412 might_sleep();
4413
4414 spin_lock_irq(&x->wait.lock);
4415 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004417 return timeout;
4418}
4419
Ingo Molnarb15136e2007-10-24 18:23:48 +02004420void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004421{
4422 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424EXPORT_SYMBOL(wait_for_completion);
4425
Ingo Molnarb15136e2007-10-24 18:23:48 +02004426unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4428{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004429 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431EXPORT_SYMBOL(wait_for_completion_timeout);
4432
Andi Kleen8cbbe862007-10-15 17:00:14 +02004433int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Andi Kleen51e97992007-10-18 21:32:55 +02004435 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4436 if (t == -ERESTARTSYS)
4437 return t;
4438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440EXPORT_SYMBOL(wait_for_completion_interruptible);
4441
Ingo Molnarb15136e2007-10-24 18:23:48 +02004442unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443wait_for_completion_interruptible_timeout(struct completion *x,
4444 unsigned long timeout)
4445{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004446 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4449
Matthew Wilcox009e5772007-12-06 12:29:54 -05004450int __sched wait_for_completion_killable(struct completion *x)
4451{
4452 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4453 if (t == -ERESTARTSYS)
4454 return t;
4455 return 0;
4456}
4457EXPORT_SYMBOL(wait_for_completion_killable);
4458
Andi Kleen8cbbe862007-10-15 17:00:14 +02004459static long __sched
4460sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004461{
4462 unsigned long flags;
4463 wait_queue_t wait;
4464
4465 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Andi Kleen8cbbe862007-10-15 17:00:14 +02004467 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Andi Kleen8cbbe862007-10-15 17:00:14 +02004469 spin_lock_irqsave(&q->lock, flags);
4470 __add_wait_queue(q, &wait);
4471 spin_unlock(&q->lock);
4472 timeout = schedule_timeout(timeout);
4473 spin_lock_irq(&q->lock);
4474 __remove_wait_queue(q, &wait);
4475 spin_unlock_irqrestore(&q->lock, flags);
4476
4477 return timeout;
4478}
4479
4480void __sched interruptible_sleep_on(wait_queue_head_t *q)
4481{
4482 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484EXPORT_SYMBOL(interruptible_sleep_on);
4485
Ingo Molnar0fec1712007-07-09 18:52:01 +02004486long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004487interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004489 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4492
Ingo Molnar0fec1712007-07-09 18:52:01 +02004493void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004495 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497EXPORT_SYMBOL(sleep_on);
4498
Ingo Molnar0fec1712007-07-09 18:52:01 +02004499long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004501 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503EXPORT_SYMBOL(sleep_on_timeout);
4504
Ingo Molnarb29739f2006-06-27 02:54:51 -07004505#ifdef CONFIG_RT_MUTEXES
4506
4507/*
4508 * rt_mutex_setprio - set the current priority of a task
4509 * @p: task
4510 * @prio: prio value (kernel-internal form)
4511 *
4512 * This function changes the 'effective' priority of a task. It does
4513 * not touch ->normal_prio like __setscheduler().
4514 *
4515 * Used by the rt_mutex code to implement priority inheritance logic.
4516 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004517void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004518{
4519 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004520 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004521 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004522 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004523
4524 BUG_ON(prio < 0 || prio > MAX_PRIO);
4525
4526 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004527 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004528
Andrew Mortond5f9f942007-05-08 20:27:06 -07004529 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004530 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004531 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004532 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004533 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004534 if (running)
4535 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004536
4537 if (rt_prio(prio))
4538 p->sched_class = &rt_sched_class;
4539 else
4540 p->sched_class = &fair_sched_class;
4541
Ingo Molnarb29739f2006-06-27 02:54:51 -07004542 p->prio = prio;
4543
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004544 if (running)
4545 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004546 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004547 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004548
4549 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004550 }
4551 task_rq_unlock(rq, &flags);
4552}
4553
4554#endif
4555
Ingo Molnar36c8b582006-07-03 00:25:41 -07004556void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Ingo Molnardd41f592007-07-09 18:51:59 +02004558 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004560 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4563 return;
4564 /*
4565 * We have to be careful, if called from sys_setpriority(),
4566 * the task might be in the middle of scheduling on another CPU.
4567 */
4568 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004569 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 /*
4571 * The RT priorities are set via sched_setscheduler(), but we still
4572 * allow the 'normal' nice value to be set - but as expected
4573 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004574 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004576 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 p->static_prio = NICE_TO_PRIO(nice);
4578 goto out_unlock;
4579 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004580 on_rq = p->se.on_rq;
Ingo Molnar6363ca52008-05-29 11:28:57 +02004581 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004582 dequeue_task(rq, p, 0);
Ingo Molnar6363ca52008-05-29 11:28:57 +02004583 dec_load(rq, p);
4584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004587 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004588 old_prio = p->prio;
4589 p->prio = effective_prio(p);
4590 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Ingo Molnardd41f592007-07-09 18:51:59 +02004592 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004593 enqueue_task(rq, p, 0);
Ingo Molnar6363ca52008-05-29 11:28:57 +02004594 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004596 * If the task increased its priority or is running and
4597 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004599 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 resched_task(rq->curr);
4601 }
4602out_unlock:
4603 task_rq_unlock(rq, &flags);
4604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605EXPORT_SYMBOL(set_user_nice);
4606
Matt Mackalle43379f2005-05-01 08:59:00 -07004607/*
4608 * can_nice - check if a task can reduce its nice value
4609 * @p: task
4610 * @nice: nice value
4611 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004612int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004613{
Matt Mackall024f4742005-08-18 11:24:19 -07004614 /* convert nice value [19,-20] to rlimit style value [1,40] */
4615 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004616
Matt Mackalle43379f2005-05-01 08:59:00 -07004617 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4618 capable(CAP_SYS_NICE));
4619}
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621#ifdef __ARCH_WANT_SYS_NICE
4622
4623/*
4624 * sys_nice - change the priority of the current process.
4625 * @increment: priority increment
4626 *
4627 * sys_setpriority is a more generic, but much slower function that
4628 * does similar things.
4629 */
4630asmlinkage long sys_nice(int increment)
4631{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004632 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 /*
4635 * Setpriority might change our priority at the same moment.
4636 * We don't have to worry. Conceptually one call occurs first
4637 * and we have a single winner.
4638 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004639 if (increment < -40)
4640 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (increment > 40)
4642 increment = 40;
4643
4644 nice = PRIO_TO_NICE(current->static_prio) + increment;
4645 if (nice < -20)
4646 nice = -20;
4647 if (nice > 19)
4648 nice = 19;
4649
Matt Mackalle43379f2005-05-01 08:59:00 -07004650 if (increment < 0 && !can_nice(current, nice))
4651 return -EPERM;
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 retval = security_task_setnice(current, nice);
4654 if (retval)
4655 return retval;
4656
4657 set_user_nice(current, nice);
4658 return 0;
4659}
4660
4661#endif
4662
4663/**
4664 * task_prio - return the priority value of a given task.
4665 * @p: the task in question.
4666 *
4667 * This is the priority value as seen by users in /proc.
4668 * RT tasks are offset by -200. Normal tasks are centered
4669 * around 0, value goes from -16 to +15.
4670 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004671int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
4673 return p->prio - MAX_RT_PRIO;
4674}
4675
4676/**
4677 * task_nice - return the nice value of a given task.
4678 * @p: the task in question.
4679 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004680int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
4682 return TASK_NICE(p);
4683}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004684EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686/**
4687 * idle_cpu - is a given cpu idle currently?
4688 * @cpu: the processor in question.
4689 */
4690int idle_cpu(int cpu)
4691{
4692 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4693}
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/**
4696 * idle_task - return the idle task for a given cpu.
4697 * @cpu: the processor in question.
4698 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004699struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
4701 return cpu_rq(cpu)->idle;
4702}
4703
4704/**
4705 * find_process_by_pid - find a process with a matching PID value.
4706 * @pid: the pid in question.
4707 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004708static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004710 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
4713/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004714static void
4715__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Ingo Molnardd41f592007-07-09 18:51:59 +02004717 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004720 switch (p->policy) {
4721 case SCHED_NORMAL:
4722 case SCHED_BATCH:
4723 case SCHED_IDLE:
4724 p->sched_class = &fair_sched_class;
4725 break;
4726 case SCHED_FIFO:
4727 case SCHED_RR:
4728 p->sched_class = &rt_sched_class;
4729 break;
4730 }
4731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004733 p->normal_prio = normal_prio(p);
4734 /* we are holding p->pi_lock already */
4735 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004736 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
4738
4739/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004740 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 * @p: the task in question.
4742 * @policy: new policy.
4743 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004744 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004745 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004747int sched_setscheduler(struct task_struct *p, int policy,
4748 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004750 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004752 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004753 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Steven Rostedt66e53932006-06-27 02:54:44 -07004755 /* may grab non-irq protected spin_locks */
4756 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757recheck:
4758 /* double check policy once rq lock held */
4759 if (policy < 0)
4760 policy = oldpolicy = p->policy;
4761 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004762 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4763 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004764 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 /*
4766 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004767 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4768 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 */
4770 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004771 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004772 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004774 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return -EINVAL;
4776
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004777 /*
4778 * Allow unprivileged RT tasks to decrease priority:
4779 */
4780 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004781 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004782 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004783
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004784 if (!lock_task_sighand(p, &flags))
4785 return -ESRCH;
4786 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4787 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004788
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004789 /* can't set/change the rt policy */
4790 if (policy != p->policy && !rlim_rtprio)
4791 return -EPERM;
4792
4793 /* can't increase priority */
4794 if (param->sched_priority > p->rt_priority &&
4795 param->sched_priority > rlim_rtprio)
4796 return -EPERM;
4797 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004798 /*
4799 * Like positive nice levels, dont allow tasks to
4800 * move out of SCHED_IDLE either:
4801 */
4802 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4803 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004804
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004805 /* can't change other user's priorities */
4806 if ((current->euid != p->euid) &&
4807 (current->euid != p->uid))
4808 return -EPERM;
4809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004811#ifdef CONFIG_RT_GROUP_SCHED
4812 /*
4813 * Do not allow realtime tasks into groups that have no runtime
4814 * assigned.
4815 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02004816 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004817 return -EPERM;
4818#endif
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 retval = security_task_setscheduler(p, policy, param);
4821 if (retval)
4822 return retval;
4823 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004824 * make sure no PI-waiters arrive (or leave) while we are
4825 * changing the priority of the task:
4826 */
4827 spin_lock_irqsave(&p->pi_lock, flags);
4828 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 * To be able to change p->policy safely, the apropriate
4830 * runqueue lock must be held.
4831 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004832 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 /* recheck policy now with rq lock held */
4834 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4835 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004836 __task_rq_unlock(rq);
4837 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 goto recheck;
4839 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004840 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004841 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004842 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004843 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004844 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004845 if (running)
4846 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004849 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004850
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004851 if (running)
4852 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004853 if (on_rq) {
4854 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004855
4856 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004858 __task_rq_unlock(rq);
4859 spin_unlock_irqrestore(&p->pi_lock, flags);
4860
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004861 rt_mutex_adjust_pi(p);
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return 0;
4864}
4865EXPORT_SYMBOL_GPL(sched_setscheduler);
4866
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004867static int
4868do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 struct sched_param lparam;
4871 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004872 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (!param || pid < 0)
4875 return -EINVAL;
4876 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4877 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004878
4879 rcu_read_lock();
4880 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004882 if (p != NULL)
4883 retval = sched_setscheduler(p, policy, &lparam);
4884 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return retval;
4887}
4888
4889/**
4890 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4891 * @pid: the pid in question.
4892 * @policy: new policy.
4893 * @param: structure containing the new RT priority.
4894 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004895asmlinkage long
4896sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
Jason Baronc21761f2006-01-18 17:43:03 -08004898 /* negative values for policy are not valid */
4899 if (policy < 0)
4900 return -EINVAL;
4901
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return do_sched_setscheduler(pid, policy, param);
4903}
4904
4905/**
4906 * sys_sched_setparam - set/change the RT priority of a thread
4907 * @pid: the pid in question.
4908 * @param: structure containing the new RT priority.
4909 */
4910asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4911{
4912 return do_sched_setscheduler(pid, -1, param);
4913}
4914
4915/**
4916 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4917 * @pid: the pid in question.
4918 */
4919asmlinkage long sys_sched_getscheduler(pid_t pid)
4920{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004921 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004922 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004925 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 retval = -ESRCH;
4928 read_lock(&tasklist_lock);
4929 p = find_process_by_pid(pid);
4930 if (p) {
4931 retval = security_task_getscheduler(p);
4932 if (!retval)
4933 retval = p->policy;
4934 }
4935 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 return retval;
4937}
4938
4939/**
4940 * sys_sched_getscheduler - get the RT priority of a thread
4941 * @pid: the pid in question.
4942 * @param: structure containing the RT priority.
4943 */
4944asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4945{
4946 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004947 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004948 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004951 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 read_lock(&tasklist_lock);
4954 p = find_process_by_pid(pid);
4955 retval = -ESRCH;
4956 if (!p)
4957 goto out_unlock;
4958
4959 retval = security_task_getscheduler(p);
4960 if (retval)
4961 goto out_unlock;
4962
4963 lp.sched_priority = p->rt_priority;
4964 read_unlock(&tasklist_lock);
4965
4966 /*
4967 * This one might sleep, we cannot do it with a spinlock held ...
4968 */
4969 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return retval;
4972
4973out_unlock:
4974 read_unlock(&tasklist_lock);
4975 return retval;
4976}
4977
Mike Travisb53e9212008-04-04 18:11:08 -07004978long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07004981 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004982 struct task_struct *p;
4983 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004985 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 read_lock(&tasklist_lock);
4987
4988 p = find_process_by_pid(pid);
4989 if (!p) {
4990 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004991 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 return -ESRCH;
4993 }
4994
4995 /*
4996 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004997 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 * usage count and then drop tasklist_lock.
4999 */
5000 get_task_struct(p);
5001 read_unlock(&tasklist_lock);
5002
5003 retval = -EPERM;
5004 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5005 !capable(CAP_SYS_NICE))
5006 goto out_unlock;
5007
David Quigleye7834f82006-06-23 02:03:59 -07005008 retval = security_task_setscheduler(p, 0, NULL);
5009 if (retval)
5010 goto out_unlock;
5011
Mike Travisf9a86fc2008-04-04 18:11:07 -07005012 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005014 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005015 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Paul Menage8707d8b2007-10-18 23:40:22 -07005017 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005018 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005019 if (!cpus_subset(new_mask, cpus_allowed)) {
5020 /*
5021 * We must have raced with a concurrent cpuset
5022 * update. Just reset the cpus_allowed to the
5023 * cpuset's cpus_allowed
5024 */
5025 new_mask = cpus_allowed;
5026 goto again;
5027 }
5028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029out_unlock:
5030 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005031 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return retval;
5033}
5034
5035static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5036 cpumask_t *new_mask)
5037{
5038 if (len < sizeof(cpumask_t)) {
5039 memset(new_mask, 0, sizeof(cpumask_t));
5040 } else if (len > sizeof(cpumask_t)) {
5041 len = sizeof(cpumask_t);
5042 }
5043 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5044}
5045
5046/**
5047 * sys_sched_setaffinity - set the cpu affinity of a process
5048 * @pid: pid of the process
5049 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5050 * @user_mask_ptr: user-space pointer to the new cpu mask
5051 */
5052asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5053 unsigned long __user *user_mask_ptr)
5054{
5055 cpumask_t new_mask;
5056 int retval;
5057
5058 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5059 if (retval)
5060 return retval;
5061
Mike Travisb53e9212008-04-04 18:11:08 -07005062 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063}
5064
5065/*
5066 * Represents all cpu's present in the system
5067 * In systems capable of hotplug, this map could dynamically grow
5068 * as new cpu's are detected in the system via any platform specific
5069 * method, such as ACPI for e.g.
5070 */
5071
Andi Kleen4cef0c62006-01-11 22:44:57 +01005072cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073EXPORT_SYMBOL(cpu_present_map);
5074
5075#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005076cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005077EXPORT_SYMBOL(cpu_online_map);
5078
Andi Kleen4cef0c62006-01-11 22:44:57 +01005079cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005080EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081#endif
5082
5083long sched_getaffinity(pid_t pid, cpumask_t *mask)
5084{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005085 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005088 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 read_lock(&tasklist_lock);
5090
5091 retval = -ESRCH;
5092 p = find_process_by_pid(pid);
5093 if (!p)
5094 goto out_unlock;
5095
David Quigleye7834f82006-06-23 02:03:59 -07005096 retval = security_task_getscheduler(p);
5097 if (retval)
5098 goto out_unlock;
5099
Jack Steiner2f7016d2006-02-01 03:05:18 -08005100 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102out_unlock:
5103 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005104 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Ulrich Drepper9531b622007-08-09 11:16:46 +02005106 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
5109/**
5110 * sys_sched_getaffinity - get the cpu affinity of a process
5111 * @pid: pid of the process
5112 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5113 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5114 */
5115asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5116 unsigned long __user *user_mask_ptr)
5117{
5118 int ret;
5119 cpumask_t mask;
5120
5121 if (len < sizeof(cpumask_t))
5122 return -EINVAL;
5123
5124 ret = sched_getaffinity(pid, &mask);
5125 if (ret < 0)
5126 return ret;
5127
5128 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5129 return -EFAULT;
5130
5131 return sizeof(cpumask_t);
5132}
5133
5134/**
5135 * sys_sched_yield - yield the current processor to other threads.
5136 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005137 * This function yields the current CPU to other tasks. If there are no
5138 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 */
5140asmlinkage long sys_sched_yield(void)
5141{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005142 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Ingo Molnar2d723762007-10-15 17:00:12 +02005144 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005145 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
5147 /*
5148 * Since we are going to call schedule() anyway, there's
5149 * no need to preempt or enable interrupts:
5150 */
5151 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005152 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 _raw_spin_unlock(&rq->lock);
5154 preempt_enable_no_resched();
5155
5156 schedule();
5157
5158 return 0;
5159}
5160
Andrew Mortone7b38402006-06-30 01:56:00 -07005161static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005163#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5164 __might_sleep(__FILE__, __LINE__);
5165#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005166 /*
5167 * The BKS might be reacquired before we have dropped
5168 * PREEMPT_ACTIVE, which could trigger a second
5169 * cond_resched() call.
5170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 do {
5172 add_preempt_count(PREEMPT_ACTIVE);
5173 schedule();
5174 sub_preempt_count(PREEMPT_ACTIVE);
5175 } while (need_resched());
5176}
5177
Herbert Xu02b67cc32008-01-25 21:08:28 +01005178int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
Ingo Molnar94142322006-12-29 16:48:13 -08005180 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5181 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 __cond_resched();
5183 return 1;
5184 }
5185 return 0;
5186}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005187EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189/*
5190 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5191 * call schedule, and on return reacquire the lock.
5192 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005193 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 * operations here to prevent schedule() from being called twice (once via
5195 * spin_unlock(), once by hand).
5196 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005197int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
Nick Piggin95c354f2008-01-30 13:31:20 +01005199 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005200 int ret = 0;
5201
Nick Piggin95c354f2008-01-30 13:31:20 +01005202 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005204 if (resched && need_resched())
5205 __cond_resched();
5206 else
5207 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005208 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213EXPORT_SYMBOL(cond_resched_lock);
5214
5215int __sched cond_resched_softirq(void)
5216{
5217 BUG_ON(!in_softirq());
5218
Ingo Molnar94142322006-12-29 16:48:13 -08005219 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005220 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 __cond_resched();
5222 local_bh_disable();
5223 return 1;
5224 }
5225 return 0;
5226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227EXPORT_SYMBOL(cond_resched_softirq);
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229/**
5230 * yield - yield the current processor to other threads.
5231 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005232 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 * thread runnable and calls sys_sched_yield().
5234 */
5235void __sched yield(void)
5236{
5237 set_current_state(TASK_RUNNING);
5238 sys_sched_yield();
5239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240EXPORT_SYMBOL(yield);
5241
5242/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005243 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 * that process accounting knows that this is a task in IO wait state.
5245 *
5246 * But don't do that if it is a deliberate, throttling IO wait (this task
5247 * has set its backing_dev_info: the queue against which it should throttle)
5248 */
5249void __sched io_schedule(void)
5250{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005251 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005253 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 atomic_inc(&rq->nr_iowait);
5255 schedule();
5256 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005257 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259EXPORT_SYMBOL(io_schedule);
5260
5261long __sched io_schedule_timeout(long timeout)
5262{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005263 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 long ret;
5265
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005266 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 atomic_inc(&rq->nr_iowait);
5268 ret = schedule_timeout(timeout);
5269 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005270 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 return ret;
5272}
5273
5274/**
5275 * sys_sched_get_priority_max - return maximum RT priority.
5276 * @policy: scheduling class.
5277 *
5278 * this syscall returns the maximum rt_priority that can be used
5279 * by a given scheduling class.
5280 */
5281asmlinkage long sys_sched_get_priority_max(int policy)
5282{
5283 int ret = -EINVAL;
5284
5285 switch (policy) {
5286 case SCHED_FIFO:
5287 case SCHED_RR:
5288 ret = MAX_USER_RT_PRIO-1;
5289 break;
5290 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005291 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005292 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 ret = 0;
5294 break;
5295 }
5296 return ret;
5297}
5298
5299/**
5300 * sys_sched_get_priority_min - return minimum RT priority.
5301 * @policy: scheduling class.
5302 *
5303 * this syscall returns the minimum rt_priority that can be used
5304 * by a given scheduling class.
5305 */
5306asmlinkage long sys_sched_get_priority_min(int policy)
5307{
5308 int ret = -EINVAL;
5309
5310 switch (policy) {
5311 case SCHED_FIFO:
5312 case SCHED_RR:
5313 ret = 1;
5314 break;
5315 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005316 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005317 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 ret = 0;
5319 }
5320 return ret;
5321}
5322
5323/**
5324 * sys_sched_rr_get_interval - return the default timeslice of a process.
5325 * @pid: pid of the process.
5326 * @interval: userspace pointer to the timeslice value.
5327 *
5328 * this syscall writes the default timeslice value of a given process
5329 * into the user-space timespec buffer. A value of '0' means infinity.
5330 */
5331asmlinkage
5332long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5333{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005334 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005335 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005336 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 retval = -ESRCH;
5343 read_lock(&tasklist_lock);
5344 p = find_process_by_pid(pid);
5345 if (!p)
5346 goto out_unlock;
5347
5348 retval = security_task_getscheduler(p);
5349 if (retval)
5350 goto out_unlock;
5351
Ingo Molnar77034932007-12-04 17:04:39 +01005352 /*
5353 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5354 * tasks that are on an otherwise idle runqueue:
5355 */
5356 time_slice = 0;
5357 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005358 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005359 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005360 struct sched_entity *se = &p->se;
5361 unsigned long flags;
5362 struct rq *rq;
5363
5364 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005365 if (rq->cfs.load.weight)
5366 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005367 task_rq_unlock(rq, &flags);
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005370 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005373
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374out_unlock:
5375 read_unlock(&tasklist_lock);
5376 return retval;
5377}
5378
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005379static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005380
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005381void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005384 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005387 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005388 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005389#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005391 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005393 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394#else
5395 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005396 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005398 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399#endif
5400#ifdef CONFIG_DEBUG_STACK_USAGE
5401 {
Al Viro10ebffd2005-11-13 16:06:56 -08005402 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 while (!*n)
5404 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005405 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 }
5407#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005408 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005409 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005411 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005414void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005416 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
Ingo Molnar4bd77322007-07-11 21:21:47 +02005418#if BITS_PER_LONG == 32
5419 printk(KERN_INFO
5420 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005422 printk(KERN_INFO
5423 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424#endif
5425 read_lock(&tasklist_lock);
5426 do_each_thread(g, p) {
5427 /*
5428 * reset the NMI-timeout, listing all files on a slow
5429 * console might take alot of time:
5430 */
5431 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005432 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005433 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 } while_each_thread(g, p);
5435
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005436 touch_all_softlockup_watchdogs();
5437
Ingo Molnardd41f592007-07-09 18:51:59 +02005438#ifdef CONFIG_SCHED_DEBUG
5439 sysrq_sched_debug_show();
5440#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005442 /*
5443 * Only show locks if all tasks are dumped:
5444 */
5445 if (state_filter == -1)
5446 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447}
5448
Ingo Molnar1df21052007-07-09 18:51:58 +02005449void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5450{
Ingo Molnardd41f592007-07-09 18:51:59 +02005451 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005452}
5453
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005454/**
5455 * init_idle - set up an idle thread for a given CPU
5456 * @idle: task in question
5457 * @cpu: cpu the idle task belongs to
5458 *
5459 * NOTE: this function does not set the idle thread's NEED_RESCHED
5460 * flag, to make booting more robust.
5461 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005462void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005464 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 unsigned long flags;
5466
Ingo Molnardd41f592007-07-09 18:51:59 +02005467 __sched_fork(idle);
5468 idle->se.exec_start = sched_clock();
5469
Ingo Molnarb29739f2006-06-27 02:54:51 -07005470 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005472 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
5474 spin_lock_irqsave(&rq->lock, flags);
5475 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005476#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5477 idle->oncpu = 1;
5478#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 spin_unlock_irqrestore(&rq->lock, flags);
5480
5481 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005482#if defined(CONFIG_PREEMPT)
5483 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5484#else
Al Viroa1261f52005-11-13 16:06:55 -08005485 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005486#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005487 /*
5488 * The idle tasks have their own, simple scheduling class:
5489 */
5490 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
5493/*
5494 * In a system that switches off the HZ timer nohz_cpu_mask
5495 * indicates which cpus entered this state. This is used
5496 * in the rcu update to wait only for active cpus. For system
5497 * which do not switch off the HZ timer nohz_cpu_mask should
5498 * always be CPU_MASK_NONE.
5499 */
5500cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5501
Ingo Molnar19978ca2007-11-09 22:39:38 +01005502/*
5503 * Increase the granularity value when there are more CPUs,
5504 * because with more CPUs the 'effective latency' as visible
5505 * to users decreases. But the relationship is not linear,
5506 * so pick a second-best guess by going with the log2 of the
5507 * number of CPUs.
5508 *
5509 * This idea comes from the SD scheduler of Con Kolivas:
5510 */
5511static inline void sched_init_granularity(void)
5512{
5513 unsigned int factor = 1 + ilog2(num_online_cpus());
5514 const unsigned long limit = 200000000;
5515
5516 sysctl_sched_min_granularity *= factor;
5517 if (sysctl_sched_min_granularity > limit)
5518 sysctl_sched_min_granularity = limit;
5519
5520 sysctl_sched_latency *= factor;
5521 if (sysctl_sched_latency > limit)
5522 sysctl_sched_latency = limit;
5523
5524 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005525}
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527#ifdef CONFIG_SMP
5528/*
5529 * This is how migration works:
5530 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005531 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 * runqueue and wake up that CPU's migration thread.
5533 * 2) we down() the locked semaphore => thread blocks.
5534 * 3) migration thread wakes up (implicitly it forces the migrated
5535 * thread off the CPU)
5536 * 4) it gets the migration request and checks whether the migrated
5537 * task is still in the wrong runqueue.
5538 * 5) if it's in the wrong runqueue then the migration thread removes
5539 * it and puts it into the right queue.
5540 * 6) migration thread up()s the semaphore.
5541 * 7) we wake up and the migration is done.
5542 */
5543
5544/*
5545 * Change a given task's CPU affinity. Migrate the thread to a
5546 * proper CPU and schedule it away if the CPU it's executing on
5547 * is removed from the allowed bitmask.
5548 *
5549 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005550 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 * call is not atomic; no spinlocks may be held.
5552 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005553int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005555 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005557 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005558 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005561 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 ret = -EINVAL;
5563 goto out;
5564 }
5565
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005566 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005567 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005568 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005569 p->cpus_allowed = *new_mask;
5570 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005571 }
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005574 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 goto out;
5576
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005577 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 /* Need help from migration thread: drop lock and wait. */
5579 task_rq_unlock(rq, &flags);
5580 wake_up_process(rq->migration_thread);
5581 wait_for_completion(&req.done);
5582 tlb_migrate_finish(p->mm);
5583 return 0;
5584 }
5585out:
5586 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return ret;
5589}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005590EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005593 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 * this because either it can't run here any more (set_cpus_allowed()
5595 * away from this CPU, or CPU going down), or because we're
5596 * attempting to rebalance this task on exec (sched_exec).
5597 *
5598 * So we race with normal scheduler movements, but that's OK, as long
5599 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005600 *
5601 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005603static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005605 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005606 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
5608 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 rq_src = cpu_rq(src_cpu);
5612 rq_dest = cpu_rq(dest_cpu);
5613
5614 double_rq_lock(rq_src, rq_dest);
5615 /* Already moved. */
5616 if (task_cpu(p) != src_cpu)
5617 goto out;
5618 /* Affinity changed (again). */
5619 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5620 goto out;
5621
Ingo Molnardd41f592007-07-09 18:51:59 +02005622 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005623 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005624 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005627 if (on_rq) {
5628 activate_task(rq_dest, p, 0);
5629 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005631 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632out:
5633 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635}
5636
5637/*
5638 * migration_thread - this is a highprio system thread that performs
5639 * thread migration by bumping thread off CPU then 'pushing' onto
5640 * another runqueue.
5641 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005642static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005645 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
5647 rq = cpu_rq(cpu);
5648 BUG_ON(rq->migration_thread != current);
5649
5650 set_current_state(TASK_INTERRUPTIBLE);
5651 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005652 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 spin_lock_irq(&rq->lock);
5656
5657 if (cpu_is_offline(cpu)) {
5658 spin_unlock_irq(&rq->lock);
5659 goto wait_to_die;
5660 }
5661
5662 if (rq->active_balance) {
5663 active_load_balance(rq, cpu);
5664 rq->active_balance = 0;
5665 }
5666
5667 head = &rq->migration_queue;
5668
5669 if (list_empty(head)) {
5670 spin_unlock_irq(&rq->lock);
5671 schedule();
5672 set_current_state(TASK_INTERRUPTIBLE);
5673 continue;
5674 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005675 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 list_del_init(head->next);
5677
Nick Piggin674311d2005-06-25 14:57:27 -07005678 spin_unlock(&rq->lock);
5679 __migrate_task(req->task, cpu, req->dest_cpu);
5680 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 complete(&req->done);
5683 }
5684 __set_current_state(TASK_RUNNING);
5685 return 0;
5686
5687wait_to_die:
5688 /* Wait for kthread_stop */
5689 set_current_state(TASK_INTERRUPTIBLE);
5690 while (!kthread_should_stop()) {
5691 schedule();
5692 set_current_state(TASK_INTERRUPTIBLE);
5693 }
5694 __set_current_state(TASK_RUNNING);
5695 return 0;
5696}
5697
5698#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005699
5700static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5701{
5702 int ret;
5703
5704 local_irq_disable();
5705 ret = __migrate_task(p, src_cpu, dest_cpu);
5706 local_irq_enable();
5707 return ret;
5708}
5709
Kirill Korotaev054b9102006-12-10 02:20:11 -08005710/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005711 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005712 * NOTE: interrupts should be disabled by the caller
5713 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005714static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005716 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005718 struct rq *rq;
5719 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Andi Kleen3a5c3592007-10-15 17:00:14 +02005721 do {
5722 /* On same node? */
5723 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5724 cpus_and(mask, mask, p->cpus_allowed);
5725 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Andi Kleen3a5c3592007-10-15 17:00:14 +02005727 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07005728 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005729 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
Andi Kleen3a5c3592007-10-15 17:00:14 +02005731 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07005732 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005733 cpumask_t cpus_allowed;
5734
5735 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07005736 /*
5737 * Try to stay on the same cpuset, where the
5738 * current cpuset may be a subset of all cpus.
5739 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005740 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005741 * called within calls to cpuset_lock/cpuset_unlock.
5742 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005743 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005744 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005745 dest_cpu = any_online_cpu(p->cpus_allowed);
5746 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Andi Kleen3a5c3592007-10-15 17:00:14 +02005748 /*
5749 * Don't tell them about moving exiting tasks or
5750 * kernel threads (both mm NULL), since they never
5751 * leave kernel.
5752 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005753 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005754 printk(KERN_INFO "process %d (%s) no "
5755 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005756 task_pid_nr(p), p->comm, dead_cpu);
5757 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005758 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005759 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760}
5761
5762/*
5763 * While a dead CPU has no uninterruptible tasks queued at this point,
5764 * it might still have a nonzero ->nr_uninterruptible counter, because
5765 * for performance reasons the counter is not stricly tracking tasks to
5766 * their home CPUs. So we just add the counter to another CPU's counter,
5767 * to keep the global sum constant after CPU-down:
5768 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005769static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770{
Mike Travis7c16ec52008-04-04 18:11:11 -07005771 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 unsigned long flags;
5773
5774 local_irq_save(flags);
5775 double_rq_lock(rq_src, rq_dest);
5776 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5777 rq_src->nr_uninterruptible = 0;
5778 double_rq_unlock(rq_src, rq_dest);
5779 local_irq_restore(flags);
5780}
5781
5782/* Run through task list and migrate tasks from the dead cpu. */
5783static void migrate_live_tasks(int src_cpu)
5784{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005785 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005787 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Ingo Molnar48f24c42006-07-03 00:25:40 -07005789 do_each_thread(t, p) {
5790 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 continue;
5792
Ingo Molnar48f24c42006-07-03 00:25:40 -07005793 if (task_cpu(p) == src_cpu)
5794 move_task_off_dead_cpu(src_cpu, p);
5795 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005797 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
5799
Ingo Molnardd41f592007-07-09 18:51:59 +02005800/*
5801 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005802 * It does so by boosting its priority to highest possible.
5803 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 */
5805void sched_idle_next(void)
5806{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005807 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005808 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 struct task_struct *p = rq->idle;
5810 unsigned long flags;
5811
5812 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005813 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
Ingo Molnar48f24c42006-07-03 00:25:40 -07005815 /*
5816 * Strictly not necessary since rest of the CPUs are stopped by now
5817 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 */
5819 spin_lock_irqsave(&rq->lock, flags);
5820
Ingo Molnardd41f592007-07-09 18:51:59 +02005821 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005822
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005823 update_rq_clock(rq);
5824 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 spin_unlock_irqrestore(&rq->lock, flags);
5827}
5828
Ingo Molnar48f24c42006-07-03 00:25:40 -07005829/*
5830 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 * offline.
5832 */
5833void idle_task_exit(void)
5834{
5835 struct mm_struct *mm = current->active_mm;
5836
5837 BUG_ON(cpu_online(smp_processor_id()));
5838
5839 if (mm != &init_mm)
5840 switch_mm(mm, &init_mm, current);
5841 mmdrop(mm);
5842}
5843
Kirill Korotaev054b9102006-12-10 02:20:11 -08005844/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005845static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005847 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005850 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005853 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Ingo Molnar48f24c42006-07-03 00:25:40 -07005855 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 /*
5858 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005859 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 * fine.
5861 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005862 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005863 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005864 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Ingo Molnar48f24c42006-07-03 00:25:40 -07005866 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867}
5868
5869/* release_task() removes task from tasklist, so we won't find dead tasks. */
5870static void migrate_dead_tasks(unsigned int dead_cpu)
5871{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005872 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005873 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Ingo Molnardd41f592007-07-09 18:51:59 +02005875 for ( ; ; ) {
5876 if (!rq->nr_running)
5877 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005878 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005879 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005880 if (!next)
5881 break;
5882 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005883
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 }
5885}
5886#endif /* CONFIG_HOTPLUG_CPU */
5887
Nick Piggine692ab52007-07-26 13:40:43 +02005888#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5889
5890static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005891 {
5892 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005893 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005894 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005895 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005896};
5897
5898static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005899 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005900 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005901 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005902 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005903 .child = sd_ctl_dir,
5904 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005905 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005906};
5907
5908static struct ctl_table *sd_alloc_ctl_entry(int n)
5909{
5910 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005911 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005912
Nick Piggine692ab52007-07-26 13:40:43 +02005913 return entry;
5914}
5915
Milton Miller6382bc92007-10-15 17:00:19 +02005916static void sd_free_ctl_entry(struct ctl_table **tablep)
5917{
Milton Millercd790072007-10-17 16:55:11 +02005918 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005919
Milton Millercd790072007-10-17 16:55:11 +02005920 /*
5921 * In the intermediate directories, both the child directory and
5922 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005923 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005924 * static strings and all have proc handlers.
5925 */
5926 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005927 if (entry->child)
5928 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005929 if (entry->proc_handler == NULL)
5930 kfree(entry->procname);
5931 }
Milton Miller6382bc92007-10-15 17:00:19 +02005932
5933 kfree(*tablep);
5934 *tablep = NULL;
5935}
5936
Nick Piggine692ab52007-07-26 13:40:43 +02005937static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005938set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005939 const char *procname, void *data, int maxlen,
5940 mode_t mode, proc_handler *proc_handler)
5941{
Nick Piggine692ab52007-07-26 13:40:43 +02005942 entry->procname = procname;
5943 entry->data = data;
5944 entry->maxlen = maxlen;
5945 entry->mode = mode;
5946 entry->proc_handler = proc_handler;
5947}
5948
5949static struct ctl_table *
5950sd_alloc_ctl_domain_table(struct sched_domain *sd)
5951{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005952 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005953
Milton Millerad1cdc12007-10-15 17:00:19 +02005954 if (table == NULL)
5955 return NULL;
5956
Alexey Dobriyane0361852007-08-09 11:16:46 +02005957 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005958 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005959 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005960 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005961 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005962 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005963 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005964 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005965 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005966 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005967 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005968 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005969 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005970 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005971 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005972 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005973 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005974 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005975 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005976 &sd->cache_nice_tries,
5977 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005978 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005979 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005980 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005981
5982 return table;
5983}
5984
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005985static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005986{
5987 struct ctl_table *entry, *table;
5988 struct sched_domain *sd;
5989 int domain_num = 0, i;
5990 char buf[32];
5991
5992 for_each_domain(cpu, sd)
5993 domain_num++;
5994 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005995 if (table == NULL)
5996 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005997
5998 i = 0;
5999 for_each_domain(cpu, sd) {
6000 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006001 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006002 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006003 entry->child = sd_alloc_ctl_domain_table(sd);
6004 entry++;
6005 i++;
6006 }
6007 return table;
6008}
6009
6010static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006011static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006012{
6013 int i, cpu_num = num_online_cpus();
6014 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6015 char buf[32];
6016
Milton Miller73785472007-10-24 18:23:48 +02006017 WARN_ON(sd_ctl_dir[0].child);
6018 sd_ctl_dir[0].child = entry;
6019
Milton Millerad1cdc12007-10-15 17:00:19 +02006020 if (entry == NULL)
6021 return;
6022
Milton Miller97b6ea72007-10-15 17:00:19 +02006023 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006024 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006025 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006026 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006027 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006028 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006029 }
Milton Miller73785472007-10-24 18:23:48 +02006030
6031 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006032 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6033}
Milton Miller6382bc92007-10-15 17:00:19 +02006034
Milton Miller73785472007-10-24 18:23:48 +02006035/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006036static void unregister_sched_domain_sysctl(void)
6037{
Milton Miller73785472007-10-24 18:23:48 +02006038 if (sd_sysctl_header)
6039 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006040 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006041 if (sd_ctl_dir[0].child)
6042 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006043}
Nick Piggine692ab52007-07-26 13:40:43 +02006044#else
Milton Miller6382bc92007-10-15 17:00:19 +02006045static void register_sched_domain_sysctl(void)
6046{
6047}
6048static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006049{
6050}
6051#endif
6052
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053/*
6054 * migration_call - callback that gets triggered when a CPU is added.
6055 * Here we can start up the necessary migration thread for the new CPU.
6056 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006057static int __cpuinit
6058migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006061 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006063 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
6065 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006068 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006069 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 if (IS_ERR(p))
6071 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 kthread_bind(p, cpu);
6073 /* Must be high prio: stop_machine expects to yield to it. */
6074 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006075 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 task_rq_unlock(rq, &flags);
6077 cpu_rq(cpu)->migration_thread = p;
6078 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006081 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006082 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006084
6085 /* Update our root-domain */
6086 rq = cpu_rq(cpu);
6087 spin_lock_irqsave(&rq->lock, flags);
6088 if (rq->rd) {
6089 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6090 cpu_set(cpu, rq->rd->online);
6091 }
6092 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095#ifdef CONFIG_HOTPLUG_CPU
6096 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006097 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006098 if (!cpu_rq(cpu)->migration_thread)
6099 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006100 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006101 kthread_bind(cpu_rq(cpu)->migration_thread,
6102 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 kthread_stop(cpu_rq(cpu)->migration_thread);
6104 cpu_rq(cpu)->migration_thread = NULL;
6105 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006108 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006109 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 migrate_live_tasks(cpu);
6111 rq = cpu_rq(cpu);
6112 kthread_stop(rq->migration_thread);
6113 rq->migration_thread = NULL;
6114 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006115 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006116 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006117 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006119 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6120 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006122 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006123 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 migrate_nr_uninterruptible(rq);
6125 BUG_ON(rq->nr_running != 0);
6126
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006127 /*
6128 * No need to migrate the tasks: it was best-effort if
6129 * they didn't take sched_hotcpu_mutex. Just wake up
6130 * the requestors.
6131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 spin_lock_irq(&rq->lock);
6133 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006134 struct migration_req *req;
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006137 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 list_del_init(&req->list);
6139 complete(&req->done);
6140 }
6141 spin_unlock_irq(&rq->lock);
6142 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006143
Gregory Haskins08f503b2008-03-10 17:59:11 -04006144 case CPU_DYING:
6145 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006146 /* Update our root-domain */
6147 rq = cpu_rq(cpu);
6148 spin_lock_irqsave(&rq->lock, flags);
6149 if (rq->rd) {
6150 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6151 cpu_clear(cpu, rq->rd->online);
6152 }
6153 spin_unlock_irqrestore(&rq->lock, flags);
6154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155#endif
6156 }
6157 return NOTIFY_OK;
6158}
6159
6160/* Register at highest priority so that task migration (migrate_all_tasks)
6161 * happens before everything else.
6162 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006163static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 .notifier_call = migration_call,
6165 .priority = 10
6166};
6167
Adrian Bunke6fe6642007-11-09 22:39:39 +01006168void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169{
6170 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006171 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006172
6173 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006174 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6175 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6177 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178}
6179#endif
6180
6181#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006182
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006183#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006184
Mike Travis7c16ec52008-04-04 18:11:11 -07006185static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6186 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006187{
6188 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006189 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006190
Mike Travis434d53b2008-04-04 18:11:04 -07006191 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006192 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006193
6194 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6195
6196 if (!(sd->flags & SD_LOAD_BALANCE)) {
6197 printk("does not load-balance\n");
6198 if (sd->parent)
6199 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6200 " has parent");
6201 return -1;
6202 }
6203
6204 printk(KERN_CONT "span %s\n", str);
6205
6206 if (!cpu_isset(cpu, sd->span)) {
6207 printk(KERN_ERR "ERROR: domain->span does not contain "
6208 "CPU%d\n", cpu);
6209 }
6210 if (!cpu_isset(cpu, group->cpumask)) {
6211 printk(KERN_ERR "ERROR: domain->groups does not contain"
6212 " CPU%d\n", cpu);
6213 }
6214
6215 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6216 do {
6217 if (!group) {
6218 printk("\n");
6219 printk(KERN_ERR "ERROR: group is NULL\n");
6220 break;
6221 }
6222
6223 if (!group->__cpu_power) {
6224 printk(KERN_CONT "\n");
6225 printk(KERN_ERR "ERROR: domain->cpu_power not "
6226 "set\n");
6227 break;
6228 }
6229
6230 if (!cpus_weight(group->cpumask)) {
6231 printk(KERN_CONT "\n");
6232 printk(KERN_ERR "ERROR: empty group\n");
6233 break;
6234 }
6235
Mike Travis7c16ec52008-04-04 18:11:11 -07006236 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006237 printk(KERN_CONT "\n");
6238 printk(KERN_ERR "ERROR: repeated CPUs\n");
6239 break;
6240 }
6241
Mike Travis7c16ec52008-04-04 18:11:11 -07006242 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006243
Mike Travis434d53b2008-04-04 18:11:04 -07006244 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006245 printk(KERN_CONT " %s", str);
6246
6247 group = group->next;
6248 } while (group != sd->groups);
6249 printk(KERN_CONT "\n");
6250
Mike Travis7c16ec52008-04-04 18:11:11 -07006251 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006252 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6253
Mike Travis7c16ec52008-04-04 18:11:11 -07006254 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006255 printk(KERN_ERR "ERROR: parent span is not a superset "
6256 "of domain->span\n");
6257 return 0;
6258}
6259
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260static void sched_domain_debug(struct sched_domain *sd, int cpu)
6261{
Mike Travis7c16ec52008-04-04 18:11:11 -07006262 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 int level = 0;
6264
Nick Piggin41c7ce92005-06-25 14:57:24 -07006265 if (!sd) {
6266 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6267 return;
6268 }
6269
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6271
Mike Travis7c16ec52008-04-04 18:11:11 -07006272 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6273 if (!groupmask) {
6274 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6275 return;
6276 }
6277
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006278 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006279 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 level++;
6282 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006283 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006284 break;
6285 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006286 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287}
6288#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006289# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290#endif
6291
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006292static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006293{
6294 if (cpus_weight(sd->span) == 1)
6295 return 1;
6296
6297 /* Following flags need at least 2 groups */
6298 if (sd->flags & (SD_LOAD_BALANCE |
6299 SD_BALANCE_NEWIDLE |
6300 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006301 SD_BALANCE_EXEC |
6302 SD_SHARE_CPUPOWER |
6303 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006304 if (sd->groups != sd->groups->next)
6305 return 0;
6306 }
6307
6308 /* Following flags don't use groups */
6309 if (sd->flags & (SD_WAKE_IDLE |
6310 SD_WAKE_AFFINE |
6311 SD_WAKE_BALANCE))
6312 return 0;
6313
6314 return 1;
6315}
6316
Ingo Molnar48f24c42006-07-03 00:25:40 -07006317static int
6318sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006319{
6320 unsigned long cflags = sd->flags, pflags = parent->flags;
6321
6322 if (sd_degenerate(parent))
6323 return 1;
6324
6325 if (!cpus_equal(sd->span, parent->span))
6326 return 0;
6327
6328 /* Does parent contain flags not in child? */
6329 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6330 if (cflags & SD_WAKE_AFFINE)
6331 pflags &= ~SD_WAKE_BALANCE;
6332 /* Flags needing groups don't count if only 1 group in parent */
6333 if (parent->groups == parent->groups->next) {
6334 pflags &= ~(SD_LOAD_BALANCE |
6335 SD_BALANCE_NEWIDLE |
6336 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006337 SD_BALANCE_EXEC |
6338 SD_SHARE_CPUPOWER |
6339 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006340 }
6341 if (~cflags & pflags)
6342 return 0;
6343
6344 return 1;
6345}
6346
Gregory Haskins57d885f2008-01-25 21:08:18 +01006347static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6348{
6349 unsigned long flags;
6350 const struct sched_class *class;
6351
6352 spin_lock_irqsave(&rq->lock, flags);
6353
6354 if (rq->rd) {
6355 struct root_domain *old_rd = rq->rd;
6356
Ingo Molnar0eab9142008-01-25 21:08:19 +01006357 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006358 if (class->leave_domain)
6359 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006360 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006361
Gregory Haskinsdc938522008-01-25 21:08:26 +01006362 cpu_clear(rq->cpu, old_rd->span);
6363 cpu_clear(rq->cpu, old_rd->online);
6364
Gregory Haskins57d885f2008-01-25 21:08:18 +01006365 if (atomic_dec_and_test(&old_rd->refcount))
6366 kfree(old_rd);
6367 }
6368
6369 atomic_inc(&rd->refcount);
6370 rq->rd = rd;
6371
Gregory Haskinsdc938522008-01-25 21:08:26 +01006372 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006373 if (cpu_isset(rq->cpu, cpu_online_map))
6374 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006375
Ingo Molnar0eab9142008-01-25 21:08:19 +01006376 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006377 if (class->join_domain)
6378 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006379 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006380
6381 spin_unlock_irqrestore(&rq->lock, flags);
6382}
6383
Gregory Haskinsdc938522008-01-25 21:08:26 +01006384static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006385{
6386 memset(rd, 0, sizeof(*rd));
6387
Gregory Haskinsdc938522008-01-25 21:08:26 +01006388 cpus_clear(rd->span);
6389 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006390}
6391
6392static void init_defrootdomain(void)
6393{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006394 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006395 atomic_set(&def_root_domain.refcount, 1);
6396}
6397
Gregory Haskinsdc938522008-01-25 21:08:26 +01006398static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006399{
6400 struct root_domain *rd;
6401
6402 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6403 if (!rd)
6404 return NULL;
6405
Gregory Haskinsdc938522008-01-25 21:08:26 +01006406 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006407
6408 return rd;
6409}
6410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006412 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 * hold the hotplug lock.
6414 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006415static void
6416cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006418 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006419 struct sched_domain *tmp;
6420
6421 /* Remove the sched domains which do not contribute to scheduling. */
6422 for (tmp = sd; tmp; tmp = tmp->parent) {
6423 struct sched_domain *parent = tmp->parent;
6424 if (!parent)
6425 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006426 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006427 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006428 if (parent->parent)
6429 parent->parent->child = tmp;
6430 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006431 }
6432
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006433 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006434 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006435 if (sd)
6436 sd->child = NULL;
6437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439 sched_domain_debug(sd, cpu);
6440
Gregory Haskins57d885f2008-01-25 21:08:18 +01006441 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006442 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443}
6444
6445/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006446static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
6448/* Setup the mask of cpus configured for isolated domains */
6449static int __init isolated_cpu_setup(char *str)
6450{
6451 int ints[NR_CPUS], i;
6452
6453 str = get_options(str, ARRAY_SIZE(ints), ints);
6454 cpus_clear(cpu_isolated_map);
6455 for (i = 1; i <= ints[0]; i++)
6456 if (ints[i] < NR_CPUS)
6457 cpu_set(ints[i], cpu_isolated_map);
6458 return 1;
6459}
6460
Ingo Molnar8927f492007-10-15 17:00:13 +02006461__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
6463/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006464 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6465 * to a function which identifies what group(along with sched group) a CPU
6466 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6467 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 *
6469 * init_sched_build_groups will build a circular linked list of the groups
6470 * covered by the given span, and will set each group's ->cpumask correctly,
6471 * and ->cpu_power to 0.
6472 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006473static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006474init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006475 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006476 struct sched_group **sg,
6477 cpumask_t *tmpmask),
6478 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479{
6480 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 int i;
6482
Mike Travis7c16ec52008-04-04 18:11:11 -07006483 cpus_clear(*covered);
6484
6485 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006486 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006487 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 int j;
6489
Mike Travis7c16ec52008-04-04 18:11:11 -07006490 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 continue;
6492
Mike Travis7c16ec52008-04-04 18:11:11 -07006493 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006494 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495
Mike Travis7c16ec52008-04-04 18:11:11 -07006496 for_each_cpu_mask(j, *span) {
6497 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 continue;
6499
Mike Travis7c16ec52008-04-04 18:11:11 -07006500 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 cpu_set(j, sg->cpumask);
6502 }
6503 if (!first)
6504 first = sg;
6505 if (last)
6506 last->next = sg;
6507 last = sg;
6508 }
6509 last->next = first;
6510}
6511
John Hawkes9c1cfda2005-09-06 15:18:14 -07006512#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513
John Hawkes9c1cfda2005-09-06 15:18:14 -07006514#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006515
John Hawkes9c1cfda2005-09-06 15:18:14 -07006516/**
6517 * find_next_best_node - find the next node to include in a sched_domain
6518 * @node: node whose sched_domain we're building
6519 * @used_nodes: nodes already in the sched_domain
6520 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006521 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006522 * finds the closest node not already in the @used_nodes map.
6523 *
6524 * Should use nodemask_t.
6525 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006526static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006527{
6528 int i, n, val, min_val, best_node = 0;
6529
6530 min_val = INT_MAX;
6531
6532 for (i = 0; i < MAX_NUMNODES; i++) {
6533 /* Start at @node */
6534 n = (node + i) % MAX_NUMNODES;
6535
6536 if (!nr_cpus_node(n))
6537 continue;
6538
6539 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006540 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006541 continue;
6542
6543 /* Simple min distance search */
6544 val = node_distance(node, n);
6545
6546 if (val < min_val) {
6547 min_val = val;
6548 best_node = n;
6549 }
6550 }
6551
Mike Travisc5f59f02008-04-04 18:11:10 -07006552 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006553 return best_node;
6554}
6555
6556/**
6557 * sched_domain_node_span - get a cpumask for a node's sched_domain
6558 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006559 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006560 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006561 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006562 * should be one that prevents unnecessary balancing, but also spreads tasks
6563 * out optimally.
6564 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006565static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006566{
Mike Travisc5f59f02008-04-04 18:11:10 -07006567 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006568 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006569 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006570
Mike Travis4bdbaad32008-04-15 16:35:52 -07006571 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006572 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006573
Mike Travis4bdbaad32008-04-15 16:35:52 -07006574 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006575 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006576
6577 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006578 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006579
Mike Travisc5f59f02008-04-04 18:11:10 -07006580 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006581 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006582 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006583}
6584#endif
6585
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006586int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006587
John Hawkes9c1cfda2005-09-06 15:18:14 -07006588/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006589 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591#ifdef CONFIG_SCHED_SMT
6592static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006593static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006594
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006595static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006596cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6597 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006599 if (sg)
6600 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 return cpu;
6602}
6603#endif
6604
Ingo Molnar48f24c42006-07-03 00:25:40 -07006605/*
6606 * multi-core sched-domains:
6607 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006608#ifdef CONFIG_SCHED_MC
6609static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006610static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006611#endif
6612
6613#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006614static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006615cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6616 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006617{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006618 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006619
6620 *mask = per_cpu(cpu_sibling_map, cpu);
6621 cpus_and(*mask, *mask, *cpu_map);
6622 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006623 if (sg)
6624 *sg = &per_cpu(sched_group_core, group);
6625 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006626}
6627#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006628static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006629cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6630 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006631{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006632 if (sg)
6633 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006634 return cpu;
6635}
6636#endif
6637
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006639static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006640
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006641static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006642cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6643 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006645 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006646#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07006647 *mask = cpu_coregroup_map(cpu);
6648 cpus_and(*mask, *mask, *cpu_map);
6649 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006650#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07006651 *mask = per_cpu(cpu_sibling_map, cpu);
6652 cpus_and(*mask, *mask, *cpu_map);
6653 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006655 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006657 if (sg)
6658 *sg = &per_cpu(sched_group_phys, group);
6659 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660}
6661
6662#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006663/*
6664 * The init_sched_build_groups can't handle what we want to do with node
6665 * groups, so roll our own. Now each node has its own list of groups which
6666 * gets dynamically allocated.
6667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006669static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006670
6671static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006672static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006673
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006674static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006675 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006677 int group;
6678
Mike Travis7c16ec52008-04-04 18:11:11 -07006679 *nodemask = node_to_cpumask(cpu_to_node(cpu));
6680 cpus_and(*nodemask, *nodemask, *cpu_map);
6681 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006682
6683 if (sg)
6684 *sg = &per_cpu(sched_group_allnodes, group);
6685 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006687
Siddha, Suresh B08069032006-03-27 01:15:23 -08006688static void init_numa_sched_groups_power(struct sched_group *group_head)
6689{
6690 struct sched_group *sg = group_head;
6691 int j;
6692
6693 if (!sg)
6694 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006695 do {
6696 for_each_cpu_mask(j, sg->cpumask) {
6697 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006698
Andi Kleen3a5c3592007-10-15 17:00:14 +02006699 sd = &per_cpu(phys_domains, j);
6700 if (j != first_cpu(sd->groups->cpumask)) {
6701 /*
6702 * Only add "power" once for each
6703 * physical package.
6704 */
6705 continue;
6706 }
6707
6708 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006709 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006710 sg = sg->next;
6711 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713#endif
6714
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006715#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006716/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07006717static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006718{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006719 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006720
6721 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006722 struct sched_group **sched_group_nodes
6723 = sched_group_nodes_bycpu[cpu];
6724
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006725 if (!sched_group_nodes)
6726 continue;
6727
6728 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006729 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6730
Mike Travis7c16ec52008-04-04 18:11:11 -07006731 *nodemask = node_to_cpumask(i);
6732 cpus_and(*nodemask, *nodemask, *cpu_map);
6733 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006734 continue;
6735
6736 if (sg == NULL)
6737 continue;
6738 sg = sg->next;
6739next_sg:
6740 oldsg = sg;
6741 sg = sg->next;
6742 kfree(oldsg);
6743 if (oldsg != sched_group_nodes[i])
6744 goto next_sg;
6745 }
6746 kfree(sched_group_nodes);
6747 sched_group_nodes_bycpu[cpu] = NULL;
6748 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006749}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006750#else
Mike Travis7c16ec52008-04-04 18:11:11 -07006751static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006752{
6753}
6754#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006755
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006757 * Initialize sched groups cpu_power.
6758 *
6759 * cpu_power indicates the capacity of sched group, which is used while
6760 * distributing the load between different sched groups in a sched domain.
6761 * Typically cpu_power for all the groups in a sched domain will be same unless
6762 * there are asymmetries in the topology. If there are asymmetries, group
6763 * having more cpu_power will pickup more load compared to the group having
6764 * less cpu_power.
6765 *
6766 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6767 * the maximum number of tasks a group can handle in the presence of other idle
6768 * or lightly loaded groups in the same sched domain.
6769 */
6770static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6771{
6772 struct sched_domain *child;
6773 struct sched_group *group;
6774
6775 WARN_ON(!sd || !sd->groups);
6776
6777 if (cpu != first_cpu(sd->groups->cpumask))
6778 return;
6779
6780 child = sd->child;
6781
Eric Dumazet5517d862007-05-08 00:32:57 -07006782 sd->groups->__cpu_power = 0;
6783
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006784 /*
6785 * For perf policy, if the groups in child domain share resources
6786 * (for example cores sharing some portions of the cache hierarchy
6787 * or SMT), then set this domain groups cpu_power such that each group
6788 * can handle only one task, when there are other idle groups in the
6789 * same sched domain.
6790 */
6791 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6792 (child->flags &
6793 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006794 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006795 return;
6796 }
6797
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006798 /*
6799 * add cpu_power of each child group to this groups cpu_power
6800 */
6801 group = child->groups;
6802 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006803 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006804 group = group->next;
6805 } while (group != child->groups);
6806}
6807
6808/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006809 * Initializers for schedule domains
6810 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6811 */
6812
6813#define SD_INIT(sd, type) sd_init_##type(sd)
6814#define SD_INIT_FUNC(type) \
6815static noinline void sd_init_##type(struct sched_domain *sd) \
6816{ \
6817 memset(sd, 0, sizeof(*sd)); \
6818 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006819 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07006820}
6821
6822SD_INIT_FUNC(CPU)
6823#ifdef CONFIG_NUMA
6824 SD_INIT_FUNC(ALLNODES)
6825 SD_INIT_FUNC(NODE)
6826#endif
6827#ifdef CONFIG_SCHED_SMT
6828 SD_INIT_FUNC(SIBLING)
6829#endif
6830#ifdef CONFIG_SCHED_MC
6831 SD_INIT_FUNC(MC)
6832#endif
6833
6834/*
6835 * To minimize stack usage kmalloc room for cpumasks and share the
6836 * space as the usage in build_sched_domains() dictates. Used only
6837 * if the amount of space is significant.
6838 */
6839struct allmasks {
6840 cpumask_t tmpmask; /* make this one first */
6841 union {
6842 cpumask_t nodemask;
6843 cpumask_t this_sibling_map;
6844 cpumask_t this_core_map;
6845 };
6846 cpumask_t send_covered;
6847
6848#ifdef CONFIG_NUMA
6849 cpumask_t domainspan;
6850 cpumask_t covered;
6851 cpumask_t notcovered;
6852#endif
6853};
6854
6855#if NR_CPUS > 128
6856#define SCHED_CPUMASK_ALLOC 1
6857#define SCHED_CPUMASK_FREE(v) kfree(v)
6858#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
6859#else
6860#define SCHED_CPUMASK_ALLOC 0
6861#define SCHED_CPUMASK_FREE(v)
6862#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
6863#endif
6864
6865#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
6866 ((unsigned long)(a) + offsetof(struct allmasks, v))
6867
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006868static int default_relax_domain_level = -1;
6869
6870static int __init setup_relax_domain_level(char *str)
6871{
6872 default_relax_domain_level = simple_strtoul(str, NULL, 0);
6873 return 1;
6874}
6875__setup("relax_domain_level=", setup_relax_domain_level);
6876
6877static void set_domain_attribute(struct sched_domain *sd,
6878 struct sched_domain_attr *attr)
6879{
6880 int request;
6881
6882 if (!attr || attr->relax_domain_level < 0) {
6883 if (default_relax_domain_level < 0)
6884 return;
6885 else
6886 request = default_relax_domain_level;
6887 } else
6888 request = attr->relax_domain_level;
6889 if (request < sd->level) {
6890 /* turn off idle balance on this domain */
6891 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
6892 } else {
6893 /* turn on idle balance on this domain */
6894 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
6895 }
6896}
6897
Mike Travis7c16ec52008-04-04 18:11:11 -07006898/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006899 * Build sched domains for a given set of cpus and attach the sched domains
6900 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006902static int __build_sched_domains(const cpumask_t *cpu_map,
6903 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904{
6905 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006906 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006907 SCHED_CPUMASK_DECLARE(allmasks);
6908 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07006909#ifdef CONFIG_NUMA
6910 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006911 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006912
6913 /*
6914 * Allocate the per-node list of sched groups
6915 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006916 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006917 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006918 if (!sched_group_nodes) {
6919 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006920 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006921 }
John Hawkesd1b55132005-09-06 15:18:14 -07006922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
Gregory Haskinsdc938522008-01-25 21:08:26 +01006924 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006925 if (!rd) {
6926 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07006927#ifdef CONFIG_NUMA
6928 kfree(sched_group_nodes);
6929#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006930 return -ENOMEM;
6931 }
6932
Mike Travis7c16ec52008-04-04 18:11:11 -07006933#if SCHED_CPUMASK_ALLOC
6934 /* get space for all scratch cpumask variables */
6935 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
6936 if (!allmasks) {
6937 printk(KERN_WARNING "Cannot alloc cpumask array\n");
6938 kfree(rd);
6939#ifdef CONFIG_NUMA
6940 kfree(sched_group_nodes);
6941#endif
6942 return -ENOMEM;
6943 }
6944#endif
6945 tmpmask = (cpumask_t *)allmasks;
6946
6947
6948#ifdef CONFIG_NUMA
6949 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6950#endif
6951
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006953 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006955 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07006957 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
Mike Travis7c16ec52008-04-04 18:11:11 -07006959 *nodemask = node_to_cpumask(cpu_to_node(i));
6960 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
6962#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006963 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07006964 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006965 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006966 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006967 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006968 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07006969 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006970 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006971 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006972 } else
6973 p = NULL;
6974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006976 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006977 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006978 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006979 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006980 if (p)
6981 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006982 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983#endif
6984
6985 p = sd;
6986 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006987 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006988 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07006989 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006991 if (p)
6992 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07006993 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006995#ifdef CONFIG_SCHED_MC
6996 p = sd;
6997 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07006998 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006999 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007000 sd->span = cpu_coregroup_map(i);
7001 cpus_and(sd->span, sd->span, *cpu_map);
7002 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007003 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007004 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007005#endif
7006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007#ifdef CONFIG_SCHED_SMT
7008 p = sd;
7009 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007010 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007011 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007012 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007013 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007015 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007016 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017#endif
7018 }
7019
7020#ifdef CONFIG_SCHED_SMT
7021 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007022 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007023 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7024 SCHED_CPUMASK_VAR(send_covered, allmasks);
7025
7026 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7027 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7028 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 continue;
7030
Ingo Molnardd41f592007-07-09 18:51:59 +02007031 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007032 &cpu_to_cpu_group,
7033 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 }
7035#endif
7036
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007037#ifdef CONFIG_SCHED_MC
7038 /* Set up multi-core groups */
7039 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007040 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7041 SCHED_CPUMASK_VAR(send_covered, allmasks);
7042
7043 *this_core_map = cpu_coregroup_map(i);
7044 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7045 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007046 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007047
Ingo Molnardd41f592007-07-09 18:51:59 +02007048 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007049 &cpu_to_core_group,
7050 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007051 }
7052#endif
7053
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 /* Set up physical groups */
7055 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007056 SCHED_CPUMASK_VAR(nodemask, allmasks);
7057 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
Mike Travis7c16ec52008-04-04 18:11:11 -07007059 *nodemask = node_to_cpumask(i);
7060 cpus_and(*nodemask, *nodemask, *cpu_map);
7061 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 continue;
7063
Mike Travis7c16ec52008-04-04 18:11:11 -07007064 init_sched_build_groups(nodemask, cpu_map,
7065 &cpu_to_phys_group,
7066 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 }
7068
7069#ifdef CONFIG_NUMA
7070 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007071 if (sd_allnodes) {
7072 SCHED_CPUMASK_VAR(send_covered, allmasks);
7073
7074 init_sched_build_groups(cpu_map, cpu_map,
7075 &cpu_to_allnodes_group,
7076 send_covered, tmpmask);
7077 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078
7079 for (i = 0; i < MAX_NUMNODES; i++) {
7080 /* Set up node groups */
7081 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007082 SCHED_CPUMASK_VAR(nodemask, allmasks);
7083 SCHED_CPUMASK_VAR(domainspan, allmasks);
7084 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007085 int j;
7086
Mike Travis7c16ec52008-04-04 18:11:11 -07007087 *nodemask = node_to_cpumask(i);
7088 cpus_clear(*covered);
7089
7090 cpus_and(*nodemask, *nodemask, *cpu_map);
7091 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007092 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007093 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007094 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007095
Mike Travis4bdbaad32008-04-15 16:35:52 -07007096 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007097 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007098
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007099 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007100 if (!sg) {
7101 printk(KERN_WARNING "Can not alloc domain group for "
7102 "node %d\n", i);
7103 goto error;
7104 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007105 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007106 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007107 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007108
John Hawkes9c1cfda2005-09-06 15:18:14 -07007109 sd = &per_cpu(node_domains, j);
7110 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007111 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007112 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007113 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007114 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007115 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007116 prev = sg;
7117
7118 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007119 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007120 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007121 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007122
Mike Travis7c16ec52008-04-04 18:11:11 -07007123 cpus_complement(*notcovered, *covered);
7124 cpus_and(*tmpmask, *notcovered, *cpu_map);
7125 cpus_and(*tmpmask, *tmpmask, *domainspan);
7126 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007127 break;
7128
Mike Travis7c16ec52008-04-04 18:11:11 -07007129 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7130 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007131 continue;
7132
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007133 sg = kmalloc_node(sizeof(struct sched_group),
7134 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007135 if (!sg) {
7136 printk(KERN_WARNING
7137 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007138 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007139 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007140 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007141 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007142 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007143 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007144 prev->next = sg;
7145 prev = sg;
7146 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148#endif
7149
7150 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007151#ifdef CONFIG_SCHED_SMT
7152 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007153 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7154
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007155 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007156 }
7157#endif
7158#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007159 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007160 struct sched_domain *sd = &per_cpu(core_domains, i);
7161
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007162 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007163 }
7164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007166 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007167 struct sched_domain *sd = &per_cpu(phys_domains, i);
7168
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007169 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 }
7171
John Hawkes9c1cfda2005-09-06 15:18:14 -07007172#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007173 for (i = 0; i < MAX_NUMNODES; i++)
7174 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007175
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007176 if (sd_allnodes) {
7177 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007178
Mike Travis7c16ec52008-04-04 18:11:11 -07007179 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7180 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007181 init_numa_sched_groups_power(sg);
7182 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007183#endif
7184
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007186 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 struct sched_domain *sd;
7188#ifdef CONFIG_SCHED_SMT
7189 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007190#elif defined(CONFIG_SCHED_MC)
7191 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192#else
7193 sd = &per_cpu(phys_domains, i);
7194#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007195 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007197
Mike Travis7c16ec52008-04-04 18:11:11 -07007198 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007199 return 0;
7200
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007201#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007202error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007203 free_sched_groups(cpu_map, tmpmask);
7204 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007205 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207}
Paul Jackson029190c2007-10-18 23:40:20 -07007208
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007209static int build_sched_domains(const cpumask_t *cpu_map)
7210{
7211 return __build_sched_domains(cpu_map, NULL);
7212}
7213
Paul Jackson029190c2007-10-18 23:40:20 -07007214static cpumask_t *doms_cur; /* current sched domains */
7215static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007216static struct sched_domain_attr *dattr_cur;
7217 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007218
7219/*
7220 * Special case: If a kmalloc of a doms_cur partition (array of
7221 * cpumask_t) fails, then fallback to a single sched domain,
7222 * as determined by the single cpumask_t fallback_doms.
7223 */
7224static cpumask_t fallback_doms;
7225
Heiko Carstens22e52b02008-03-12 18:31:59 +01007226void __attribute__((weak)) arch_update_cpu_topology(void)
7227{
7228}
7229
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007230/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007231 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007232 * For now this just excludes isolated cpus, but could be used to
7233 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007234 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007235static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007236{
Milton Miller73785472007-10-24 18:23:48 +02007237 int err;
7238
Heiko Carstens22e52b02008-03-12 18:31:59 +01007239 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007240 ndoms_cur = 1;
7241 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7242 if (!doms_cur)
7243 doms_cur = &fallback_doms;
7244 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007245 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007246 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007247 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007248
7249 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007250}
7251
Mike Travis7c16ec52008-04-04 18:11:11 -07007252static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7253 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254{
Mike Travis7c16ec52008-04-04 18:11:11 -07007255 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007258/*
7259 * Detach sched domains from a group of cpus specified in cpu_map
7260 * These cpus will now be attached to the NULL domain
7261 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007262static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007263{
Mike Travis7c16ec52008-04-04 18:11:11 -07007264 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007265 int i;
7266
Milton Miller6382bc92007-10-15 17:00:19 +02007267 unregister_sched_domain_sysctl();
7268
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007269 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007270 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007271 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007272 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007273}
7274
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007275/* handle null as "default" */
7276static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7277 struct sched_domain_attr *new, int idx_new)
7278{
7279 struct sched_domain_attr tmp;
7280
7281 /* fast path */
7282 if (!new && !cur)
7283 return 1;
7284
7285 tmp = SD_ATTR_INIT;
7286 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7287 new ? (new + idx_new) : &tmp,
7288 sizeof(struct sched_domain_attr));
7289}
7290
Paul Jackson029190c2007-10-18 23:40:20 -07007291/*
7292 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007293 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007294 * doms_new[] to the current sched domain partitioning, doms_cur[].
7295 * It destroys each deleted domain and builds each new domain.
7296 *
7297 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007298 * The masks don't intersect (don't overlap.) We should setup one
7299 * sched domain for each mask. CPUs not in any of the cpumasks will
7300 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007301 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7302 * it as it is.
7303 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007304 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7305 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007306 * failed the kmalloc call, then it can pass in doms_new == NULL,
7307 * and partition_sched_domains() will fallback to the single partition
7308 * 'fallback_doms'.
7309 *
7310 * Call with hotplug lock held
7311 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007312void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7313 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007314{
7315 int i, j;
7316
Heiko Carstens712555e2008-04-28 11:33:07 +02007317 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007318
Milton Miller73785472007-10-24 18:23:48 +02007319 /* always unregister in case we don't destroy any domains */
7320 unregister_sched_domain_sysctl();
7321
Paul Jackson029190c2007-10-18 23:40:20 -07007322 if (doms_new == NULL) {
7323 ndoms_new = 1;
7324 doms_new = &fallback_doms;
7325 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007326 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007327 }
7328
7329 /* Destroy deleted domains */
7330 for (i = 0; i < ndoms_cur; i++) {
7331 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007332 if (cpus_equal(doms_cur[i], doms_new[j])
7333 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007334 goto match1;
7335 }
7336 /* no match - a current sched domain not in new doms_new[] */
7337 detach_destroy_domains(doms_cur + i);
7338match1:
7339 ;
7340 }
7341
7342 /* Build new domains */
7343 for (i = 0; i < ndoms_new; i++) {
7344 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007345 if (cpus_equal(doms_new[i], doms_cur[j])
7346 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007347 goto match2;
7348 }
7349 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007350 __build_sched_domains(doms_new + i,
7351 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007352match2:
7353 ;
7354 }
7355
7356 /* Remember the new sched domains */
7357 if (doms_cur != &fallback_doms)
7358 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007359 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007360 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007361 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007362 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007363
7364 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007365
Heiko Carstens712555e2008-04-28 11:33:07 +02007366 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007367}
7368
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007369#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007370int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007371{
7372 int err;
7373
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007374 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007375 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007376 detach_destroy_domains(&cpu_online_map);
7377 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007378 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007379 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007380
7381 return err;
7382}
7383
7384static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7385{
7386 int ret;
7387
7388 if (buf[0] != '0' && buf[0] != '1')
7389 return -EINVAL;
7390
7391 if (smt)
7392 sched_smt_power_savings = (buf[0] == '1');
7393 else
7394 sched_mc_power_savings = (buf[0] == '1');
7395
7396 ret = arch_reinit_sched_domains();
7397
7398 return ret ? ret : count;
7399}
7400
Adrian Bunk6707de002007-08-12 18:08:19 +02007401#ifdef CONFIG_SCHED_MC
7402static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7403{
7404 return sprintf(page, "%u\n", sched_mc_power_savings);
7405}
7406static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7407 const char *buf, size_t count)
7408{
7409 return sched_power_savings_store(buf, count, 0);
7410}
7411static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7412 sched_mc_power_savings_store);
7413#endif
7414
7415#ifdef CONFIG_SCHED_SMT
7416static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7417{
7418 return sprintf(page, "%u\n", sched_smt_power_savings);
7419}
7420static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7421 const char *buf, size_t count)
7422{
7423 return sched_power_savings_store(buf, count, 1);
7424}
7425static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7426 sched_smt_power_savings_store);
7427#endif
7428
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007429int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7430{
7431 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007432
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007433#ifdef CONFIG_SCHED_SMT
7434 if (smt_capable())
7435 err = sysfs_create_file(&cls->kset.kobj,
7436 &attr_sched_smt_power_savings.attr);
7437#endif
7438#ifdef CONFIG_SCHED_MC
7439 if (!err && mc_capable())
7440 err = sysfs_create_file(&cls->kset.kobj,
7441 &attr_sched_mc_power_savings.attr);
7442#endif
7443 return err;
7444}
7445#endif
7446
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007448 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007450 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 * which will prevent rebalancing while the sched domains are recalculated.
7452 */
7453static int update_sched_domains(struct notifier_block *nfb,
7454 unsigned long action, void *hcpu)
7455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 switch (action) {
7457 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007458 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007460 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007461 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 return NOTIFY_OK;
7463
7464 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007465 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007467 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007469 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007471 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 /*
7473 * Fall through and re-initialise the domains.
7474 */
7475 break;
7476 default:
7477 return NOTIFY_DONE;
7478 }
7479
7480 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007481 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
7483 return NOTIFY_OK;
7484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486void __init sched_init_smp(void)
7487{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007488 cpumask_t non_isolated_cpus;
7489
Mike Travis434d53b2008-04-04 18:11:04 -07007490#if defined(CONFIG_NUMA)
7491 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7492 GFP_KERNEL);
7493 BUG_ON(sched_group_nodes_bycpu == NULL);
7494#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007495 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007496 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007497 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007498 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007499 if (cpus_empty(non_isolated_cpus))
7500 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007501 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007502 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 /* XXX: Theoretical race here - CPU may be hotplugged now */
7504 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007505 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007506
7507 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007508 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007509 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007510 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511}
7512#else
7513void __init sched_init_smp(void)
7514{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007515 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516}
7517#endif /* CONFIG_SMP */
7518
7519int in_sched_functions(unsigned long addr)
7520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 return in_lock_functions(addr) ||
7522 (addr >= (unsigned long)__sched_text_start
7523 && addr < (unsigned long)__sched_text_end);
7524}
7525
Alexey Dobriyana9957442007-10-15 17:00:13 +02007526static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007527{
7528 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007529 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007530#ifdef CONFIG_FAIR_GROUP_SCHED
7531 cfs_rq->rq = rq;
7532#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007533 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007534}
7535
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007536static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7537{
7538 struct rt_prio_array *array;
7539 int i;
7540
7541 array = &rt_rq->active;
7542 for (i = 0; i < MAX_RT_PRIO; i++) {
7543 INIT_LIST_HEAD(array->queue + i);
7544 __clear_bit(i, array->bitmap);
7545 }
7546 /* delimiter for bitsearch: */
7547 __set_bit(MAX_RT_PRIO, array->bitmap);
7548
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007549#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007550 rt_rq->highest_prio = MAX_RT_PRIO;
7551#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007552#ifdef CONFIG_SMP
7553 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007554 rt_rq->overloaded = 0;
7555#endif
7556
7557 rt_rq->rt_time = 0;
7558 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007559 rt_rq->rt_runtime = 0;
7560 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007561
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007562#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007563 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007564 rt_rq->rq = rq;
7565#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007566}
7567
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007568#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007569static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7570 struct sched_entity *se, int cpu, int add,
7571 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007572{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007573 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007574 tg->cfs_rq[cpu] = cfs_rq;
7575 init_cfs_rq(cfs_rq, rq);
7576 cfs_rq->tg = tg;
7577 if (add)
7578 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7579
7580 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007581 /* se could be NULL for init_task_group */
7582 if (!se)
7583 return;
7584
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007585 if (!parent)
7586 se->cfs_rq = &rq->cfs;
7587 else
7588 se->cfs_rq = parent->my_q;
7589
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007590 se->my_q = cfs_rq;
7591 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007592 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007593 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007594}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007595#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007596
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007597#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007598static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7599 struct sched_rt_entity *rt_se, int cpu, int add,
7600 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007601{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007602 struct rq *rq = cpu_rq(cpu);
7603
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007604 tg->rt_rq[cpu] = rt_rq;
7605 init_rt_rq(rt_rq, rq);
7606 rt_rq->tg = tg;
7607 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007608 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007609 if (add)
7610 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7611
7612 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007613 if (!rt_se)
7614 return;
7615
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007616 if (!parent)
7617 rt_se->rt_rq = &rq->rt;
7618 else
7619 rt_se->rt_rq = parent->my_q;
7620
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007621 rt_se->rt_rq = &rq->rt;
7622 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007623 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007624 INIT_LIST_HEAD(&rt_se->run_list);
7625}
7626#endif
7627
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628void __init sched_init(void)
7629{
Ingo Molnardd41f592007-07-09 18:51:59 +02007630 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007631 unsigned long alloc_size = 0, ptr;
7632
7633#ifdef CONFIG_FAIR_GROUP_SCHED
7634 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7635#endif
7636#ifdef CONFIG_RT_GROUP_SCHED
7637 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7638#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007639#ifdef CONFIG_USER_SCHED
7640 alloc_size *= 2;
7641#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007642 /*
7643 * As sched_init() is called before page_alloc is setup,
7644 * we use alloc_bootmem().
7645 */
7646 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07007647 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07007648
7649#ifdef CONFIG_FAIR_GROUP_SCHED
7650 init_task_group.se = (struct sched_entity **)ptr;
7651 ptr += nr_cpu_ids * sizeof(void **);
7652
7653 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7654 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007655
7656#ifdef CONFIG_USER_SCHED
7657 root_task_group.se = (struct sched_entity **)ptr;
7658 ptr += nr_cpu_ids * sizeof(void **);
7659
7660 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
7661 ptr += nr_cpu_ids * sizeof(void **);
7662#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007663#endif
7664#ifdef CONFIG_RT_GROUP_SCHED
7665 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7666 ptr += nr_cpu_ids * sizeof(void **);
7667
7668 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007669 ptr += nr_cpu_ids * sizeof(void **);
7670
7671#ifdef CONFIG_USER_SCHED
7672 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
7673 ptr += nr_cpu_ids * sizeof(void **);
7674
7675 root_task_group.rt_rq = (struct rt_rq **)ptr;
7676 ptr += nr_cpu_ids * sizeof(void **);
7677#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007678#endif
7679 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007680
Gregory Haskins57d885f2008-01-25 21:08:18 +01007681#ifdef CONFIG_SMP
7682 init_defrootdomain();
7683#endif
7684
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007685 init_rt_bandwidth(&def_rt_bandwidth,
7686 global_rt_period(), global_rt_runtime());
7687
7688#ifdef CONFIG_RT_GROUP_SCHED
7689 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7690 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007691#ifdef CONFIG_USER_SCHED
7692 init_rt_bandwidth(&root_task_group.rt_bandwidth,
7693 global_rt_period(), RUNTIME_INF);
7694#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007695#endif
7696
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007697#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007698 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007699 INIT_LIST_HEAD(&init_task_group.children);
7700
7701#ifdef CONFIG_USER_SCHED
7702 INIT_LIST_HEAD(&root_task_group.children);
7703 init_task_group.parent = &root_task_group;
7704 list_add(&init_task_group.siblings, &root_task_group.children);
7705#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007706#endif
7707
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007708 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007709 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710
7711 rq = cpu_rq(i);
7712 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007713 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007714 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007715 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007716 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007717#ifdef CONFIG_FAIR_GROUP_SCHED
7718 init_task_group.shares = init_task_group_load;
7719 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007720#ifdef CONFIG_CGROUP_SCHED
7721 /*
7722 * How much cpu bandwidth does init_task_group get?
7723 *
7724 * In case of task-groups formed thr' the cgroup filesystem, it
7725 * gets 100% of the cpu resources in the system. This overall
7726 * system cpu resource is divided among the tasks of
7727 * init_task_group and its child task-groups in a fair manner,
7728 * based on each entity's (task or task-group's) weight
7729 * (se->load.weight).
7730 *
7731 * In other words, if init_task_group has 10 tasks of weight
7732 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7733 * then A0's share of the cpu resource is:
7734 *
7735 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
7736 *
7737 * We achieve this by letting init_task_group's tasks sit
7738 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7739 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007740 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007741#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007742 root_task_group.shares = NICE_0_LOAD;
7743 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007744 /*
7745 * In case of task-groups formed thr' the user id of tasks,
7746 * init_task_group represents tasks belonging to root user.
7747 * Hence it forms a sibling of all subsequent groups formed.
7748 * In this case, init_task_group gets only a fraction of overall
7749 * system cpu resource, based on the weight assigned to root
7750 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
7751 * by letting tasks of init_task_group sit in a separate cfs_rq
7752 * (init_cfs_rq) and having one entity represent this group of
7753 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
7754 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007755 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007756 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007757 &per_cpu(init_sched_entity, i), i, 1,
7758 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007759
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007760#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02007761#endif /* CONFIG_FAIR_GROUP_SCHED */
7762
7763 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007764#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007765 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007766#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007767 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007768#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007769 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007770 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007771 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007772 &per_cpu(init_sched_rt_entity, i), i, 1,
7773 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007774#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
Ingo Molnardd41f592007-07-09 18:51:59 +02007777 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7778 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007780 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007781 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007783 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007785 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 rq->migration_thread = NULL;
7787 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007788 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007790 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 }
7793
Peter Williams2dd73a42006-06-27 02:54:34 -07007794 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007795
Avi Kivitye107be32007-07-26 13:40:43 +02007796#ifdef CONFIG_PREEMPT_NOTIFIERS
7797 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7798#endif
7799
Christoph Lameterc9819f42006-12-10 02:20:25 -08007800#ifdef CONFIG_SMP
7801 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7802#endif
7803
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007804#ifdef CONFIG_RT_MUTEXES
7805 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7806#endif
7807
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 /*
7809 * The boot idle thread does lazy MMU switching as well:
7810 */
7811 atomic_inc(&init_mm.mm_count);
7812 enter_lazy_tlb(&init_mm, current);
7813
7814 /*
7815 * Make us the idle thread. Technically, schedule() should not be
7816 * called from this thread, however somewhere below it might be,
7817 * but because we are the idle thread, we just pick up running again
7818 * when this runqueue becomes "idle".
7819 */
7820 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007821 /*
7822 * During early bootup we pretend to be a normal task:
7823 */
7824 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007825
7826 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827}
7828
7829#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7830void __might_sleep(char *file, int line)
7831{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007832#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 static unsigned long prev_jiffy; /* ratelimiting */
7834
7835 if ((in_atomic() || irqs_disabled()) &&
7836 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7837 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7838 return;
7839 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007840 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 " context at %s:%d\n", file, line);
7842 printk("in_atomic():%d, irqs_disabled():%d\n",
7843 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007844 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007845 if (irqs_disabled())
7846 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 dump_stack();
7848 }
7849#endif
7850}
7851EXPORT_SYMBOL(__might_sleep);
7852#endif
7853
7854#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007855static void normalize_task(struct rq *rq, struct task_struct *p)
7856{
7857 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007858
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007859 update_rq_clock(rq);
7860 on_rq = p->se.on_rq;
7861 if (on_rq)
7862 deactivate_task(rq, p, 0);
7863 __setscheduler(rq, p, SCHED_NORMAL, 0);
7864 if (on_rq) {
7865 activate_task(rq, p, 0);
7866 resched_task(rq->curr);
7867 }
7868}
7869
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870void normalize_rt_tasks(void)
7871{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007872 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007874 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007876 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007877 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007878 /*
7879 * Only normalize user tasks:
7880 */
7881 if (!p->mm)
7882 continue;
7883
Ingo Molnardd41f592007-07-09 18:51:59 +02007884 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007885#ifdef CONFIG_SCHEDSTATS
7886 p->se.wait_start = 0;
7887 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007888 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007889#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007890
7891 if (!rt_task(p)) {
7892 /*
7893 * Renice negative nice level userspace
7894 * tasks back to 0:
7895 */
7896 if (TASK_NICE(p) < 0 && p->mm)
7897 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007901 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007902 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903
Ingo Molnar178be792007-10-15 17:00:18 +02007904 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007905
Ingo Molnarb29739f2006-06-27 02:54:51 -07007906 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007907 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007908 } while_each_thread(g, p);
7909
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007910 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911}
7912
7913#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007914
7915#ifdef CONFIG_IA64
7916/*
7917 * These functions are only useful for the IA64 MCA handling.
7918 *
7919 * They can only be called when the whole system has been
7920 * stopped - every CPU needs to be quiescent, and no scheduling
7921 * activity can take place. Using them for anything else would
7922 * be a serious bug, and as a result, they aren't even visible
7923 * under any other configuration.
7924 */
7925
7926/**
7927 * curr_task - return the current task for a given cpu.
7928 * @cpu: the processor in question.
7929 *
7930 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7931 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007932struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007933{
7934 return cpu_curr(cpu);
7935}
7936
7937/**
7938 * set_curr_task - set the current task for a given cpu.
7939 * @cpu: the processor in question.
7940 * @p: the task pointer to set.
7941 *
7942 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007943 * are serviced on a separate stack. It allows the architecture to switch the
7944 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007945 * must be called with all CPU's synchronized, and interrupts disabled, the
7946 * and caller must save the original value of the current task (see
7947 * curr_task() above) and restore that value before reenabling interrupts and
7948 * re-starting the system.
7949 *
7950 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7951 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007952void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007953{
7954 cpu_curr(cpu) = p;
7955}
7956
7957#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007958
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007959#ifdef CONFIG_FAIR_GROUP_SCHED
7960static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007961{
7962 int i;
7963
7964 for_each_possible_cpu(i) {
7965 if (tg->cfs_rq)
7966 kfree(tg->cfs_rq[i]);
7967 if (tg->se)
7968 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007969 }
7970
7971 kfree(tg->cfs_rq);
7972 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007973}
7974
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007975static
7976int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007977{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007978 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007979 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007980 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007981 int i;
7982
Mike Travis434d53b2008-04-04 18:11:04 -07007983 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007984 if (!tg->cfs_rq)
7985 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07007986 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007987 if (!tg->se)
7988 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007989
7990 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007991
7992 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007993 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007994
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007995 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7996 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007997 if (!cfs_rq)
7998 goto err;
7999
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008000 se = kmalloc_node(sizeof(struct sched_entity),
8001 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008002 if (!se)
8003 goto err;
8004
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008005 parent_se = parent ? parent->se[i] : NULL;
8006 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008007 }
8008
8009 return 1;
8010
8011 err:
8012 return 0;
8013}
8014
8015static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8016{
8017 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8018 &cpu_rq(cpu)->leaf_cfs_rq_list);
8019}
8020
8021static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8022{
8023 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8024}
8025#else
8026static inline void free_fair_sched_group(struct task_group *tg)
8027{
8028}
8029
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008030static inline
8031int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008032{
8033 return 1;
8034}
8035
8036static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8037{
8038}
8039
8040static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8041{
8042}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008043#endif
8044
8045#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008046static void free_rt_sched_group(struct task_group *tg)
8047{
8048 int i;
8049
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008050 destroy_rt_bandwidth(&tg->rt_bandwidth);
8051
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008052 for_each_possible_cpu(i) {
8053 if (tg->rt_rq)
8054 kfree(tg->rt_rq[i]);
8055 if (tg->rt_se)
8056 kfree(tg->rt_se[i]);
8057 }
8058
8059 kfree(tg->rt_rq);
8060 kfree(tg->rt_se);
8061}
8062
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008063static
8064int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008065{
8066 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008067 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008068 struct rq *rq;
8069 int i;
8070
Mike Travis434d53b2008-04-04 18:11:04 -07008071 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008072 if (!tg->rt_rq)
8073 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008074 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008075 if (!tg->rt_se)
8076 goto err;
8077
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008078 init_rt_bandwidth(&tg->rt_bandwidth,
8079 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008080
8081 for_each_possible_cpu(i) {
8082 rq = cpu_rq(i);
8083
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008084 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8085 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8086 if (!rt_rq)
8087 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008088
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008089 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8090 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8091 if (!rt_se)
8092 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008093
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008094 parent_se = parent ? parent->rt_se[i] : NULL;
8095 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008096 }
8097
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008098 return 1;
8099
8100 err:
8101 return 0;
8102}
8103
8104static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8105{
8106 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8107 &cpu_rq(cpu)->leaf_rt_rq_list);
8108}
8109
8110static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8111{
8112 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8113}
8114#else
8115static inline void free_rt_sched_group(struct task_group *tg)
8116{
8117}
8118
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008119static inline
8120int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008121{
8122 return 1;
8123}
8124
8125static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8126{
8127}
8128
8129static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8130{
8131}
8132#endif
8133
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008134#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008135static void free_sched_group(struct task_group *tg)
8136{
8137 free_fair_sched_group(tg);
8138 free_rt_sched_group(tg);
8139 kfree(tg);
8140}
8141
8142/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008143struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008144{
8145 struct task_group *tg;
8146 unsigned long flags;
8147 int i;
8148
8149 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8150 if (!tg)
8151 return ERR_PTR(-ENOMEM);
8152
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008153 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008154 goto err;
8155
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008156 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008157 goto err;
8158
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008159 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008160 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008161 register_fair_sched_group(tg, i);
8162 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008163 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008164 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008165
8166 WARN_ON(!parent); /* root should already exist */
8167
8168 tg->parent = parent;
8169 list_add_rcu(&tg->siblings, &parent->children);
8170 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008171 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008172
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008173 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008174
8175err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008176 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008177 return ERR_PTR(-ENOMEM);
8178}
8179
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008180/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008181static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008182{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008183 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008184 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008185}
8186
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008187/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008188void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008189{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008190 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008191 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008192
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008193 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008194 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008195 unregister_fair_sched_group(tg, i);
8196 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008197 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008198 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008199 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008200 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008201
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008202 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008203 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008204}
8205
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008206/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008207 * The caller of this function should have put the task in its new group
8208 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8209 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008210 */
8211void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008212{
8213 int on_rq, running;
8214 unsigned long flags;
8215 struct rq *rq;
8216
8217 rq = task_rq_lock(tsk, &flags);
8218
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008219 update_rq_clock(rq);
8220
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008221 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008222 on_rq = tsk->se.on_rq;
8223
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008224 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008225 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008226 if (unlikely(running))
8227 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008228
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008229 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008230
Peter Zijlstra810b3812008-02-29 15:21:01 -05008231#ifdef CONFIG_FAIR_GROUP_SCHED
8232 if (tsk->sched_class->moved_group)
8233 tsk->sched_class->moved_group(tsk);
8234#endif
8235
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008236 if (unlikely(running))
8237 tsk->sched_class->set_curr_task(rq);
8238 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008239 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008240
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008241 task_rq_unlock(rq, &flags);
8242}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008243#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008244
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008245#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6363ca52008-05-29 11:28:57 +02008246static void set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008247{
8248 struct cfs_rq *cfs_rq = se->cfs_rq;
Ingo Molnar6363ca52008-05-29 11:28:57 +02008249 struct rq *rq = cfs_rq->rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008250 int on_rq;
8251
Ingo Molnar6363ca52008-05-29 11:28:57 +02008252 spin_lock_irq(&rq->lock);
8253
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008254 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008255 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008256 dequeue_entity(cfs_rq, se, 0);
8257
8258 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008259 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008260
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008261 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008262 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008263
Ingo Molnar6363ca52008-05-29 11:28:57 +02008264 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008265}
8266
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008267static DEFINE_MUTEX(shares_mutex);
8268
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008269int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008270{
8271 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008272 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008273
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008274 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008275 * We can't change the weight of the root cgroup.
8276 */
8277 if (!tg->se[0])
8278 return -EINVAL;
8279
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008280 if (shares < MIN_SHARES)
8281 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008282 else if (shares > MAX_SHARES)
8283 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008284
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008285 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008286 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008287 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008288
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008289 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008290 for_each_possible_cpu(i)
8291 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008292 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008293 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008294
8295 /* wait for any ongoing reference to this group to finish */
8296 synchronize_sched();
8297
8298 /*
8299 * Now we are free to modify the group's share on each cpu
8300 * w/o tripping rebalance_share or load_balance_fair.
8301 */
8302 tg->shares = shares;
Ingo Molnar6363ca52008-05-29 11:28:57 +02008303 for_each_possible_cpu(i)
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008304 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008305
8306 /*
8307 * Enable load balance activity on this group, by inserting it back on
8308 * each cpu's rq->leaf_cfs_rq_list.
8309 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008310 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008311 for_each_possible_cpu(i)
8312 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008313 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008314 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008315done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008316 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008317 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008318}
8319
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008320unsigned long sched_group_shares(struct task_group *tg)
8321{
8322 return tg->shares;
8323}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008324#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008325
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008326#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008327/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008328 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008329 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008330static DEFINE_MUTEX(rt_constraints_mutex);
8331
8332static unsigned long to_ratio(u64 period, u64 runtime)
8333{
8334 if (runtime == RUNTIME_INF)
8335 return 1ULL << 16;
8336
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008337 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008338}
8339
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008340#ifdef CONFIG_CGROUP_SCHED
8341static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8342{
8343 struct task_group *tgi, *parent = tg->parent;
8344 unsigned long total = 0;
8345
8346 if (!parent) {
8347 if (global_rt_period() < period)
8348 return 0;
8349
8350 return to_ratio(period, runtime) <
8351 to_ratio(global_rt_period(), global_rt_runtime());
8352 }
8353
8354 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8355 return 0;
8356
8357 rcu_read_lock();
8358 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8359 if (tgi == tg)
8360 continue;
8361
8362 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8363 tgi->rt_bandwidth.rt_runtime);
8364 }
8365 rcu_read_unlock();
8366
8367 return total + to_ratio(period, runtime) <
8368 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8369 parent->rt_bandwidth.rt_runtime);
8370}
8371#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008372static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008373{
8374 struct task_group *tgi;
8375 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008376 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008377 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008378
8379 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008380 list_for_each_entry_rcu(tgi, &task_groups, list) {
8381 if (tgi == tg)
8382 continue;
8383
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008384 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8385 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008386 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008387 rcu_read_unlock();
8388
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008389 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008390}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008391#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008392
Dhaval Giani521f1a242008-02-28 15:21:56 +05308393/* Must be called with tasklist_lock held */
8394static inline int tg_has_rt_tasks(struct task_group *tg)
8395{
8396 struct task_struct *g, *p;
8397 do_each_thread(g, p) {
8398 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8399 return 1;
8400 } while_each_thread(g, p);
8401 return 0;
8402}
8403
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008404static int tg_set_bandwidth(struct task_group *tg,
8405 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008406{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008407 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008408
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008409 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308410 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008411 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308412 err = -EBUSY;
8413 goto unlock;
8414 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008415 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8416 err = -EINVAL;
8417 goto unlock;
8418 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008419
8420 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008421 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8422 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008423
8424 for_each_possible_cpu(i) {
8425 struct rt_rq *rt_rq = tg->rt_rq[i];
8426
8427 spin_lock(&rt_rq->rt_runtime_lock);
8428 rt_rq->rt_runtime = rt_runtime;
8429 spin_unlock(&rt_rq->rt_runtime_lock);
8430 }
8431 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008432 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308433 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008434 mutex_unlock(&rt_constraints_mutex);
8435
8436 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008437}
8438
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008439int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8440{
8441 u64 rt_runtime, rt_period;
8442
8443 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8444 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8445 if (rt_runtime_us < 0)
8446 rt_runtime = RUNTIME_INF;
8447
8448 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8449}
8450
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008451long sched_group_rt_runtime(struct task_group *tg)
8452{
8453 u64 rt_runtime_us;
8454
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008455 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008456 return -1;
8457
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008458 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008459 do_div(rt_runtime_us, NSEC_PER_USEC);
8460 return rt_runtime_us;
8461}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008462
8463int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8464{
8465 u64 rt_runtime, rt_period;
8466
8467 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8468 rt_runtime = tg->rt_bandwidth.rt_runtime;
8469
8470 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8471}
8472
8473long sched_group_rt_period(struct task_group *tg)
8474{
8475 u64 rt_period_us;
8476
8477 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8478 do_div(rt_period_us, NSEC_PER_USEC);
8479 return rt_period_us;
8480}
8481
8482static int sched_rt_global_constraints(void)
8483{
8484 int ret = 0;
8485
8486 mutex_lock(&rt_constraints_mutex);
8487 if (!__rt_schedulable(NULL, 1, 0))
8488 ret = -EINVAL;
8489 mutex_unlock(&rt_constraints_mutex);
8490
8491 return ret;
8492}
8493#else
8494static int sched_rt_global_constraints(void)
8495{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008496 unsigned long flags;
8497 int i;
8498
8499 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8500 for_each_possible_cpu(i) {
8501 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8502
8503 spin_lock(&rt_rq->rt_runtime_lock);
8504 rt_rq->rt_runtime = global_rt_runtime();
8505 spin_unlock(&rt_rq->rt_runtime_lock);
8506 }
8507 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8508
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008509 return 0;
8510}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008511#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008512
8513int sched_rt_handler(struct ctl_table *table, int write,
8514 struct file *filp, void __user *buffer, size_t *lenp,
8515 loff_t *ppos)
8516{
8517 int ret;
8518 int old_period, old_runtime;
8519 static DEFINE_MUTEX(mutex);
8520
8521 mutex_lock(&mutex);
8522 old_period = sysctl_sched_rt_period;
8523 old_runtime = sysctl_sched_rt_runtime;
8524
8525 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8526
8527 if (!ret && write) {
8528 ret = sched_rt_global_constraints();
8529 if (ret) {
8530 sysctl_sched_rt_period = old_period;
8531 sysctl_sched_rt_runtime = old_runtime;
8532 } else {
8533 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8534 def_rt_bandwidth.rt_period =
8535 ns_to_ktime(global_rt_period());
8536 }
8537 }
8538 mutex_unlock(&mutex);
8539
8540 return ret;
8541}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008542
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008543#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008544
8545/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008546static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008547{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008548 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8549 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008550}
8551
8552static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008553cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008554{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008555 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008556
Paul Menage2b01dfe2007-10-24 18:23:50 +02008557 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008558 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008559 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008560 return &init_task_group.css;
8561 }
8562
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008563 parent = cgroup_tg(cgrp->parent);
8564 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008565 if (IS_ERR(tg))
8566 return ERR_PTR(-ENOMEM);
8567
8568 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008569 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008570
8571 return &tg->css;
8572}
8573
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008574static void
8575cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008576{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008577 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008578
8579 sched_destroy_group(tg);
8580}
8581
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008582static int
8583cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8584 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008585{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008586#ifdef CONFIG_RT_GROUP_SCHED
8587 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008588 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008589 return -EINVAL;
8590#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008591 /* We don't support RT-tasks being in separate groups */
8592 if (tsk->sched_class != &fair_sched_class)
8593 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008594#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008595
8596 return 0;
8597}
8598
8599static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008600cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008601 struct cgroup *old_cont, struct task_struct *tsk)
8602{
8603 sched_move_task(tsk);
8604}
8605
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008606#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008607static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008608 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008609{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008610 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008611}
8612
Paul Menagef4c753b2008-04-29 00:59:56 -07008613static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008614{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008615 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008616
8617 return (u64) tg->shares;
8618}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008619#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008620
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008621#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008622static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008623 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008624{
Paul Menage06ecb272008-04-29 01:00:06 -07008625 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008626}
8627
Paul Menage06ecb272008-04-29 01:00:06 -07008628static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008629{
Paul Menage06ecb272008-04-29 01:00:06 -07008630 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008631}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008632
8633static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8634 u64 rt_period_us)
8635{
8636 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8637}
8638
8639static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8640{
8641 return sched_group_rt_period(cgroup_tg(cgrp));
8642}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008643#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008644
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008645static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008646#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008647 {
8648 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008649 .read_u64 = cpu_shares_read_u64,
8650 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008651 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008652#endif
8653#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008654 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008655 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008656 .read_s64 = cpu_rt_runtime_read,
8657 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008658 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008659 {
8660 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008661 .read_u64 = cpu_rt_period_read_uint,
8662 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008663 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008664#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008665};
8666
8667static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8668{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008669 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008670}
8671
8672struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008673 .name = "cpu",
8674 .create = cpu_cgroup_create,
8675 .destroy = cpu_cgroup_destroy,
8676 .can_attach = cpu_cgroup_can_attach,
8677 .attach = cpu_cgroup_attach,
8678 .populate = cpu_cgroup_populate,
8679 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008680 .early_init = 1,
8681};
8682
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008683#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008684
8685#ifdef CONFIG_CGROUP_CPUACCT
8686
8687/*
8688 * CPU accounting code for task groups.
8689 *
8690 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8691 * (balbir@in.ibm.com).
8692 */
8693
8694/* track cpu usage of a group of tasks */
8695struct cpuacct {
8696 struct cgroup_subsys_state css;
8697 /* cpuusage holds pointer to a u64-type object on every cpu */
8698 u64 *cpuusage;
8699};
8700
8701struct cgroup_subsys cpuacct_subsys;
8702
8703/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308704static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008705{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308706 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008707 struct cpuacct, css);
8708}
8709
8710/* return cpu accounting group to which this task belongs */
8711static inline struct cpuacct *task_ca(struct task_struct *tsk)
8712{
8713 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8714 struct cpuacct, css);
8715}
8716
8717/* create a new cpu accounting group */
8718static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308719 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008720{
8721 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8722
8723 if (!ca)
8724 return ERR_PTR(-ENOMEM);
8725
8726 ca->cpuusage = alloc_percpu(u64);
8727 if (!ca->cpuusage) {
8728 kfree(ca);
8729 return ERR_PTR(-ENOMEM);
8730 }
8731
8732 return &ca->css;
8733}
8734
8735/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008736static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308737cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008738{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308739 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008740
8741 free_percpu(ca->cpuusage);
8742 kfree(ca);
8743}
8744
8745/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308746static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008747{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308748 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008749 u64 totalcpuusage = 0;
8750 int i;
8751
8752 for_each_possible_cpu(i) {
8753 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8754
8755 /*
8756 * Take rq->lock to make 64-bit addition safe on 32-bit
8757 * platforms.
8758 */
8759 spin_lock_irq(&cpu_rq(i)->lock);
8760 totalcpuusage += *cpuusage;
8761 spin_unlock_irq(&cpu_rq(i)->lock);
8762 }
8763
8764 return totalcpuusage;
8765}
8766
Dhaval Giani0297b802008-02-29 10:02:44 +05308767static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
8768 u64 reset)
8769{
8770 struct cpuacct *ca = cgroup_ca(cgrp);
8771 int err = 0;
8772 int i;
8773
8774 if (reset) {
8775 err = -EINVAL;
8776 goto out;
8777 }
8778
8779 for_each_possible_cpu(i) {
8780 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8781
8782 spin_lock_irq(&cpu_rq(i)->lock);
8783 *cpuusage = 0;
8784 spin_unlock_irq(&cpu_rq(i)->lock);
8785 }
8786out:
8787 return err;
8788}
8789
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008790static struct cftype files[] = {
8791 {
8792 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07008793 .read_u64 = cpuusage_read,
8794 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008795 },
8796};
8797
Dhaval Giani32cd7562008-02-29 10:02:43 +05308798static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008799{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308800 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008801}
8802
8803/*
8804 * charge this task's execution time to its accounting group.
8805 *
8806 * called with rq->lock held.
8807 */
8808static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8809{
8810 struct cpuacct *ca;
8811
8812 if (!cpuacct_subsys.active)
8813 return;
8814
8815 ca = task_ca(tsk);
8816 if (ca) {
8817 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8818
8819 *cpuusage += cputime;
8820 }
8821}
8822
8823struct cgroup_subsys cpuacct_subsys = {
8824 .name = "cpuacct",
8825 .create = cpuacct_create,
8826 .destroy = cpuacct_destroy,
8827 .populate = cpuacct_populate,
8828 .subsys_id = cpuacct_subsys_id,
8829};
8830#endif /* CONFIG_CGROUP_CPUACCT */