blob: d941ddc9ec1d488feca3b436776c515e9ea9fb95 [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/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080078 * Scheduler clock - returns current time in nanosec units.
79 * This is default implementation.
80 * Architectures and sub-architectures can override this.
81 */
82unsigned long long __attribute__((weak)) sched_clock(void)
83{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010084 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080085}
86
87/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Convert user-nice values [ -20 ... 0 ... 19 ]
89 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
90 * and back.
91 */
92#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
93#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
94#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
95
96/*
97 * 'User priority' is the nice value converted to something we
98 * can work with better when scaling various scheduler parameters,
99 * it's a [ 0 ... 39 ] range.
100 */
101#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
102#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
103#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
104
105/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100106 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100108#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200110#define NICE_0_LOAD SCHED_LOAD_SCALE
111#define NICE_0_SHIFT SCHED_LOAD_SHIFT
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * These are the 'tuning knobs' of the scheduler:
115 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200116 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * Timeslices get refilled after they expire.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700120
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200121/*
122 * single value that denotes runtime == period, ie unlimited time.
123 */
124#define RUNTIME_INF ((u64)~0ULL)
125
Eric Dumazet5517d862007-05-08 00:32:57 -0700126#ifdef CONFIG_SMP
127/*
128 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
129 * Since cpu_power is a 'constant', we can use a reciprocal divide.
130 */
131static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
132{
133 return reciprocal_divide(load, sg->reciprocal_cpu_power);
134}
135
136/*
137 * Each time a sched group cpu_power is changed,
138 * we must compute its reciprocal value
139 */
140static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
141{
142 sg->__cpu_power += val;
143 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
144}
145#endif
146
Ingo Molnare05606d2007-07-09 18:51:59 +0200147static inline int rt_policy(int policy)
148{
149 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
150 return 1;
151 return 0;
152}
153
154static inline int task_has_rt_policy(struct task_struct *p)
155{
156 return rt_policy(p->policy);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200160 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200162struct rt_prio_array {
163 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
164 struct list_head queue[MAX_RT_PRIO];
165};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200167struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100168 /* nests inside the rq lock: */
169 spinlock_t rt_runtime_lock;
170 ktime_t rt_period;
171 u64 rt_runtime;
172 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200173};
174
175static struct rt_bandwidth def_rt_bandwidth;
176
177static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
178
179static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
180{
181 struct rt_bandwidth *rt_b =
182 container_of(timer, struct rt_bandwidth, rt_period_timer);
183 ktime_t now;
184 int overrun;
185 int idle = 0;
186
187 for (;;) {
188 now = hrtimer_cb_get_time(timer);
189 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
190
191 if (!overrun)
192 break;
193
194 idle = do_sched_rt_period_timer(rt_b, overrun);
195 }
196
197 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
198}
199
200static
201void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
202{
203 rt_b->rt_period = ns_to_ktime(period);
204 rt_b->rt_runtime = runtime;
205
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200206 spin_lock_init(&rt_b->rt_runtime_lock);
207
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200208 hrtimer_init(&rt_b->rt_period_timer,
209 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
210 rt_b->rt_period_timer.function = sched_rt_period_timer;
211 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
212}
213
214static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
215{
216 ktime_t now;
217
218 if (rt_b->rt_runtime == RUNTIME_INF)
219 return;
220
221 if (hrtimer_active(&rt_b->rt_period_timer))
222 return;
223
224 spin_lock(&rt_b->rt_runtime_lock);
225 for (;;) {
226 if (hrtimer_active(&rt_b->rt_period_timer))
227 break;
228
229 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
230 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
231 hrtimer_start(&rt_b->rt_period_timer,
232 rt_b->rt_period_timer.expires,
233 HRTIMER_MODE_ABS);
234 }
235 spin_unlock(&rt_b->rt_runtime_lock);
236}
237
238#ifdef CONFIG_RT_GROUP_SCHED
239static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
240{
241 hrtimer_cancel(&rt_b->rt_period_timer);
242}
243#endif
244
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100245#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700247#include <linux/cgroup.h>
248
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200249struct cfs_rq;
250
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100251static LIST_HEAD(task_groups);
252
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200253/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200254struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100255#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700256 struct cgroup_subsys_state css;
257#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100258
259#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200260 /* schedulable entities of this group on each cpu */
261 struct sched_entity **se;
262 /* runqueue "owned" by this group on each cpu */
263 struct cfs_rq **cfs_rq;
264 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#endif
266
267#ifdef CONFIG_RT_GROUP_SCHED
268 struct sched_rt_entity **rt_se;
269 struct rt_rq **rt_rq;
270
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200271 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100272#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100273
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100274 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100275 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200276
277 struct task_group *parent;
278 struct list_head siblings;
279 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200280};
281
Dhaval Giani354d60c2008-04-19 19:44:59 +0200282#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200283
284/*
285 * Root task group.
286 * Every UID task group (including init_task_group aka UID-0) will
287 * be a child to this group.
288 */
289struct task_group root_task_group;
290
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100291#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200292/* Default task group's sched entity on each cpu */
293static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
294/* Default task group's cfs_rq on each cpu */
295static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100296#endif
297
298#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100299static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
300static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100301#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200302#else
303#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200304#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100305
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100306/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100307 * a task group's cpu shares.
308 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100309static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100310
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100311/* doms_cur_mutex serializes access to doms_cur[] array */
312static DEFINE_MUTEX(doms_cur_mutex);
313
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100314#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100315#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100316# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200317#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100318# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200319#endif
320
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200321#define MIN_SHARES 2
322
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100323static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100324#endif
325
326/* Default task group.
327 * Every task in system belong to this group at bootup.
328 */
Mike Travis434d53b2008-04-04 18:11:04 -0700329struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200330
331/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200332static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200333{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200334 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200335
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100336#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700339 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
340 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100342 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200343#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200344 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345}
346
347/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100348static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200349{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100350#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100351 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
352 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100353#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100354
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100355#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100356 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
357 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100358#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200359}
360
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100361static inline void lock_doms_cur(void)
362{
363 mutex_lock(&doms_cur_mutex);
364}
365
366static inline void unlock_doms_cur(void)
367{
368 mutex_unlock(&doms_cur_mutex);
369}
370
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200371#else
372
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100373static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100374static inline void lock_doms_cur(void) { }
375static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200376
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100377#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200378
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200379/* CFS-related fields in a runqueue */
380struct cfs_rq {
381 struct load_weight load;
382 unsigned long nr_running;
383
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200384 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200385 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200386
387 struct rb_root tasks_timeline;
388 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200389
390 struct list_head tasks;
391 struct list_head *balance_iterator;
392
393 /*
394 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200395 * It is set to NULL otherwise (i.e when none are currently running).
396 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100397 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200398
399 unsigned long nr_spread_over;
400
Ingo Molnar62160e32007-10-15 17:00:03 +0200401#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200402 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
403
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100404 /*
405 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200406 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
407 * (like users, containers etc.)
408 *
409 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
410 * list is used during load balance.
411 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100412 struct list_head leaf_cfs_rq_list;
413 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200414
415#ifdef CONFIG_SMP
416 unsigned long task_weight;
417 unsigned long shares;
418 /*
419 * We need space to build a sched_domain wide view of the full task
420 * group tree, in order to avoid depending on dynamic memory allocation
421 * during the load balancing we place this in the per cpu task group
422 * hierarchy. This limits the load balancing to one instance per cpu,
423 * but more should not be needed anyway.
424 */
425 struct aggregate_struct {
426 /*
427 * load = weight(cpus) * f(tg)
428 *
429 * Where f(tg) is the recursive weight fraction assigned to
430 * this group.
431 */
432 unsigned long load;
433
434 /*
435 * part of the group weight distributed to this span.
436 */
437 unsigned long shares;
438
439 /*
440 * The sum of all runqueue weights within this span.
441 */
442 unsigned long rq_weight;
443
444 /*
445 * Weight contributed by tasks; this is the part we can
446 * influence by moving tasks around.
447 */
448 unsigned long task_weight;
449 } aggregate;
450#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200451#endif
452};
453
454/* Real-Time classes' related field in a runqueue: */
455struct rt_rq {
456 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100457 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100458#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100459 int highest_prio; /* highest queued rt task prio */
460#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100461#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100462 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100463 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100464#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100465 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100466 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200467 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100468 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200469 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100470
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100471#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100472 unsigned long rt_nr_boosted;
473
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100474 struct rq *rq;
475 struct list_head leaf_rt_rq_list;
476 struct task_group *tg;
477 struct sched_rt_entity *rt_se;
478#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200479};
480
Gregory Haskins57d885f2008-01-25 21:08:18 +0100481#ifdef CONFIG_SMP
482
483/*
484 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100485 * variables. Each exclusive cpuset essentially defines an island domain by
486 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100487 * exclusive cpuset is created, we also create and attach a new root-domain
488 * object.
489 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100490 */
491struct root_domain {
492 atomic_t refcount;
493 cpumask_t span;
494 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100495
Ingo Molnar0eab9142008-01-25 21:08:19 +0100496 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100497 * The "RT overload" flag: it gets set if a CPU has more than
498 * one runnable RT task.
499 */
500 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100501 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100502};
503
Gregory Haskinsdc938522008-01-25 21:08:26 +0100504/*
505 * By default the system creates a single root-domain with all cpus as
506 * members (mimicking the global state we have today).
507 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100508static struct root_domain def_root_domain;
509
510#endif
511
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * This is the main, per-CPU runqueue data structure.
514 *
515 * Locking rule: those places that want to lock multiple runqueues
516 * (such as the load balancing or the thread migration code), lock
517 * acquire operations must be ordered by ascending &runqueue.
518 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700519struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200520 /* runqueue lock: */
521 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * nr_running and cpu_load should be in the same cacheline because
525 * remote CPUs use both these fields when doing load calculation.
526 */
527 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200528 #define CPU_LOAD_IDX_MAX 5
529 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700530 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700531#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200532 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700533 unsigned char in_nohz_recently;
534#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200535 /* capture load from *all* tasks on this cpu: */
536 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537 unsigned long nr_load_updates;
538 u64 nr_switches;
539
540 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100541 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100542
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200543#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200544 /* list of leaf cfs_rq on this cpu: */
545 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100546#endif
547#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100548 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * This is part of a global counter where only the total sum
553 * over all CPUs matters. A task can increase this counter on
554 * one CPU and if it got migrated afterwards it may decrease
555 * it on another CPU. Always updated under the runqueue lock:
556 */
557 unsigned long nr_uninterruptible;
558
Ingo Molnar36c8b582006-07-03 00:25:41 -0700559 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800560 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200562
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200563 u64 clock, prev_clock_raw;
564 s64 clock_max_delta;
565
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100566 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200567 u64 idle_clock;
568 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200569 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 atomic_t nr_iowait;
572
573#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100574 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct sched_domain *sd;
576
577 /* For active balancing */
578 int active_balance;
579 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200580 /* cpu of this runqueue: */
581 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar36c8b582006-07-03 00:25:41 -0700583 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct list_head migration_queue;
585#endif
586
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100587#ifdef CONFIG_SCHED_HRTICK
588 unsigned long hrtick_flags;
589 ktime_t hrtick_expire;
590 struct hrtimer hrtick_timer;
591#endif
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#ifdef CONFIG_SCHEDSTATS
594 /* latency stats */
595 struct sched_info rq_sched_info;
596
597 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200598 unsigned int yld_exp_empty;
599 unsigned int yld_act_empty;
600 unsigned int yld_both_empty;
601 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200604 unsigned int sched_switch;
605 unsigned int sched_count;
606 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200609 unsigned int ttwu_count;
610 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200611
612 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200613 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700615 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616};
617
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700618static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ingo Molnardd41f592007-07-09 18:51:59 +0200620static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
621{
622 rq->curr->sched_class->check_preempt_curr(rq, p);
623}
624
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700625static inline int cpu_of(struct rq *rq)
626{
627#ifdef CONFIG_SMP
628 return rq->cpu;
629#else
630 return 0;
631#endif
632}
633
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200634#ifdef CONFIG_NO_HZ
635static inline bool nohz_on(int cpu)
636{
637 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
638}
639
640static inline u64 max_skipped_ticks(struct rq *rq)
641{
642 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
643}
644
645static inline void update_last_tick_seen(struct rq *rq)
646{
647 rq->last_tick_seen = jiffies;
648}
649#else
650static inline u64 max_skipped_ticks(struct rq *rq)
651{
652 return 1;
653}
654
655static inline void update_last_tick_seen(struct rq *rq)
656{
657}
658#endif
659
Nick Piggin674311d2005-06-25 14:57:27 -0700660/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200661 * Update the per-runqueue clock, as finegrained as the platform can give
662 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200663 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200664static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200665{
666 u64 prev_raw = rq->prev_clock_raw;
667 u64 now = sched_clock();
668 s64 delta = now - prev_raw;
669 u64 clock = rq->clock;
670
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200671#ifdef CONFIG_SCHED_DEBUG
672 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
673#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200674 /*
675 * Protect against sched_clock() occasionally going backwards:
676 */
677 if (unlikely(delta < 0)) {
678 clock++;
679 rq->clock_warps++;
680 } else {
681 /*
682 * Catch too large forward jumps too:
683 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200684 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
685 u64 max_time = rq->tick_timestamp + max_jump;
686
687 if (unlikely(clock + delta > max_time)) {
688 if (clock < max_time)
689 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200690 else
691 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200692 rq->clock_overflows++;
693 } else {
694 if (unlikely(delta > rq->clock_max_delta))
695 rq->clock_max_delta = delta;
696 clock += delta;
697 }
698 }
699
700 rq->prev_clock_raw = now;
701 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200702}
703
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200704static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200705{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200706 if (likely(smp_processor_id() == cpu_of(rq)))
707 __update_rq_clock(rq);
708}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200709
Ingo Molnar20d315d2007-07-09 18:51:58 +0200710/*
Nick Piggin674311d2005-06-25 14:57:27 -0700711 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700712 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700713 *
714 * The domain tree of any CPU may only be accessed from within
715 * preempt-disabled sections.
716 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700717#define for_each_domain(cpu, __sd) \
718 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
721#define this_rq() (&__get_cpu_var(runqueues))
722#define task_rq(p) cpu_rq(task_cpu(p))
723#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
724
Ingo Molnare436d802007-07-19 21:28:35 +0200725/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200726 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
727 */
728#ifdef CONFIG_SCHED_DEBUG
729# define const_debug __read_mostly
730#else
731# define const_debug static const
732#endif
733
734/*
735 * Debugging: various feature bits
736 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200737
738#define SCHED_FEAT(name, enabled) \
739 __SCHED_FEAT_##name ,
740
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200741enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200742#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200743};
744
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200745#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200746
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200747#define SCHED_FEAT(name, enabled) \
748 (1UL << __SCHED_FEAT_##name) * enabled |
749
750const_debug unsigned int sysctl_sched_features =
751#include "sched_features.h"
752 0;
753
754#undef SCHED_FEAT
755
756#ifdef CONFIG_SCHED_DEBUG
757#define SCHED_FEAT(name, enabled) \
758 #name ,
759
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700760static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200761#include "sched_features.h"
762 NULL
763};
764
765#undef SCHED_FEAT
766
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700767static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200768{
769 filp->private_data = inode->i_private;
770 return 0;
771}
772
773static ssize_t
774sched_feat_read(struct file *filp, char __user *ubuf,
775 size_t cnt, loff_t *ppos)
776{
777 char *buf;
778 int r = 0;
779 int len = 0;
780 int i;
781
782 for (i = 0; sched_feat_names[i]; i++) {
783 len += strlen(sched_feat_names[i]);
784 len += 4;
785 }
786
787 buf = kmalloc(len + 2, GFP_KERNEL);
788 if (!buf)
789 return -ENOMEM;
790
791 for (i = 0; sched_feat_names[i]; i++) {
792 if (sysctl_sched_features & (1UL << i))
793 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
794 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200795 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200796 }
797
798 r += sprintf(buf + r, "\n");
799 WARN_ON(r >= len + 2);
800
801 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
802
803 kfree(buf);
804
805 return r;
806}
807
808static ssize_t
809sched_feat_write(struct file *filp, const char __user *ubuf,
810 size_t cnt, loff_t *ppos)
811{
812 char buf[64];
813 char *cmp = buf;
814 int neg = 0;
815 int i;
816
817 if (cnt > 63)
818 cnt = 63;
819
820 if (copy_from_user(&buf, ubuf, cnt))
821 return -EFAULT;
822
823 buf[cnt] = 0;
824
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200825 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200826 neg = 1;
827 cmp += 3;
828 }
829
830 for (i = 0; sched_feat_names[i]; i++) {
831 int len = strlen(sched_feat_names[i]);
832
833 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
834 if (neg)
835 sysctl_sched_features &= ~(1UL << i);
836 else
837 sysctl_sched_features |= (1UL << i);
838 break;
839 }
840 }
841
842 if (!sched_feat_names[i])
843 return -EINVAL;
844
845 filp->f_pos += cnt;
846
847 return cnt;
848}
849
850static struct file_operations sched_feat_fops = {
851 .open = sched_feat_open,
852 .read = sched_feat_read,
853 .write = sched_feat_write,
854};
855
856static __init int sched_init_debug(void)
857{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200858 debugfs_create_file("sched_features", 0644, NULL, NULL,
859 &sched_feat_fops);
860
861 return 0;
862}
863late_initcall(sched_init_debug);
864
865#endif
866
867#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200868
869/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100870 * Number of tasks to iterate in a single balance run.
871 * Limited because this is done with IRQs disabled.
872 */
873const_debug unsigned int sysctl_sched_nr_migrate = 32;
874
875/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100876 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100877 * default: 1s
878 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100879unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100880
Ingo Molnar6892b752008-02-13 14:02:36 +0100881static __read_mostly int scheduler_running;
882
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100883/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100884 * part of the period that we allow rt tasks to run in us.
885 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100886 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100887int sysctl_sched_rt_runtime = 950000;
888
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200889static inline u64 global_rt_period(void)
890{
891 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
892}
893
894static inline u64 global_rt_runtime(void)
895{
896 if (sysctl_sched_rt_period < 0)
897 return RUNTIME_INF;
898
899 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
900}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100901
Ingo Molnar27ec4402008-02-28 21:00:21 +0100902static const unsigned long long time_sync_thresh = 100000;
903
904static DEFINE_PER_CPU(unsigned long long, time_offset);
905static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
906
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100907/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100908 * Global lock which we take every now and then to synchronize
909 * the CPUs time. This method is not warp-safe, but it's good
910 * enough to synchronize slowly diverging time sources and thus
911 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200912 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100913static DEFINE_SPINLOCK(time_sync_lock);
914static unsigned long long prev_global_time;
915
916static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
917{
918 unsigned long flags;
919
920 spin_lock_irqsave(&time_sync_lock, flags);
921
922 if (time < prev_global_time) {
923 per_cpu(time_offset, cpu) += prev_global_time - time;
924 time = prev_global_time;
925 } else {
926 prev_global_time = time;
927 }
928
929 spin_unlock_irqrestore(&time_sync_lock, flags);
930
931 return time;
932}
933
934static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200935{
Ingo Molnare436d802007-07-19 21:28:35 +0200936 unsigned long long now;
937 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200938 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200939
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100940 /*
941 * Only call sched_clock() if the scheduler has already been
942 * initialized (some code might call cpu_clock() very early):
943 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100944 if (unlikely(!scheduler_running))
945 return 0;
946
947 local_irq_save(flags);
948 rq = cpu_rq(cpu);
949 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200950 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200951 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200952
953 return now;
954}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100955
956/*
957 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
958 * clock constructed from sched_clock():
959 */
960unsigned long long cpu_clock(int cpu)
961{
962 unsigned long long prev_cpu_time, time, delta_time;
963
964 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
965 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
966 delta_time = time-prev_cpu_time;
967
968 if (unlikely(delta_time > time_sync_thresh))
969 time = __sync_cpu_clock(time, cpu);
970
971 return time;
972}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200973EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700976# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700978#ifndef finish_arch_switch
979# define finish_arch_switch(prev) do { } while (0)
980#endif
981
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100982static inline int task_current(struct rq *rq, struct task_struct *p)
983{
984 return rq->curr == p;
985}
986
Nick Piggin4866cde2005-06-25 14:57:23 -0700987#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700988static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700989{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100990 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700991}
992
Ingo Molnar70b97a72006-07-03 00:25:42 -0700993static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700994{
995}
996
Ingo Molnar70b97a72006-07-03 00:25:42 -0700997static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700998{
Ingo Molnarda04c032005-09-13 11:17:59 +0200999#ifdef CONFIG_DEBUG_SPINLOCK
1000 /* this is a valid case when another task releases the spinlock */
1001 rq->lock.owner = current;
1002#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001003 /*
1004 * If we are tracking spinlock dependencies then we have to
1005 * fix up the runqueue lock - which gets 'carried over' from
1006 * prev into current:
1007 */
1008 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1009
Nick Piggin4866cde2005-06-25 14:57:23 -07001010 spin_unlock_irq(&rq->lock);
1011}
1012
1013#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001014static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -07001015{
1016#ifdef CONFIG_SMP
1017 return p->oncpu;
1018#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01001019 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -07001020#endif
1021}
1022
Ingo Molnar70b97a72006-07-03 00:25:42 -07001023static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001024{
1025#ifdef CONFIG_SMP
1026 /*
1027 * We can optimise this out completely for !SMP, because the
1028 * SMP rebalancing from interrupt is the only thing that cares
1029 * here.
1030 */
1031 next->oncpu = 1;
1032#endif
1033#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1034 spin_unlock_irq(&rq->lock);
1035#else
1036 spin_unlock(&rq->lock);
1037#endif
1038}
1039
Ingo Molnar70b97a72006-07-03 00:25:42 -07001040static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -07001041{
1042#ifdef CONFIG_SMP
1043 /*
1044 * After ->oncpu is cleared, the task can be moved to a different CPU.
1045 * We must ensure this doesn't happen until the switch is completely
1046 * finished.
1047 */
1048 smp_wmb();
1049 prev->oncpu = 0;
1050#endif
1051#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1052 local_irq_enable();
1053#endif
1054}
1055#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001058 * __task_rq_lock - lock the runqueue a given task resides on.
1059 * Must be called interrupts disabled.
1060 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001061static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001062 __acquires(rq->lock)
1063{
Andi Kleen3a5c3592007-10-15 17:00:14 +02001064 for (;;) {
1065 struct rq *rq = task_rq(p);
1066 spin_lock(&rq->lock);
1067 if (likely(rq == task_rq(p)))
1068 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001069 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001070 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001071}
1072
1073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001075 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * explicitly disabling preemption.
1077 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001078static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 __acquires(rq->lock)
1080{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001081 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Andi Kleen3a5c3592007-10-15 17:00:14 +02001083 for (;;) {
1084 local_irq_save(*flags);
1085 rq = task_rq(p);
1086 spin_lock(&rq->lock);
1087 if (likely(rq == task_rq(p)))
1088 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Alexey Dobriyana9957442007-10-15 17:00:13 +02001093static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001094 __releases(rq->lock)
1095{
1096 spin_unlock(&rq->lock);
1097}
1098
Ingo Molnar70b97a72006-07-03 00:25:42 -07001099static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 __releases(rq->lock)
1101{
1102 spin_unlock_irqrestore(&rq->lock, *flags);
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001106 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001108static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 __acquires(rq->lock)
1110{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001111 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 local_irq_disable();
1114 rq = this_rq();
1115 spin_lock(&rq->lock);
1116
1117 return rq;
1118}
1119
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001120/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001121 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001122 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001123void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001124{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001125 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001126
Andrew Mortond478c2c2008-04-26 11:30:34 -07001127 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001128 spin_lock(&rq->lock);
1129 __update_rq_clock(rq);
1130 spin_unlock(&rq->lock);
1131 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001132}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001133EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1134
1135/*
1136 * We just idled delta nanoseconds (called with irqs disabled):
1137 */
1138void sched_clock_idle_wakeup_event(u64 delta_ns)
1139{
1140 struct rq *rq = cpu_rq(smp_processor_id());
1141 u64 now = sched_clock();
1142
Andrew Mortond478c2c2008-04-26 11:30:34 -07001143 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001144 rq->idle_clock += delta_ns;
1145 /*
1146 * Override the previous timestamp and ignore all
1147 * sched_clock() deltas that occured while we idled,
1148 * and use the PM-provided delta_ns to advance the
1149 * rq clock:
1150 */
1151 spin_lock(&rq->lock);
1152 rq->prev_clock_raw = now;
1153 rq->clock += delta_ns;
1154 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001155 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001156}
1157EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001158
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001159static void __resched_task(struct task_struct *p, int tif_bit);
1160
1161static inline void resched_task(struct task_struct *p)
1162{
1163 __resched_task(p, TIF_NEED_RESCHED);
1164}
1165
1166#ifdef CONFIG_SCHED_HRTICK
1167/*
1168 * Use HR-timers to deliver accurate preemption points.
1169 *
1170 * Its all a bit involved since we cannot program an hrt while holding the
1171 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1172 * reschedule event.
1173 *
1174 * When we get rescheduled we reprogram the hrtick_timer outside of the
1175 * rq->lock.
1176 */
1177static inline void resched_hrt(struct task_struct *p)
1178{
1179 __resched_task(p, TIF_HRTICK_RESCHED);
1180}
1181
1182static inline void resched_rq(struct rq *rq)
1183{
1184 unsigned long flags;
1185
1186 spin_lock_irqsave(&rq->lock, flags);
1187 resched_task(rq->curr);
1188 spin_unlock_irqrestore(&rq->lock, flags);
1189}
1190
1191enum {
1192 HRTICK_SET, /* re-programm hrtick_timer */
1193 HRTICK_RESET, /* not a new slice */
1194};
1195
1196/*
1197 * Use hrtick when:
1198 * - enabled by features
1199 * - hrtimer is actually high res
1200 */
1201static inline int hrtick_enabled(struct rq *rq)
1202{
1203 if (!sched_feat(HRTICK))
1204 return 0;
1205 return hrtimer_is_hres_active(&rq->hrtick_timer);
1206}
1207
1208/*
1209 * Called to set the hrtick timer state.
1210 *
1211 * called with rq->lock held and irqs disabled
1212 */
1213static void hrtick_start(struct rq *rq, u64 delay, int reset)
1214{
1215 assert_spin_locked(&rq->lock);
1216
1217 /*
1218 * preempt at: now + delay
1219 */
1220 rq->hrtick_expire =
1221 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1222 /*
1223 * indicate we need to program the timer
1224 */
1225 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1226 if (reset)
1227 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1228
1229 /*
1230 * New slices are called from the schedule path and don't need a
1231 * forced reschedule.
1232 */
1233 if (reset)
1234 resched_hrt(rq->curr);
1235}
1236
1237static void hrtick_clear(struct rq *rq)
1238{
1239 if (hrtimer_active(&rq->hrtick_timer))
1240 hrtimer_cancel(&rq->hrtick_timer);
1241}
1242
1243/*
1244 * Update the timer from the possible pending state.
1245 */
1246static void hrtick_set(struct rq *rq)
1247{
1248 ktime_t time;
1249 int set, reset;
1250 unsigned long flags;
1251
1252 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1253
1254 spin_lock_irqsave(&rq->lock, flags);
1255 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1256 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1257 time = rq->hrtick_expire;
1258 clear_thread_flag(TIF_HRTICK_RESCHED);
1259 spin_unlock_irqrestore(&rq->lock, flags);
1260
1261 if (set) {
1262 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1263 if (reset && !hrtimer_active(&rq->hrtick_timer))
1264 resched_rq(rq);
1265 } else
1266 hrtick_clear(rq);
1267}
1268
1269/*
1270 * High-resolution timer tick.
1271 * Runs from hardirq context with interrupts disabled.
1272 */
1273static enum hrtimer_restart hrtick(struct hrtimer *timer)
1274{
1275 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1276
1277 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1278
1279 spin_lock(&rq->lock);
1280 __update_rq_clock(rq);
1281 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1282 spin_unlock(&rq->lock);
1283
1284 return HRTIMER_NORESTART;
1285}
1286
1287static inline void init_rq_hrtick(struct rq *rq)
1288{
1289 rq->hrtick_flags = 0;
1290 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1291 rq->hrtick_timer.function = hrtick;
1292 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1293}
1294
1295void hrtick_resched(void)
1296{
1297 struct rq *rq;
1298 unsigned long flags;
1299
1300 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1301 return;
1302
1303 local_irq_save(flags);
1304 rq = cpu_rq(smp_processor_id());
1305 hrtick_set(rq);
1306 local_irq_restore(flags);
1307}
1308#else
1309static inline void hrtick_clear(struct rq *rq)
1310{
1311}
1312
1313static inline void hrtick_set(struct rq *rq)
1314{
1315}
1316
1317static inline void init_rq_hrtick(struct rq *rq)
1318{
1319}
1320
1321void hrtick_resched(void)
1322{
1323}
1324#endif
1325
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001326/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001327 * resched_task - mark a task 'to be rescheduled now'.
1328 *
1329 * On UP this means the setting of the need_resched flag, on SMP it
1330 * might also involve a cross-CPU call to trigger the scheduler on
1331 * the target CPU.
1332 */
1333#ifdef CONFIG_SMP
1334
1335#ifndef tsk_is_polling
1336#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1337#endif
1338
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001339static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001340{
1341 int cpu;
1342
1343 assert_spin_locked(&task_rq(p)->lock);
1344
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001345 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001346 return;
1347
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001348 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001349
1350 cpu = task_cpu(p);
1351 if (cpu == smp_processor_id())
1352 return;
1353
1354 /* NEED_RESCHED must be visible before we test polling */
1355 smp_mb();
1356 if (!tsk_is_polling(p))
1357 smp_send_reschedule(cpu);
1358}
1359
1360static void resched_cpu(int cpu)
1361{
1362 struct rq *rq = cpu_rq(cpu);
1363 unsigned long flags;
1364
1365 if (!spin_trylock_irqsave(&rq->lock, flags))
1366 return;
1367 resched_task(cpu_curr(cpu));
1368 spin_unlock_irqrestore(&rq->lock, flags);
1369}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001370
1371#ifdef CONFIG_NO_HZ
1372/*
1373 * When add_timer_on() enqueues a timer into the timer wheel of an
1374 * idle CPU then this timer might expire before the next timer event
1375 * which is scheduled to wake up that CPU. In case of a completely
1376 * idle system the next event might even be infinite time into the
1377 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1378 * leaves the inner idle loop so the newly added timer is taken into
1379 * account when the CPU goes back to idle and evaluates the timer
1380 * wheel for the next timer event.
1381 */
1382void wake_up_idle_cpu(int cpu)
1383{
1384 struct rq *rq = cpu_rq(cpu);
1385
1386 if (cpu == smp_processor_id())
1387 return;
1388
1389 /*
1390 * This is safe, as this function is called with the timer
1391 * wheel base lock of (cpu) held. When the CPU is on the way
1392 * to idle and has not yet set rq->curr to idle then it will
1393 * be serialized on the timer wheel base lock and take the new
1394 * timer into account automatically.
1395 */
1396 if (rq->curr != rq->idle)
1397 return;
1398
1399 /*
1400 * We can set TIF_RESCHED on the idle task of the other CPU
1401 * lockless. The worst case is that the other CPU runs the
1402 * idle task through an additional NOOP schedule()
1403 */
1404 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1405
1406 /* NEED_RESCHED must be visible before we test polling */
1407 smp_mb();
1408 if (!tsk_is_polling(rq->idle))
1409 smp_send_reschedule(cpu);
1410}
1411#endif
1412
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001413#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001414static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001415{
1416 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001417 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001418}
1419#endif
1420
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001421#if BITS_PER_LONG == 32
1422# define WMULT_CONST (~0UL)
1423#else
1424# define WMULT_CONST (1UL << 32)
1425#endif
1426
1427#define WMULT_SHIFT 32
1428
Ingo Molnar194081e2007-08-09 11:16:51 +02001429/*
1430 * Shift right and round:
1431 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001432#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001433
Peter Zijlstra8f1bc382008-04-19 19:45:00 +02001434/*
1435 * delta *= weight / lw
1436 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001437static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001438calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1439 struct load_weight *lw)
1440{
1441 u64 tmp;
1442
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001443 if (!lw->inv_weight)
1444 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001445
1446 tmp = (u64)delta_exec * weight;
1447 /*
1448 * Check whether we'd overflow the 64-bit multiplication:
1449 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001450 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001451 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001452 WMULT_SHIFT/2);
1453 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001454 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001455
Ingo Molnarecf691d2007-08-02 17:41:40 +02001456 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001457}
1458
Ingo Molnar10919852007-10-15 17:00:04 +02001459static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001460{
1461 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001462 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001463}
1464
Ingo Molnar10919852007-10-15 17:00:04 +02001465static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001466{
1467 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001468 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001472 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1473 * of tasks with abnormal "nice" values across CPUs the contribution that
1474 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001475 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001476 * scaled version of the new time slice allocation that they receive on time
1477 * slice expiry etc.
1478 */
1479
Ingo Molnardd41f592007-07-09 18:51:59 +02001480#define WEIGHT_IDLEPRIO 2
1481#define WMULT_IDLEPRIO (1 << 31)
1482
1483/*
1484 * Nice levels are multiplicative, with a gentle 10% change for every
1485 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1486 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1487 * that remained on nice 0.
1488 *
1489 * The "10% effect" is relative and cumulative: from _any_ nice level,
1490 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001491 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1492 * If a task goes up by ~10% and another task goes down by ~10% then
1493 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001494 */
1495static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001496 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1497 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1498 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1499 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1500 /* 0 */ 1024, 820, 655, 526, 423,
1501 /* 5 */ 335, 272, 215, 172, 137,
1502 /* 10 */ 110, 87, 70, 56, 45,
1503 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001504};
1505
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001506/*
1507 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1508 *
1509 * In cases where the weight does not change often, we can use the
1510 * precalculated inverse to speed up arithmetics by turning divisions
1511 * into multiplications:
1512 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001513static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001514 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1515 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1516 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1517 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1518 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1519 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1520 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1521 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001522};
Peter Williams2dd73a42006-06-27 02:54:34 -07001523
Ingo Molnardd41f592007-07-09 18:51:59 +02001524static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1525
1526/*
1527 * runqueue iterator, to support SMP load-balancing between different
1528 * scheduling classes, without having to expose their internal data
1529 * structures to the load-balancing proper:
1530 */
1531struct rq_iterator {
1532 void *arg;
1533 struct task_struct *(*start)(void *);
1534 struct task_struct *(*next)(void *);
1535};
1536
Peter Williamse1d14842007-10-24 18:23:51 +02001537#ifdef CONFIG_SMP
1538static unsigned long
1539balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1540 unsigned long max_load_move, struct sched_domain *sd,
1541 enum cpu_idle_type idle, int *all_pinned,
1542 int *this_best_prio, struct rq_iterator *iterator);
1543
1544static int
1545iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1546 struct sched_domain *sd, enum cpu_idle_type idle,
1547 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001548#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001549
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001550#ifdef CONFIG_CGROUP_CPUACCT
1551static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1552#else
1553static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1554#endif
1555
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001556static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1557{
1558 update_load_add(&rq->load, load);
1559}
1560
1561static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1562{
1563 update_load_sub(&rq->load, load);
1564}
1565
Gregory Haskinse7693a32008-01-25 21:08:09 +01001566#ifdef CONFIG_SMP
1567static unsigned long source_load(int cpu, int type);
1568static unsigned long target_load(int cpu, int type);
1569static unsigned long cpu_avg_load_per_task(int cpu);
1570static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001571
1572#ifdef CONFIG_FAIR_GROUP_SCHED
1573
1574/*
1575 * Group load balancing.
1576 *
1577 * We calculate a few balance domain wide aggregate numbers; load and weight.
1578 * Given the pictures below, and assuming each item has equal weight:
1579 *
1580 * root 1 - thread
1581 * / | \ A - group
1582 * A 1 B
1583 * /|\ / \
1584 * C 2 D 3 4
1585 * | |
1586 * 5 6
1587 *
1588 * load:
1589 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1590 * which equals 1/9-th of the total load.
1591 *
1592 * shares:
1593 * The weight of this group on the selected cpus.
1594 *
1595 * rq_weight:
1596 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1597 * B would get 2.
1598 *
1599 * task_weight:
1600 * Part of the rq_weight contributed by tasks; all groups except B would
1601 * get 1, B gets 2.
1602 */
1603
1604static inline struct aggregate_struct *
1605aggregate(struct task_group *tg, struct sched_domain *sd)
1606{
1607 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1608}
1609
1610typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1611
1612/*
1613 * Iterate the full tree, calling @down when first entering a node and @up when
1614 * leaving it for the final time.
1615 */
1616static
1617void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1618 struct sched_domain *sd)
1619{
1620 struct task_group *parent, *child;
1621
1622 rcu_read_lock();
1623 parent = &root_task_group;
1624down:
1625 (*down)(parent, sd);
1626 list_for_each_entry_rcu(child, &parent->children, siblings) {
1627 parent = child;
1628 goto down;
1629
1630up:
1631 continue;
1632 }
1633 (*up)(parent, sd);
1634
1635 child = parent;
1636 parent = parent->parent;
1637 if (parent)
1638 goto up;
1639 rcu_read_unlock();
1640}
1641
1642/*
1643 * Calculate the aggregate runqueue weight.
1644 */
1645static
1646void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1647{
1648 unsigned long rq_weight = 0;
1649 unsigned long task_weight = 0;
1650 int i;
1651
1652 for_each_cpu_mask(i, sd->span) {
1653 rq_weight += tg->cfs_rq[i]->load.weight;
1654 task_weight += tg->cfs_rq[i]->task_weight;
1655 }
1656
1657 aggregate(tg, sd)->rq_weight = rq_weight;
1658 aggregate(tg, sd)->task_weight = task_weight;
1659}
1660
1661/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001662 * Compute the weight of this group on the given cpus.
1663 */
1664static
1665void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1666{
1667 unsigned long shares = 0;
1668 int i;
1669
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001670 for_each_cpu_mask(i, sd->span)
1671 shares += tg->cfs_rq[i]->shares;
1672
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001673 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1674 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001675
1676 aggregate(tg, sd)->shares = shares;
1677}
1678
1679/*
1680 * Compute the load fraction assigned to this group, relies on the aggregate
1681 * weight and this group's parent's load, i.e. top-down.
1682 */
1683static
1684void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1685{
1686 unsigned long load;
1687
1688 if (!tg->parent) {
1689 int i;
1690
1691 load = 0;
1692 for_each_cpu_mask(i, sd->span)
1693 load += cpu_rq(i)->load.weight;
1694
1695 } else {
1696 load = aggregate(tg->parent, sd)->load;
1697
1698 /*
1699 * shares is our weight in the parent's rq so
1700 * shares/parent->rq_weight gives our fraction of the load
1701 */
1702 load *= aggregate(tg, sd)->shares;
1703 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1704 }
1705
1706 aggregate(tg, sd)->load = load;
1707}
1708
1709static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1710
1711/*
1712 * Calculate and set the cpu's group shares.
1713 */
1714static void
1715__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1716 int tcpu)
1717{
1718 int boost = 0;
1719 unsigned long shares;
1720 unsigned long rq_weight;
1721
1722 if (!tg->se[tcpu])
1723 return;
1724
1725 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1726
1727 /*
1728 * If there are currently no tasks on the cpu pretend there is one of
1729 * average load so that when a new task gets to run here it will not
1730 * get delayed by group starvation.
1731 */
1732 if (!rq_weight) {
1733 boost = 1;
1734 rq_weight = NICE_0_LOAD;
1735 }
1736
1737 /*
1738 * \Sum shares * rq_weight
1739 * shares = -----------------------
1740 * \Sum rq_weight
1741 *
1742 */
1743 shares = aggregate(tg, sd)->shares * rq_weight;
1744 shares /= aggregate(tg, sd)->rq_weight + 1;
1745
1746 /*
1747 * record the actual number of shares, not the boosted amount.
1748 */
1749 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1750
1751 if (shares < MIN_SHARES)
1752 shares = MIN_SHARES;
1753
1754 __set_se_shares(tg->se[tcpu], shares);
1755}
1756
1757/*
1758 * Re-adjust the weights on the cpu the task came from and on the cpu the
1759 * task went to.
1760 */
1761static void
1762__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1763 int scpu, int dcpu)
1764{
1765 unsigned long shares;
1766
1767 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1768
1769 __update_group_shares_cpu(tg, sd, scpu);
1770 __update_group_shares_cpu(tg, sd, dcpu);
1771
1772 /*
1773 * ensure we never loose shares due to rounding errors in the
1774 * above redistribution.
1775 */
1776 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1777 if (shares)
1778 tg->cfs_rq[dcpu]->shares += shares;
1779}
1780
1781/*
1782 * Because changing a group's shares changes the weight of the super-group
1783 * we need to walk up the tree and change all shares until we hit the root.
1784 */
1785static void
1786move_group_shares(struct task_group *tg, struct sched_domain *sd,
1787 int scpu, int dcpu)
1788{
1789 while (tg) {
1790 __move_group_shares(tg, sd, scpu, dcpu);
1791 tg = tg->parent;
1792 }
1793}
1794
1795static
1796void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1797{
1798 unsigned long shares = aggregate(tg, sd)->shares;
1799 int i;
1800
1801 for_each_cpu_mask(i, sd->span) {
1802 struct rq *rq = cpu_rq(i);
1803 unsigned long flags;
1804
1805 spin_lock_irqsave(&rq->lock, flags);
1806 __update_group_shares_cpu(tg, sd, i);
1807 spin_unlock_irqrestore(&rq->lock, flags);
1808 }
1809
1810 aggregate_group_shares(tg, sd);
1811
1812 /*
1813 * ensure we never loose shares due to rounding errors in the
1814 * above redistribution.
1815 */
1816 shares -= aggregate(tg, sd)->shares;
1817 if (shares) {
1818 tg->cfs_rq[sd->first_cpu]->shares += shares;
1819 aggregate(tg, sd)->shares += shares;
1820 }
1821}
1822
1823/*
1824 * Calculate the accumulative weight and recursive load of each task group
1825 * while walking down the tree.
1826 */
1827static
1828void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1829{
1830 aggregate_group_weight(tg, sd);
1831 aggregate_group_shares(tg, sd);
1832 aggregate_group_load(tg, sd);
1833}
1834
1835/*
1836 * Rebalance the cpu shares while walking back up the tree.
1837 */
1838static
1839void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1840{
1841 aggregate_group_set_shares(tg, sd);
1842}
1843
1844static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1845
1846static void __init init_aggregate(void)
1847{
1848 int i;
1849
1850 for_each_possible_cpu(i)
1851 spin_lock_init(&per_cpu(aggregate_lock, i));
1852}
1853
1854static int get_aggregate(struct sched_domain *sd)
1855{
1856 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1857 return 0;
1858
1859 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1860 return 1;
1861}
1862
1863static void put_aggregate(struct sched_domain *sd)
1864{
1865 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1866}
1867
1868static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1869{
1870 cfs_rq->shares = shares;
1871}
1872
1873#else
1874
1875static inline void init_aggregate(void)
1876{
1877}
1878
1879static inline int get_aggregate(struct sched_domain *sd)
1880{
1881 return 0;
1882}
1883
1884static inline void put_aggregate(struct sched_domain *sd)
1885{
1886}
1887#endif
1888
1889#else /* CONFIG_SMP */
1890
1891#ifdef CONFIG_FAIR_GROUP_SCHED
1892static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1893{
1894}
1895#endif
1896
Gregory Haskinse7693a32008-01-25 21:08:09 +01001897#endif /* CONFIG_SMP */
1898
Ingo Molnardd41f592007-07-09 18:51:59 +02001899#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001900#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001901#include "sched_fair.c"
1902#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001903#ifdef CONFIG_SCHED_DEBUG
1904# include "sched_debug.c"
1905#endif
1906
1907#define sched_class_highest (&rt_sched_class)
1908
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001909static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001910{
1911 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001912}
1913
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001914static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001915{
1916 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001917}
1918
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001919static void set_load_weight(struct task_struct *p)
1920{
1921 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001922 p->se.load.weight = prio_to_weight[0] * 2;
1923 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1924 return;
1925 }
1926
1927 /*
1928 * SCHED_IDLE tasks get minimal weight:
1929 */
1930 if (p->policy == SCHED_IDLE) {
1931 p->se.load.weight = WEIGHT_IDLEPRIO;
1932 p->se.load.inv_weight = WMULT_IDLEPRIO;
1933 return;
1934 }
1935
1936 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1937 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001938}
1939
Ingo Molnar8159f872007-08-09 11:16:49 +02001940static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001941{
1942 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001943 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001944 p->se.on_rq = 1;
1945}
1946
Ingo Molnar69be72c2007-08-09 11:16:49 +02001947static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001948{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001949 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001950 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001951}
1952
1953/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001954 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001955 */
Ingo Molnar14531182007-07-09 18:51:59 +02001956static inline int __normal_prio(struct task_struct *p)
1957{
Ingo Molnardd41f592007-07-09 18:51:59 +02001958 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001959}
1960
1961/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001962 * Calculate the expected normal priority: i.e. priority
1963 * without taking RT-inheritance into account. Might be
1964 * boosted by interactivity modifiers. Changes upon fork,
1965 * setprio syscalls, and whenever the interactivity
1966 * estimator recalculates.
1967 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001968static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001969{
1970 int prio;
1971
Ingo Molnare05606d2007-07-09 18:51:59 +02001972 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001973 prio = MAX_RT_PRIO-1 - p->rt_priority;
1974 else
1975 prio = __normal_prio(p);
1976 return prio;
1977}
1978
1979/*
1980 * Calculate the current priority, i.e. the priority
1981 * taken into account by the scheduler. This value might
1982 * be boosted by RT tasks, or might be boosted by
1983 * interactivity modifiers. Will be RT if the task got
1984 * RT-boosted. If not then it returns p->normal_prio.
1985 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001986static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001987{
1988 p->normal_prio = normal_prio(p);
1989 /*
1990 * If we are RT tasks or we were boosted to RT priority,
1991 * keep the priority unchanged. Otherwise, update priority
1992 * to the normal priority:
1993 */
1994 if (!rt_prio(p->prio))
1995 return p->normal_prio;
1996 return p->prio;
1997}
1998
1999/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002000 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002002static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002004 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002005 rq->nr_uninterruptible--;
2006
Ingo Molnar8159f872007-08-09 11:16:49 +02002007 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002008 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * deactivate_task - remove a task from the runqueue.
2013 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002014static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002016 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002017 rq->nr_uninterruptible++;
2018
Ingo Molnar69be72c2007-08-09 11:16:49 +02002019 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002020 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/**
2024 * task_curr - is this task currently executing on a CPU?
2025 * @p: the task in question.
2026 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002027inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 return cpu_curr(task_cpu(p)) == p;
2030}
2031
Peter Williams2dd73a42006-06-27 02:54:34 -07002032/* Used instead of source_load when we know the type == 0 */
2033unsigned long weighted_cpuload(const int cpu)
2034{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002035 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002036}
2037
2038static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2039{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01002040 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002041#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002042 /*
2043 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2044 * successfuly executed on another CPU. We must ensure that updates of
2045 * per-task data have been completed by this moment.
2046 */
2047 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002048 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002049#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002050}
2051
Steven Rostedtcb469842008-01-25 21:08:22 +01002052static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2053 const struct sched_class *prev_class,
2054 int oldprio, int running)
2055{
2056 if (prev_class != p->sched_class) {
2057 if (prev_class->switched_from)
2058 prev_class->switched_from(rq, p, running);
2059 p->sched_class->switched_to(rq, p, running);
2060 } else
2061 p->sched_class->prio_changed(rq, p, oldprio, running);
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002065
Ingo Molnarcc367732007-10-15 17:00:18 +02002066/*
2067 * Is this task likely cache-hot:
2068 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002069static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002070task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2071{
2072 s64 delta;
2073
Ingo Molnarf540a602008-03-15 17:10:34 +01002074 /*
2075 * Buddy candidates are cache hot:
2076 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002077 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002078 return 1;
2079
Ingo Molnarcc367732007-10-15 17:00:18 +02002080 if (p->sched_class != &fair_sched_class)
2081 return 0;
2082
Ingo Molnar6bc16652007-10-15 17:00:18 +02002083 if (sysctl_sched_migration_cost == -1)
2084 return 1;
2085 if (sysctl_sched_migration_cost == 0)
2086 return 0;
2087
Ingo Molnarcc367732007-10-15 17:00:18 +02002088 delta = now - p->se.exec_start;
2089
2090 return delta < (s64)sysctl_sched_migration_cost;
2091}
2092
2093
Ingo Molnardd41f592007-07-09 18:51:59 +02002094void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002095{
Ingo Molnardd41f592007-07-09 18:51:59 +02002096 int old_cpu = task_cpu(p);
2097 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002098 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2099 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002100 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002101
2102 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002103
2104#ifdef CONFIG_SCHEDSTATS
2105 if (p->se.wait_start)
2106 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002107 if (p->se.sleep_start)
2108 p->se.sleep_start -= clock_offset;
2109 if (p->se.block_start)
2110 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002111 if (old_cpu != new_cpu) {
2112 schedstat_inc(p, se.nr_migrations);
2113 if (task_hot(p, old_rq->clock, NULL))
2114 schedstat_inc(p, se.nr_forced2_migrations);
2115 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002116#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002117 p->se.vruntime -= old_cfsrq->min_vruntime -
2118 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002119
2120 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002121}
2122
Ingo Molnar70b97a72006-07-03 00:25:42 -07002123struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Ingo Molnar36c8b582006-07-03 00:25:41 -07002126 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int dest_cpu;
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002130};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132/*
2133 * The task's runqueue lock must be held.
2134 * Returns true if you have to wait for migration thread.
2135 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002136static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002137migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002139 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 /*
2142 * If the task is not on a runqueue (and not running), then
2143 * it is sufficient to simply update the task's cpu field.
2144 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002145 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 set_task_cpu(p, dest_cpu);
2147 return 0;
2148 }
2149
2150 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 req->task = p;
2152 req->dest_cpu = dest_cpu;
2153 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return 1;
2156}
2157
2158/*
2159 * wait_task_inactive - wait for a thread to unschedule.
2160 *
2161 * The caller must ensure that the task *will* unschedule sometime soon,
2162 * else this function might spin for a *long* time. This function can't
2163 * be called with interrupts off, or it may introduce deadlock with
2164 * smp_call_function() if an IPI is sent by the same process we are
2165 * waiting to become inactive.
2166 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002167void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002170 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002171 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Andi Kleen3a5c3592007-10-15 17:00:14 +02002173 for (;;) {
2174 /*
2175 * We do the initial early heuristics without holding
2176 * any task-queue locks at all. We'll only try to get
2177 * the runqueue lock when things look like they will
2178 * work out!
2179 */
2180 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002181
Andi Kleen3a5c3592007-10-15 17:00:14 +02002182 /*
2183 * If the task is actively running on another CPU
2184 * still, just relax and busy-wait without holding
2185 * any locks.
2186 *
2187 * NOTE! Since we don't hold any locks, it's not
2188 * even sure that "rq" stays as the right runqueue!
2189 * But we don't care, since "task_running()" will
2190 * return false if the runqueue has changed and p
2191 * is actually now running somewhere else!
2192 */
2193 while (task_running(rq, p))
2194 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002195
Andi Kleen3a5c3592007-10-15 17:00:14 +02002196 /*
2197 * Ok, time to look more closely! We need the rq
2198 * lock now, to be *sure*. If we're wrong, we'll
2199 * just go back and repeat.
2200 */
2201 rq = task_rq_lock(p, &flags);
2202 running = task_running(rq, p);
2203 on_rq = p->se.on_rq;
2204 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002205
Andi Kleen3a5c3592007-10-15 17:00:14 +02002206 /*
2207 * Was it really running after all now that we
2208 * checked with the proper locks actually held?
2209 *
2210 * Oops. Go back and try again..
2211 */
2212 if (unlikely(running)) {
2213 cpu_relax();
2214 continue;
2215 }
2216
2217 /*
2218 * It's not enough that it's not actively running,
2219 * it must be off the runqueue _entirely_, and not
2220 * preempted!
2221 *
2222 * So if it wa still runnable (but just not actively
2223 * running right now), it's preempted, and we should
2224 * yield - it could be a while.
2225 */
2226 if (unlikely(on_rq)) {
2227 schedule_timeout_uninterruptible(1);
2228 continue;
2229 }
2230
2231 /*
2232 * Ahh, all good. It wasn't running, and it wasn't
2233 * runnable, which means that it will never become
2234 * running in the future either. We're all done!
2235 */
2236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240/***
2241 * kick_process - kick a running thread to enter/exit the kernel
2242 * @p: the to-be-kicked thread
2243 *
2244 * Cause a process which is running on another CPU to enter
2245 * kernel-mode, without any delay. (to get signals handled.)
2246 *
2247 * NOTE: this function doesnt have to take the runqueue lock,
2248 * because all it wants to ensure is that the remote task enters
2249 * the kernel. If the IPI races and the task has been migrated
2250 * to another CPU then no harm is done and the purpose has been
2251 * achieved as well.
2252 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002253void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int cpu;
2256
2257 preempt_disable();
2258 cpu = task_cpu(p);
2259 if ((cpu != smp_processor_id()) && task_curr(p))
2260 smp_send_reschedule(cpu);
2261 preempt_enable();
2262}
2263
2264/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002265 * Return a low guess at the load of a migration-source cpu weighted
2266 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *
2268 * We want to under-estimate the load of migration sources, to
2269 * balance conservatively.
2270 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002271static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002272{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002273 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002274 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002275
Peter Williams2dd73a42006-06-27 02:54:34 -07002276 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002277 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002278
Ingo Molnardd41f592007-07-09 18:51:59 +02002279 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002283 * Return a high guess at the load of a migration-target cpu weighted
2284 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002286static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002287{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002288 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002289 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002290
Peter Williams2dd73a42006-06-27 02:54:34 -07002291 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002292 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002293
Ingo Molnardd41f592007-07-09 18:51:59 +02002294 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002295}
2296
2297/*
2298 * Return the average load per task on the cpu's run queue
2299 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002300static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002301{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002302 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002303 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002304 unsigned long n = rq->nr_running;
2305
Ingo Molnardd41f592007-07-09 18:51:59 +02002306 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Nick Piggin147cbb42005-06-25 14:57:19 -07002309/*
2310 * find_idlest_group finds and returns the least busy CPU group within the
2311 * domain.
2312 */
2313static struct sched_group *
2314find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2315{
2316 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2317 unsigned long min_load = ULONG_MAX, this_load = 0;
2318 int load_idx = sd->forkexec_idx;
2319 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2320
2321 do {
2322 unsigned long load, avg_load;
2323 int local_group;
2324 int i;
2325
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002326 /* Skip over this group if it has no CPUs allowed */
2327 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002328 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002329
Nick Piggin147cbb42005-06-25 14:57:19 -07002330 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002331
2332 /* Tally up the load of all CPUs in the group */
2333 avg_load = 0;
2334
2335 for_each_cpu_mask(i, group->cpumask) {
2336 /* Bias balancing toward cpus of our domain */
2337 if (local_group)
2338 load = source_load(i, load_idx);
2339 else
2340 load = target_load(i, load_idx);
2341
2342 avg_load += load;
2343 }
2344
2345 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002346 avg_load = sg_div_cpu_power(group,
2347 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002348
2349 if (local_group) {
2350 this_load = avg_load;
2351 this = group;
2352 } else if (avg_load < min_load) {
2353 min_load = avg_load;
2354 idlest = group;
2355 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002356 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002357
2358 if (!idlest || 100*this_load < imbalance*min_load)
2359 return NULL;
2360 return idlest;
2361}
2362
2363/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002364 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002365 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002366static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002367find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2368 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002369{
2370 unsigned long load, min_load = ULONG_MAX;
2371 int idlest = -1;
2372 int i;
2373
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002374 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002375 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002376
Mike Travis7c16ec52008-04-04 18:11:11 -07002377 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002378 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002379
2380 if (load < min_load || (load == min_load && i == this_cpu)) {
2381 min_load = load;
2382 idlest = i;
2383 }
2384 }
2385
2386 return idlest;
2387}
2388
Nick Piggin476d1392005-06-25 14:57:29 -07002389/*
2390 * sched_balance_self: balance the current task (running on cpu) in domains
2391 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2392 * SD_BALANCE_EXEC.
2393 *
2394 * Balance, ie. select the least loaded group.
2395 *
2396 * Returns the target CPU number, or the same CPU if no balancing is needed.
2397 *
2398 * preempt must be disabled.
2399 */
2400static int sched_balance_self(int cpu, int flag)
2401{
2402 struct task_struct *t = current;
2403 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002404
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002405 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002406 /*
2407 * If power savings logic is enabled for a domain, stop there.
2408 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002409 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2410 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002411 if (tmp->flags & flag)
2412 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002413 }
Nick Piggin476d1392005-06-25 14:57:29 -07002414
2415 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002416 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002417 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002418 int new_cpu, weight;
2419
2420 if (!(sd->flags & flag)) {
2421 sd = sd->child;
2422 continue;
2423 }
Nick Piggin476d1392005-06-25 14:57:29 -07002424
2425 span = sd->span;
2426 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002427 if (!group) {
2428 sd = sd->child;
2429 continue;
2430 }
Nick Piggin476d1392005-06-25 14:57:29 -07002431
Mike Travis7c16ec52008-04-04 18:11:11 -07002432 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002433 if (new_cpu == -1 || new_cpu == cpu) {
2434 /* Now try balancing at a lower domain level of cpu */
2435 sd = sd->child;
2436 continue;
2437 }
Nick Piggin476d1392005-06-25 14:57:29 -07002438
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002439 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002440 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002441 sd = NULL;
2442 weight = cpus_weight(span);
2443 for_each_domain(cpu, tmp) {
2444 if (weight <= cpus_weight(tmp->span))
2445 break;
2446 if (tmp->flags & flag)
2447 sd = tmp;
2448 }
2449 /* while loop will break here if sd == NULL */
2450 }
2451
2452 return cpu;
2453}
2454
2455#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457/***
2458 * try_to_wake_up - wake up a thread
2459 * @p: the to-be-woken-up thread
2460 * @state: the mask of task states that can be woken
2461 * @sync: do a synchronous wakeup?
2462 *
2463 * Put it on the run-queue if it's not already there. The "current"
2464 * thread is always on the run-queue (except when the actual
2465 * re-schedule is in progress), and as such you're allowed to do
2466 * the simpler "current->state = TASK_RUNNING" to mark yourself
2467 * runnable without the overhead of this.
2468 *
2469 * returns failure only if the task is already active.
2470 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002471static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Ingo Molnarcc367732007-10-15 17:00:18 +02002473 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 unsigned long flags;
2475 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002476 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Ingo Molnarb85d0662008-03-16 20:03:22 +01002478 if (!sched_feat(SYNC_WAKEUPS))
2479 sync = 0;
2480
Linus Torvalds04e2f172008-02-23 18:05:03 -08002481 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 rq = task_rq_lock(p, &flags);
2483 old_state = p->state;
2484 if (!(old_state & state))
2485 goto out;
2486
Ingo Molnardd41f592007-07-09 18:51:59 +02002487 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto out_running;
2489
2490 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002491 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 this_cpu = smp_processor_id();
2493
2494#ifdef CONFIG_SMP
2495 if (unlikely(task_running(rq, p)))
2496 goto out_activate;
2497
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002498 cpu = p->sched_class->select_task_rq(p, sync);
2499 if (cpu != orig_cpu) {
2500 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 task_rq_unlock(rq, &flags);
2502 /* might preempt at this point */
2503 rq = task_rq_lock(p, &flags);
2504 old_state = p->state;
2505 if (!(old_state & state))
2506 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002507 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 goto out_running;
2509
2510 this_cpu = smp_processor_id();
2511 cpu = task_cpu(p);
2512 }
2513
Gregory Haskinse7693a32008-01-25 21:08:09 +01002514#ifdef CONFIG_SCHEDSTATS
2515 schedstat_inc(rq, ttwu_count);
2516 if (cpu == this_cpu)
2517 schedstat_inc(rq, ttwu_local);
2518 else {
2519 struct sched_domain *sd;
2520 for_each_domain(this_cpu, sd) {
2521 if (cpu_isset(cpu, sd->span)) {
2522 schedstat_inc(sd, ttwu_wake_remote);
2523 break;
2524 }
2525 }
2526 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002527#endif
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529out_activate:
2530#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002531 schedstat_inc(p, se.nr_wakeups);
2532 if (sync)
2533 schedstat_inc(p, se.nr_wakeups_sync);
2534 if (orig_cpu != cpu)
2535 schedstat_inc(p, se.nr_wakeups_migrate);
2536 if (cpu == this_cpu)
2537 schedstat_inc(p, se.nr_wakeups_local);
2538 else
2539 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002540 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002541 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 success = 1;
2543
2544out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002545 check_preempt_curr(rq, p);
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002548#ifdef CONFIG_SMP
2549 if (p->sched_class->task_wake_up)
2550 p->sched_class->task_wake_up(rq, p);
2551#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552out:
2553 task_rq_unlock(rq, &flags);
2554
2555 return success;
2556}
2557
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002558int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002560 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562EXPORT_SYMBOL(wake_up_process);
2563
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002564int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 return try_to_wake_up(p, state, 0);
2567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/*
2570 * Perform scheduler related setup for a newly forked process p.
2571 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002572 *
2573 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002575static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Ingo Molnardd41f592007-07-09 18:51:59 +02002577 p->se.exec_start = 0;
2578 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002579 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002580 p->se.last_wakeup = 0;
2581 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002582
2583#ifdef CONFIG_SCHEDSTATS
2584 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002585 p->se.sum_sleep_runtime = 0;
2586 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002587 p->se.block_start = 0;
2588 p->se.sleep_max = 0;
2589 p->se.block_max = 0;
2590 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002591 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002592 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002593#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002594
Peter Zijlstrafa717062008-01-25 21:08:27 +01002595 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002596 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002597 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002598
Avi Kivitye107be32007-07-26 13:40:43 +02002599#ifdef CONFIG_PREEMPT_NOTIFIERS
2600 INIT_HLIST_HEAD(&p->preempt_notifiers);
2601#endif
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /*
2604 * We mark the process as running here, but have not actually
2605 * inserted it onto the runqueue yet. This guarantees that
2606 * nobody will actually run it, and a signal or other external
2607 * event cannot wake it up and insert it on the runqueue either.
2608 */
2609 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002610}
2611
2612/*
2613 * fork()/clone()-time setup:
2614 */
2615void sched_fork(struct task_struct *p, int clone_flags)
2616{
2617 int cpu = get_cpu();
2618
2619 __sched_fork(p);
2620
2621#ifdef CONFIG_SMP
2622 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2623#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002624 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002625
2626 /*
2627 * Make sure we do not leak PI boosting priority to the child:
2628 */
2629 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002630 if (!rt_prio(p->prio))
2631 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002632
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002633#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002634 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002635 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002637#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002638 p->oncpu = 0;
2639#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002641 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002642 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002644 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
2647/*
2648 * wake_up_new_task - wake up a newly created task for the first time.
2649 *
2650 * This function will do some initial scheduler statistics housekeeping
2651 * that must be done for every newly created context, then puts the task
2652 * on the runqueue and wakes it.
2653 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002654void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002657 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002661 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 p->prio = effective_prio(p);
2664
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002665 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002666 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002669 * Let the scheduling class do new task startup
2670 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002672 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002673 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002675 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002676#ifdef CONFIG_SMP
2677 if (p->sched_class->task_wake_up)
2678 p->sched_class->task_wake_up(rq, p);
2679#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002680 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Avi Kivitye107be32007-07-26 13:40:43 +02002683#ifdef CONFIG_PREEMPT_NOTIFIERS
2684
2685/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002686 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2687 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002688 */
2689void preempt_notifier_register(struct preempt_notifier *notifier)
2690{
2691 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2692}
2693EXPORT_SYMBOL_GPL(preempt_notifier_register);
2694
2695/**
2696 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002697 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002698 *
2699 * This is safe to call from within a preemption notifier.
2700 */
2701void preempt_notifier_unregister(struct preempt_notifier *notifier)
2702{
2703 hlist_del(&notifier->link);
2704}
2705EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2706
2707static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2708{
2709 struct preempt_notifier *notifier;
2710 struct hlist_node *node;
2711
2712 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2713 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2714}
2715
2716static void
2717fire_sched_out_preempt_notifiers(struct task_struct *curr,
2718 struct task_struct *next)
2719{
2720 struct preempt_notifier *notifier;
2721 struct hlist_node *node;
2722
2723 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2724 notifier->ops->sched_out(notifier, next);
2725}
2726
2727#else
2728
2729static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2730{
2731}
2732
2733static void
2734fire_sched_out_preempt_notifiers(struct task_struct *curr,
2735 struct task_struct *next)
2736{
2737}
2738
2739#endif
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002742 * prepare_task_switch - prepare to switch tasks
2743 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002744 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002745 * @next: the task we are going to switch to.
2746 *
2747 * This is called with the rq lock held and interrupts off. It must
2748 * be paired with a subsequent finish_task_switch after the context
2749 * switch.
2750 *
2751 * prepare_task_switch sets up locking and calls architecture specific
2752 * hooks.
2753 */
Avi Kivitye107be32007-07-26 13:40:43 +02002754static inline void
2755prepare_task_switch(struct rq *rq, struct task_struct *prev,
2756 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002757{
Avi Kivitye107be32007-07-26 13:40:43 +02002758 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002759 prepare_lock_switch(rq, next);
2760 prepare_arch_switch(next);
2761}
2762
2763/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002765 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 * @prev: the thread we just switched away from.
2767 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002768 * finish_task_switch must be called after the context switch, paired
2769 * with a prepare_task_switch call before the context switch.
2770 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2771 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 *
2773 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002774 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 * with the lock held can cause deadlocks; see schedule() for
2776 * details.)
2777 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002778static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 __releases(rq->lock)
2780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002782 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 rq->prev_mm = NULL;
2785
2786 /*
2787 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002788 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002789 * schedule one last time. The schedule call will never return, and
2790 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002791 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 * still held, otherwise prev could be scheduled on another cpu, die
2793 * there before we look at prev->state, and then the reference would
2794 * be dropped twice.
2795 * Manfred Spraul <manfred@colorfullife.com>
2796 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002797 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002798 finish_arch_switch(prev);
2799 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002800#ifdef CONFIG_SMP
2801 if (current->sched_class->post_schedule)
2802 current->sched_class->post_schedule(rq);
2803#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002804
Avi Kivitye107be32007-07-26 13:40:43 +02002805 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (mm)
2807 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002808 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002809 /*
2810 * Remove function-return probe instances associated with this
2811 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002812 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002813 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818/**
2819 * schedule_tail - first thing a freshly forked thread must call.
2820 * @prev: the thread we just switched away from.
2821 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002822asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 __releases(rq->lock)
2824{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002825 struct rq *rq = this_rq();
2826
Nick Piggin4866cde2005-06-25 14:57:23 -07002827 finish_task_switch(rq, prev);
2828#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2829 /* In this case, finish_task_switch does not reenable preemption */
2830 preempt_enable();
2831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002833 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836/*
2837 * context_switch - switch to the new MM and the new
2838 * thread's register state.
2839 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002840static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002841context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002842 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Ingo Molnardd41f592007-07-09 18:51:59 +02002844 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Avi Kivitye107be32007-07-26 13:40:43 +02002846 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002847 mm = next->mm;
2848 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002849 /*
2850 * For paravirt, this is coupled with an exit in switch_to to
2851 * combine the page table reload and the switch backend into
2852 * one hypercall.
2853 */
2854 arch_enter_lazy_cpu_mode();
2855
Ingo Molnardd41f592007-07-09 18:51:59 +02002856 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 next->active_mm = oldmm;
2858 atomic_inc(&oldmm->mm_count);
2859 enter_lazy_tlb(oldmm, next);
2860 } else
2861 switch_mm(oldmm, mm, next);
2862
Ingo Molnardd41f592007-07-09 18:51:59 +02002863 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 rq->prev_mm = oldmm;
2866 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002867 /*
2868 * Since the runqueue lock will be released by the next
2869 * task (which is an invalid locking op but in the case
2870 * of the scheduler it's an obvious special-case), so we
2871 * do an early lockdep release here:
2872 */
2873#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002874 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002875#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /* Here we just switch the register state and the stack. */
2878 switch_to(prev, next, prev);
2879
Ingo Molnardd41f592007-07-09 18:51:59 +02002880 barrier();
2881 /*
2882 * this_rq must be evaluated again because prev may have moved
2883 * CPUs since it called schedule(), thus the 'rq' on its stack
2884 * frame will be invalid.
2885 */
2886 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
2889/*
2890 * nr_running, nr_uninterruptible and nr_context_switches:
2891 *
2892 * externally visible scheduler statistics: current number of runnable
2893 * threads, current number of uninterruptible-sleeping threads, total
2894 * number of context switches performed since bootup.
2895 */
2896unsigned long nr_running(void)
2897{
2898 unsigned long i, sum = 0;
2899
2900 for_each_online_cpu(i)
2901 sum += cpu_rq(i)->nr_running;
2902
2903 return sum;
2904}
2905
2906unsigned long nr_uninterruptible(void)
2907{
2908 unsigned long i, sum = 0;
2909
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002910 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 sum += cpu_rq(i)->nr_uninterruptible;
2912
2913 /*
2914 * Since we read the counters lockless, it might be slightly
2915 * inaccurate. Do not allow it to go below zero though:
2916 */
2917 if (unlikely((long)sum < 0))
2918 sum = 0;
2919
2920 return sum;
2921}
2922
2923unsigned long long nr_context_switches(void)
2924{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002925 int i;
2926 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002928 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 sum += cpu_rq(i)->nr_switches;
2930
2931 return sum;
2932}
2933
2934unsigned long nr_iowait(void)
2935{
2936 unsigned long i, sum = 0;
2937
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002938 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2940
2941 return sum;
2942}
2943
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002944unsigned long nr_active(void)
2945{
2946 unsigned long i, running = 0, uninterruptible = 0;
2947
2948 for_each_online_cpu(i) {
2949 running += cpu_rq(i)->nr_running;
2950 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2951 }
2952
2953 if (unlikely((long)uninterruptible < 0))
2954 uninterruptible = 0;
2955
2956 return running + uninterruptible;
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002960 * Update rq->cpu_load[] statistics. This function is usually called every
2961 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002962 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002963static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002964{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002965 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002966 int i, scale;
2967
2968 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002969
2970 /* Update our load: */
2971 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2972 unsigned long old_load, new_load;
2973
2974 /* scale is effectively 1 << i now, and >> i divides by scale */
2975
2976 old_load = this_rq->cpu_load[i];
2977 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002978 /*
2979 * Round up the averaging division if load is increasing. This
2980 * prevents us from getting stuck on 9 if the load is 10, for
2981 * example.
2982 */
2983 if (new_load > old_load)
2984 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002985 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2986 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002987}
2988
Ingo Molnardd41f592007-07-09 18:51:59 +02002989#ifdef CONFIG_SMP
2990
Ingo Molnar48f24c42006-07-03 00:25:40 -07002991/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 * double_rq_lock - safely lock two runqueues
2993 *
2994 * Note this does not disable interrupts like task_rq_lock,
2995 * you need to do so manually before calling.
2996 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002997static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 __acquires(rq1->lock)
2999 __acquires(rq2->lock)
3000{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003001 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (rq1 == rq2) {
3003 spin_lock(&rq1->lock);
3004 __acquire(rq2->lock); /* Fake it out ;) */
3005 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003006 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 spin_lock(&rq1->lock);
3008 spin_lock(&rq2->lock);
3009 } else {
3010 spin_lock(&rq2->lock);
3011 spin_lock(&rq1->lock);
3012 }
3013 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003014 update_rq_clock(rq1);
3015 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
3018/*
3019 * double_rq_unlock - safely unlock two runqueues
3020 *
3021 * Note this does not restore interrupts like task_rq_unlock,
3022 * you need to do so manually after calling.
3023 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003024static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 __releases(rq1->lock)
3026 __releases(rq2->lock)
3027{
3028 spin_unlock(&rq1->lock);
3029 if (rq1 != rq2)
3030 spin_unlock(&rq2->lock);
3031 else
3032 __release(rq2->lock);
3033}
3034
3035/*
3036 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3037 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003038static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 __releases(this_rq->lock)
3040 __acquires(busiest->lock)
3041 __acquires(this_rq->lock)
3042{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003043 int ret = 0;
3044
Kirill Korotaev054b9102006-12-10 02:20:11 -08003045 if (unlikely(!irqs_disabled())) {
3046 /* printk() doesn't work good under rq->lock */
3047 spin_unlock(&this_rq->lock);
3048 BUG_ON(1);
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003051 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 spin_unlock(&this_rq->lock);
3053 spin_lock(&busiest->lock);
3054 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003055 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 } else
3057 spin_lock(&busiest->lock);
3058 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003059 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
3062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 * If dest_cpu is allowed for this process, migrate the task to it.
3064 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003065 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 * the cpu_allowed mask is restored.
3067 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003068static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003070 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003072 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 rq = task_rq_lock(p, &flags);
3075 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3076 || unlikely(cpu_is_offline(dest_cpu)))
3077 goto out;
3078
3079 /* force the process onto the specified CPU */
3080 if (migrate_task(p, dest_cpu, &req)) {
3081 /* Need to wait for migration thread (might exit: take ref). */
3082 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 get_task_struct(mt);
3085 task_rq_unlock(rq, &flags);
3086 wake_up_process(mt);
3087 put_task_struct(mt);
3088 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return;
3091 }
3092out:
3093 task_rq_unlock(rq, &flags);
3094}
3095
3096/*
Nick Piggin476d1392005-06-25 14:57:29 -07003097 * sched_exec - execve() is a valuable balancing opportunity, because at
3098 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 */
3100void sched_exec(void)
3101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003103 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003105 if (new_cpu != this_cpu)
3106 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109/*
3110 * pull_task - move a task from a remote runqueue to the local runqueue.
3111 * Both runqueues must be locked.
3112 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003113static void pull_task(struct rq *src_rq, struct task_struct *p,
3114 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003116 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003118 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /*
3120 * Note that idle threads have a prio of MAX_PRIO, for this test
3121 * to be always true for them.
3122 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003123 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126/*
3127 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3128 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003129static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003130int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003131 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003132 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
3134 /*
3135 * We do not migrate tasks that are:
3136 * 1) running (obviously), or
3137 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3138 * 3) are cache-hot on their current CPU.
3139 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003140 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3141 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003143 }
Nick Piggin81026792005-06-25 14:57:07 -07003144 *all_pinned = 0;
3145
Ingo Molnarcc367732007-10-15 17:00:18 +02003146 if (task_running(rq, p)) {
3147 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003148 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Ingo Molnarda84d962007-10-15 17:00:18 +02003151 /*
3152 * Aggressive migration if:
3153 * 1) task is cache cold, or
3154 * 2) too many balance attempts have failed.
3155 */
3156
Ingo Molnar6bc16652007-10-15 17:00:18 +02003157 if (!task_hot(p, rq->clock, sd) ||
3158 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003159#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003160 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003161 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003162 schedstat_inc(p, se.nr_forced_migrations);
3163 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003164#endif
3165 return 1;
3166 }
3167
Ingo Molnarcc367732007-10-15 17:00:18 +02003168 if (task_hot(p, rq->clock, sd)) {
3169 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003170 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return 1;
3173}
3174
Peter Williamse1d14842007-10-24 18:23:51 +02003175static unsigned long
3176balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3177 unsigned long max_load_move, struct sched_domain *sd,
3178 enum cpu_idle_type idle, int *all_pinned,
3179 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003180{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003181 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003182 struct task_struct *p;
3183 long rem_load_move = max_load_move;
3184
Peter Williamse1d14842007-10-24 18:23:51 +02003185 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003186 goto out;
3187
3188 pinned = 1;
3189
3190 /*
3191 * Start the load-balancing iterator:
3192 */
3193 p = iterator->start(iterator->arg);
3194next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003195 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003196 goto out;
3197 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003198 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003199 * skip a task if it will be the highest priority task (i.e. smallest
3200 * prio value) on its new queue regardless of its load weight
3201 */
3202 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3203 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003204 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003205 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003206 p = iterator->next(iterator->arg);
3207 goto next;
3208 }
3209
3210 pull_task(busiest, p, this_rq, this_cpu);
3211 pulled++;
3212 rem_load_move -= p->se.load.weight;
3213
3214 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003215 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003216 */
Peter Williamse1d14842007-10-24 18:23:51 +02003217 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003218 if (p->prio < *this_best_prio)
3219 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003220 p = iterator->next(iterator->arg);
3221 goto next;
3222 }
3223out:
3224 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003225 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003226 * so we can safely collect pull_task() stats here rather than
3227 * inside pull_task().
3228 */
3229 schedstat_add(sd, lb_gained[idle], pulled);
3230
3231 if (all_pinned)
3232 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003233
3234 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003235}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237/*
Peter Williams43010652007-08-09 11:16:46 +02003238 * move_tasks tries to move up to max_load_move weighted load from busiest to
3239 * this_rq, as part of a balancing operation within domain "sd".
3240 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 *
3242 * Called with both runqueues locked.
3243 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003244static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003245 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003246 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003247 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003249 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003250 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003251 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Ingo Molnardd41f592007-07-09 18:51:59 +02003253 do {
Peter Williams43010652007-08-09 11:16:46 +02003254 total_load_moved +=
3255 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003256 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003257 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003258 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003259 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Peter Williams43010652007-08-09 11:16:46 +02003261 return total_load_moved > 0;
3262}
3263
Peter Williamse1d14842007-10-24 18:23:51 +02003264static int
3265iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3266 struct sched_domain *sd, enum cpu_idle_type idle,
3267 struct rq_iterator *iterator)
3268{
3269 struct task_struct *p = iterator->start(iterator->arg);
3270 int pinned = 0;
3271
3272 while (p) {
3273 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3274 pull_task(busiest, p, this_rq, this_cpu);
3275 /*
3276 * Right now, this is only the second place pull_task()
3277 * is called, so we can safely collect pull_task()
3278 * stats here rather than inside pull_task().
3279 */
3280 schedstat_inc(sd, lb_gained[idle]);
3281
3282 return 1;
3283 }
3284 p = iterator->next(iterator->arg);
3285 }
3286
3287 return 0;
3288}
3289
Peter Williams43010652007-08-09 11:16:46 +02003290/*
3291 * move_one_task tries to move exactly one task from busiest to this_rq, as
3292 * part of active balancing operations within "domain".
3293 * Returns 1 if successful and 0 otherwise.
3294 *
3295 * Called with both runqueues locked.
3296 */
3297static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3298 struct sched_domain *sd, enum cpu_idle_type idle)
3299{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003300 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003301
3302 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003303 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003304 return 1;
3305
3306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309/*
3310 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003311 * domain. It calculates and returns the amount of weighted load which
3312 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 */
3314static struct sched_group *
3315find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003316 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003317 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
3319 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3320 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003321 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003322 unsigned long busiest_load_per_task, busiest_nr_running;
3323 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003324 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003325#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3326 int power_savings_balance = 1;
3327 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3328 unsigned long min_nr_running = ULONG_MAX;
3329 struct sched_group *group_min = NULL, *group_leader = NULL;
3330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003333 busiest_load_per_task = busiest_nr_running = 0;
3334 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003335 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003336 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003337 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003338 load_idx = sd->newidle_idx;
3339 else
3340 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003343 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 int local_group;
3345 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003346 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003347 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003348 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 local_group = cpu_isset(this_cpu, group->cpumask);
3351
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003352 if (local_group)
3353 balance_cpu = first_cpu(group->cpumask);
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003356 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003357 max_cpu_load = 0;
3358 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003361 struct rq *rq;
3362
3363 if (!cpu_isset(i, *cpus))
3364 continue;
3365
3366 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003367
Suresh Siddha9439aab2007-07-19 21:28:35 +02003368 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003369 *sd_idle = 0;
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003372 if (local_group) {
3373 if (idle_cpu(i) && !first_idle_cpu) {
3374 first_idle_cpu = 1;
3375 balance_cpu = i;
3376 }
3377
Nick Piggina2000572006-02-10 01:51:02 -08003378 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003379 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003380 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003381 if (load > max_cpu_load)
3382 max_cpu_load = load;
3383 if (min_cpu_load > load)
3384 min_cpu_load = load;
3385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003388 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003389 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003392 /*
3393 * First idle cpu or the first cpu(busiest) in this sched group
3394 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003395 * domains. In the newly idle case, we will allow all the cpu's
3396 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003397 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003398 if (idle != CPU_NEWLY_IDLE && local_group &&
3399 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003400 *balance = 0;
3401 goto ret;
3402 }
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003405 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003408 avg_load = sg_div_cpu_power(group,
3409 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Ken Chen908a7c12007-10-17 16:55:11 +02003411 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3412 __group_imb = 1;
3413
Eric Dumazet5517d862007-05-08 00:32:57 -07003414 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (local_group) {
3417 this_load = avg_load;
3418 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003419 this_nr_running = sum_nr_running;
3420 this_load_per_task = sum_weighted_load;
3421 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003422 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 max_load = avg_load;
3424 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003425 busiest_nr_running = sum_nr_running;
3426 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003427 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003429
3430#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3431 /*
3432 * Busy processors will not participate in power savings
3433 * balance.
3434 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003435 if (idle == CPU_NOT_IDLE ||
3436 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3437 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003438
3439 /*
3440 * If the local group is idle or completely loaded
3441 * no need to do power savings balance at this domain
3442 */
3443 if (local_group && (this_nr_running >= group_capacity ||
3444 !this_nr_running))
3445 power_savings_balance = 0;
3446
Ingo Molnardd41f592007-07-09 18:51:59 +02003447 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003448 * If a group is already running at full capacity or idle,
3449 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003450 */
3451 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003452 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003453 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003454
Ingo Molnardd41f592007-07-09 18:51:59 +02003455 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003456 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003457 * This is the group from where we need to pick up the load
3458 * for saving power
3459 */
3460 if ((sum_nr_running < min_nr_running) ||
3461 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003462 first_cpu(group->cpumask) <
3463 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003464 group_min = group;
3465 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003466 min_load_per_task = sum_weighted_load /
3467 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003468 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003469
Ingo Molnardd41f592007-07-09 18:51:59 +02003470 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003471 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003472 * capacity but still has some space to pick up some load
3473 * from other group and save more power
3474 */
3475 if (sum_nr_running <= group_capacity - 1) {
3476 if (sum_nr_running > leader_nr_running ||
3477 (sum_nr_running == leader_nr_running &&
3478 first_cpu(group->cpumask) >
3479 first_cpu(group_leader->cpumask))) {
3480 group_leader = group;
3481 leader_nr_running = sum_nr_running;
3482 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003483 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003484group_next:
3485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 group = group->next;
3487 } while (group != sd->groups);
3488
Peter Williams2dd73a42006-06-27 02:54:34 -07003489 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 goto out_balanced;
3491
3492 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3493
3494 if (this_load >= avg_load ||
3495 100*max_load <= sd->imbalance_pct*this_load)
3496 goto out_balanced;
3497
Peter Williams2dd73a42006-06-27 02:54:34 -07003498 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003499 if (group_imb)
3500 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /*
3503 * We're trying to get all the cpus to the average_load, so we don't
3504 * want to push ourselves above the average load, nor do we wish to
3505 * reduce the max loaded cpu below the average load, as either of these
3506 * actions would just result in more rebalancing later, and ping-pong
3507 * tasks around. Thus we look for the minimum possible imbalance.
3508 * Negative imbalances (*we* are more loaded than anyone else) will
3509 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003510 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 * appear as very large values with unsigned longs.
3512 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003513 if (max_load <= busiest_load_per_task)
3514 goto out_balanced;
3515
3516 /*
3517 * In the presence of smp nice balancing, certain scenarios can have
3518 * max load less than avg load(as we skip the groups at or below
3519 * its cpu_power, while calculating max_load..)
3520 */
3521 if (max_load < avg_load) {
3522 *imbalance = 0;
3523 goto small_imbalance;
3524 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003525
3526 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003527 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003530 *imbalance = min(max_pull * busiest->__cpu_power,
3531 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 / SCHED_LOAD_SCALE;
3533
Peter Williams2dd73a42006-06-27 02:54:34 -07003534 /*
3535 * if *imbalance is less than the average load per runnable task
3536 * there is no gaurantee that any tasks will be moved so we'll have
3537 * a think about bumping its value to force at least one task to be
3538 * moved
3539 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003540 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003541 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003542 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Peter Williams2dd73a42006-06-27 02:54:34 -07003544small_imbalance:
3545 pwr_move = pwr_now = 0;
3546 imbn = 2;
3547 if (this_nr_running) {
3548 this_load_per_task /= this_nr_running;
3549 if (busiest_load_per_task > this_load_per_task)
3550 imbn = 1;
3551 } else
3552 this_load_per_task = SCHED_LOAD_SCALE;
3553
Ingo Molnardd41f592007-07-09 18:51:59 +02003554 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3555 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003556 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return busiest;
3558 }
3559
3560 /*
3561 * OK, we don't have enough imbalance to justify moving tasks,
3562 * however we may be able to increase total CPU power used by
3563 * moving them.
3564 */
3565
Eric Dumazet5517d862007-05-08 00:32:57 -07003566 pwr_now += busiest->__cpu_power *
3567 min(busiest_load_per_task, max_load);
3568 pwr_now += this->__cpu_power *
3569 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 pwr_now /= SCHED_LOAD_SCALE;
3571
3572 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003573 tmp = sg_div_cpu_power(busiest,
3574 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003576 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003577 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003580 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003581 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003582 tmp = sg_div_cpu_power(this,
3583 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003585 tmp = sg_div_cpu_power(this,
3586 busiest_load_per_task * SCHED_LOAD_SCALE);
3587 pwr_move += this->__cpu_power *
3588 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 pwr_move /= SCHED_LOAD_SCALE;
3590
3591 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003592 if (pwr_move > pwr_now)
3593 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return busiest;
3597
3598out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003599#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003600 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003601 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003603 if (this == group_leader && group_leader != group_min) {
3604 *imbalance = min_load_per_task;
3605 return group_min;
3606 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003607#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003608ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 *imbalance = 0;
3610 return NULL;
3611}
3612
3613/*
3614 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3615 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003616static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003617find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003618 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003620 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003621 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 int i;
3623
3624 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003625 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003626
3627 if (!cpu_isset(i, *cpus))
3628 continue;
3629
Ingo Molnar48f24c42006-07-03 00:25:40 -07003630 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003631 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Ingo Molnardd41f592007-07-09 18:51:59 +02003633 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003634 continue;
3635
Ingo Molnardd41f592007-07-09 18:51:59 +02003636 if (wl > max_load) {
3637 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003638 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640 }
3641
3642 return busiest;
3643}
3644
3645/*
Nick Piggin77391d72005-06-25 14:57:30 -07003646 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3647 * so long as it is large enough.
3648 */
3649#define MAX_PINNED_INTERVAL 512
3650
3651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3653 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003655static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003656 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003657 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Peter Williams43010652007-08-09 11:16:46 +02003659 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003662 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003663 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003664 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003665
Mike Travis7c16ec52008-04-04 18:11:11 -07003666 cpus_setall(*cpus);
3667
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003668 unlock_aggregate = get_aggregate(sd);
3669
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003670 /*
3671 * When power savings policy is enabled for the parent domain, idle
3672 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003673 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003674 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003675 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003676 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003677 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003678 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Ingo Molnar2d723762007-10-15 17:00:12 +02003680 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003682redo:
3683 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003684 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003685
Chen, Kenneth W06066712006-12-10 02:20:35 -08003686 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003687 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (!group) {
3690 schedstat_inc(sd, lb_nobusyg[idle]);
3691 goto out_balanced;
3692 }
3693
Mike Travis7c16ec52008-04-04 18:11:11 -07003694 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 if (!busiest) {
3696 schedstat_inc(sd, lb_nobusyq[idle]);
3697 goto out_balanced;
3698 }
3699
Nick Piggindb935db2005-06-25 14:57:11 -07003700 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702 schedstat_add(sd, lb_imbalance[idle], imbalance);
3703
Peter Williams43010652007-08-09 11:16:46 +02003704 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (busiest->nr_running > 1) {
3706 /*
3707 * Attempt to move tasks. If find_busiest_group has found
3708 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003709 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 * correctly treated as an imbalance.
3711 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003712 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003713 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003714 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003715 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003716 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003717 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003718
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003719 /*
3720 * some other cpu did the load balance for us.
3721 */
Peter Williams43010652007-08-09 11:16:46 +02003722 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003723 resched_cpu(this_cpu);
3724
Nick Piggin81026792005-06-25 14:57:07 -07003725 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003726 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003727 cpu_clear(cpu_of(busiest), *cpus);
3728 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003729 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003730 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
Nick Piggin81026792005-06-25 14:57:07 -07003733
Peter Williams43010652007-08-09 11:16:46 +02003734 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 schedstat_inc(sd, lb_failed[idle]);
3736 sd->nr_balance_failed++;
3737
3738 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003740 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003741
3742 /* don't kick the migration_thread, if the curr
3743 * task on busiest cpu can't be moved to this_cpu
3744 */
3745 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003746 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003747 all_pinned = 1;
3748 goto out_one_pinned;
3749 }
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (!busiest->active_balance) {
3752 busiest->active_balance = 1;
3753 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003754 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003756 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003757 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 wake_up_process(busiest->migration_thread);
3759
3760 /*
3761 * We've kicked active balancing, reset the failure
3762 * counter.
3763 */
Nick Piggin39507452005-06-25 14:57:09 -07003764 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
Nick Piggin81026792005-06-25 14:57:07 -07003766 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 sd->nr_balance_failed = 0;
3768
Nick Piggin81026792005-06-25 14:57:07 -07003769 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 /* We were unbalanced, so reset the balancing interval */
3771 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003772 } else {
3773 /*
3774 * If we've begun active balancing, start to back off. This
3775 * case may not be covered by the all_pinned logic if there
3776 * is only 1 task on the busy runqueue (because we don't call
3777 * move_tasks).
3778 */
3779 if (sd->balance_interval < sd->max_interval)
3780 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
Peter Williams43010652007-08-09 11:16:46 +02003783 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003784 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003785 ld_moved = -1;
3786
3787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 schedstat_inc(sd, lb_balanced[idle]);
3791
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003792 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003793
3794out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003796 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3797 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 sd->balance_interval *= 2;
3799
Ingo Molnar48f24c42006-07-03 00:25:40 -07003800 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003801 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003802 ld_moved = -1;
3803 else
3804 ld_moved = 0;
3805out:
3806 if (unlock_aggregate)
3807 put_aggregate(sd);
3808 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
3811/*
3812 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3813 * tasks if there is an imbalance.
3814 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003815 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 * this_rq is locked.
3817 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003818static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003819load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3820 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
3822 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003823 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003825 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003826 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003827 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003828
3829 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003830
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003831 /*
3832 * When power savings policy is enabled for the parent domain, idle
3833 * sibling can pick up load irrespective of busy siblings. In this case,
3834 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003835 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003836 */
3837 if (sd->flags & SD_SHARE_CPUPOWER &&
3838 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003839 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Ingo Molnar2d723762007-10-15 17:00:12 +02003841 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003842redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003843 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003844 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003846 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003847 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849
Mike Travis7c16ec52008-04-04 18:11:11 -07003850 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003851 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003852 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003853 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855
Nick Piggindb935db2005-06-25 14:57:11 -07003856 BUG_ON(busiest == this_rq);
3857
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003858 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003859
Peter Williams43010652007-08-09 11:16:46 +02003860 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003861 if (busiest->nr_running > 1) {
3862 /* Attempt to move tasks */
3863 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003864 /* this_rq->clock is already updated */
3865 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003866 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003867 imbalance, sd, CPU_NEWLY_IDLE,
3868 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003869 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003870
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003871 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003872 cpu_clear(cpu_of(busiest), *cpus);
3873 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003874 goto redo;
3875 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003876 }
3877
Peter Williams43010652007-08-09 11:16:46 +02003878 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003879 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003880 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3881 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003882 return -1;
3883 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003884 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Peter Williams43010652007-08-09 11:16:46 +02003886 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003887
3888out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003889 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003890 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003891 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003892 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003893 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003894
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898/*
3899 * idle_balance is called by schedule() if this_cpu is about to become
3900 * idle. Attempts to pull tasks from other CPUs.
3901 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003902static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
3904 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003905 int pulled_task = -1;
3906 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003907 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003910 unsigned long interval;
3911
3912 if (!(sd->flags & SD_LOAD_BALANCE))
3913 continue;
3914
3915 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003916 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003917 pulled_task = load_balance_newidle(this_cpu, this_rq,
3918 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003919
3920 interval = msecs_to_jiffies(sd->balance_interval);
3921 if (time_after(next_balance, sd->last_balance + interval))
3922 next_balance = sd->last_balance + interval;
3923 if (pulled_task)
3924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003926 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003927 /*
3928 * We are going idle. next_balance may be set based on
3929 * a busy processor. So reset next_balance.
3930 */
3931 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
3935/*
3936 * active_load_balance is run by migration threads. It pushes running tasks
3937 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3938 * running on each physical CPU where possible, and avoids physical /
3939 * logical imbalances.
3940 *
3941 * Called with busiest_rq locked.
3942 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003943static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
Nick Piggin39507452005-06-25 14:57:09 -07003945 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003946 struct sched_domain *sd;
3947 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003948
Ingo Molnar48f24c42006-07-03 00:25:40 -07003949 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003950 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003951 return;
3952
3953 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 /*
Nick Piggin39507452005-06-25 14:57:09 -07003956 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003957 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003958 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 */
Nick Piggin39507452005-06-25 14:57:09 -07003960 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Nick Piggin39507452005-06-25 14:57:09 -07003962 /* move a task from busiest_rq to target_rq */
3963 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003964 update_rq_clock(busiest_rq);
3965 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Nick Piggin39507452005-06-25 14:57:09 -07003967 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003968 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003969 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003970 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003971 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Ingo Molnar48f24c42006-07-03 00:25:40 -07003974 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003975 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Peter Williams43010652007-08-09 11:16:46 +02003977 if (move_one_task(target_rq, target_cpu, busiest_rq,
3978 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003979 schedstat_inc(sd, alb_pushed);
3980 else
3981 schedstat_inc(sd, alb_failed);
3982 }
Nick Piggin39507452005-06-25 14:57:09 -07003983 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003986#ifdef CONFIG_NO_HZ
3987static struct {
3988 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003989 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003990} nohz ____cacheline_aligned = {
3991 .load_balancer = ATOMIC_INIT(-1),
3992 .cpu_mask = CPU_MASK_NONE,
3993};
3994
Christoph Lameter7835b982006-12-10 02:20:22 -08003995/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003996 * This routine will try to nominate the ilb (idle load balancing)
3997 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3998 * load balancing on behalf of all those cpus. If all the cpus in the system
3999 * go into this tickless mode, then there will be no ilb owner (as there is
4000 * no need for one) and all the cpus will sleep till the next wakeup event
4001 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004002 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004003 * For the ilb owner, tick is not stopped. And this tick will be used
4004 * for idle load balancing. ilb owner will still be part of
4005 * nohz.cpu_mask..
4006 *
4007 * While stopping the tick, this cpu will become the ilb owner if there
4008 * is no other owner. And will be the owner till that cpu becomes busy
4009 * or if all cpus in the system stop their ticks at which point
4010 * there is no need for ilb owner.
4011 *
4012 * When the ilb owner becomes busy, it nominates another owner, during the
4013 * next busy scheduler_tick()
4014 */
4015int select_nohz_load_balancer(int stop_tick)
4016{
4017 int cpu = smp_processor_id();
4018
4019 if (stop_tick) {
4020 cpu_set(cpu, nohz.cpu_mask);
4021 cpu_rq(cpu)->in_nohz_recently = 1;
4022
4023 /*
4024 * If we are going offline and still the leader, give up!
4025 */
4026 if (cpu_is_offline(cpu) &&
4027 atomic_read(&nohz.load_balancer) == cpu) {
4028 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4029 BUG();
4030 return 0;
4031 }
4032
4033 /* time for ilb owner also to sleep */
4034 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4035 if (atomic_read(&nohz.load_balancer) == cpu)
4036 atomic_set(&nohz.load_balancer, -1);
4037 return 0;
4038 }
4039
4040 if (atomic_read(&nohz.load_balancer) == -1) {
4041 /* make me the ilb owner */
4042 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4043 return 1;
4044 } else if (atomic_read(&nohz.load_balancer) == cpu)
4045 return 1;
4046 } else {
4047 if (!cpu_isset(cpu, nohz.cpu_mask))
4048 return 0;
4049
4050 cpu_clear(cpu, nohz.cpu_mask);
4051
4052 if (atomic_read(&nohz.load_balancer) == cpu)
4053 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4054 BUG();
4055 }
4056 return 0;
4057}
4058#endif
4059
4060static DEFINE_SPINLOCK(balancing);
4061
4062/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004063 * It checks each scheduling domain to see if it is due to be balanced,
4064 * and initiates a balancing operation if so.
4065 *
4066 * Balancing parameters are set up in arch_init_sched_domains.
4067 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004068static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004069{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004070 int balance = 1;
4071 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004072 unsigned long interval;
4073 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004074 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004075 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004076 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004077 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004079 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 if (!(sd->flags & SD_LOAD_BALANCE))
4081 continue;
4082
4083 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004084 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 interval *= sd->busy_factor;
4086
4087 /* scale ms to jiffies */
4088 interval = msecs_to_jiffies(interval);
4089 if (unlikely(!interval))
4090 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004091 if (interval > HZ*NR_CPUS/10)
4092 interval = HZ*NR_CPUS/10;
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Christoph Lameter08c183f2006-12-10 02:20:29 -08004095 if (sd->flags & SD_SERIALIZE) {
4096 if (!spin_trylock(&balancing))
4097 goto out;
4098 }
4099
Christoph Lameterc9819f42006-12-10 02:20:25 -08004100 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004101 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004102 /*
4103 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004104 * longer idle, or one of our SMT siblings is
4105 * not idle.
4106 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004107 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004109 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004111 if (sd->flags & SD_SERIALIZE)
4112 spin_unlock(&balancing);
4113out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004114 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004115 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004116 update_next_balance = 1;
4117 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004118
4119 /*
4120 * Stop the load balance at this level. There is another
4121 * CPU in our sched group which is doing load balancing more
4122 * actively.
4123 */
4124 if (!balance)
4125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004127
4128 /*
4129 * next_balance will be updated only when there is a need.
4130 * When the cpu is attached to null domain for ex, it will not be
4131 * updated.
4132 */
4133 if (likely(update_next_balance))
4134 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004135}
4136
4137/*
4138 * run_rebalance_domains is triggered when needed from the scheduler tick.
4139 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4140 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4141 */
4142static void run_rebalance_domains(struct softirq_action *h)
4143{
Ingo Molnardd41f592007-07-09 18:51:59 +02004144 int this_cpu = smp_processor_id();
4145 struct rq *this_rq = cpu_rq(this_cpu);
4146 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4147 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004148
Ingo Molnardd41f592007-07-09 18:51:59 +02004149 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004150
4151#ifdef CONFIG_NO_HZ
4152 /*
4153 * If this cpu is the owner for idle load balancing, then do the
4154 * balancing on behalf of the other idle cpus whose ticks are
4155 * stopped.
4156 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004157 if (this_rq->idle_at_tick &&
4158 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004159 cpumask_t cpus = nohz.cpu_mask;
4160 struct rq *rq;
4161 int balance_cpu;
4162
Ingo Molnardd41f592007-07-09 18:51:59 +02004163 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004164 for_each_cpu_mask(balance_cpu, cpus) {
4165 /*
4166 * If this cpu gets work to do, stop the load balancing
4167 * work being done for other cpus. Next load
4168 * balancing owner will pick it up.
4169 */
4170 if (need_resched())
4171 break;
4172
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004173 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004174
4175 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004176 if (time_after(this_rq->next_balance, rq->next_balance))
4177 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004178 }
4179 }
4180#endif
4181}
4182
4183/*
4184 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4185 *
4186 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4187 * idle load balancing owner or decide to stop the periodic load balancing,
4188 * if the whole system is idle.
4189 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004190static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004191{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004192#ifdef CONFIG_NO_HZ
4193 /*
4194 * If we were in the nohz mode recently and busy at the current
4195 * scheduler tick, then check if we need to nominate new idle
4196 * load balancer.
4197 */
4198 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4199 rq->in_nohz_recently = 0;
4200
4201 if (atomic_read(&nohz.load_balancer) == cpu) {
4202 cpu_clear(cpu, nohz.cpu_mask);
4203 atomic_set(&nohz.load_balancer, -1);
4204 }
4205
4206 if (atomic_read(&nohz.load_balancer) == -1) {
4207 /*
4208 * simple selection for now: Nominate the
4209 * first cpu in the nohz list to be the next
4210 * ilb owner.
4211 *
4212 * TBD: Traverse the sched domains and nominate
4213 * the nearest cpu in the nohz.cpu_mask.
4214 */
4215 int ilb = first_cpu(nohz.cpu_mask);
4216
Mike Travis434d53b2008-04-04 18:11:04 -07004217 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004218 resched_cpu(ilb);
4219 }
4220 }
4221
4222 /*
4223 * If this cpu is idle and doing idle load balancing for all the
4224 * cpus with ticks stopped, is it time for that to stop?
4225 */
4226 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4227 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4228 resched_cpu(cpu);
4229 return;
4230 }
4231
4232 /*
4233 * If this cpu is idle and the idle load balancing is done by
4234 * someone else, then no need raise the SCHED_SOFTIRQ
4235 */
4236 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4237 cpu_isset(cpu, nohz.cpu_mask))
4238 return;
4239#endif
4240 if (time_after_eq(jiffies, rq->next_balance))
4241 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
Ingo Molnardd41f592007-07-09 18:51:59 +02004243
4244#else /* CONFIG_SMP */
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246/*
4247 * on UP we do not need to balance between CPUs:
4248 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004249static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
4251}
Ingo Molnardd41f592007-07-09 18:51:59 +02004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253#endif
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255DEFINE_PER_CPU(struct kernel_stat, kstat);
4256
4257EXPORT_PER_CPU_SYMBOL(kstat);
4258
4259/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004260 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4261 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004263unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004266 u64 ns, delta_exec;
4267 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004268
Ingo Molnar41b86e92007-07-09 18:51:58 +02004269 rq = task_rq_lock(p, &flags);
4270 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004271 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004272 update_rq_clock(rq);
4273 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004274 if ((s64)delta_exec > 0)
4275 ns += delta_exec;
4276 }
4277 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return ns;
4280}
4281
4282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 * Account user cpu time to a process.
4284 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 * @cputime: the cpu time spent in user space since the last update
4286 */
4287void account_user_time(struct task_struct *p, cputime_t cputime)
4288{
4289 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4290 cputime64_t tmp;
4291
4292 p->utime = cputime_add(p->utime, cputime);
4293
4294 /* Add user time to cpustat. */
4295 tmp = cputime_to_cputime64(cputime);
4296 if (TASK_NICE(p) > 0)
4297 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4298 else
4299 cpustat->user = cputime64_add(cpustat->user, tmp);
4300}
4301
4302/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004303 * Account guest cpu time to a process.
4304 * @p: the process that the cpu time gets accounted to
4305 * @cputime: the cpu time spent in virtual machine since the last update
4306 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004307static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004308{
4309 cputime64_t tmp;
4310 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4311
4312 tmp = cputime_to_cputime64(cputime);
4313
4314 p->utime = cputime_add(p->utime, cputime);
4315 p->gtime = cputime_add(p->gtime, cputime);
4316
4317 cpustat->user = cputime64_add(cpustat->user, tmp);
4318 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4319}
4320
4321/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004322 * Account scaled user cpu time to a process.
4323 * @p: the process that the cpu time gets accounted to
4324 * @cputime: the cpu time spent in user space since the last update
4325 */
4326void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4327{
4328 p->utimescaled = cputime_add(p->utimescaled, cputime);
4329}
4330
4331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 * Account system cpu time to a process.
4333 * @p: the process that the cpu time gets accounted to
4334 * @hardirq_offset: the offset to subtract from hardirq_count()
4335 * @cputime: the cpu time spent in kernel space since the last update
4336 */
4337void account_system_time(struct task_struct *p, int hardirq_offset,
4338 cputime_t cputime)
4339{
4340 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004341 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 cputime64_t tmp;
4343
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004344 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4345 account_guest_time(p, cputime);
4346 return;
4347 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 p->stime = cputime_add(p->stime, cputime);
4350
4351 /* Add system time to cpustat. */
4352 tmp = cputime_to_cputime64(cputime);
4353 if (hardirq_count() - hardirq_offset)
4354 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4355 else if (softirq_count())
4356 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004357 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004359 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4361 else
4362 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4363 /* Account for system time used */
4364 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
4367/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004368 * Account scaled system cpu time to a process.
4369 * @p: the process that the cpu time gets accounted to
4370 * @hardirq_offset: the offset to subtract from hardirq_count()
4371 * @cputime: the cpu time spent in kernel space since the last update
4372 */
4373void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4374{
4375 p->stimescaled = cputime_add(p->stimescaled, cputime);
4376}
4377
4378/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 * Account for involuntary wait time.
4380 * @p: the process from which the cpu time has been stolen
4381 * @steal: the cpu time spent in involuntary wait
4382 */
4383void account_steal_time(struct task_struct *p, cputime_t steal)
4384{
4385 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4386 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004387 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 if (p == rq->idle) {
4390 p->stime = cputime_add(p->stime, steal);
4391 if (atomic_read(&rq->nr_iowait) > 0)
4392 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4393 else
4394 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004395 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4397}
4398
Christoph Lameter7835b982006-12-10 02:20:22 -08004399/*
4400 * This function gets called by the timer code, with HZ frequency.
4401 * We call it with interrupts disabled.
4402 *
4403 * It also gets called by the fork code, when changing the parent's
4404 * timeslices.
4405 */
4406void scheduler_tick(void)
4407{
Christoph Lameter7835b982006-12-10 02:20:22 -08004408 int cpu = smp_processor_id();
4409 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004410 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004411 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004412
Ingo Molnardd41f592007-07-09 18:51:59 +02004413 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004414 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004415 /*
4416 * Let rq->clock advance by at least TICK_NSEC:
4417 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004418 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004419 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004420 rq->clock_underflows++;
4421 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004422 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004423 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004424 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004425 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004426 spin_unlock(&rq->lock);
4427
Christoph Lametere418e1c2006-12-10 02:20:23 -08004428#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004429 rq->idle_at_tick = idle_cpu(cpu);
4430 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004431#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4435
Srinivasa Ds43627582008-02-23 15:24:04 -08004436void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 /*
4439 * Underflow?
4440 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004441 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 preempt_count() += val;
4444 /*
4445 * Spinlock count overflowing soon?
4446 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004447 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4448 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450EXPORT_SYMBOL(add_preempt_count);
4451
Srinivasa Ds43627582008-02-23 15:24:04 -08004452void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
4454 /*
4455 * Underflow?
4456 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004457 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 /*
4460 * Is the spinlock portion underflowing?
4461 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004462 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4463 !(preempt_count() & PREEMPT_MASK)))
4464 return;
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 preempt_count() -= val;
4467}
4468EXPORT_SYMBOL(sub_preempt_count);
4469
4470#endif
4471
4472/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004473 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004475static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Satyam Sharma838225b2007-10-24 18:23:50 +02004477 struct pt_regs *regs = get_irq_regs();
4478
4479 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4480 prev->comm, prev->pid, preempt_count());
4481
Ingo Molnardd41f592007-07-09 18:51:59 +02004482 debug_show_held_locks(prev);
4483 if (irqs_disabled())
4484 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004485
4486 if (regs)
4487 show_regs(regs);
4488 else
4489 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Ingo Molnardd41f592007-07-09 18:51:59 +02004492/*
4493 * Various schedule()-time debugging checks and statistics:
4494 */
4495static inline void schedule_debug(struct task_struct *prev)
4496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004498 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 * schedule() atomically, we ignore that path for now.
4500 * Otherwise, whine if we are scheduling when we should not be.
4501 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004502 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4503 __schedule_bug(prev);
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4506
Ingo Molnar2d723762007-10-15 17:00:12 +02004507 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004508#ifdef CONFIG_SCHEDSTATS
4509 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004510 schedstat_inc(this_rq(), bkl_count);
4511 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004512 }
4513#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004514}
4515
4516/*
4517 * Pick up the highest-prio task:
4518 */
4519static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004520pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004521{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004522 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004523 struct task_struct *p;
4524
4525 /*
4526 * Optimization: we know that if all tasks are in
4527 * the fair class we can call that function directly:
4528 */
4529 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004530 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004531 if (likely(p))
4532 return p;
4533 }
4534
4535 class = sched_class_highest;
4536 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004537 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004538 if (p)
4539 return p;
4540 /*
4541 * Will never be NULL as the idle class always
4542 * returns a non-NULL p:
4543 */
4544 class = class->next;
4545 }
4546}
4547
4548/*
4549 * schedule() is the main scheduler function.
4550 */
4551asmlinkage void __sched schedule(void)
4552{
4553 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004554 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004555 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004556 int cpu;
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558need_resched:
4559 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004560 cpu = smp_processor_id();
4561 rq = cpu_rq(cpu);
4562 rcu_qsctr_inc(cpu);
4563 prev = rq->curr;
4564 switch_count = &prev->nivcsw;
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 release_kernel_lock(prev);
4567need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Ingo Molnardd41f592007-07-09 18:51:59 +02004569 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004571 hrtick_clear(rq);
4572
Ingo Molnar1e819952007-10-15 17:00:13 +02004573 /*
4574 * Do the rq-clock update outside the rq lock:
4575 */
4576 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004577 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004578 spin_lock(&rq->lock);
4579 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Ingo Molnardd41f592007-07-09 18:51:59 +02004581 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4582 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004583 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004584 prev->state = TASK_RUNNING;
4585 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004586 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004587 }
4588 switch_count = &prev->nvcsw;
4589 }
4590
Steven Rostedt9a897c52008-01-25 21:08:22 +01004591#ifdef CONFIG_SMP
4592 if (prev->sched_class->pre_schedule)
4593 prev->sched_class->pre_schedule(rq, prev);
4594#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004595
Ingo Molnardd41f592007-07-09 18:51:59 +02004596 if (unlikely(!rq->nr_running))
4597 idle_balance(cpu, rq);
4598
Ingo Molnar31ee5292007-08-09 11:16:49 +02004599 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004600 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 rq->nr_switches++;
4606 rq->curr = next;
4607 ++*switch_count;
4608
Ingo Molnardd41f592007-07-09 18:51:59 +02004609 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004610 /*
4611 * the context switch might have flipped the stack from under
4612 * us, hence refresh the local variables.
4613 */
4614 cpu = smp_processor_id();
4615 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 } else
4617 spin_unlock_irq(&rq->lock);
4618
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004619 hrtick_set(rq);
4620
4621 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 preempt_enable_no_resched();
4625 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4626 goto need_resched;
4627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628EXPORT_SYMBOL(schedule);
4629
4630#ifdef CONFIG_PREEMPT
4631/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004632 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004633 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 * occur there and call schedule directly.
4635 */
4636asmlinkage void __sched preempt_schedule(void)
4637{
4638 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct task_struct *task = current;
4640 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /*
4643 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004644 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004646 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return;
4648
Andi Kleen3a5c3592007-10-15 17:00:14 +02004649 do {
4650 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Andi Kleen3a5c3592007-10-15 17:00:14 +02004652 /*
4653 * We keep the big kernel semaphore locked, but we
4654 * clear ->lock_depth so that schedule() doesnt
4655 * auto-release the semaphore:
4656 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004657 saved_lock_depth = task->lock_depth;
4658 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004659 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004660 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004661 sub_preempt_count(PREEMPT_ACTIVE);
4662
4663 /*
4664 * Check again in case we missed a preemption opportunity
4665 * between schedule and now.
4666 */
4667 barrier();
4668 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670EXPORT_SYMBOL(preempt_schedule);
4671
4672/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004673 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 * off of irq context.
4675 * Note, that this is called and return with irqs disabled. This will
4676 * protect us against recursive calling from irq.
4677 */
4678asmlinkage void __sched preempt_schedule_irq(void)
4679{
4680 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 struct task_struct *task = current;
4682 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004683
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004684 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 BUG_ON(ti->preempt_count || !irqs_disabled());
4686
Andi Kleen3a5c3592007-10-15 17:00:14 +02004687 do {
4688 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Andi Kleen3a5c3592007-10-15 17:00:14 +02004690 /*
4691 * We keep the big kernel semaphore locked, but we
4692 * clear ->lock_depth so that schedule() doesnt
4693 * auto-release the semaphore:
4694 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004695 saved_lock_depth = task->lock_depth;
4696 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004697 local_irq_enable();
4698 schedule();
4699 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004700 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004701 sub_preempt_count(PREEMPT_ACTIVE);
4702
4703 /*
4704 * Check again in case we missed a preemption opportunity
4705 * between schedule and now.
4706 */
4707 barrier();
4708 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
4711#endif /* CONFIG_PREEMPT */
4712
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004713int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4714 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004716 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718EXPORT_SYMBOL(default_wake_function);
4719
4720/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004721 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4722 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 * number) then we wake all the non-exclusive tasks and one exclusive task.
4724 *
4725 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004726 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4728 */
4729static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4730 int nr_exclusive, int sync, void *key)
4731{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004732 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004734 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004735 unsigned flags = curr->flags;
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004738 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 break;
4740 }
4741}
4742
4743/**
4744 * __wake_up - wake up threads blocked on a waitqueue.
4745 * @q: the waitqueue
4746 * @mode: which threads
4747 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004748 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004750void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004751 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
4753 unsigned long flags;
4754
4755 spin_lock_irqsave(&q->lock, flags);
4756 __wake_up_common(q, mode, nr_exclusive, 0, key);
4757 spin_unlock_irqrestore(&q->lock, flags);
4758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759EXPORT_SYMBOL(__wake_up);
4760
4761/*
4762 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4763 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004764void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
4766 __wake_up_common(q, mode, 1, 0, NULL);
4767}
4768
4769/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004770 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 * @q: the waitqueue
4772 * @mode: which threads
4773 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4774 *
4775 * The sync wakeup differs that the waker knows that it will schedule
4776 * away soon, so while the target thread will be woken up, it will not
4777 * be migrated to another CPU - ie. the two threads are 'synchronized'
4778 * with each other. This can prevent needless bouncing between CPUs.
4779 *
4780 * On UP it can prevent extra preemption.
4781 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004782void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004783__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
4785 unsigned long flags;
4786 int sync = 1;
4787
4788 if (unlikely(!q))
4789 return;
4790
4791 if (unlikely(!nr_exclusive))
4792 sync = 0;
4793
4794 spin_lock_irqsave(&q->lock, flags);
4795 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4796 spin_unlock_irqrestore(&q->lock, flags);
4797}
4798EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4799
Ingo Molnarb15136e2007-10-24 18:23:48 +02004800void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
4802 unsigned long flags;
4803
4804 spin_lock_irqsave(&x->wait.lock, flags);
4805 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004806 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 spin_unlock_irqrestore(&x->wait.lock, flags);
4808}
4809EXPORT_SYMBOL(complete);
4810
Ingo Molnarb15136e2007-10-24 18:23:48 +02004811void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
4813 unsigned long flags;
4814
4815 spin_lock_irqsave(&x->wait.lock, flags);
4816 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004817 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 spin_unlock_irqrestore(&x->wait.lock, flags);
4819}
4820EXPORT_SYMBOL(complete_all);
4821
Andi Kleen8cbbe862007-10-15 17:00:14 +02004822static inline long __sched
4823do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (!x->done) {
4826 DECLARE_WAITQUEUE(wait, current);
4827
4828 wait.flags |= WQ_FLAG_EXCLUSIVE;
4829 __add_wait_queue_tail(&x->wait, &wait);
4830 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004831 if ((state == TASK_INTERRUPTIBLE &&
4832 signal_pending(current)) ||
4833 (state == TASK_KILLABLE &&
4834 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004835 __remove_wait_queue(&x->wait, &wait);
4836 return -ERESTARTSYS;
4837 }
4838 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004840 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004842 if (!timeout) {
4843 __remove_wait_queue(&x->wait, &wait);
4844 return timeout;
4845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 } while (!x->done);
4847 __remove_wait_queue(&x->wait, &wait);
4848 }
4849 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004850 return timeout;
4851}
4852
4853static long __sched
4854wait_for_common(struct completion *x, long timeout, int state)
4855{
4856 might_sleep();
4857
4858 spin_lock_irq(&x->wait.lock);
4859 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004861 return timeout;
4862}
4863
Ingo Molnarb15136e2007-10-24 18:23:48 +02004864void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004865{
4866 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
4868EXPORT_SYMBOL(wait_for_completion);
4869
Ingo Molnarb15136e2007-10-24 18:23:48 +02004870unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4872{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004873 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875EXPORT_SYMBOL(wait_for_completion_timeout);
4876
Andi Kleen8cbbe862007-10-15 17:00:14 +02004877int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Andi Kleen51e97992007-10-18 21:32:55 +02004879 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4880 if (t == -ERESTARTSYS)
4881 return t;
4882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884EXPORT_SYMBOL(wait_for_completion_interruptible);
4885
Ingo Molnarb15136e2007-10-24 18:23:48 +02004886unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887wait_for_completion_interruptible_timeout(struct completion *x,
4888 unsigned long timeout)
4889{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004890 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4893
Matthew Wilcox009e5772007-12-06 12:29:54 -05004894int __sched wait_for_completion_killable(struct completion *x)
4895{
4896 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4897 if (t == -ERESTARTSYS)
4898 return t;
4899 return 0;
4900}
4901EXPORT_SYMBOL(wait_for_completion_killable);
4902
Andi Kleen8cbbe862007-10-15 17:00:14 +02004903static long __sched
4904sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004905{
4906 unsigned long flags;
4907 wait_queue_t wait;
4908
4909 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Andi Kleen8cbbe862007-10-15 17:00:14 +02004911 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Andi Kleen8cbbe862007-10-15 17:00:14 +02004913 spin_lock_irqsave(&q->lock, flags);
4914 __add_wait_queue(q, &wait);
4915 spin_unlock(&q->lock);
4916 timeout = schedule_timeout(timeout);
4917 spin_lock_irq(&q->lock);
4918 __remove_wait_queue(q, &wait);
4919 spin_unlock_irqrestore(&q->lock, flags);
4920
4921 return timeout;
4922}
4923
4924void __sched interruptible_sleep_on(wait_queue_head_t *q)
4925{
4926 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928EXPORT_SYMBOL(interruptible_sleep_on);
4929
Ingo Molnar0fec1712007-07-09 18:52:01 +02004930long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004931interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004933 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4936
Ingo Molnar0fec1712007-07-09 18:52:01 +02004937void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004939 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941EXPORT_SYMBOL(sleep_on);
4942
Ingo Molnar0fec1712007-07-09 18:52:01 +02004943long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004945 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947EXPORT_SYMBOL(sleep_on_timeout);
4948
Ingo Molnarb29739f2006-06-27 02:54:51 -07004949#ifdef CONFIG_RT_MUTEXES
4950
4951/*
4952 * rt_mutex_setprio - set the current priority of a task
4953 * @p: task
4954 * @prio: prio value (kernel-internal form)
4955 *
4956 * This function changes the 'effective' priority of a task. It does
4957 * not touch ->normal_prio like __setscheduler().
4958 *
4959 * Used by the rt_mutex code to implement priority inheritance logic.
4960 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004961void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004962{
4963 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004964 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004965 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004966 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004967
4968 BUG_ON(prio < 0 || prio > MAX_PRIO);
4969
4970 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004971 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004972
Andrew Mortond5f9f942007-05-08 20:27:06 -07004973 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004974 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004975 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004976 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004977 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004978 if (running)
4979 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004980
4981 if (rt_prio(prio))
4982 p->sched_class = &rt_sched_class;
4983 else
4984 p->sched_class = &fair_sched_class;
4985
Ingo Molnarb29739f2006-06-27 02:54:51 -07004986 p->prio = prio;
4987
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004988 if (running)
4989 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004990 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004991 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004992
4993 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004994 }
4995 task_rq_unlock(rq, &flags);
4996}
4997
4998#endif
4999
Ingo Molnar36c8b582006-07-03 00:25:41 -07005000void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
Ingo Molnardd41f592007-07-09 18:51:59 +02005002 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005004 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5007 return;
5008 /*
5009 * We have to be careful, if called from sys_setpriority(),
5010 * the task might be in the middle of scheduling on another CPU.
5011 */
5012 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005013 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 /*
5015 * The RT priorities are set via sched_setscheduler(), but we still
5016 * allow the 'normal' nice value to be set - but as expected
5017 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005018 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005020 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 p->static_prio = NICE_TO_PRIO(nice);
5022 goto out_unlock;
5023 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005024 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005025 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005026 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005029 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005030 old_prio = p->prio;
5031 p->prio = effective_prio(p);
5032 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Ingo Molnardd41f592007-07-09 18:51:59 +02005034 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005035 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005037 * If the task increased its priority or is running and
5038 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005040 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 resched_task(rq->curr);
5042 }
5043out_unlock:
5044 task_rq_unlock(rq, &flags);
5045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046EXPORT_SYMBOL(set_user_nice);
5047
Matt Mackalle43379f2005-05-01 08:59:00 -07005048/*
5049 * can_nice - check if a task can reduce its nice value
5050 * @p: task
5051 * @nice: nice value
5052 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005053int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005054{
Matt Mackall024f4742005-08-18 11:24:19 -07005055 /* convert nice value [19,-20] to rlimit style value [1,40] */
5056 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005057
Matt Mackalle43379f2005-05-01 08:59:00 -07005058 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5059 capable(CAP_SYS_NICE));
5060}
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062#ifdef __ARCH_WANT_SYS_NICE
5063
5064/*
5065 * sys_nice - change the priority of the current process.
5066 * @increment: priority increment
5067 *
5068 * sys_setpriority is a more generic, but much slower function that
5069 * does similar things.
5070 */
5071asmlinkage long sys_nice(int increment)
5072{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005073 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
5075 /*
5076 * Setpriority might change our priority at the same moment.
5077 * We don't have to worry. Conceptually one call occurs first
5078 * and we have a single winner.
5079 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005080 if (increment < -40)
5081 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 if (increment > 40)
5083 increment = 40;
5084
5085 nice = PRIO_TO_NICE(current->static_prio) + increment;
5086 if (nice < -20)
5087 nice = -20;
5088 if (nice > 19)
5089 nice = 19;
5090
Matt Mackalle43379f2005-05-01 08:59:00 -07005091 if (increment < 0 && !can_nice(current, nice))
5092 return -EPERM;
5093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 retval = security_task_setnice(current, nice);
5095 if (retval)
5096 return retval;
5097
5098 set_user_nice(current, nice);
5099 return 0;
5100}
5101
5102#endif
5103
5104/**
5105 * task_prio - return the priority value of a given task.
5106 * @p: the task in question.
5107 *
5108 * This is the priority value as seen by users in /proc.
5109 * RT tasks are offset by -200. Normal tasks are centered
5110 * around 0, value goes from -16 to +15.
5111 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005112int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
5114 return p->prio - MAX_RT_PRIO;
5115}
5116
5117/**
5118 * task_nice - return the nice value of a given task.
5119 * @p: the task in question.
5120 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005121int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
5123 return TASK_NICE(p);
5124}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005125EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
5127/**
5128 * idle_cpu - is a given cpu idle currently?
5129 * @cpu: the processor in question.
5130 */
5131int idle_cpu(int cpu)
5132{
5133 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5134}
5135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136/**
5137 * idle_task - return the idle task for a given cpu.
5138 * @cpu: the processor in question.
5139 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005140struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
5142 return cpu_rq(cpu)->idle;
5143}
5144
5145/**
5146 * find_process_by_pid - find a process with a matching PID value.
5147 * @pid: the pid in question.
5148 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005149static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005151 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152}
5153
5154/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005155static void
5156__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157{
Ingo Molnardd41f592007-07-09 18:51:59 +02005158 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005161 switch (p->policy) {
5162 case SCHED_NORMAL:
5163 case SCHED_BATCH:
5164 case SCHED_IDLE:
5165 p->sched_class = &fair_sched_class;
5166 break;
5167 case SCHED_FIFO:
5168 case SCHED_RR:
5169 p->sched_class = &rt_sched_class;
5170 break;
5171 }
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005174 p->normal_prio = normal_prio(p);
5175 /* we are holding p->pi_lock already */
5176 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005177 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178}
5179
5180/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005181 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 * @p: the task in question.
5183 * @policy: new policy.
5184 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005185 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005186 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005188int sched_setscheduler(struct task_struct *p, int policy,
5189 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005191 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005193 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005194 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Steven Rostedt66e53932006-06-27 02:54:44 -07005196 /* may grab non-irq protected spin_locks */
5197 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198recheck:
5199 /* double check policy once rq lock held */
5200 if (policy < 0)
5201 policy = oldpolicy = p->policy;
5202 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005203 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5204 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 /*
5207 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005208 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5209 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 */
5211 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005212 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005213 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005215 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return -EINVAL;
5217
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005218 /*
5219 * Allow unprivileged RT tasks to decrease priority:
5220 */
5221 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005222 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005223 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005224
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005225 if (!lock_task_sighand(p, &flags))
5226 return -ESRCH;
5227 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5228 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005229
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005230 /* can't set/change the rt policy */
5231 if (policy != p->policy && !rlim_rtprio)
5232 return -EPERM;
5233
5234 /* can't increase priority */
5235 if (param->sched_priority > p->rt_priority &&
5236 param->sched_priority > rlim_rtprio)
5237 return -EPERM;
5238 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005239 /*
5240 * Like positive nice levels, dont allow tasks to
5241 * move out of SCHED_IDLE either:
5242 */
5243 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5244 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005245
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005246 /* can't change other user's priorities */
5247 if ((current->euid != p->euid) &&
5248 (current->euid != p->uid))
5249 return -EPERM;
5250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005252#ifdef CONFIG_RT_GROUP_SCHED
5253 /*
5254 * Do not allow realtime tasks into groups that have no runtime
5255 * assigned.
5256 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005257 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005258 return -EPERM;
5259#endif
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 retval = security_task_setscheduler(p, policy, param);
5262 if (retval)
5263 return retval;
5264 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005265 * make sure no PI-waiters arrive (or leave) while we are
5266 * changing the priority of the task:
5267 */
5268 spin_lock_irqsave(&p->pi_lock, flags);
5269 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 * To be able to change p->policy safely, the apropriate
5271 * runqueue lock must be held.
5272 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005273 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 /* recheck policy now with rq lock held */
5275 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5276 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005277 __task_rq_unlock(rq);
5278 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 goto recheck;
5280 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005281 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005282 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005283 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005284 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005285 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005286 if (running)
5287 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005290 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005291
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005292 if (running)
5293 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005294 if (on_rq) {
5295 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005296
5297 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005299 __task_rq_unlock(rq);
5300 spin_unlock_irqrestore(&p->pi_lock, flags);
5301
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005302 rt_mutex_adjust_pi(p);
5303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return 0;
5305}
5306EXPORT_SYMBOL_GPL(sched_setscheduler);
5307
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005308static int
5309do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 struct sched_param lparam;
5312 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005313 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
5315 if (!param || pid < 0)
5316 return -EINVAL;
5317 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5318 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005319
5320 rcu_read_lock();
5321 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005323 if (p != NULL)
5324 retval = sched_setscheduler(p, policy, &lparam);
5325 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return retval;
5328}
5329
5330/**
5331 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5332 * @pid: the pid in question.
5333 * @policy: new policy.
5334 * @param: structure containing the new RT priority.
5335 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005336asmlinkage long
5337sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
Jason Baronc21761f2006-01-18 17:43:03 -08005339 /* negative values for policy are not valid */
5340 if (policy < 0)
5341 return -EINVAL;
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 return do_sched_setscheduler(pid, policy, param);
5344}
5345
5346/**
5347 * sys_sched_setparam - set/change the RT priority of a thread
5348 * @pid: the pid in question.
5349 * @param: structure containing the new RT priority.
5350 */
5351asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5352{
5353 return do_sched_setscheduler(pid, -1, param);
5354}
5355
5356/**
5357 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5358 * @pid: the pid in question.
5359 */
5360asmlinkage long sys_sched_getscheduler(pid_t pid)
5361{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005362 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005363 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
5365 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368 retval = -ESRCH;
5369 read_lock(&tasklist_lock);
5370 p = find_process_by_pid(pid);
5371 if (p) {
5372 retval = security_task_getscheduler(p);
5373 if (!retval)
5374 retval = p->policy;
5375 }
5376 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return retval;
5378}
5379
5380/**
5381 * sys_sched_getscheduler - get the RT priority of a thread
5382 * @pid: the pid in question.
5383 * @param: structure containing the RT priority.
5384 */
5385asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5386{
5387 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005388 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005389 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
5391 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
5394 read_lock(&tasklist_lock);
5395 p = find_process_by_pid(pid);
5396 retval = -ESRCH;
5397 if (!p)
5398 goto out_unlock;
5399
5400 retval = security_task_getscheduler(p);
5401 if (retval)
5402 goto out_unlock;
5403
5404 lp.sched_priority = p->rt_priority;
5405 read_unlock(&tasklist_lock);
5406
5407 /*
5408 * This one might sleep, we cannot do it with a spinlock held ...
5409 */
5410 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return retval;
5413
5414out_unlock:
5415 read_unlock(&tasklist_lock);
5416 return retval;
5417}
5418
Mike Travisb53e9212008-04-04 18:11:08 -07005419long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005422 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005423 struct task_struct *p;
5424 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005426 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 read_lock(&tasklist_lock);
5428
5429 p = find_process_by_pid(pid);
5430 if (!p) {
5431 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005432 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 return -ESRCH;
5434 }
5435
5436 /*
5437 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005438 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 * usage count and then drop tasklist_lock.
5440 */
5441 get_task_struct(p);
5442 read_unlock(&tasklist_lock);
5443
5444 retval = -EPERM;
5445 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5446 !capable(CAP_SYS_NICE))
5447 goto out_unlock;
5448
David Quigleye7834f82006-06-23 02:03:59 -07005449 retval = security_task_setscheduler(p, 0, NULL);
5450 if (retval)
5451 goto out_unlock;
5452
Mike Travisf9a86fc2008-04-04 18:11:07 -07005453 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005455 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005456 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Paul Menage8707d8b2007-10-18 23:40:22 -07005458 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005459 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005460 if (!cpus_subset(new_mask, cpus_allowed)) {
5461 /*
5462 * We must have raced with a concurrent cpuset
5463 * update. Just reset the cpus_allowed to the
5464 * cpuset's cpus_allowed
5465 */
5466 new_mask = cpus_allowed;
5467 goto again;
5468 }
5469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470out_unlock:
5471 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005472 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 return retval;
5474}
5475
5476static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5477 cpumask_t *new_mask)
5478{
5479 if (len < sizeof(cpumask_t)) {
5480 memset(new_mask, 0, sizeof(cpumask_t));
5481 } else if (len > sizeof(cpumask_t)) {
5482 len = sizeof(cpumask_t);
5483 }
5484 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5485}
5486
5487/**
5488 * sys_sched_setaffinity - set the cpu affinity of a process
5489 * @pid: pid of the process
5490 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5491 * @user_mask_ptr: user-space pointer to the new cpu mask
5492 */
5493asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5494 unsigned long __user *user_mask_ptr)
5495{
5496 cpumask_t new_mask;
5497 int retval;
5498
5499 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5500 if (retval)
5501 return retval;
5502
Mike Travisb53e9212008-04-04 18:11:08 -07005503 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504}
5505
5506/*
5507 * Represents all cpu's present in the system
5508 * In systems capable of hotplug, this map could dynamically grow
5509 * as new cpu's are detected in the system via any platform specific
5510 * method, such as ACPI for e.g.
5511 */
5512
Andi Kleen4cef0c62006-01-11 22:44:57 +01005513cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514EXPORT_SYMBOL(cpu_present_map);
5515
5516#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005517cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005518EXPORT_SYMBOL(cpu_online_map);
5519
Andi Kleen4cef0c62006-01-11 22:44:57 +01005520cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005521EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522#endif
5523
5524long sched_getaffinity(pid_t pid, cpumask_t *mask)
5525{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005526 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005529 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 read_lock(&tasklist_lock);
5531
5532 retval = -ESRCH;
5533 p = find_process_by_pid(pid);
5534 if (!p)
5535 goto out_unlock;
5536
David Quigleye7834f82006-06-23 02:03:59 -07005537 retval = security_task_getscheduler(p);
5538 if (retval)
5539 goto out_unlock;
5540
Jack Steiner2f7016d2006-02-01 03:05:18 -08005541 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
5543out_unlock:
5544 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005545 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Ulrich Drepper9531b622007-08-09 11:16:46 +02005547 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
5549
5550/**
5551 * sys_sched_getaffinity - get the cpu affinity of a process
5552 * @pid: pid of the process
5553 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5554 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5555 */
5556asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5557 unsigned long __user *user_mask_ptr)
5558{
5559 int ret;
5560 cpumask_t mask;
5561
5562 if (len < sizeof(cpumask_t))
5563 return -EINVAL;
5564
5565 ret = sched_getaffinity(pid, &mask);
5566 if (ret < 0)
5567 return ret;
5568
5569 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5570 return -EFAULT;
5571
5572 return sizeof(cpumask_t);
5573}
5574
5575/**
5576 * sys_sched_yield - yield the current processor to other threads.
5577 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005578 * This function yields the current CPU to other tasks. If there are no
5579 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 */
5581asmlinkage long sys_sched_yield(void)
5582{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005583 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Ingo Molnar2d723762007-10-15 17:00:12 +02005585 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005586 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
5588 /*
5589 * Since we are going to call schedule() anyway, there's
5590 * no need to preempt or enable interrupts:
5591 */
5592 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005593 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 _raw_spin_unlock(&rq->lock);
5595 preempt_enable_no_resched();
5596
5597 schedule();
5598
5599 return 0;
5600}
5601
Andrew Mortone7b38402006-06-30 01:56:00 -07005602static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005604#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5605 __might_sleep(__FILE__, __LINE__);
5606#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005607 /*
5608 * The BKS might be reacquired before we have dropped
5609 * PREEMPT_ACTIVE, which could trigger a second
5610 * cond_resched() call.
5611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 do {
5613 add_preempt_count(PREEMPT_ACTIVE);
5614 schedule();
5615 sub_preempt_count(PREEMPT_ACTIVE);
5616 } while (need_resched());
5617}
5618
Herbert Xu02b67cc2008-01-25 21:08:28 +01005619#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5620int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
Ingo Molnar94142322006-12-29 16:48:13 -08005622 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5623 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 __cond_resched();
5625 return 1;
5626 }
5627 return 0;
5628}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005629EXPORT_SYMBOL(_cond_resched);
5630#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
5632/*
5633 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5634 * call schedule, and on return reacquire the lock.
5635 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005636 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 * operations here to prevent schedule() from being called twice (once via
5638 * spin_unlock(), once by hand).
5639 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005640int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Nick Piggin95c354f2008-01-30 13:31:20 +01005642 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005643 int ret = 0;
5644
Nick Piggin95c354f2008-01-30 13:31:20 +01005645 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005647 if (resched && need_resched())
5648 __cond_resched();
5649 else
5650 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005651 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005654 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656EXPORT_SYMBOL(cond_resched_lock);
5657
5658int __sched cond_resched_softirq(void)
5659{
5660 BUG_ON(!in_softirq());
5661
Ingo Molnar94142322006-12-29 16:48:13 -08005662 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005663 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 __cond_resched();
5665 local_bh_disable();
5666 return 1;
5667 }
5668 return 0;
5669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670EXPORT_SYMBOL(cond_resched_softirq);
5671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672/**
5673 * yield - yield the current processor to other threads.
5674 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005675 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 * thread runnable and calls sys_sched_yield().
5677 */
5678void __sched yield(void)
5679{
5680 set_current_state(TASK_RUNNING);
5681 sys_sched_yield();
5682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683EXPORT_SYMBOL(yield);
5684
5685/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005686 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 * that process accounting knows that this is a task in IO wait state.
5688 *
5689 * But don't do that if it is a deliberate, throttling IO wait (this task
5690 * has set its backing_dev_info: the queue against which it should throttle)
5691 */
5692void __sched io_schedule(void)
5693{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005694 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005696 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 atomic_inc(&rq->nr_iowait);
5698 schedule();
5699 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005700 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702EXPORT_SYMBOL(io_schedule);
5703
5704long __sched io_schedule_timeout(long timeout)
5705{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005706 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 long ret;
5708
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005709 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 atomic_inc(&rq->nr_iowait);
5711 ret = schedule_timeout(timeout);
5712 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005713 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 return ret;
5715}
5716
5717/**
5718 * sys_sched_get_priority_max - return maximum RT priority.
5719 * @policy: scheduling class.
5720 *
5721 * this syscall returns the maximum rt_priority that can be used
5722 * by a given scheduling class.
5723 */
5724asmlinkage long sys_sched_get_priority_max(int policy)
5725{
5726 int ret = -EINVAL;
5727
5728 switch (policy) {
5729 case SCHED_FIFO:
5730 case SCHED_RR:
5731 ret = MAX_USER_RT_PRIO-1;
5732 break;
5733 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005734 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005735 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 ret = 0;
5737 break;
5738 }
5739 return ret;
5740}
5741
5742/**
5743 * sys_sched_get_priority_min - return minimum RT priority.
5744 * @policy: scheduling class.
5745 *
5746 * this syscall returns the minimum rt_priority that can be used
5747 * by a given scheduling class.
5748 */
5749asmlinkage long sys_sched_get_priority_min(int policy)
5750{
5751 int ret = -EINVAL;
5752
5753 switch (policy) {
5754 case SCHED_FIFO:
5755 case SCHED_RR:
5756 ret = 1;
5757 break;
5758 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005759 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005760 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 ret = 0;
5762 }
5763 return ret;
5764}
5765
5766/**
5767 * sys_sched_rr_get_interval - return the default timeslice of a process.
5768 * @pid: pid of the process.
5769 * @interval: userspace pointer to the timeslice value.
5770 *
5771 * this syscall writes the default timeslice value of a given process
5772 * into the user-space timespec buffer. A value of '0' means infinity.
5773 */
5774asmlinkage
5775long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5776{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005777 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005778 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005779 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 retval = -ESRCH;
5786 read_lock(&tasklist_lock);
5787 p = find_process_by_pid(pid);
5788 if (!p)
5789 goto out_unlock;
5790
5791 retval = security_task_getscheduler(p);
5792 if (retval)
5793 goto out_unlock;
5794
Ingo Molnar77034932007-12-04 17:04:39 +01005795 /*
5796 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5797 * tasks that are on an otherwise idle runqueue:
5798 */
5799 time_slice = 0;
5800 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005801 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005802 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005803 struct sched_entity *se = &p->se;
5804 unsigned long flags;
5805 struct rq *rq;
5806
5807 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005808 if (rq->cfs.load.weight)
5809 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005810 task_rq_unlock(rq, &flags);
5811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005813 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817out_unlock:
5818 read_unlock(&tasklist_lock);
5819 return retval;
5820}
5821
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005822static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005823
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005824void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005827 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005830 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005831 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005832#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005834 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005836 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837#else
5838 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005839 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005841 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842#endif
5843#ifdef CONFIG_DEBUG_STACK_USAGE
5844 {
Al Viro10ebffd2005-11-13 16:06:56 -08005845 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 while (!*n)
5847 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005848 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 }
5850#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005851 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005852 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005854 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855}
5856
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005857void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005859 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Ingo Molnar4bd77322007-07-11 21:21:47 +02005861#if BITS_PER_LONG == 32
5862 printk(KERN_INFO
5863 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005865 printk(KERN_INFO
5866 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867#endif
5868 read_lock(&tasklist_lock);
5869 do_each_thread(g, p) {
5870 /*
5871 * reset the NMI-timeout, listing all files on a slow
5872 * console might take alot of time:
5873 */
5874 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005875 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005876 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 } while_each_thread(g, p);
5878
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005879 touch_all_softlockup_watchdogs();
5880
Ingo Molnardd41f592007-07-09 18:51:59 +02005881#ifdef CONFIG_SCHED_DEBUG
5882 sysrq_sched_debug_show();
5883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005885 /*
5886 * Only show locks if all tasks are dumped:
5887 */
5888 if (state_filter == -1)
5889 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890}
5891
Ingo Molnar1df21052007-07-09 18:51:58 +02005892void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5893{
Ingo Molnardd41f592007-07-09 18:51:59 +02005894 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005895}
5896
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005897/**
5898 * init_idle - set up an idle thread for a given CPU
5899 * @idle: task in question
5900 * @cpu: cpu the idle task belongs to
5901 *
5902 * NOTE: this function does not set the idle thread's NEED_RESCHED
5903 * flag, to make booting more robust.
5904 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005905void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005907 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 unsigned long flags;
5909
Ingo Molnardd41f592007-07-09 18:51:59 +02005910 __sched_fork(idle);
5911 idle->se.exec_start = sched_clock();
5912
Ingo Molnarb29739f2006-06-27 02:54:51 -07005913 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005915 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 spin_lock_irqsave(&rq->lock, flags);
5918 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005919#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5920 idle->oncpu = 1;
5921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 spin_unlock_irqrestore(&rq->lock, flags);
5923
5924 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005925 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005926
Ingo Molnardd41f592007-07-09 18:51:59 +02005927 /*
5928 * The idle tasks have their own, simple scheduling class:
5929 */
5930 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931}
5932
5933/*
5934 * In a system that switches off the HZ timer nohz_cpu_mask
5935 * indicates which cpus entered this state. This is used
5936 * in the rcu update to wait only for active cpus. For system
5937 * which do not switch off the HZ timer nohz_cpu_mask should
5938 * always be CPU_MASK_NONE.
5939 */
5940cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5941
Ingo Molnar19978ca2007-11-09 22:39:38 +01005942/*
5943 * Increase the granularity value when there are more CPUs,
5944 * because with more CPUs the 'effective latency' as visible
5945 * to users decreases. But the relationship is not linear,
5946 * so pick a second-best guess by going with the log2 of the
5947 * number of CPUs.
5948 *
5949 * This idea comes from the SD scheduler of Con Kolivas:
5950 */
5951static inline void sched_init_granularity(void)
5952{
5953 unsigned int factor = 1 + ilog2(num_online_cpus());
5954 const unsigned long limit = 200000000;
5955
5956 sysctl_sched_min_granularity *= factor;
5957 if (sysctl_sched_min_granularity > limit)
5958 sysctl_sched_min_granularity = limit;
5959
5960 sysctl_sched_latency *= factor;
5961 if (sysctl_sched_latency > limit)
5962 sysctl_sched_latency = limit;
5963
5964 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005965}
5966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967#ifdef CONFIG_SMP
5968/*
5969 * This is how migration works:
5970 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005971 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 * runqueue and wake up that CPU's migration thread.
5973 * 2) we down() the locked semaphore => thread blocks.
5974 * 3) migration thread wakes up (implicitly it forces the migrated
5975 * thread off the CPU)
5976 * 4) it gets the migration request and checks whether the migrated
5977 * task is still in the wrong runqueue.
5978 * 5) if it's in the wrong runqueue then the migration thread removes
5979 * it and puts it into the right queue.
5980 * 6) migration thread up()s the semaphore.
5981 * 7) we wake up and the migration is done.
5982 */
5983
5984/*
5985 * Change a given task's CPU affinity. Migrate the thread to a
5986 * proper CPU and schedule it away if the CPU it's executing on
5987 * is removed from the allowed bitmask.
5988 *
5989 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005990 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 * call is not atomic; no spinlocks may be held.
5992 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005993int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005995 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005997 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005998 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006001 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 ret = -EINVAL;
6003 goto out;
6004 }
6005
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006006 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006007 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006008 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006009 p->cpus_allowed = *new_mask;
6010 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006011 }
6012
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006014 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 goto out;
6016
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006017 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 /* Need help from migration thread: drop lock and wait. */
6019 task_rq_unlock(rq, &flags);
6020 wake_up_process(rq->migration_thread);
6021 wait_for_completion(&req.done);
6022 tlb_migrate_finish(p->mm);
6023 return 0;
6024 }
6025out:
6026 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 return ret;
6029}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006030EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
6032/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006033 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 * this because either it can't run here any more (set_cpus_allowed()
6035 * away from this CPU, or CPU going down), or because we're
6036 * attempting to rebalance this task on exec (sched_exec).
6037 *
6038 * So we race with normal scheduler movements, but that's OK, as long
6039 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006040 *
6041 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006043static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006045 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006046 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
6051 rq_src = cpu_rq(src_cpu);
6052 rq_dest = cpu_rq(dest_cpu);
6053
6054 double_rq_lock(rq_src, rq_dest);
6055 /* Already moved. */
6056 if (task_cpu(p) != src_cpu)
6057 goto out;
6058 /* Affinity changed (again). */
6059 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6060 goto out;
6061
Ingo Molnardd41f592007-07-09 18:51:59 +02006062 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006063 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006064 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006067 if (on_rq) {
6068 activate_task(rq_dest, p, 0);
6069 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006071 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072out:
6073 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075}
6076
6077/*
6078 * migration_thread - this is a highprio system thread that performs
6079 * thread migration by bumping thread off CPU then 'pushing' onto
6080 * another runqueue.
6081 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006082static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006085 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 rq = cpu_rq(cpu);
6088 BUG_ON(rq->migration_thread != current);
6089
6090 set_current_state(TASK_INTERRUPTIBLE);
6091 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006092 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 spin_lock_irq(&rq->lock);
6096
6097 if (cpu_is_offline(cpu)) {
6098 spin_unlock_irq(&rq->lock);
6099 goto wait_to_die;
6100 }
6101
6102 if (rq->active_balance) {
6103 active_load_balance(rq, cpu);
6104 rq->active_balance = 0;
6105 }
6106
6107 head = &rq->migration_queue;
6108
6109 if (list_empty(head)) {
6110 spin_unlock_irq(&rq->lock);
6111 schedule();
6112 set_current_state(TASK_INTERRUPTIBLE);
6113 continue;
6114 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006115 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 list_del_init(head->next);
6117
Nick Piggin674311d2005-06-25 14:57:27 -07006118 spin_unlock(&rq->lock);
6119 __migrate_task(req->task, cpu, req->dest_cpu);
6120 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 complete(&req->done);
6123 }
6124 __set_current_state(TASK_RUNNING);
6125 return 0;
6126
6127wait_to_die:
6128 /* Wait for kthread_stop */
6129 set_current_state(TASK_INTERRUPTIBLE);
6130 while (!kthread_should_stop()) {
6131 schedule();
6132 set_current_state(TASK_INTERRUPTIBLE);
6133 }
6134 __set_current_state(TASK_RUNNING);
6135 return 0;
6136}
6137
6138#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006139
6140static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6141{
6142 int ret;
6143
6144 local_irq_disable();
6145 ret = __migrate_task(p, src_cpu, dest_cpu);
6146 local_irq_enable();
6147 return ret;
6148}
6149
Kirill Korotaev054b9102006-12-10 02:20:11 -08006150/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006151 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006152 * NOTE: interrupts should be disabled by the caller
6153 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006154static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006156 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006158 struct rq *rq;
6159 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Andi Kleen3a5c3592007-10-15 17:00:14 +02006161 do {
6162 /* On same node? */
6163 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6164 cpus_and(mask, mask, p->cpus_allowed);
6165 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Andi Kleen3a5c3592007-10-15 17:00:14 +02006167 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006168 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006169 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Andi Kleen3a5c3592007-10-15 17:00:14 +02006171 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006172 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006173 cpumask_t cpus_allowed;
6174
6175 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006176 /*
6177 * Try to stay on the same cpuset, where the
6178 * current cpuset may be a subset of all cpus.
6179 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006180 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006181 * called within calls to cpuset_lock/cpuset_unlock.
6182 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006183 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006184 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006185 dest_cpu = any_online_cpu(p->cpus_allowed);
6186 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Andi Kleen3a5c3592007-10-15 17:00:14 +02006188 /*
6189 * Don't tell them about moving exiting tasks or
6190 * kernel threads (both mm NULL), since they never
6191 * leave kernel.
6192 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006193 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006194 printk(KERN_INFO "process %d (%s) no "
6195 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006196 task_pid_nr(p), p->comm, dead_cpu);
6197 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006198 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006199 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200}
6201
6202/*
6203 * While a dead CPU has no uninterruptible tasks queued at this point,
6204 * it might still have a nonzero ->nr_uninterruptible counter, because
6205 * for performance reasons the counter is not stricly tracking tasks to
6206 * their home CPUs. So we just add the counter to another CPU's counter,
6207 * to keep the global sum constant after CPU-down:
6208 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006209static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
Mike Travis7c16ec52008-04-04 18:11:11 -07006211 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 unsigned long flags;
6213
6214 local_irq_save(flags);
6215 double_rq_lock(rq_src, rq_dest);
6216 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6217 rq_src->nr_uninterruptible = 0;
6218 double_rq_unlock(rq_src, rq_dest);
6219 local_irq_restore(flags);
6220}
6221
6222/* Run through task list and migrate tasks from the dead cpu. */
6223static void migrate_live_tasks(int src_cpu)
6224{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006225 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006227 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Ingo Molnar48f24c42006-07-03 00:25:40 -07006229 do_each_thread(t, p) {
6230 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 continue;
6232
Ingo Molnar48f24c42006-07-03 00:25:40 -07006233 if (task_cpu(p) == src_cpu)
6234 move_task_off_dead_cpu(src_cpu, p);
6235 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006237 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238}
6239
Ingo Molnardd41f592007-07-09 18:51:59 +02006240/*
6241 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006242 * It does so by boosting its priority to highest possible.
6243 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 */
6245void sched_idle_next(void)
6246{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006247 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006248 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 struct task_struct *p = rq->idle;
6250 unsigned long flags;
6251
6252 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006253 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Ingo Molnar48f24c42006-07-03 00:25:40 -07006255 /*
6256 * Strictly not necessary since rest of the CPUs are stopped by now
6257 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 */
6259 spin_lock_irqsave(&rq->lock, flags);
6260
Ingo Molnardd41f592007-07-09 18:51:59 +02006261 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006262
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006263 update_rq_clock(rq);
6264 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
6266 spin_unlock_irqrestore(&rq->lock, flags);
6267}
6268
Ingo Molnar48f24c42006-07-03 00:25:40 -07006269/*
6270 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 * offline.
6272 */
6273void idle_task_exit(void)
6274{
6275 struct mm_struct *mm = current->active_mm;
6276
6277 BUG_ON(cpu_online(smp_processor_id()));
6278
6279 if (mm != &init_mm)
6280 switch_mm(mm, &init_mm, current);
6281 mmdrop(mm);
6282}
6283
Kirill Korotaev054b9102006-12-10 02:20:11 -08006284/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006285static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006287 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006290 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
6292 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006293 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Ingo Molnar48f24c42006-07-03 00:25:40 -07006295 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297 /*
6298 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006299 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 * fine.
6301 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006302 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006303 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006304 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
Ingo Molnar48f24c42006-07-03 00:25:40 -07006306 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
6309/* release_task() removes task from tasklist, so we won't find dead tasks. */
6310static void migrate_dead_tasks(unsigned int dead_cpu)
6311{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006312 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006313 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Ingo Molnardd41f592007-07-09 18:51:59 +02006315 for ( ; ; ) {
6316 if (!rq->nr_running)
6317 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006318 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006319 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006320 if (!next)
6321 break;
6322 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006323
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 }
6325}
6326#endif /* CONFIG_HOTPLUG_CPU */
6327
Nick Piggine692ab52007-07-26 13:40:43 +02006328#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6329
6330static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006331 {
6332 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006333 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006334 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006335 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006336};
6337
6338static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006339 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006340 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006341 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006342 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006343 .child = sd_ctl_dir,
6344 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006345 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006346};
6347
6348static struct ctl_table *sd_alloc_ctl_entry(int n)
6349{
6350 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006351 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006352
Nick Piggine692ab52007-07-26 13:40:43 +02006353 return entry;
6354}
6355
Milton Miller6382bc92007-10-15 17:00:19 +02006356static void sd_free_ctl_entry(struct ctl_table **tablep)
6357{
Milton Millercd790072007-10-17 16:55:11 +02006358 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006359
Milton Millercd790072007-10-17 16:55:11 +02006360 /*
6361 * In the intermediate directories, both the child directory and
6362 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006363 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02006364 * static strings and all have proc handlers.
6365 */
6366 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006367 if (entry->child)
6368 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02006369 if (entry->proc_handler == NULL)
6370 kfree(entry->procname);
6371 }
Milton Miller6382bc92007-10-15 17:00:19 +02006372
6373 kfree(*tablep);
6374 *tablep = NULL;
6375}
6376
Nick Piggine692ab52007-07-26 13:40:43 +02006377static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006378set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006379 const char *procname, void *data, int maxlen,
6380 mode_t mode, proc_handler *proc_handler)
6381{
Nick Piggine692ab52007-07-26 13:40:43 +02006382 entry->procname = procname;
6383 entry->data = data;
6384 entry->maxlen = maxlen;
6385 entry->mode = mode;
6386 entry->proc_handler = proc_handler;
6387}
6388
6389static struct ctl_table *
6390sd_alloc_ctl_domain_table(struct sched_domain *sd)
6391{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006392 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006393
Milton Millerad1cdc12007-10-15 17:00:19 +02006394 if (table == NULL)
6395 return NULL;
6396
Alexey Dobriyane0361852007-08-09 11:16:46 +02006397 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006398 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006399 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006400 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006401 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006402 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006403 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006404 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006405 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006406 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006407 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006408 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006409 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006410 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006411 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006412 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006413 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006414 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006415 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006416 &sd->cache_nice_tries,
6417 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006418 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006419 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006420 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006421
6422 return table;
6423}
6424
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006425static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006426{
6427 struct ctl_table *entry, *table;
6428 struct sched_domain *sd;
6429 int domain_num = 0, i;
6430 char buf[32];
6431
6432 for_each_domain(cpu, sd)
6433 domain_num++;
6434 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006435 if (table == NULL)
6436 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006437
6438 i = 0;
6439 for_each_domain(cpu, sd) {
6440 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006441 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006442 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006443 entry->child = sd_alloc_ctl_domain_table(sd);
6444 entry++;
6445 i++;
6446 }
6447 return table;
6448}
6449
6450static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006451static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006452{
6453 int i, cpu_num = num_online_cpus();
6454 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6455 char buf[32];
6456
Milton Miller73785472007-10-24 18:23:48 +02006457 WARN_ON(sd_ctl_dir[0].child);
6458 sd_ctl_dir[0].child = entry;
6459
Milton Millerad1cdc12007-10-15 17:00:19 +02006460 if (entry == NULL)
6461 return;
6462
Milton Miller97b6ea72007-10-15 17:00:19 +02006463 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006464 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006465 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006466 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006467 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006468 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006469 }
Milton Miller73785472007-10-24 18:23:48 +02006470
6471 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006472 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6473}
Milton Miller6382bc92007-10-15 17:00:19 +02006474
Milton Miller73785472007-10-24 18:23:48 +02006475/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006476static void unregister_sched_domain_sysctl(void)
6477{
Milton Miller73785472007-10-24 18:23:48 +02006478 if (sd_sysctl_header)
6479 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006480 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006481 if (sd_ctl_dir[0].child)
6482 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006483}
Nick Piggine692ab52007-07-26 13:40:43 +02006484#else
Milton Miller6382bc92007-10-15 17:00:19 +02006485static void register_sched_domain_sysctl(void)
6486{
6487}
6488static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006489{
6490}
6491#endif
6492
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493/*
6494 * migration_call - callback that gets triggered when a CPU is added.
6495 * Here we can start up the necessary migration thread for the new CPU.
6496 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006497static int __cpuinit
6498migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006501 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006503 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006506
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006508 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006509 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 if (IS_ERR(p))
6511 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 kthread_bind(p, cpu);
6513 /* Must be high prio: stop_machine expects to yield to it. */
6514 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006515 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 task_rq_unlock(rq, &flags);
6517 cpu_rq(cpu)->migration_thread = p;
6518 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006519
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006521 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006522 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006524
6525 /* Update our root-domain */
6526 rq = cpu_rq(cpu);
6527 spin_lock_irqsave(&rq->lock, flags);
6528 if (rq->rd) {
6529 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6530 cpu_set(cpu, rq->rd->online);
6531 }
6532 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535#ifdef CONFIG_HOTPLUG_CPU
6536 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006537 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006538 if (!cpu_rq(cpu)->migration_thread)
6539 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006540 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006541 kthread_bind(cpu_rq(cpu)->migration_thread,
6542 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 kthread_stop(cpu_rq(cpu)->migration_thread);
6544 cpu_rq(cpu)->migration_thread = NULL;
6545 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006546
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006548 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006549 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 migrate_live_tasks(cpu);
6551 rq = cpu_rq(cpu);
6552 kthread_stop(rq->migration_thread);
6553 rq->migration_thread = NULL;
6554 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006555 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006556 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006557 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006559 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6560 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006562 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006563 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 migrate_nr_uninterruptible(rq);
6565 BUG_ON(rq->nr_running != 0);
6566
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006567 /*
6568 * No need to migrate the tasks: it was best-effort if
6569 * they didn't take sched_hotcpu_mutex. Just wake up
6570 * the requestors.
6571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 spin_lock_irq(&rq->lock);
6573 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006574 struct migration_req *req;
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006577 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 list_del_init(&req->list);
6579 complete(&req->done);
6580 }
6581 spin_unlock_irq(&rq->lock);
6582 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006583
Gregory Haskins08f503b2008-03-10 17:59:11 -04006584 case CPU_DYING:
6585 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006586 /* Update our root-domain */
6587 rq = cpu_rq(cpu);
6588 spin_lock_irqsave(&rq->lock, flags);
6589 if (rq->rd) {
6590 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6591 cpu_clear(cpu, rq->rd->online);
6592 }
6593 spin_unlock_irqrestore(&rq->lock, flags);
6594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595#endif
6596 }
6597 return NOTIFY_OK;
6598}
6599
6600/* Register at highest priority so that task migration (migrate_all_tasks)
6601 * happens before everything else.
6602 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006603static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 .notifier_call = migration_call,
6605 .priority = 10
6606};
6607
Adrian Bunke6fe6642007-11-09 22:39:39 +01006608void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609{
6610 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006611 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006612
6613 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006614 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6615 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6617 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618}
6619#endif
6620
6621#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006622
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006623#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006624
Mike Travis7c16ec52008-04-04 18:11:11 -07006625static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6626 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006627{
6628 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006629 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006630
Mike Travis434d53b2008-04-04 18:11:04 -07006631 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006632 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006633
6634 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6635
6636 if (!(sd->flags & SD_LOAD_BALANCE)) {
6637 printk("does not load-balance\n");
6638 if (sd->parent)
6639 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6640 " has parent");
6641 return -1;
6642 }
6643
6644 printk(KERN_CONT "span %s\n", str);
6645
6646 if (!cpu_isset(cpu, sd->span)) {
6647 printk(KERN_ERR "ERROR: domain->span does not contain "
6648 "CPU%d\n", cpu);
6649 }
6650 if (!cpu_isset(cpu, group->cpumask)) {
6651 printk(KERN_ERR "ERROR: domain->groups does not contain"
6652 " CPU%d\n", cpu);
6653 }
6654
6655 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6656 do {
6657 if (!group) {
6658 printk("\n");
6659 printk(KERN_ERR "ERROR: group is NULL\n");
6660 break;
6661 }
6662
6663 if (!group->__cpu_power) {
6664 printk(KERN_CONT "\n");
6665 printk(KERN_ERR "ERROR: domain->cpu_power not "
6666 "set\n");
6667 break;
6668 }
6669
6670 if (!cpus_weight(group->cpumask)) {
6671 printk(KERN_CONT "\n");
6672 printk(KERN_ERR "ERROR: empty group\n");
6673 break;
6674 }
6675
Mike Travis7c16ec52008-04-04 18:11:11 -07006676 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006677 printk(KERN_CONT "\n");
6678 printk(KERN_ERR "ERROR: repeated CPUs\n");
6679 break;
6680 }
6681
Mike Travis7c16ec52008-04-04 18:11:11 -07006682 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006683
Mike Travis434d53b2008-04-04 18:11:04 -07006684 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006685 printk(KERN_CONT " %s", str);
6686
6687 group = group->next;
6688 } while (group != sd->groups);
6689 printk(KERN_CONT "\n");
6690
Mike Travis7c16ec52008-04-04 18:11:11 -07006691 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006692 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6693
Mike Travis7c16ec52008-04-04 18:11:11 -07006694 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006695 printk(KERN_ERR "ERROR: parent span is not a superset "
6696 "of domain->span\n");
6697 return 0;
6698}
6699
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700static void sched_domain_debug(struct sched_domain *sd, int cpu)
6701{
Mike Travis7c16ec52008-04-04 18:11:11 -07006702 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 int level = 0;
6704
Nick Piggin41c7ce92005-06-25 14:57:24 -07006705 if (!sd) {
6706 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6707 return;
6708 }
6709
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6711
Mike Travis7c16ec52008-04-04 18:11:11 -07006712 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6713 if (!groupmask) {
6714 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6715 return;
6716 }
6717
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006718 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006719 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 level++;
6722 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006723 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006724 break;
6725 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006726 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727}
6728#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006729# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730#endif
6731
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006732static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006733{
6734 if (cpus_weight(sd->span) == 1)
6735 return 1;
6736
6737 /* Following flags need at least 2 groups */
6738 if (sd->flags & (SD_LOAD_BALANCE |
6739 SD_BALANCE_NEWIDLE |
6740 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006741 SD_BALANCE_EXEC |
6742 SD_SHARE_CPUPOWER |
6743 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006744 if (sd->groups != sd->groups->next)
6745 return 0;
6746 }
6747
6748 /* Following flags don't use groups */
6749 if (sd->flags & (SD_WAKE_IDLE |
6750 SD_WAKE_AFFINE |
6751 SD_WAKE_BALANCE))
6752 return 0;
6753
6754 return 1;
6755}
6756
Ingo Molnar48f24c42006-07-03 00:25:40 -07006757static int
6758sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006759{
6760 unsigned long cflags = sd->flags, pflags = parent->flags;
6761
6762 if (sd_degenerate(parent))
6763 return 1;
6764
6765 if (!cpus_equal(sd->span, parent->span))
6766 return 0;
6767
6768 /* Does parent contain flags not in child? */
6769 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6770 if (cflags & SD_WAKE_AFFINE)
6771 pflags &= ~SD_WAKE_BALANCE;
6772 /* Flags needing groups don't count if only 1 group in parent */
6773 if (parent->groups == parent->groups->next) {
6774 pflags &= ~(SD_LOAD_BALANCE |
6775 SD_BALANCE_NEWIDLE |
6776 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006777 SD_BALANCE_EXEC |
6778 SD_SHARE_CPUPOWER |
6779 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006780 }
6781 if (~cflags & pflags)
6782 return 0;
6783
6784 return 1;
6785}
6786
Gregory Haskins57d885f2008-01-25 21:08:18 +01006787static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6788{
6789 unsigned long flags;
6790 const struct sched_class *class;
6791
6792 spin_lock_irqsave(&rq->lock, flags);
6793
6794 if (rq->rd) {
6795 struct root_domain *old_rd = rq->rd;
6796
Ingo Molnar0eab9142008-01-25 21:08:19 +01006797 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006798 if (class->leave_domain)
6799 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006800 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006801
Gregory Haskinsdc938522008-01-25 21:08:26 +01006802 cpu_clear(rq->cpu, old_rd->span);
6803 cpu_clear(rq->cpu, old_rd->online);
6804
Gregory Haskins57d885f2008-01-25 21:08:18 +01006805 if (atomic_dec_and_test(&old_rd->refcount))
6806 kfree(old_rd);
6807 }
6808
6809 atomic_inc(&rd->refcount);
6810 rq->rd = rd;
6811
Gregory Haskinsdc938522008-01-25 21:08:26 +01006812 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006813 if (cpu_isset(rq->cpu, cpu_online_map))
6814 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006815
Ingo Molnar0eab9142008-01-25 21:08:19 +01006816 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006817 if (class->join_domain)
6818 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006819 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006820
6821 spin_unlock_irqrestore(&rq->lock, flags);
6822}
6823
Gregory Haskinsdc938522008-01-25 21:08:26 +01006824static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006825{
6826 memset(rd, 0, sizeof(*rd));
6827
Gregory Haskinsdc938522008-01-25 21:08:26 +01006828 cpus_clear(rd->span);
6829 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006830}
6831
6832static void init_defrootdomain(void)
6833{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006834 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006835 atomic_set(&def_root_domain.refcount, 1);
6836}
6837
Gregory Haskinsdc938522008-01-25 21:08:26 +01006838static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006839{
6840 struct root_domain *rd;
6841
6842 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6843 if (!rd)
6844 return NULL;
6845
Gregory Haskinsdc938522008-01-25 21:08:26 +01006846 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006847
6848 return rd;
6849}
6850
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006852 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 * hold the hotplug lock.
6854 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006855static void
6856cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006858 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006859 struct sched_domain *tmp;
6860
6861 /* Remove the sched domains which do not contribute to scheduling. */
6862 for (tmp = sd; tmp; tmp = tmp->parent) {
6863 struct sched_domain *parent = tmp->parent;
6864 if (!parent)
6865 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006866 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006867 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006868 if (parent->parent)
6869 parent->parent->child = tmp;
6870 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006871 }
6872
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006873 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006874 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006875 if (sd)
6876 sd->child = NULL;
6877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
6879 sched_domain_debug(sd, cpu);
6880
Gregory Haskins57d885f2008-01-25 21:08:18 +01006881 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006882 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
6885/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006886static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
6888/* Setup the mask of cpus configured for isolated domains */
6889static int __init isolated_cpu_setup(char *str)
6890{
6891 int ints[NR_CPUS], i;
6892
6893 str = get_options(str, ARRAY_SIZE(ints), ints);
6894 cpus_clear(cpu_isolated_map);
6895 for (i = 1; i <= ints[0]; i++)
6896 if (ints[i] < NR_CPUS)
6897 cpu_set(ints[i], cpu_isolated_map);
6898 return 1;
6899}
6900
Ingo Molnar8927f492007-10-15 17:00:13 +02006901__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006904 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6905 * to a function which identifies what group(along with sched group) a CPU
6906 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6907 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 *
6909 * init_sched_build_groups will build a circular linked list of the groups
6910 * covered by the given span, and will set each group's ->cpumask correctly,
6911 * and ->cpu_power to 0.
6912 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006913static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006914init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006915 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006916 struct sched_group **sg,
6917 cpumask_t *tmpmask),
6918 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919{
6920 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 int i;
6922
Mike Travis7c16ec52008-04-04 18:11:11 -07006923 cpus_clear(*covered);
6924
6925 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006926 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006927 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 int j;
6929
Mike Travis7c16ec52008-04-04 18:11:11 -07006930 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 continue;
6932
Mike Travis7c16ec52008-04-04 18:11:11 -07006933 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006934 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
Mike Travis7c16ec52008-04-04 18:11:11 -07006936 for_each_cpu_mask(j, *span) {
6937 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 continue;
6939
Mike Travis7c16ec52008-04-04 18:11:11 -07006940 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 cpu_set(j, sg->cpumask);
6942 }
6943 if (!first)
6944 first = sg;
6945 if (last)
6946 last->next = sg;
6947 last = sg;
6948 }
6949 last->next = first;
6950}
6951
John Hawkes9c1cfda2005-09-06 15:18:14 -07006952#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
John Hawkes9c1cfda2005-09-06 15:18:14 -07006954#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006955
John Hawkes9c1cfda2005-09-06 15:18:14 -07006956/**
6957 * find_next_best_node - find the next node to include in a sched_domain
6958 * @node: node whose sched_domain we're building
6959 * @used_nodes: nodes already in the sched_domain
6960 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006961 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006962 * finds the closest node not already in the @used_nodes map.
6963 *
6964 * Should use nodemask_t.
6965 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006966static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006967{
6968 int i, n, val, min_val, best_node = 0;
6969
6970 min_val = INT_MAX;
6971
6972 for (i = 0; i < MAX_NUMNODES; i++) {
6973 /* Start at @node */
6974 n = (node + i) % MAX_NUMNODES;
6975
6976 if (!nr_cpus_node(n))
6977 continue;
6978
6979 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006980 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006981 continue;
6982
6983 /* Simple min distance search */
6984 val = node_distance(node, n);
6985
6986 if (val < min_val) {
6987 min_val = val;
6988 best_node = n;
6989 }
6990 }
6991
Mike Travisc5f59f02008-04-04 18:11:10 -07006992 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006993 return best_node;
6994}
6995
6996/**
6997 * sched_domain_node_span - get a cpumask for a node's sched_domain
6998 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006999 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007000 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007001 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007002 * should be one that prevents unnecessary balancing, but also spreads tasks
7003 * out optimally.
7004 */
Mike Travis4bdbaad2008-04-15 16:35:52 -07007005static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007006{
Mike Travisc5f59f02008-04-04 18:11:10 -07007007 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007008 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007009 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007010
Mike Travis4bdbaad2008-04-15 16:35:52 -07007011 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007012 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007013
Mike Travis4bdbaad2008-04-15 16:35:52 -07007014 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007015 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007016
7017 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007018 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007019
Mike Travisc5f59f02008-04-04 18:11:10 -07007020 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad2008-04-15 16:35:52 -07007021 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007022 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007023}
7024#endif
7025
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007026int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007027
John Hawkes9c1cfda2005-09-06 15:18:14 -07007028/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007029 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031#ifdef CONFIG_SCHED_SMT
7032static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007033static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007034
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007035static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007036cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7037 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007039 if (sg)
7040 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 return cpu;
7042}
7043#endif
7044
Ingo Molnar48f24c42006-07-03 00:25:40 -07007045/*
7046 * multi-core sched-domains:
7047 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007048#ifdef CONFIG_SCHED_MC
7049static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007050static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007051#endif
7052
7053#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007054static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007055cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7056 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007057{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007058 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007059
7060 *mask = per_cpu(cpu_sibling_map, cpu);
7061 cpus_and(*mask, *mask, *cpu_map);
7062 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007063 if (sg)
7064 *sg = &per_cpu(sched_group_core, group);
7065 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007066}
7067#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007068static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007069cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7070 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007071{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007072 if (sg)
7073 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007074 return cpu;
7075}
7076#endif
7077
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007079static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007080
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007081static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007082cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7083 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007085 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007086#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007087 *mask = cpu_coregroup_map(cpu);
7088 cpus_and(*mask, *mask, *cpu_map);
7089 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007090#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007091 *mask = per_cpu(cpu_sibling_map, cpu);
7092 cpus_and(*mask, *mask, *cpu_map);
7093 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007095 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007097 if (sg)
7098 *sg = &per_cpu(sched_group_phys, group);
7099 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100}
7101
7102#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007103/*
7104 * The init_sched_build_groups can't handle what we want to do with node
7105 * groups, so roll our own. Now each node has its own list of groups which
7106 * gets dynamically allocated.
7107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007109static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007110
7111static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007112static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007113
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007114static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007115 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007117 int group;
7118
Mike Travis7c16ec52008-04-04 18:11:11 -07007119 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7120 cpus_and(*nodemask, *nodemask, *cpu_map);
7121 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007122
7123 if (sg)
7124 *sg = &per_cpu(sched_group_allnodes, group);
7125 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007127
Siddha, Suresh B08069032006-03-27 01:15:23 -08007128static void init_numa_sched_groups_power(struct sched_group *group_head)
7129{
7130 struct sched_group *sg = group_head;
7131 int j;
7132
7133 if (!sg)
7134 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007135 do {
7136 for_each_cpu_mask(j, sg->cpumask) {
7137 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007138
Andi Kleen3a5c3592007-10-15 17:00:14 +02007139 sd = &per_cpu(phys_domains, j);
7140 if (j != first_cpu(sd->groups->cpumask)) {
7141 /*
7142 * Only add "power" once for each
7143 * physical package.
7144 */
7145 continue;
7146 }
7147
7148 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007149 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007150 sg = sg->next;
7151 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153#endif
7154
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007155#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007156/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007157static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007158{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007159 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007160
7161 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007162 struct sched_group **sched_group_nodes
7163 = sched_group_nodes_bycpu[cpu];
7164
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007165 if (!sched_group_nodes)
7166 continue;
7167
7168 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007169 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7170
Mike Travis7c16ec52008-04-04 18:11:11 -07007171 *nodemask = node_to_cpumask(i);
7172 cpus_and(*nodemask, *nodemask, *cpu_map);
7173 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007174 continue;
7175
7176 if (sg == NULL)
7177 continue;
7178 sg = sg->next;
7179next_sg:
7180 oldsg = sg;
7181 sg = sg->next;
7182 kfree(oldsg);
7183 if (oldsg != sched_group_nodes[i])
7184 goto next_sg;
7185 }
7186 kfree(sched_group_nodes);
7187 sched_group_nodes_bycpu[cpu] = NULL;
7188 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007189}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007190#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007191static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007192{
7193}
7194#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007195
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007197 * Initialize sched groups cpu_power.
7198 *
7199 * cpu_power indicates the capacity of sched group, which is used while
7200 * distributing the load between different sched groups in a sched domain.
7201 * Typically cpu_power for all the groups in a sched domain will be same unless
7202 * there are asymmetries in the topology. If there are asymmetries, group
7203 * having more cpu_power will pickup more load compared to the group having
7204 * less cpu_power.
7205 *
7206 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7207 * the maximum number of tasks a group can handle in the presence of other idle
7208 * or lightly loaded groups in the same sched domain.
7209 */
7210static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7211{
7212 struct sched_domain *child;
7213 struct sched_group *group;
7214
7215 WARN_ON(!sd || !sd->groups);
7216
7217 if (cpu != first_cpu(sd->groups->cpumask))
7218 return;
7219
7220 child = sd->child;
7221
Eric Dumazet5517d862007-05-08 00:32:57 -07007222 sd->groups->__cpu_power = 0;
7223
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007224 /*
7225 * For perf policy, if the groups in child domain share resources
7226 * (for example cores sharing some portions of the cache hierarchy
7227 * or SMT), then set this domain groups cpu_power such that each group
7228 * can handle only one task, when there are other idle groups in the
7229 * same sched domain.
7230 */
7231 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7232 (child->flags &
7233 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007234 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007235 return;
7236 }
7237
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007238 /*
7239 * add cpu_power of each child group to this groups cpu_power
7240 */
7241 group = child->groups;
7242 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007243 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007244 group = group->next;
7245 } while (group != child->groups);
7246}
7247
7248/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007249 * Initializers for schedule domains
7250 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7251 */
7252
7253#define SD_INIT(sd, type) sd_init_##type(sd)
7254#define SD_INIT_FUNC(type) \
7255static noinline void sd_init_##type(struct sched_domain *sd) \
7256{ \
7257 memset(sd, 0, sizeof(*sd)); \
7258 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007259 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007260}
7261
7262SD_INIT_FUNC(CPU)
7263#ifdef CONFIG_NUMA
7264 SD_INIT_FUNC(ALLNODES)
7265 SD_INIT_FUNC(NODE)
7266#endif
7267#ifdef CONFIG_SCHED_SMT
7268 SD_INIT_FUNC(SIBLING)
7269#endif
7270#ifdef CONFIG_SCHED_MC
7271 SD_INIT_FUNC(MC)
7272#endif
7273
7274/*
7275 * To minimize stack usage kmalloc room for cpumasks and share the
7276 * space as the usage in build_sched_domains() dictates. Used only
7277 * if the amount of space is significant.
7278 */
7279struct allmasks {
7280 cpumask_t tmpmask; /* make this one first */
7281 union {
7282 cpumask_t nodemask;
7283 cpumask_t this_sibling_map;
7284 cpumask_t this_core_map;
7285 };
7286 cpumask_t send_covered;
7287
7288#ifdef CONFIG_NUMA
7289 cpumask_t domainspan;
7290 cpumask_t covered;
7291 cpumask_t notcovered;
7292#endif
7293};
7294
7295#if NR_CPUS > 128
7296#define SCHED_CPUMASK_ALLOC 1
7297#define SCHED_CPUMASK_FREE(v) kfree(v)
7298#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7299#else
7300#define SCHED_CPUMASK_ALLOC 0
7301#define SCHED_CPUMASK_FREE(v)
7302#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7303#endif
7304
7305#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7306 ((unsigned long)(a) + offsetof(struct allmasks, v))
7307
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007308static int default_relax_domain_level = -1;
7309
7310static int __init setup_relax_domain_level(char *str)
7311{
7312 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7313 return 1;
7314}
7315__setup("relax_domain_level=", setup_relax_domain_level);
7316
7317static void set_domain_attribute(struct sched_domain *sd,
7318 struct sched_domain_attr *attr)
7319{
7320 int request;
7321
7322 if (!attr || attr->relax_domain_level < 0) {
7323 if (default_relax_domain_level < 0)
7324 return;
7325 else
7326 request = default_relax_domain_level;
7327 } else
7328 request = attr->relax_domain_level;
7329 if (request < sd->level) {
7330 /* turn off idle balance on this domain */
7331 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7332 } else {
7333 /* turn on idle balance on this domain */
7334 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7335 }
7336}
7337
Mike Travis7c16ec52008-04-04 18:11:11 -07007338/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007339 * Build sched domains for a given set of cpus and attach the sched domains
7340 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007342static int __build_sched_domains(const cpumask_t *cpu_map,
7343 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344{
7345 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007346 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007347 SCHED_CPUMASK_DECLARE(allmasks);
7348 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007349#ifdef CONFIG_NUMA
7350 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007351 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007352
7353 /*
7354 * Allocate the per-node list of sched groups
7355 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007356 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007357 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007358 if (!sched_group_nodes) {
7359 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007360 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007361 }
John Hawkesd1b55132005-09-06 15:18:14 -07007362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
Gregory Haskinsdc938522008-01-25 21:08:26 +01007364 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007365 if (!rd) {
7366 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007367#ifdef CONFIG_NUMA
7368 kfree(sched_group_nodes);
7369#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007370 return -ENOMEM;
7371 }
7372
Mike Travis7c16ec52008-04-04 18:11:11 -07007373#if SCHED_CPUMASK_ALLOC
7374 /* get space for all scratch cpumask variables */
7375 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7376 if (!allmasks) {
7377 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7378 kfree(rd);
7379#ifdef CONFIG_NUMA
7380 kfree(sched_group_nodes);
7381#endif
7382 return -ENOMEM;
7383 }
7384#endif
7385 tmpmask = (cpumask_t *)allmasks;
7386
7387
7388#ifdef CONFIG_NUMA
7389 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7390#endif
7391
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007393 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007395 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007397 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
Mike Travis7c16ec52008-04-04 18:11:11 -07007399 *nodemask = node_to_cpumask(cpu_to_node(i));
7400 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
7402#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007403 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007404 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007405 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007406 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007407 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007408 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007409 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007410 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007411 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007412 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007413 } else
7414 p = NULL;
7415
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007417 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007418 set_domain_attribute(sd, attr);
Mike Travis4bdbaad2008-04-15 16:35:52 -07007419 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007420 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007421 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007422 if (p)
7423 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007424 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425#endif
7426
7427 p = sd;
7428 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007429 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007430 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007431 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007432 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007434 if (p)
7435 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007438#ifdef CONFIG_SCHED_MC
7439 p = sd;
7440 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007441 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007442 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007443 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007444 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007445 cpus_and(sd->span, sd->span, *cpu_map);
7446 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007447 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007448 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007449#endif
7450
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451#ifdef CONFIG_SCHED_SMT
7452 p = sd;
7453 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007454 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007455 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007456 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007457 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007458 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007460 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007461 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462#endif
7463 }
7464
7465#ifdef CONFIG_SCHED_SMT
7466 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007467 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007468 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7469 SCHED_CPUMASK_VAR(send_covered, allmasks);
7470
7471 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7472 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7473 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 continue;
7475
Ingo Molnardd41f592007-07-09 18:51:59 +02007476 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007477 &cpu_to_cpu_group,
7478 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
7480#endif
7481
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007482#ifdef CONFIG_SCHED_MC
7483 /* Set up multi-core groups */
7484 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007485 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7486 SCHED_CPUMASK_VAR(send_covered, allmasks);
7487
7488 *this_core_map = cpu_coregroup_map(i);
7489 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7490 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007491 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007492
Ingo Molnardd41f592007-07-09 18:51:59 +02007493 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007494 &cpu_to_core_group,
7495 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007496 }
7497#endif
7498
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 /* Set up physical groups */
7500 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007501 SCHED_CPUMASK_VAR(nodemask, allmasks);
7502 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 *nodemask = node_to_cpumask(i);
7505 cpus_and(*nodemask, *nodemask, *cpu_map);
7506 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 continue;
7508
Mike Travis7c16ec52008-04-04 18:11:11 -07007509 init_sched_build_groups(nodemask, cpu_map,
7510 &cpu_to_phys_group,
7511 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
7513
7514#ifdef CONFIG_NUMA
7515 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007516 if (sd_allnodes) {
7517 SCHED_CPUMASK_VAR(send_covered, allmasks);
7518
7519 init_sched_build_groups(cpu_map, cpu_map,
7520 &cpu_to_allnodes_group,
7521 send_covered, tmpmask);
7522 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007523
7524 for (i = 0; i < MAX_NUMNODES; i++) {
7525 /* Set up node groups */
7526 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007527 SCHED_CPUMASK_VAR(nodemask, allmasks);
7528 SCHED_CPUMASK_VAR(domainspan, allmasks);
7529 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007530 int j;
7531
Mike Travis7c16ec52008-04-04 18:11:11 -07007532 *nodemask = node_to_cpumask(i);
7533 cpus_clear(*covered);
7534
7535 cpus_and(*nodemask, *nodemask, *cpu_map);
7536 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007537 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007538 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007539 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007540
Mike Travis4bdbaad2008-04-15 16:35:52 -07007541 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007542 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007543
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007544 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007545 if (!sg) {
7546 printk(KERN_WARNING "Can not alloc domain group for "
7547 "node %d\n", i);
7548 goto error;
7549 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007550 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007551 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007552 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007553
John Hawkes9c1cfda2005-09-06 15:18:14 -07007554 sd = &per_cpu(node_domains, j);
7555 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007556 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007557 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007558 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007559 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007560 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007561 prev = sg;
7562
7563 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007564 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007565 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007566 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007567
Mike Travis7c16ec52008-04-04 18:11:11 -07007568 cpus_complement(*notcovered, *covered);
7569 cpus_and(*tmpmask, *notcovered, *cpu_map);
7570 cpus_and(*tmpmask, *tmpmask, *domainspan);
7571 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007572 break;
7573
Mike Travis7c16ec52008-04-04 18:11:11 -07007574 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7575 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007576 continue;
7577
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007578 sg = kmalloc_node(sizeof(struct sched_group),
7579 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007580 if (!sg) {
7581 printk(KERN_WARNING
7582 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007583 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007584 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007585 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007586 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007587 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007588 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007589 prev->next = sg;
7590 prev = sg;
7591 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593#endif
7594
7595 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007596#ifdef CONFIG_SCHED_SMT
7597 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007598 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7599
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007600 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007601 }
7602#endif
7603#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007604 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007605 struct sched_domain *sd = &per_cpu(core_domains, i);
7606
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007607 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007608 }
7609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007611 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007612 struct sched_domain *sd = &per_cpu(phys_domains, i);
7613
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007614 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 }
7616
John Hawkes9c1cfda2005-09-06 15:18:14 -07007617#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007618 for (i = 0; i < MAX_NUMNODES; i++)
7619 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007620
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007621 if (sd_allnodes) {
7622 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007623
Mike Travis7c16ec52008-04-04 18:11:11 -07007624 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7625 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007626 init_numa_sched_groups_power(sg);
7627 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628#endif
7629
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007631 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 struct sched_domain *sd;
7633#ifdef CONFIG_SCHED_SMT
7634 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007635#elif defined(CONFIG_SCHED_MC)
7636 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637#else
7638 sd = &per_cpu(phys_domains, i);
7639#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007640 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007642
Mike Travis7c16ec52008-04-04 18:11:11 -07007643 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007644 return 0;
7645
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007646#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007647error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007648 free_sched_groups(cpu_map, tmpmask);
7649 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007650 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652}
Paul Jackson029190c2007-10-18 23:40:20 -07007653
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007654static int build_sched_domains(const cpumask_t *cpu_map)
7655{
7656 return __build_sched_domains(cpu_map, NULL);
7657}
7658
Paul Jackson029190c2007-10-18 23:40:20 -07007659static cpumask_t *doms_cur; /* current sched domains */
7660static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007661static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7662 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007663
7664/*
7665 * Special case: If a kmalloc of a doms_cur partition (array of
7666 * cpumask_t) fails, then fallback to a single sched domain,
7667 * as determined by the single cpumask_t fallback_doms.
7668 */
7669static cpumask_t fallback_doms;
7670
Heiko Carstens22e52b02008-03-12 18:31:59 +01007671void __attribute__((weak)) arch_update_cpu_topology(void)
7672{
7673}
7674
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007675/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007676 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007677 * For now this just excludes isolated cpus, but could be used to
7678 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007679 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007680static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007681{
Milton Miller73785472007-10-24 18:23:48 +02007682 int err;
7683
Heiko Carstens22e52b02008-03-12 18:31:59 +01007684 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007685 ndoms_cur = 1;
7686 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7687 if (!doms_cur)
7688 doms_cur = &fallback_doms;
7689 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007690 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007691 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007692 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007693
7694 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007695}
7696
Mike Travis7c16ec52008-04-04 18:11:11 -07007697static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7698 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699{
Mike Travis7c16ec52008-04-04 18:11:11 -07007700 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007703/*
7704 * Detach sched domains from a group of cpus specified in cpu_map
7705 * These cpus will now be attached to the NULL domain
7706 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007707static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007708{
Mike Travis7c16ec52008-04-04 18:11:11 -07007709 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007710 int i;
7711
Milton Miller6382bc92007-10-15 17:00:19 +02007712 unregister_sched_domain_sysctl();
7713
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007714 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007715 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007716 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007717 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007718}
7719
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007720/* handle null as "default" */
7721static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7722 struct sched_domain_attr *new, int idx_new)
7723{
7724 struct sched_domain_attr tmp;
7725
7726 /* fast path */
7727 if (!new && !cur)
7728 return 1;
7729
7730 tmp = SD_ATTR_INIT;
7731 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7732 new ? (new + idx_new) : &tmp,
7733 sizeof(struct sched_domain_attr));
7734}
7735
Paul Jackson029190c2007-10-18 23:40:20 -07007736/*
7737 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007738 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007739 * doms_new[] to the current sched domain partitioning, doms_cur[].
7740 * It destroys each deleted domain and builds each new domain.
7741 *
7742 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007743 * The masks don't intersect (don't overlap.) We should setup one
7744 * sched domain for each mask. CPUs not in any of the cpumasks will
7745 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007746 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7747 * it as it is.
7748 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007749 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7750 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007751 * failed the kmalloc call, then it can pass in doms_new == NULL,
7752 * and partition_sched_domains() will fallback to the single partition
7753 * 'fallback_doms'.
7754 *
7755 * Call with hotplug lock held
7756 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007757void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7758 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007759{
7760 int i, j;
7761
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007762 lock_doms_cur();
7763
Milton Miller73785472007-10-24 18:23:48 +02007764 /* always unregister in case we don't destroy any domains */
7765 unregister_sched_domain_sysctl();
7766
Paul Jackson029190c2007-10-18 23:40:20 -07007767 if (doms_new == NULL) {
7768 ndoms_new = 1;
7769 doms_new = &fallback_doms;
7770 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007771 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007772 }
7773
7774 /* Destroy deleted domains */
7775 for (i = 0; i < ndoms_cur; i++) {
7776 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007777 if (cpus_equal(doms_cur[i], doms_new[j])
7778 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007779 goto match1;
7780 }
7781 /* no match - a current sched domain not in new doms_new[] */
7782 detach_destroy_domains(doms_cur + i);
7783match1:
7784 ;
7785 }
7786
7787 /* Build new domains */
7788 for (i = 0; i < ndoms_new; i++) {
7789 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007790 if (cpus_equal(doms_new[i], doms_cur[j])
7791 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007792 goto match2;
7793 }
7794 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007795 __build_sched_domains(doms_new + i,
7796 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007797match2:
7798 ;
7799 }
7800
7801 /* Remember the new sched domains */
7802 if (doms_cur != &fallback_doms)
7803 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007804 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007805 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007806 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007807 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007808
7809 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007810
7811 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007812}
7813
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007814#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007815int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007816{
7817 int err;
7818
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007819 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007820 detach_destroy_domains(&cpu_online_map);
7821 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007822 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007823
7824 return err;
7825}
7826
7827static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7828{
7829 int ret;
7830
7831 if (buf[0] != '0' && buf[0] != '1')
7832 return -EINVAL;
7833
7834 if (smt)
7835 sched_smt_power_savings = (buf[0] == '1');
7836 else
7837 sched_mc_power_savings = (buf[0] == '1');
7838
7839 ret = arch_reinit_sched_domains();
7840
7841 return ret ? ret : count;
7842}
7843
Adrian Bunk6707de002007-08-12 18:08:19 +02007844#ifdef CONFIG_SCHED_MC
7845static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7846{
7847 return sprintf(page, "%u\n", sched_mc_power_savings);
7848}
7849static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7850 const char *buf, size_t count)
7851{
7852 return sched_power_savings_store(buf, count, 0);
7853}
7854static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7855 sched_mc_power_savings_store);
7856#endif
7857
7858#ifdef CONFIG_SCHED_SMT
7859static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7860{
7861 return sprintf(page, "%u\n", sched_smt_power_savings);
7862}
7863static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7864 const char *buf, size_t count)
7865{
7866 return sched_power_savings_store(buf, count, 1);
7867}
7868static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7869 sched_smt_power_savings_store);
7870#endif
7871
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007872int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7873{
7874 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007875
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007876#ifdef CONFIG_SCHED_SMT
7877 if (smt_capable())
7878 err = sysfs_create_file(&cls->kset.kobj,
7879 &attr_sched_smt_power_savings.attr);
7880#endif
7881#ifdef CONFIG_SCHED_MC
7882 if (!err && mc_capable())
7883 err = sysfs_create_file(&cls->kset.kobj,
7884 &attr_sched_mc_power_savings.attr);
7885#endif
7886 return err;
7887}
7888#endif
7889
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007891 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007893 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 * which will prevent rebalancing while the sched domains are recalculated.
7895 */
7896static int update_sched_domains(struct notifier_block *nfb,
7897 unsigned long action, void *hcpu)
7898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 switch (action) {
7900 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007901 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007903 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007904 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 return NOTIFY_OK;
7906
7907 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007908 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007910 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007912 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007914 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 /*
7916 * Fall through and re-initialise the domains.
7917 */
7918 break;
7919 default:
7920 return NOTIFY_DONE;
7921 }
7922
7923 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007924 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
7926 return NOTIFY_OK;
7927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928
7929void __init sched_init_smp(void)
7930{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007931 cpumask_t non_isolated_cpus;
7932
Mike Travis434d53b2008-04-04 18:11:04 -07007933#if defined(CONFIG_NUMA)
7934 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7935 GFP_KERNEL);
7936 BUG_ON(sched_group_nodes_bycpu == NULL);
7937#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007938 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007939 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007940 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007941 if (cpus_empty(non_isolated_cpus))
7942 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007943 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 /* XXX: Theoretical race here - CPU may be hotplugged now */
7945 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007946
7947 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007948 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007949 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007950 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951}
7952#else
7953void __init sched_init_smp(void)
7954{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007955 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956}
7957#endif /* CONFIG_SMP */
7958
7959int in_sched_functions(unsigned long addr)
7960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 return in_lock_functions(addr) ||
7962 (addr >= (unsigned long)__sched_text_start
7963 && addr < (unsigned long)__sched_text_end);
7964}
7965
Alexey Dobriyana9957442007-10-15 17:00:13 +02007966static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007967{
7968 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007969 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007970#ifdef CONFIG_FAIR_GROUP_SCHED
7971 cfs_rq->rq = rq;
7972#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007973 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007974}
7975
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007976static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7977{
7978 struct rt_prio_array *array;
7979 int i;
7980
7981 array = &rt_rq->active;
7982 for (i = 0; i < MAX_RT_PRIO; i++) {
7983 INIT_LIST_HEAD(array->queue + i);
7984 __clear_bit(i, array->bitmap);
7985 }
7986 /* delimiter for bitsearch: */
7987 __set_bit(MAX_RT_PRIO, array->bitmap);
7988
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007989#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007990 rt_rq->highest_prio = MAX_RT_PRIO;
7991#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007992#ifdef CONFIG_SMP
7993 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007994 rt_rq->overloaded = 0;
7995#endif
7996
7997 rt_rq->rt_time = 0;
7998 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007999 rt_rq->rt_runtime = 0;
8000 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008001
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008002#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008003 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008004 rt_rq->rq = rq;
8005#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008006}
8007
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008008#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008009static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8010 struct sched_entity *se, int cpu, int add,
8011 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008012{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008013 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008014 tg->cfs_rq[cpu] = cfs_rq;
8015 init_cfs_rq(cfs_rq, rq);
8016 cfs_rq->tg = tg;
8017 if (add)
8018 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8019
8020 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008021 /* se could be NULL for init_task_group */
8022 if (!se)
8023 return;
8024
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008025 if (!parent)
8026 se->cfs_rq = &rq->cfs;
8027 else
8028 se->cfs_rq = parent->my_q;
8029
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008030 se->my_q = cfs_rq;
8031 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008032 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008033 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008034}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008035#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008036
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008037#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008038static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8039 struct sched_rt_entity *rt_se, int cpu, int add,
8040 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008041{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008042 struct rq *rq = cpu_rq(cpu);
8043
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008044 tg->rt_rq[cpu] = rt_rq;
8045 init_rt_rq(rt_rq, rq);
8046 rt_rq->tg = tg;
8047 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008048 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008049 if (add)
8050 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8051
8052 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008053 if (!rt_se)
8054 return;
8055
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008056 if (!parent)
8057 rt_se->rt_rq = &rq->rt;
8058 else
8059 rt_se->rt_rq = parent->my_q;
8060
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008061 rt_se->rt_rq = &rq->rt;
8062 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008063 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008064 INIT_LIST_HEAD(&rt_se->run_list);
8065}
8066#endif
8067
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068void __init sched_init(void)
8069{
Ingo Molnardd41f592007-07-09 18:51:59 +02008070 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008071 unsigned long alloc_size = 0, ptr;
8072
8073#ifdef CONFIG_FAIR_GROUP_SCHED
8074 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8075#endif
8076#ifdef CONFIG_RT_GROUP_SCHED
8077 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8078#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008079#ifdef CONFIG_USER_SCHED
8080 alloc_size *= 2;
8081#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008082 /*
8083 * As sched_init() is called before page_alloc is setup,
8084 * we use alloc_bootmem().
8085 */
8086 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008087 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008088
8089#ifdef CONFIG_FAIR_GROUP_SCHED
8090 init_task_group.se = (struct sched_entity **)ptr;
8091 ptr += nr_cpu_ids * sizeof(void **);
8092
8093 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8094 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008095
8096#ifdef CONFIG_USER_SCHED
8097 root_task_group.se = (struct sched_entity **)ptr;
8098 ptr += nr_cpu_ids * sizeof(void **);
8099
8100 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8101 ptr += nr_cpu_ids * sizeof(void **);
8102#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008103#endif
8104#ifdef CONFIG_RT_GROUP_SCHED
8105 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8106 ptr += nr_cpu_ids * sizeof(void **);
8107
8108 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008109 ptr += nr_cpu_ids * sizeof(void **);
8110
8111#ifdef CONFIG_USER_SCHED
8112 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8113 ptr += nr_cpu_ids * sizeof(void **);
8114
8115 root_task_group.rt_rq = (struct rt_rq **)ptr;
8116 ptr += nr_cpu_ids * sizeof(void **);
8117#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008118#endif
8119 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008120
Gregory Haskins57d885f2008-01-25 21:08:18 +01008121#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008122 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008123 init_defrootdomain();
8124#endif
8125
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008126 init_rt_bandwidth(&def_rt_bandwidth,
8127 global_rt_period(), global_rt_runtime());
8128
8129#ifdef CONFIG_RT_GROUP_SCHED
8130 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8131 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008132#ifdef CONFIG_USER_SCHED
8133 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8134 global_rt_period(), RUNTIME_INF);
8135#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008136#endif
8137
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008138#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008139 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008140 INIT_LIST_HEAD(&init_task_group.children);
8141
8142#ifdef CONFIG_USER_SCHED
8143 INIT_LIST_HEAD(&root_task_group.children);
8144 init_task_group.parent = &root_task_group;
8145 list_add(&init_task_group.siblings, &root_task_group.children);
8146#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008147#endif
8148
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008149 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008150 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151
8152 rq = cpu_rq(i);
8153 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008154 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008155 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008156 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008157 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008158 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008159 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008160#ifdef CONFIG_FAIR_GROUP_SCHED
8161 init_task_group.shares = init_task_group_load;
8162 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008163#ifdef CONFIG_CGROUP_SCHED
8164 /*
8165 * How much cpu bandwidth does init_task_group get?
8166 *
8167 * In case of task-groups formed thr' the cgroup filesystem, it
8168 * gets 100% of the cpu resources in the system. This overall
8169 * system cpu resource is divided among the tasks of
8170 * init_task_group and its child task-groups in a fair manner,
8171 * based on each entity's (task or task-group's) weight
8172 * (se->load.weight).
8173 *
8174 * In other words, if init_task_group has 10 tasks of weight
8175 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8176 * then A0's share of the cpu resource is:
8177 *
8178 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8179 *
8180 * We achieve this by letting init_task_group's tasks sit
8181 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8182 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008183 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008184#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008185 root_task_group.shares = NICE_0_LOAD;
8186 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008187 /*
8188 * In case of task-groups formed thr' the user id of tasks,
8189 * init_task_group represents tasks belonging to root user.
8190 * Hence it forms a sibling of all subsequent groups formed.
8191 * In this case, init_task_group gets only a fraction of overall
8192 * system cpu resource, based on the weight assigned to root
8193 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8194 * by letting tasks of init_task_group sit in a separate cfs_rq
8195 * (init_cfs_rq) and having one entity represent this group of
8196 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8197 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008198 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008199 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008200 &per_cpu(init_sched_entity, i), i, 1,
8201 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008202
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008203#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008204#endif /* CONFIG_FAIR_GROUP_SCHED */
8205
8206 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008207#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008208 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008209#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008210 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008211#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008212 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008213 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008214 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008215 &per_cpu(init_sched_rt_entity, i), i, 1,
8216 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008217#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
Ingo Molnardd41f592007-07-09 18:51:59 +02008220 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8221 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008223 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008224 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008226 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008228 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 rq->migration_thread = NULL;
8230 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008231 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008233 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235 }
8236
Peter Williams2dd73a42006-06-27 02:54:34 -07008237 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008238
Avi Kivitye107be32007-07-26 13:40:43 +02008239#ifdef CONFIG_PREEMPT_NOTIFIERS
8240 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8241#endif
8242
Christoph Lameterc9819f42006-12-10 02:20:25 -08008243#ifdef CONFIG_SMP
8244 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8245#endif
8246
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008247#ifdef CONFIG_RT_MUTEXES
8248 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8249#endif
8250
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 /*
8252 * The boot idle thread does lazy MMU switching as well:
8253 */
8254 atomic_inc(&init_mm.mm_count);
8255 enter_lazy_tlb(&init_mm, current);
8256
8257 /*
8258 * Make us the idle thread. Technically, schedule() should not be
8259 * called from this thread, however somewhere below it might be,
8260 * but because we are the idle thread, we just pick up running again
8261 * when this runqueue becomes "idle".
8262 */
8263 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008264 /*
8265 * During early bootup we pretend to be a normal task:
8266 */
8267 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008268
8269 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270}
8271
8272#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8273void __might_sleep(char *file, int line)
8274{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008275#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 static unsigned long prev_jiffy; /* ratelimiting */
8277
8278 if ((in_atomic() || irqs_disabled()) &&
8279 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8280 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8281 return;
8282 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008283 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 " context at %s:%d\n", file, line);
8285 printk("in_atomic():%d, irqs_disabled():%d\n",
8286 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008287 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008288 if (irqs_disabled())
8289 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 dump_stack();
8291 }
8292#endif
8293}
8294EXPORT_SYMBOL(__might_sleep);
8295#endif
8296
8297#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008298static void normalize_task(struct rq *rq, struct task_struct *p)
8299{
8300 int on_rq;
8301 update_rq_clock(rq);
8302 on_rq = p->se.on_rq;
8303 if (on_rq)
8304 deactivate_task(rq, p, 0);
8305 __setscheduler(rq, p, SCHED_NORMAL, 0);
8306 if (on_rq) {
8307 activate_task(rq, p, 0);
8308 resched_task(rq->curr);
8309 }
8310}
8311
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312void normalize_rt_tasks(void)
8313{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008314 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008316 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008318 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008319 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008320 /*
8321 * Only normalize user tasks:
8322 */
8323 if (!p->mm)
8324 continue;
8325
Ingo Molnardd41f592007-07-09 18:51:59 +02008326 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008327#ifdef CONFIG_SCHEDSTATS
8328 p->se.wait_start = 0;
8329 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008330 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008331#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008332 task_rq(p)->clock = 0;
8333
8334 if (!rt_task(p)) {
8335 /*
8336 * Renice negative nice level userspace
8337 * tasks back to 0:
8338 */
8339 if (TASK_NICE(p) < 0 && p->mm)
8340 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008344 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008345 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346
Ingo Molnar178be792007-10-15 17:00:18 +02008347 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008348
Ingo Molnarb29739f2006-06-27 02:54:51 -07008349 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008350 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008351 } while_each_thread(g, p);
8352
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008353 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354}
8355
8356#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008357
8358#ifdef CONFIG_IA64
8359/*
8360 * These functions are only useful for the IA64 MCA handling.
8361 *
8362 * They can only be called when the whole system has been
8363 * stopped - every CPU needs to be quiescent, and no scheduling
8364 * activity can take place. Using them for anything else would
8365 * be a serious bug, and as a result, they aren't even visible
8366 * under any other configuration.
8367 */
8368
8369/**
8370 * curr_task - return the current task for a given cpu.
8371 * @cpu: the processor in question.
8372 *
8373 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8374 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008375struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008376{
8377 return cpu_curr(cpu);
8378}
8379
8380/**
8381 * set_curr_task - set the current task for a given cpu.
8382 * @cpu: the processor in question.
8383 * @p: the task pointer to set.
8384 *
8385 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008386 * are serviced on a separate stack. It allows the architecture to switch the
8387 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008388 * must be called with all CPU's synchronized, and interrupts disabled, the
8389 * and caller must save the original value of the current task (see
8390 * curr_task() above) and restore that value before reenabling interrupts and
8391 * re-starting the system.
8392 *
8393 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8394 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008395void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008396{
8397 cpu_curr(cpu) = p;
8398}
8399
8400#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008401
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008402#ifdef CONFIG_FAIR_GROUP_SCHED
8403static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008404{
8405 int i;
8406
8407 for_each_possible_cpu(i) {
8408 if (tg->cfs_rq)
8409 kfree(tg->cfs_rq[i]);
8410 if (tg->se)
8411 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008412 }
8413
8414 kfree(tg->cfs_rq);
8415 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008416}
8417
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008418static
8419int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008420{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008421 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008422 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008423 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008424 int i;
8425
Mike Travis434d53b2008-04-04 18:11:04 -07008426 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008427 if (!tg->cfs_rq)
8428 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008429 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008430 if (!tg->se)
8431 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008432
8433 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008434
8435 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008436 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008437
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008438 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8439 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008440 if (!cfs_rq)
8441 goto err;
8442
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008443 se = kmalloc_node(sizeof(struct sched_entity),
8444 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008445 if (!se)
8446 goto err;
8447
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008448 parent_se = parent ? parent->se[i] : NULL;
8449 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008450 }
8451
8452 return 1;
8453
8454 err:
8455 return 0;
8456}
8457
8458static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8459{
8460 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8461 &cpu_rq(cpu)->leaf_cfs_rq_list);
8462}
8463
8464static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8465{
8466 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8467}
8468#else
8469static inline void free_fair_sched_group(struct task_group *tg)
8470{
8471}
8472
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008473static inline
8474int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008475{
8476 return 1;
8477}
8478
8479static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8480{
8481}
8482
8483static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8484{
8485}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008486#endif
8487
8488#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008489static void free_rt_sched_group(struct task_group *tg)
8490{
8491 int i;
8492
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008493 destroy_rt_bandwidth(&tg->rt_bandwidth);
8494
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008495 for_each_possible_cpu(i) {
8496 if (tg->rt_rq)
8497 kfree(tg->rt_rq[i]);
8498 if (tg->rt_se)
8499 kfree(tg->rt_se[i]);
8500 }
8501
8502 kfree(tg->rt_rq);
8503 kfree(tg->rt_se);
8504}
8505
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008506static
8507int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008508{
8509 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008510 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008511 struct rq *rq;
8512 int i;
8513
Mike Travis434d53b2008-04-04 18:11:04 -07008514 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008515 if (!tg->rt_rq)
8516 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008517 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008518 if (!tg->rt_se)
8519 goto err;
8520
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008521 init_rt_bandwidth(&tg->rt_bandwidth,
8522 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008523
8524 for_each_possible_cpu(i) {
8525 rq = cpu_rq(i);
8526
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008527 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8528 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8529 if (!rt_rq)
8530 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008531
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008532 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8533 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8534 if (!rt_se)
8535 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008536
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008537 parent_se = parent ? parent->rt_se[i] : NULL;
8538 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008539 }
8540
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008541 return 1;
8542
8543 err:
8544 return 0;
8545}
8546
8547static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8548{
8549 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8550 &cpu_rq(cpu)->leaf_rt_rq_list);
8551}
8552
8553static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8554{
8555 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8556}
8557#else
8558static inline void free_rt_sched_group(struct task_group *tg)
8559{
8560}
8561
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008562static inline
8563int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008564{
8565 return 1;
8566}
8567
8568static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8569{
8570}
8571
8572static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8573{
8574}
8575#endif
8576
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008577#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008578static void free_sched_group(struct task_group *tg)
8579{
8580 free_fair_sched_group(tg);
8581 free_rt_sched_group(tg);
8582 kfree(tg);
8583}
8584
8585/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008586struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008587{
8588 struct task_group *tg;
8589 unsigned long flags;
8590 int i;
8591
8592 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8593 if (!tg)
8594 return ERR_PTR(-ENOMEM);
8595
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008596 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008597 goto err;
8598
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008599 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008600 goto err;
8601
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008602 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008603 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008604 register_fair_sched_group(tg, i);
8605 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008606 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008607 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008608
8609 WARN_ON(!parent); /* root should already exist */
8610
8611 tg->parent = parent;
8612 list_add_rcu(&tg->siblings, &parent->children);
8613 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008614 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008615
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008616 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008617
8618err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008619 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008620 return ERR_PTR(-ENOMEM);
8621}
8622
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008623/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008624static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008625{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008626 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008627 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008628}
8629
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008630/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008631void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008632{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008633 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008634 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008635
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008636 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008637 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008638 unregister_fair_sched_group(tg, i);
8639 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008640 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008641 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008642 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008643 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008644
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008645 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008646 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008647}
8648
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008649/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008650 * The caller of this function should have put the task in its new group
8651 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8652 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008653 */
8654void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008655{
8656 int on_rq, running;
8657 unsigned long flags;
8658 struct rq *rq;
8659
8660 rq = task_rq_lock(tsk, &flags);
8661
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008662 update_rq_clock(rq);
8663
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008664 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008665 on_rq = tsk->se.on_rq;
8666
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008667 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008668 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008669 if (unlikely(running))
8670 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008671
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008672 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008673
Peter Zijlstra810b3812008-02-29 15:21:01 -05008674#ifdef CONFIG_FAIR_GROUP_SCHED
8675 if (tsk->sched_class->moved_group)
8676 tsk->sched_class->moved_group(tsk);
8677#endif
8678
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008679 if (unlikely(running))
8680 tsk->sched_class->set_curr_task(rq);
8681 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008682 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008683
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008684 task_rq_unlock(rq, &flags);
8685}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008686#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008687
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008688#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008689static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008690{
8691 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008692 int on_rq;
8693
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008694 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008695 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008696 dequeue_entity(cfs_rq, se, 0);
8697
8698 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008699 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008700
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008701 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008702 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008703}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008704
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008705static void set_se_shares(struct sched_entity *se, unsigned long shares)
8706{
8707 struct cfs_rq *cfs_rq = se->cfs_rq;
8708 struct rq *rq = cfs_rq->rq;
8709 unsigned long flags;
8710
8711 spin_lock_irqsave(&rq->lock, flags);
8712 __set_se_shares(se, shares);
8713 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008714}
8715
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008716static DEFINE_MUTEX(shares_mutex);
8717
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008718int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008719{
8720 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008721 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008722
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008723 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008724 * We can't change the weight of the root cgroup.
8725 */
8726 if (!tg->se[0])
8727 return -EINVAL;
8728
8729 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008730 * A weight of 0 or 1 can cause arithmetics problems.
8731 * (The default weight is 1024 - so there's no practical
8732 * limitation from this.)
8733 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008734 if (shares < MIN_SHARES)
8735 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008736
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008737 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008738 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008739 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008741 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008742 for_each_possible_cpu(i)
8743 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008744 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008745 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008746
8747 /* wait for any ongoing reference to this group to finish */
8748 synchronize_sched();
8749
8750 /*
8751 * Now we are free to modify the group's share on each cpu
8752 * w/o tripping rebalance_share or load_balance_fair.
8753 */
8754 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008755 for_each_possible_cpu(i) {
8756 /*
8757 * force a rebalance
8758 */
8759 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8760 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8761 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008762
8763 /*
8764 * Enable load balance activity on this group, by inserting it back on
8765 * each cpu's rq->leaf_cfs_rq_list.
8766 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008767 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008768 for_each_possible_cpu(i)
8769 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008770 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008771 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008772done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008773 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008774 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008775}
8776
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008777unsigned long sched_group_shares(struct task_group *tg)
8778{
8779 return tg->shares;
8780}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008781#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008782
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008783#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008784/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008785 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008786 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008787static DEFINE_MUTEX(rt_constraints_mutex);
8788
8789static unsigned long to_ratio(u64 period, u64 runtime)
8790{
8791 if (runtime == RUNTIME_INF)
8792 return 1ULL << 16;
8793
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008794 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008795}
8796
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008797#ifdef CONFIG_CGROUP_SCHED
8798static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8799{
8800 struct task_group *tgi, *parent = tg->parent;
8801 unsigned long total = 0;
8802
8803 if (!parent) {
8804 if (global_rt_period() < period)
8805 return 0;
8806
8807 return to_ratio(period, runtime) <
8808 to_ratio(global_rt_period(), global_rt_runtime());
8809 }
8810
8811 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8812 return 0;
8813
8814 rcu_read_lock();
8815 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8816 if (tgi == tg)
8817 continue;
8818
8819 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8820 tgi->rt_bandwidth.rt_runtime);
8821 }
8822 rcu_read_unlock();
8823
8824 return total + to_ratio(period, runtime) <
8825 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8826 parent->rt_bandwidth.rt_runtime);
8827}
8828#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008829static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008830{
8831 struct task_group *tgi;
8832 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008833 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008834 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008835
8836 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008837 list_for_each_entry_rcu(tgi, &task_groups, list) {
8838 if (tgi == tg)
8839 continue;
8840
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008841 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8842 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008843 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008844 rcu_read_unlock();
8845
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008846 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008847}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008848#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008849
Dhaval Giani521f1a242008-02-28 15:21:56 +05308850/* Must be called with tasklist_lock held */
8851static inline int tg_has_rt_tasks(struct task_group *tg)
8852{
8853 struct task_struct *g, *p;
8854 do_each_thread(g, p) {
8855 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8856 return 1;
8857 } while_each_thread(g, p);
8858 return 0;
8859}
8860
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008861static int tg_set_bandwidth(struct task_group *tg,
8862 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008863{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008864 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008865
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008866 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308867 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008868 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308869 err = -EBUSY;
8870 goto unlock;
8871 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008872 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8873 err = -EINVAL;
8874 goto unlock;
8875 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008876
8877 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008878 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8879 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008880
8881 for_each_possible_cpu(i) {
8882 struct rt_rq *rt_rq = tg->rt_rq[i];
8883
8884 spin_lock(&rt_rq->rt_runtime_lock);
8885 rt_rq->rt_runtime = rt_runtime;
8886 spin_unlock(&rt_rq->rt_runtime_lock);
8887 }
8888 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008889 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308890 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008891 mutex_unlock(&rt_constraints_mutex);
8892
8893 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008894}
8895
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008896int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8897{
8898 u64 rt_runtime, rt_period;
8899
8900 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8901 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8902 if (rt_runtime_us < 0)
8903 rt_runtime = RUNTIME_INF;
8904
8905 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8906}
8907
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008908long sched_group_rt_runtime(struct task_group *tg)
8909{
8910 u64 rt_runtime_us;
8911
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008912 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008913 return -1;
8914
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008915 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008916 do_div(rt_runtime_us, NSEC_PER_USEC);
8917 return rt_runtime_us;
8918}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008919
8920int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8921{
8922 u64 rt_runtime, rt_period;
8923
8924 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8925 rt_runtime = tg->rt_bandwidth.rt_runtime;
8926
8927 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8928}
8929
8930long sched_group_rt_period(struct task_group *tg)
8931{
8932 u64 rt_period_us;
8933
8934 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8935 do_div(rt_period_us, NSEC_PER_USEC);
8936 return rt_period_us;
8937}
8938
8939static int sched_rt_global_constraints(void)
8940{
8941 int ret = 0;
8942
8943 mutex_lock(&rt_constraints_mutex);
8944 if (!__rt_schedulable(NULL, 1, 0))
8945 ret = -EINVAL;
8946 mutex_unlock(&rt_constraints_mutex);
8947
8948 return ret;
8949}
8950#else
8951static int sched_rt_global_constraints(void)
8952{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008953 unsigned long flags;
8954 int i;
8955
8956 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8957 for_each_possible_cpu(i) {
8958 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8959
8960 spin_lock(&rt_rq->rt_runtime_lock);
8961 rt_rq->rt_runtime = global_rt_runtime();
8962 spin_unlock(&rt_rq->rt_runtime_lock);
8963 }
8964 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8965
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008966 return 0;
8967}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008968#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008969
8970int sched_rt_handler(struct ctl_table *table, int write,
8971 struct file *filp, void __user *buffer, size_t *lenp,
8972 loff_t *ppos)
8973{
8974 int ret;
8975 int old_period, old_runtime;
8976 static DEFINE_MUTEX(mutex);
8977
8978 mutex_lock(&mutex);
8979 old_period = sysctl_sched_rt_period;
8980 old_runtime = sysctl_sched_rt_runtime;
8981
8982 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8983
8984 if (!ret && write) {
8985 ret = sched_rt_global_constraints();
8986 if (ret) {
8987 sysctl_sched_rt_period = old_period;
8988 sysctl_sched_rt_runtime = old_runtime;
8989 } else {
8990 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8991 def_rt_bandwidth.rt_period =
8992 ns_to_ktime(global_rt_period());
8993 }
8994 }
8995 mutex_unlock(&mutex);
8996
8997 return ret;
8998}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008999
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009000#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009001
9002/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009003static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009004{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009005 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9006 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009007}
9008
9009static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009010cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009011{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009012 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009013
Paul Menage2b01dfe2007-10-24 18:23:50 +02009014 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009015 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009016 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009017 return &init_task_group.css;
9018 }
9019
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009020 parent = cgroup_tg(cgrp->parent);
9021 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009022 if (IS_ERR(tg))
9023 return ERR_PTR(-ENOMEM);
9024
9025 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009026 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009027
9028 return &tg->css;
9029}
9030
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009031static void
9032cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009033{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009034 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009035
9036 sched_destroy_group(tg);
9037}
9038
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009039static int
9040cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9041 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009042{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009043#ifdef CONFIG_RT_GROUP_SCHED
9044 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009045 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009046 return -EINVAL;
9047#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009048 /* We don't support RT-tasks being in separate groups */
9049 if (tsk->sched_class != &fair_sched_class)
9050 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009051#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009052
9053 return 0;
9054}
9055
9056static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009057cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009058 struct cgroup *old_cont, struct task_struct *tsk)
9059{
9060 sched_move_task(tsk);
9061}
9062
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009063#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009064static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009065 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009066{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009067 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009068}
9069
Paul Menagef4c753b2008-04-29 00:59:56 -07009070static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009072 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009073
9074 return (u64) tg->shares;
9075}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009076#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009078#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009079static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009080 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009081{
Paul Menage06ecb272008-04-29 01:00:06 -07009082 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009083}
9084
Paul Menage06ecb272008-04-29 01:00:06 -07009085static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009086{
Paul Menage06ecb272008-04-29 01:00:06 -07009087 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009088}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009089
9090static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9091 u64 rt_period_us)
9092{
9093 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9094}
9095
9096static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9097{
9098 return sched_group_rt_period(cgroup_tg(cgrp));
9099}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009100#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009101
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009102static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009103#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009104 {
9105 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009106 .read_u64 = cpu_shares_read_u64,
9107 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009108 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009109#endif
9110#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009111 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009112 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009113 .read_s64 = cpu_rt_runtime_read,
9114 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009115 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009116 {
9117 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009118 .read_u64 = cpu_rt_period_read_uint,
9119 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009120 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009121#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122};
9123
9124static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9125{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009126 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009127}
9128
9129struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009130 .name = "cpu",
9131 .create = cpu_cgroup_create,
9132 .destroy = cpu_cgroup_destroy,
9133 .can_attach = cpu_cgroup_can_attach,
9134 .attach = cpu_cgroup_attach,
9135 .populate = cpu_cgroup_populate,
9136 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009137 .early_init = 1,
9138};
9139
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009140#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009141
9142#ifdef CONFIG_CGROUP_CPUACCT
9143
9144/*
9145 * CPU accounting code for task groups.
9146 *
9147 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9148 * (balbir@in.ibm.com).
9149 */
9150
9151/* track cpu usage of a group of tasks */
9152struct cpuacct {
9153 struct cgroup_subsys_state css;
9154 /* cpuusage holds pointer to a u64-type object on every cpu */
9155 u64 *cpuusage;
9156};
9157
9158struct cgroup_subsys cpuacct_subsys;
9159
9160/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309161static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009162{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309163 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009164 struct cpuacct, css);
9165}
9166
9167/* return cpu accounting group to which this task belongs */
9168static inline struct cpuacct *task_ca(struct task_struct *tsk)
9169{
9170 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9171 struct cpuacct, css);
9172}
9173
9174/* create a new cpu accounting group */
9175static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309176 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009177{
9178 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9179
9180 if (!ca)
9181 return ERR_PTR(-ENOMEM);
9182
9183 ca->cpuusage = alloc_percpu(u64);
9184 if (!ca->cpuusage) {
9185 kfree(ca);
9186 return ERR_PTR(-ENOMEM);
9187 }
9188
9189 return &ca->css;
9190}
9191
9192/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009193static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309194cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009195{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309196 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009197
9198 free_percpu(ca->cpuusage);
9199 kfree(ca);
9200}
9201
9202/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309203static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009204{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309205 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009206 u64 totalcpuusage = 0;
9207 int i;
9208
9209 for_each_possible_cpu(i) {
9210 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9211
9212 /*
9213 * Take rq->lock to make 64-bit addition safe on 32-bit
9214 * platforms.
9215 */
9216 spin_lock_irq(&cpu_rq(i)->lock);
9217 totalcpuusage += *cpuusage;
9218 spin_unlock_irq(&cpu_rq(i)->lock);
9219 }
9220
9221 return totalcpuusage;
9222}
9223
Dhaval Giani0297b802008-02-29 10:02:44 +05309224static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9225 u64 reset)
9226{
9227 struct cpuacct *ca = cgroup_ca(cgrp);
9228 int err = 0;
9229 int i;
9230
9231 if (reset) {
9232 err = -EINVAL;
9233 goto out;
9234 }
9235
9236 for_each_possible_cpu(i) {
9237 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9238
9239 spin_lock_irq(&cpu_rq(i)->lock);
9240 *cpuusage = 0;
9241 spin_unlock_irq(&cpu_rq(i)->lock);
9242 }
9243out:
9244 return err;
9245}
9246
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009247static struct cftype files[] = {
9248 {
9249 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009250 .read_u64 = cpuusage_read,
9251 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009252 },
9253};
9254
Dhaval Giani32cd7562008-02-29 10:02:43 +05309255static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009256{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309257 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009258}
9259
9260/*
9261 * charge this task's execution time to its accounting group.
9262 *
9263 * called with rq->lock held.
9264 */
9265static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9266{
9267 struct cpuacct *ca;
9268
9269 if (!cpuacct_subsys.active)
9270 return;
9271
9272 ca = task_ca(tsk);
9273 if (ca) {
9274 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9275
9276 *cpuusage += cputime;
9277 }
9278}
9279
9280struct cgroup_subsys cpuacct_subsys = {
9281 .name = "cpuacct",
9282 .create = cpuacct_create,
9283 .destroy = cpuacct_destroy,
9284 .populate = cpuacct_populate,
9285 .subsys_id = cpuacct_subsys_id,
9286};
9287#endif /* CONFIG_CGROUP_CPUACCT */