blob: 0014b03adaca0af7d09d25d3b5c0df3c007b2462 [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
760__read_mostly char *sched_feat_names[] = {
761#include "sched_features.h"
762 NULL
763};
764
765#undef SCHED_FEAT
766
767int sched_feat_open(struct inode *inode, struct file *filp)
768{
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
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001127 spin_lock(&rq->lock);
1128 __update_rq_clock(rq);
1129 spin_unlock(&rq->lock);
1130 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001131}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001132EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1133
1134/*
1135 * We just idled delta nanoseconds (called with irqs disabled):
1136 */
1137void sched_clock_idle_wakeup_event(u64 delta_ns)
1138{
1139 struct rq *rq = cpu_rq(smp_processor_id());
1140 u64 now = sched_clock();
1141
1142 rq->idle_clock += delta_ns;
1143 /*
1144 * Override the previous timestamp and ignore all
1145 * sched_clock() deltas that occured while we idled,
1146 * and use the PM-provided delta_ns to advance the
1147 * rq clock:
1148 */
1149 spin_lock(&rq->lock);
1150 rq->prev_clock_raw = now;
1151 rq->clock += delta_ns;
1152 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001153 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001154}
1155EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001156
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001157static void __resched_task(struct task_struct *p, int tif_bit);
1158
1159static inline void resched_task(struct task_struct *p)
1160{
1161 __resched_task(p, TIF_NEED_RESCHED);
1162}
1163
1164#ifdef CONFIG_SCHED_HRTICK
1165/*
1166 * Use HR-timers to deliver accurate preemption points.
1167 *
1168 * Its all a bit involved since we cannot program an hrt while holding the
1169 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1170 * reschedule event.
1171 *
1172 * When we get rescheduled we reprogram the hrtick_timer outside of the
1173 * rq->lock.
1174 */
1175static inline void resched_hrt(struct task_struct *p)
1176{
1177 __resched_task(p, TIF_HRTICK_RESCHED);
1178}
1179
1180static inline void resched_rq(struct rq *rq)
1181{
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&rq->lock, flags);
1185 resched_task(rq->curr);
1186 spin_unlock_irqrestore(&rq->lock, flags);
1187}
1188
1189enum {
1190 HRTICK_SET, /* re-programm hrtick_timer */
1191 HRTICK_RESET, /* not a new slice */
1192};
1193
1194/*
1195 * Use hrtick when:
1196 * - enabled by features
1197 * - hrtimer is actually high res
1198 */
1199static inline int hrtick_enabled(struct rq *rq)
1200{
1201 if (!sched_feat(HRTICK))
1202 return 0;
1203 return hrtimer_is_hres_active(&rq->hrtick_timer);
1204}
1205
1206/*
1207 * Called to set the hrtick timer state.
1208 *
1209 * called with rq->lock held and irqs disabled
1210 */
1211static void hrtick_start(struct rq *rq, u64 delay, int reset)
1212{
1213 assert_spin_locked(&rq->lock);
1214
1215 /*
1216 * preempt at: now + delay
1217 */
1218 rq->hrtick_expire =
1219 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1220 /*
1221 * indicate we need to program the timer
1222 */
1223 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1224 if (reset)
1225 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1226
1227 /*
1228 * New slices are called from the schedule path and don't need a
1229 * forced reschedule.
1230 */
1231 if (reset)
1232 resched_hrt(rq->curr);
1233}
1234
1235static void hrtick_clear(struct rq *rq)
1236{
1237 if (hrtimer_active(&rq->hrtick_timer))
1238 hrtimer_cancel(&rq->hrtick_timer);
1239}
1240
1241/*
1242 * Update the timer from the possible pending state.
1243 */
1244static void hrtick_set(struct rq *rq)
1245{
1246 ktime_t time;
1247 int set, reset;
1248 unsigned long flags;
1249
1250 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1251
1252 spin_lock_irqsave(&rq->lock, flags);
1253 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1254 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1255 time = rq->hrtick_expire;
1256 clear_thread_flag(TIF_HRTICK_RESCHED);
1257 spin_unlock_irqrestore(&rq->lock, flags);
1258
1259 if (set) {
1260 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1261 if (reset && !hrtimer_active(&rq->hrtick_timer))
1262 resched_rq(rq);
1263 } else
1264 hrtick_clear(rq);
1265}
1266
1267/*
1268 * High-resolution timer tick.
1269 * Runs from hardirq context with interrupts disabled.
1270 */
1271static enum hrtimer_restart hrtick(struct hrtimer *timer)
1272{
1273 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1274
1275 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1276
1277 spin_lock(&rq->lock);
1278 __update_rq_clock(rq);
1279 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1280 spin_unlock(&rq->lock);
1281
1282 return HRTIMER_NORESTART;
1283}
1284
1285static inline void init_rq_hrtick(struct rq *rq)
1286{
1287 rq->hrtick_flags = 0;
1288 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1289 rq->hrtick_timer.function = hrtick;
1290 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1291}
1292
1293void hrtick_resched(void)
1294{
1295 struct rq *rq;
1296 unsigned long flags;
1297
1298 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1299 return;
1300
1301 local_irq_save(flags);
1302 rq = cpu_rq(smp_processor_id());
1303 hrtick_set(rq);
1304 local_irq_restore(flags);
1305}
1306#else
1307static inline void hrtick_clear(struct rq *rq)
1308{
1309}
1310
1311static inline void hrtick_set(struct rq *rq)
1312{
1313}
1314
1315static inline void init_rq_hrtick(struct rq *rq)
1316{
1317}
1318
1319void hrtick_resched(void)
1320{
1321}
1322#endif
1323
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001324/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001325 * resched_task - mark a task 'to be rescheduled now'.
1326 *
1327 * On UP this means the setting of the need_resched flag, on SMP it
1328 * might also involve a cross-CPU call to trigger the scheduler on
1329 * the target CPU.
1330 */
1331#ifdef CONFIG_SMP
1332
1333#ifndef tsk_is_polling
1334#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1335#endif
1336
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001337static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001338{
1339 int cpu;
1340
1341 assert_spin_locked(&task_rq(p)->lock);
1342
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001343 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001344 return;
1345
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001346 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001347
1348 cpu = task_cpu(p);
1349 if (cpu == smp_processor_id())
1350 return;
1351
1352 /* NEED_RESCHED must be visible before we test polling */
1353 smp_mb();
1354 if (!tsk_is_polling(p))
1355 smp_send_reschedule(cpu);
1356}
1357
1358static void resched_cpu(int cpu)
1359{
1360 struct rq *rq = cpu_rq(cpu);
1361 unsigned long flags;
1362
1363 if (!spin_trylock_irqsave(&rq->lock, flags))
1364 return;
1365 resched_task(cpu_curr(cpu));
1366 spin_unlock_irqrestore(&rq->lock, flags);
1367}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001368
1369#ifdef CONFIG_NO_HZ
1370/*
1371 * When add_timer_on() enqueues a timer into the timer wheel of an
1372 * idle CPU then this timer might expire before the next timer event
1373 * which is scheduled to wake up that CPU. In case of a completely
1374 * idle system the next event might even be infinite time into the
1375 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1376 * leaves the inner idle loop so the newly added timer is taken into
1377 * account when the CPU goes back to idle and evaluates the timer
1378 * wheel for the next timer event.
1379 */
1380void wake_up_idle_cpu(int cpu)
1381{
1382 struct rq *rq = cpu_rq(cpu);
1383
1384 if (cpu == smp_processor_id())
1385 return;
1386
1387 /*
1388 * This is safe, as this function is called with the timer
1389 * wheel base lock of (cpu) held. When the CPU is on the way
1390 * to idle and has not yet set rq->curr to idle then it will
1391 * be serialized on the timer wheel base lock and take the new
1392 * timer into account automatically.
1393 */
1394 if (rq->curr != rq->idle)
1395 return;
1396
1397 /*
1398 * We can set TIF_RESCHED on the idle task of the other CPU
1399 * lockless. The worst case is that the other CPU runs the
1400 * idle task through an additional NOOP schedule()
1401 */
1402 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1403
1404 /* NEED_RESCHED must be visible before we test polling */
1405 smp_mb();
1406 if (!tsk_is_polling(rq->idle))
1407 smp_send_reschedule(cpu);
1408}
1409#endif
1410
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001411#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001412static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001413{
1414 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001415 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001416}
1417#endif
1418
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001419#if BITS_PER_LONG == 32
1420# define WMULT_CONST (~0UL)
1421#else
1422# define WMULT_CONST (1UL << 32)
1423#endif
1424
1425#define WMULT_SHIFT 32
1426
Ingo Molnar194081e2007-08-09 11:16:51 +02001427/*
1428 * Shift right and round:
1429 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001430#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001431
Peter Zijlstra8f1bc382008-04-19 19:45:00 +02001432/*
1433 * delta *= weight / lw
1434 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001435static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001436calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1437 struct load_weight *lw)
1438{
1439 u64 tmp;
1440
1441 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001442 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001443
1444 tmp = (u64)delta_exec * weight;
1445 /*
1446 * Check whether we'd overflow the 64-bit multiplication:
1447 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001448 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001449 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001450 WMULT_SHIFT/2);
1451 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001452 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001453
Ingo Molnarecf691d2007-08-02 17:41:40 +02001454 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001455}
1456
Ingo Molnar10919852007-10-15 17:00:04 +02001457static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001458{
1459 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001460 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001461}
1462
Ingo Molnar10919852007-10-15 17:00:04 +02001463static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001464{
1465 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001466 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001470 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1471 * of tasks with abnormal "nice" values across CPUs the contribution that
1472 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001473 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001474 * scaled version of the new time slice allocation that they receive on time
1475 * slice expiry etc.
1476 */
1477
Ingo Molnardd41f592007-07-09 18:51:59 +02001478#define WEIGHT_IDLEPRIO 2
1479#define WMULT_IDLEPRIO (1 << 31)
1480
1481/*
1482 * Nice levels are multiplicative, with a gentle 10% change for every
1483 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1484 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1485 * that remained on nice 0.
1486 *
1487 * The "10% effect" is relative and cumulative: from _any_ nice level,
1488 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001489 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1490 * If a task goes up by ~10% and another task goes down by ~10% then
1491 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001492 */
1493static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001494 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1495 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1496 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1497 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1498 /* 0 */ 1024, 820, 655, 526, 423,
1499 /* 5 */ 335, 272, 215, 172, 137,
1500 /* 10 */ 110, 87, 70, 56, 45,
1501 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001502};
1503
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001504/*
1505 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1506 *
1507 * In cases where the weight does not change often, we can use the
1508 * precalculated inverse to speed up arithmetics by turning divisions
1509 * into multiplications:
1510 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001511static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001512 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1513 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1514 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1515 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1516 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1517 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1518 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1519 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001520};
Peter Williams2dd73a42006-06-27 02:54:34 -07001521
Ingo Molnardd41f592007-07-09 18:51:59 +02001522static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1523
1524/*
1525 * runqueue iterator, to support SMP load-balancing between different
1526 * scheduling classes, without having to expose their internal data
1527 * structures to the load-balancing proper:
1528 */
1529struct rq_iterator {
1530 void *arg;
1531 struct task_struct *(*start)(void *);
1532 struct task_struct *(*next)(void *);
1533};
1534
Peter Williamse1d14842007-10-24 18:23:51 +02001535#ifdef CONFIG_SMP
1536static unsigned long
1537balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1538 unsigned long max_load_move, struct sched_domain *sd,
1539 enum cpu_idle_type idle, int *all_pinned,
1540 int *this_best_prio, struct rq_iterator *iterator);
1541
1542static int
1543iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1544 struct sched_domain *sd, enum cpu_idle_type idle,
1545 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001546#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001547
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001548#ifdef CONFIG_CGROUP_CPUACCT
1549static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1550#else
1551static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1552#endif
1553
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001554static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1555{
1556 update_load_add(&rq->load, load);
1557}
1558
1559static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1560{
1561 update_load_sub(&rq->load, load);
1562}
1563
Gregory Haskinse7693a32008-01-25 21:08:09 +01001564#ifdef CONFIG_SMP
1565static unsigned long source_load(int cpu, int type);
1566static unsigned long target_load(int cpu, int type);
1567static unsigned long cpu_avg_load_per_task(int cpu);
1568static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001569
1570#ifdef CONFIG_FAIR_GROUP_SCHED
1571
1572/*
1573 * Group load balancing.
1574 *
1575 * We calculate a few balance domain wide aggregate numbers; load and weight.
1576 * Given the pictures below, and assuming each item has equal weight:
1577 *
1578 * root 1 - thread
1579 * / | \ A - group
1580 * A 1 B
1581 * /|\ / \
1582 * C 2 D 3 4
1583 * | |
1584 * 5 6
1585 *
1586 * load:
1587 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1588 * which equals 1/9-th of the total load.
1589 *
1590 * shares:
1591 * The weight of this group on the selected cpus.
1592 *
1593 * rq_weight:
1594 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1595 * B would get 2.
1596 *
1597 * task_weight:
1598 * Part of the rq_weight contributed by tasks; all groups except B would
1599 * get 1, B gets 2.
1600 */
1601
1602static inline struct aggregate_struct *
1603aggregate(struct task_group *tg, struct sched_domain *sd)
1604{
1605 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1606}
1607
1608typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1609
1610/*
1611 * Iterate the full tree, calling @down when first entering a node and @up when
1612 * leaving it for the final time.
1613 */
1614static
1615void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1616 struct sched_domain *sd)
1617{
1618 struct task_group *parent, *child;
1619
1620 rcu_read_lock();
1621 parent = &root_task_group;
1622down:
1623 (*down)(parent, sd);
1624 list_for_each_entry_rcu(child, &parent->children, siblings) {
1625 parent = child;
1626 goto down;
1627
1628up:
1629 continue;
1630 }
1631 (*up)(parent, sd);
1632
1633 child = parent;
1634 parent = parent->parent;
1635 if (parent)
1636 goto up;
1637 rcu_read_unlock();
1638}
1639
1640/*
1641 * Calculate the aggregate runqueue weight.
1642 */
1643static
1644void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1645{
1646 unsigned long rq_weight = 0;
1647 unsigned long task_weight = 0;
1648 int i;
1649
1650 for_each_cpu_mask(i, sd->span) {
1651 rq_weight += tg->cfs_rq[i]->load.weight;
1652 task_weight += tg->cfs_rq[i]->task_weight;
1653 }
1654
1655 aggregate(tg, sd)->rq_weight = rq_weight;
1656 aggregate(tg, sd)->task_weight = task_weight;
1657}
1658
1659/*
1660 * Redistribute tg->shares amongst all tg->cfs_rq[]s.
1661 */
1662static void __aggregate_redistribute_shares(struct task_group *tg)
1663{
1664 int i, max_cpu = smp_processor_id();
1665 unsigned long rq_weight = 0;
1666 unsigned long shares, max_shares = 0, shares_rem = tg->shares;
1667
1668 for_each_possible_cpu(i)
1669 rq_weight += tg->cfs_rq[i]->load.weight;
1670
1671 for_each_possible_cpu(i) {
1672 /*
1673 * divide shares proportional to the rq_weights.
1674 */
1675 shares = tg->shares * tg->cfs_rq[i]->load.weight;
1676 shares /= rq_weight + 1;
1677
1678 tg->cfs_rq[i]->shares = shares;
1679
1680 if (shares > max_shares) {
1681 max_shares = shares;
1682 max_cpu = i;
1683 }
1684 shares_rem -= shares;
1685 }
1686
1687 /*
1688 * Ensure it all adds up to tg->shares; we can loose a few
1689 * due to rounding down when computing the per-cpu shares.
1690 */
1691 if (shares_rem)
1692 tg->cfs_rq[max_cpu]->shares += shares_rem;
1693}
1694
1695/*
1696 * Compute the weight of this group on the given cpus.
1697 */
1698static
1699void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1700{
1701 unsigned long shares = 0;
1702 int i;
1703
1704again:
1705 for_each_cpu_mask(i, sd->span)
1706 shares += tg->cfs_rq[i]->shares;
1707
1708 /*
1709 * When the span doesn't have any shares assigned, but does have
1710 * tasks to run do a machine wide rebalance (should be rare).
1711 */
1712 if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) {
1713 __aggregate_redistribute_shares(tg);
1714 goto again;
1715 }
1716
1717 aggregate(tg, sd)->shares = shares;
1718}
1719
1720/*
1721 * Compute the load fraction assigned to this group, relies on the aggregate
1722 * weight and this group's parent's load, i.e. top-down.
1723 */
1724static
1725void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1726{
1727 unsigned long load;
1728
1729 if (!tg->parent) {
1730 int i;
1731
1732 load = 0;
1733 for_each_cpu_mask(i, sd->span)
1734 load += cpu_rq(i)->load.weight;
1735
1736 } else {
1737 load = aggregate(tg->parent, sd)->load;
1738
1739 /*
1740 * shares is our weight in the parent's rq so
1741 * shares/parent->rq_weight gives our fraction of the load
1742 */
1743 load *= aggregate(tg, sd)->shares;
1744 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1745 }
1746
1747 aggregate(tg, sd)->load = load;
1748}
1749
1750static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1751
1752/*
1753 * Calculate and set the cpu's group shares.
1754 */
1755static void
1756__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1757 int tcpu)
1758{
1759 int boost = 0;
1760 unsigned long shares;
1761 unsigned long rq_weight;
1762
1763 if (!tg->se[tcpu])
1764 return;
1765
1766 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1767
1768 /*
1769 * If there are currently no tasks on the cpu pretend there is one of
1770 * average load so that when a new task gets to run here it will not
1771 * get delayed by group starvation.
1772 */
1773 if (!rq_weight) {
1774 boost = 1;
1775 rq_weight = NICE_0_LOAD;
1776 }
1777
1778 /*
1779 * \Sum shares * rq_weight
1780 * shares = -----------------------
1781 * \Sum rq_weight
1782 *
1783 */
1784 shares = aggregate(tg, sd)->shares * rq_weight;
1785 shares /= aggregate(tg, sd)->rq_weight + 1;
1786
1787 /*
1788 * record the actual number of shares, not the boosted amount.
1789 */
1790 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1791
1792 if (shares < MIN_SHARES)
1793 shares = MIN_SHARES;
1794
1795 __set_se_shares(tg->se[tcpu], shares);
1796}
1797
1798/*
1799 * Re-adjust the weights on the cpu the task came from and on the cpu the
1800 * task went to.
1801 */
1802static void
1803__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1804 int scpu, int dcpu)
1805{
1806 unsigned long shares;
1807
1808 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1809
1810 __update_group_shares_cpu(tg, sd, scpu);
1811 __update_group_shares_cpu(tg, sd, dcpu);
1812
1813 /*
1814 * ensure we never loose shares due to rounding errors in the
1815 * above redistribution.
1816 */
1817 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1818 if (shares)
1819 tg->cfs_rq[dcpu]->shares += shares;
1820}
1821
1822/*
1823 * Because changing a group's shares changes the weight of the super-group
1824 * we need to walk up the tree and change all shares until we hit the root.
1825 */
1826static void
1827move_group_shares(struct task_group *tg, struct sched_domain *sd,
1828 int scpu, int dcpu)
1829{
1830 while (tg) {
1831 __move_group_shares(tg, sd, scpu, dcpu);
1832 tg = tg->parent;
1833 }
1834}
1835
1836static
1837void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1838{
1839 unsigned long shares = aggregate(tg, sd)->shares;
1840 int i;
1841
1842 for_each_cpu_mask(i, sd->span) {
1843 struct rq *rq = cpu_rq(i);
1844 unsigned long flags;
1845
1846 spin_lock_irqsave(&rq->lock, flags);
1847 __update_group_shares_cpu(tg, sd, i);
1848 spin_unlock_irqrestore(&rq->lock, flags);
1849 }
1850
1851 aggregate_group_shares(tg, sd);
1852
1853 /*
1854 * ensure we never loose shares due to rounding errors in the
1855 * above redistribution.
1856 */
1857 shares -= aggregate(tg, sd)->shares;
1858 if (shares) {
1859 tg->cfs_rq[sd->first_cpu]->shares += shares;
1860 aggregate(tg, sd)->shares += shares;
1861 }
1862}
1863
1864/*
1865 * Calculate the accumulative weight and recursive load of each task group
1866 * while walking down the tree.
1867 */
1868static
1869void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1870{
1871 aggregate_group_weight(tg, sd);
1872 aggregate_group_shares(tg, sd);
1873 aggregate_group_load(tg, sd);
1874}
1875
1876/*
1877 * Rebalance the cpu shares while walking back up the tree.
1878 */
1879static
1880void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1881{
1882 aggregate_group_set_shares(tg, sd);
1883}
1884
1885static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1886
1887static void __init init_aggregate(void)
1888{
1889 int i;
1890
1891 for_each_possible_cpu(i)
1892 spin_lock_init(&per_cpu(aggregate_lock, i));
1893}
1894
1895static int get_aggregate(struct sched_domain *sd)
1896{
1897 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1898 return 0;
1899
1900 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1901 return 1;
1902}
1903
1904static void put_aggregate(struct sched_domain *sd)
1905{
1906 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1907}
1908
1909static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1910{
1911 cfs_rq->shares = shares;
1912}
1913
1914#else
1915
1916static inline void init_aggregate(void)
1917{
1918}
1919
1920static inline int get_aggregate(struct sched_domain *sd)
1921{
1922 return 0;
1923}
1924
1925static inline void put_aggregate(struct sched_domain *sd)
1926{
1927}
1928#endif
1929
1930#else /* CONFIG_SMP */
1931
1932#ifdef CONFIG_FAIR_GROUP_SCHED
1933static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1934{
1935}
1936#endif
1937
Gregory Haskinse7693a32008-01-25 21:08:09 +01001938#endif /* CONFIG_SMP */
1939
Ingo Molnardd41f592007-07-09 18:51:59 +02001940#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001941#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001942#include "sched_fair.c"
1943#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001944#ifdef CONFIG_SCHED_DEBUG
1945# include "sched_debug.c"
1946#endif
1947
1948#define sched_class_highest (&rt_sched_class)
1949
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001950static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001951{
1952 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001953}
1954
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001955static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001956{
1957 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001958}
1959
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001960static void set_load_weight(struct task_struct *p)
1961{
1962 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001963 p->se.load.weight = prio_to_weight[0] * 2;
1964 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1965 return;
1966 }
1967
1968 /*
1969 * SCHED_IDLE tasks get minimal weight:
1970 */
1971 if (p->policy == SCHED_IDLE) {
1972 p->se.load.weight = WEIGHT_IDLEPRIO;
1973 p->se.load.inv_weight = WMULT_IDLEPRIO;
1974 return;
1975 }
1976
1977 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1978 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001979}
1980
Ingo Molnar8159f872007-08-09 11:16:49 +02001981static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001982{
1983 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001984 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001985 p->se.on_rq = 1;
1986}
1987
Ingo Molnar69be72c2007-08-09 11:16:49 +02001988static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001989{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001990 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001991 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001992}
1993
1994/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001995 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001996 */
Ingo Molnar14531182007-07-09 18:51:59 +02001997static inline int __normal_prio(struct task_struct *p)
1998{
Ingo Molnardd41f592007-07-09 18:51:59 +02001999 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002000}
2001
2002/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002003 * Calculate the expected normal priority: i.e. priority
2004 * without taking RT-inheritance into account. Might be
2005 * boosted by interactivity modifiers. Changes upon fork,
2006 * setprio syscalls, and whenever the interactivity
2007 * estimator recalculates.
2008 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002009static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002010{
2011 int prio;
2012
Ingo Molnare05606d2007-07-09 18:51:59 +02002013 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002014 prio = MAX_RT_PRIO-1 - p->rt_priority;
2015 else
2016 prio = __normal_prio(p);
2017 return prio;
2018}
2019
2020/*
2021 * Calculate the current priority, i.e. the priority
2022 * taken into account by the scheduler. This value might
2023 * be boosted by RT tasks, or might be boosted by
2024 * interactivity modifiers. Will be RT if the task got
2025 * RT-boosted. If not then it returns p->normal_prio.
2026 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002027static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002028{
2029 p->normal_prio = normal_prio(p);
2030 /*
2031 * If we are RT tasks or we were boosted to RT priority,
2032 * keep the priority unchanged. Otherwise, update priority
2033 * to the normal priority:
2034 */
2035 if (!rt_prio(p->prio))
2036 return p->normal_prio;
2037 return p->prio;
2038}
2039
2040/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002041 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002043static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002045 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002046 rq->nr_uninterruptible--;
2047
Ingo Molnar8159f872007-08-09 11:16:49 +02002048 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002049 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
2052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * deactivate_task - remove a task from the runqueue.
2054 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002055static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002057 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002058 rq->nr_uninterruptible++;
2059
Ingo Molnar69be72c2007-08-09 11:16:49 +02002060 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002061 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/**
2065 * task_curr - is this task currently executing on a CPU?
2066 * @p: the task in question.
2067 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002068inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 return cpu_curr(task_cpu(p)) == p;
2071}
2072
Peter Williams2dd73a42006-06-27 02:54:34 -07002073/* Used instead of source_load when we know the type == 0 */
2074unsigned long weighted_cpuload(const int cpu)
2075{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002076 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002077}
2078
2079static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2080{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01002081 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002082#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002083 /*
2084 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2085 * successfuly executed on another CPU. We must ensure that updates of
2086 * per-task data have been completed by this moment.
2087 */
2088 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002089 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002090#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002091}
2092
Steven Rostedtcb469842008-01-25 21:08:22 +01002093static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2094 const struct sched_class *prev_class,
2095 int oldprio, int running)
2096{
2097 if (prev_class != p->sched_class) {
2098 if (prev_class->switched_from)
2099 prev_class->switched_from(rq, p, running);
2100 p->sched_class->switched_to(rq, p, running);
2101 } else
2102 p->sched_class->prio_changed(rq, p, oldprio, running);
2103}
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002106
Ingo Molnarcc367732007-10-15 17:00:18 +02002107/*
2108 * Is this task likely cache-hot:
2109 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002110static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002111task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2112{
2113 s64 delta;
2114
Ingo Molnarf540a602008-03-15 17:10:34 +01002115 /*
2116 * Buddy candidates are cache hot:
2117 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002118 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002119 return 1;
2120
Ingo Molnarcc367732007-10-15 17:00:18 +02002121 if (p->sched_class != &fair_sched_class)
2122 return 0;
2123
Ingo Molnar6bc16652007-10-15 17:00:18 +02002124 if (sysctl_sched_migration_cost == -1)
2125 return 1;
2126 if (sysctl_sched_migration_cost == 0)
2127 return 0;
2128
Ingo Molnarcc367732007-10-15 17:00:18 +02002129 delta = now - p->se.exec_start;
2130
2131 return delta < (s64)sysctl_sched_migration_cost;
2132}
2133
2134
Ingo Molnardd41f592007-07-09 18:51:59 +02002135void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002136{
Ingo Molnardd41f592007-07-09 18:51:59 +02002137 int old_cpu = task_cpu(p);
2138 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002139 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2140 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002141 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002142
2143 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002144
2145#ifdef CONFIG_SCHEDSTATS
2146 if (p->se.wait_start)
2147 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002148 if (p->se.sleep_start)
2149 p->se.sleep_start -= clock_offset;
2150 if (p->se.block_start)
2151 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002152 if (old_cpu != new_cpu) {
2153 schedstat_inc(p, se.nr_migrations);
2154 if (task_hot(p, old_rq->clock, NULL))
2155 schedstat_inc(p, se.nr_forced2_migrations);
2156 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002157#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002158 p->se.vruntime -= old_cfsrq->min_vruntime -
2159 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002160
2161 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002162}
2163
Ingo Molnar70b97a72006-07-03 00:25:42 -07002164struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Ingo Molnar36c8b582006-07-03 00:25:41 -07002167 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int dest_cpu;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173/*
2174 * The task's runqueue lock must be held.
2175 * Returns true if you have to wait for migration thread.
2176 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002177static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002178migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002180 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /*
2183 * If the task is not on a runqueue (and not running), then
2184 * it is sufficient to simply update the task's cpu field.
2185 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002186 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 set_task_cpu(p, dest_cpu);
2188 return 0;
2189 }
2190
2191 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 req->task = p;
2193 req->dest_cpu = dest_cpu;
2194 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return 1;
2197}
2198
2199/*
2200 * wait_task_inactive - wait for a thread to unschedule.
2201 *
2202 * The caller must ensure that the task *will* unschedule sometime soon,
2203 * else this function might spin for a *long* time. This function can't
2204 * be called with interrupts off, or it may introduce deadlock with
2205 * smp_call_function() if an IPI is sent by the same process we are
2206 * waiting to become inactive.
2207 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002208void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002211 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002212 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Andi Kleen3a5c3592007-10-15 17:00:14 +02002214 for (;;) {
2215 /*
2216 * We do the initial early heuristics without holding
2217 * any task-queue locks at all. We'll only try to get
2218 * the runqueue lock when things look like they will
2219 * work out!
2220 */
2221 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002222
Andi Kleen3a5c3592007-10-15 17:00:14 +02002223 /*
2224 * If the task is actively running on another CPU
2225 * still, just relax and busy-wait without holding
2226 * any locks.
2227 *
2228 * NOTE! Since we don't hold any locks, it's not
2229 * even sure that "rq" stays as the right runqueue!
2230 * But we don't care, since "task_running()" will
2231 * return false if the runqueue has changed and p
2232 * is actually now running somewhere else!
2233 */
2234 while (task_running(rq, p))
2235 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002236
Andi Kleen3a5c3592007-10-15 17:00:14 +02002237 /*
2238 * Ok, time to look more closely! We need the rq
2239 * lock now, to be *sure*. If we're wrong, we'll
2240 * just go back and repeat.
2241 */
2242 rq = task_rq_lock(p, &flags);
2243 running = task_running(rq, p);
2244 on_rq = p->se.on_rq;
2245 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002246
Andi Kleen3a5c3592007-10-15 17:00:14 +02002247 /*
2248 * Was it really running after all now that we
2249 * checked with the proper locks actually held?
2250 *
2251 * Oops. Go back and try again..
2252 */
2253 if (unlikely(running)) {
2254 cpu_relax();
2255 continue;
2256 }
2257
2258 /*
2259 * It's not enough that it's not actively running,
2260 * it must be off the runqueue _entirely_, and not
2261 * preempted!
2262 *
2263 * So if it wa still runnable (but just not actively
2264 * running right now), it's preempted, and we should
2265 * yield - it could be a while.
2266 */
2267 if (unlikely(on_rq)) {
2268 schedule_timeout_uninterruptible(1);
2269 continue;
2270 }
2271
2272 /*
2273 * Ahh, all good. It wasn't running, and it wasn't
2274 * runnable, which means that it will never become
2275 * running in the future either. We're all done!
2276 */
2277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
2281/***
2282 * kick_process - kick a running thread to enter/exit the kernel
2283 * @p: the to-be-kicked thread
2284 *
2285 * Cause a process which is running on another CPU to enter
2286 * kernel-mode, without any delay. (to get signals handled.)
2287 *
2288 * NOTE: this function doesnt have to take the runqueue lock,
2289 * because all it wants to ensure is that the remote task enters
2290 * the kernel. If the IPI races and the task has been migrated
2291 * to another CPU then no harm is done and the purpose has been
2292 * achieved as well.
2293 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002294void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 int cpu;
2297
2298 preempt_disable();
2299 cpu = task_cpu(p);
2300 if ((cpu != smp_processor_id()) && task_curr(p))
2301 smp_send_reschedule(cpu);
2302 preempt_enable();
2303}
2304
2305/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002306 * Return a low guess at the load of a migration-source cpu weighted
2307 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 *
2309 * We want to under-estimate the load of migration sources, to
2310 * balance conservatively.
2311 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002312static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002313{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002314 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002315 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002316
Peter Williams2dd73a42006-06-27 02:54:34 -07002317 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002318 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002319
Ingo Molnardd41f592007-07-09 18:51:59 +02002320 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002324 * Return a high guess at the load of a migration-target cpu weighted
2325 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002327static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002328{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002329 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002330 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002331
Peter Williams2dd73a42006-06-27 02:54:34 -07002332 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002333 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002334
Ingo Molnardd41f592007-07-09 18:51:59 +02002335 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002336}
2337
2338/*
2339 * Return the average load per task on the cpu's run queue
2340 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002341static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002342{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002343 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002344 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002345 unsigned long n = rq->nr_running;
2346
Ingo Molnardd41f592007-07-09 18:51:59 +02002347 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Nick Piggin147cbb42005-06-25 14:57:19 -07002350/*
2351 * find_idlest_group finds and returns the least busy CPU group within the
2352 * domain.
2353 */
2354static struct sched_group *
2355find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2356{
2357 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2358 unsigned long min_load = ULONG_MAX, this_load = 0;
2359 int load_idx = sd->forkexec_idx;
2360 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2361
2362 do {
2363 unsigned long load, avg_load;
2364 int local_group;
2365 int i;
2366
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002367 /* Skip over this group if it has no CPUs allowed */
2368 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002369 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002370
Nick Piggin147cbb42005-06-25 14:57:19 -07002371 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002372
2373 /* Tally up the load of all CPUs in the group */
2374 avg_load = 0;
2375
2376 for_each_cpu_mask(i, group->cpumask) {
2377 /* Bias balancing toward cpus of our domain */
2378 if (local_group)
2379 load = source_load(i, load_idx);
2380 else
2381 load = target_load(i, load_idx);
2382
2383 avg_load += load;
2384 }
2385
2386 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002387 avg_load = sg_div_cpu_power(group,
2388 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002389
2390 if (local_group) {
2391 this_load = avg_load;
2392 this = group;
2393 } else if (avg_load < min_load) {
2394 min_load = avg_load;
2395 idlest = group;
2396 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002397 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002398
2399 if (!idlest || 100*this_load < imbalance*min_load)
2400 return NULL;
2401 return idlest;
2402}
2403
2404/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002405 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002406 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002407static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002408find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2409 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002410{
2411 unsigned long load, min_load = ULONG_MAX;
2412 int idlest = -1;
2413 int i;
2414
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002415 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002416 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002417
Mike Travis7c16ec52008-04-04 18:11:11 -07002418 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002419 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002420
2421 if (load < min_load || (load == min_load && i == this_cpu)) {
2422 min_load = load;
2423 idlest = i;
2424 }
2425 }
2426
2427 return idlest;
2428}
2429
Nick Piggin476d1392005-06-25 14:57:29 -07002430/*
2431 * sched_balance_self: balance the current task (running on cpu) in domains
2432 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2433 * SD_BALANCE_EXEC.
2434 *
2435 * Balance, ie. select the least loaded group.
2436 *
2437 * Returns the target CPU number, or the same CPU if no balancing is needed.
2438 *
2439 * preempt must be disabled.
2440 */
2441static int sched_balance_self(int cpu, int flag)
2442{
2443 struct task_struct *t = current;
2444 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002445
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002446 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002447 /*
2448 * If power savings logic is enabled for a domain, stop there.
2449 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002450 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2451 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002452 if (tmp->flags & flag)
2453 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002454 }
Nick Piggin476d1392005-06-25 14:57:29 -07002455
2456 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002457 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002458 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002459 int new_cpu, weight;
2460
2461 if (!(sd->flags & flag)) {
2462 sd = sd->child;
2463 continue;
2464 }
Nick Piggin476d1392005-06-25 14:57:29 -07002465
2466 span = sd->span;
2467 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002468 if (!group) {
2469 sd = sd->child;
2470 continue;
2471 }
Nick Piggin476d1392005-06-25 14:57:29 -07002472
Mike Travis7c16ec52008-04-04 18:11:11 -07002473 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002474 if (new_cpu == -1 || new_cpu == cpu) {
2475 /* Now try balancing at a lower domain level of cpu */
2476 sd = sd->child;
2477 continue;
2478 }
Nick Piggin476d1392005-06-25 14:57:29 -07002479
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002480 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002481 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002482 sd = NULL;
2483 weight = cpus_weight(span);
2484 for_each_domain(cpu, tmp) {
2485 if (weight <= cpus_weight(tmp->span))
2486 break;
2487 if (tmp->flags & flag)
2488 sd = tmp;
2489 }
2490 /* while loop will break here if sd == NULL */
2491 }
2492
2493 return cpu;
2494}
2495
2496#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498/***
2499 * try_to_wake_up - wake up a thread
2500 * @p: the to-be-woken-up thread
2501 * @state: the mask of task states that can be woken
2502 * @sync: do a synchronous wakeup?
2503 *
2504 * Put it on the run-queue if it's not already there. The "current"
2505 * thread is always on the run-queue (except when the actual
2506 * re-schedule is in progress), and as such you're allowed to do
2507 * the simpler "current->state = TASK_RUNNING" to mark yourself
2508 * runnable without the overhead of this.
2509 *
2510 * returns failure only if the task is already active.
2511 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002512static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Ingo Molnarcc367732007-10-15 17:00:18 +02002514 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 unsigned long flags;
2516 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002517 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Ingo Molnarb85d0662008-03-16 20:03:22 +01002519 if (!sched_feat(SYNC_WAKEUPS))
2520 sync = 0;
2521
Linus Torvalds04e2f172008-02-23 18:05:03 -08002522 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 rq = task_rq_lock(p, &flags);
2524 old_state = p->state;
2525 if (!(old_state & state))
2526 goto out;
2527
Ingo Molnardd41f592007-07-09 18:51:59 +02002528 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto out_running;
2530
2531 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002532 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 this_cpu = smp_processor_id();
2534
2535#ifdef CONFIG_SMP
2536 if (unlikely(task_running(rq, p)))
2537 goto out_activate;
2538
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002539 cpu = p->sched_class->select_task_rq(p, sync);
2540 if (cpu != orig_cpu) {
2541 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 task_rq_unlock(rq, &flags);
2543 /* might preempt at this point */
2544 rq = task_rq_lock(p, &flags);
2545 old_state = p->state;
2546 if (!(old_state & state))
2547 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002548 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 goto out_running;
2550
2551 this_cpu = smp_processor_id();
2552 cpu = task_cpu(p);
2553 }
2554
Gregory Haskinse7693a32008-01-25 21:08:09 +01002555#ifdef CONFIG_SCHEDSTATS
2556 schedstat_inc(rq, ttwu_count);
2557 if (cpu == this_cpu)
2558 schedstat_inc(rq, ttwu_local);
2559 else {
2560 struct sched_domain *sd;
2561 for_each_domain(this_cpu, sd) {
2562 if (cpu_isset(cpu, sd->span)) {
2563 schedstat_inc(sd, ttwu_wake_remote);
2564 break;
2565 }
2566 }
2567 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002568#endif
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570out_activate:
2571#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002572 schedstat_inc(p, se.nr_wakeups);
2573 if (sync)
2574 schedstat_inc(p, se.nr_wakeups_sync);
2575 if (orig_cpu != cpu)
2576 schedstat_inc(p, se.nr_wakeups_migrate);
2577 if (cpu == this_cpu)
2578 schedstat_inc(p, se.nr_wakeups_local);
2579 else
2580 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002581 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002582 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 success = 1;
2584
2585out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002586 check_preempt_curr(rq, p);
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002589#ifdef CONFIG_SMP
2590 if (p->sched_class->task_wake_up)
2591 p->sched_class->task_wake_up(rq, p);
2592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593out:
2594 task_rq_unlock(rq, &flags);
2595
2596 return success;
2597}
2598
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002599int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002601 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603EXPORT_SYMBOL(wake_up_process);
2604
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002605int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 return try_to_wake_up(p, state, 0);
2608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610/*
2611 * Perform scheduler related setup for a newly forked process p.
2612 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002613 *
2614 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002616static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Ingo Molnardd41f592007-07-09 18:51:59 +02002618 p->se.exec_start = 0;
2619 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002620 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002621 p->se.last_wakeup = 0;
2622 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002623
2624#ifdef CONFIG_SCHEDSTATS
2625 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002626 p->se.sum_sleep_runtime = 0;
2627 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002628 p->se.block_start = 0;
2629 p->se.sleep_max = 0;
2630 p->se.block_max = 0;
2631 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002632 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002633 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002634#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002635
Peter Zijlstrafa717062008-01-25 21:08:27 +01002636 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002637 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002638 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002639
Avi Kivitye107be32007-07-26 13:40:43 +02002640#ifdef CONFIG_PREEMPT_NOTIFIERS
2641 INIT_HLIST_HEAD(&p->preempt_notifiers);
2642#endif
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /*
2645 * We mark the process as running here, but have not actually
2646 * inserted it onto the runqueue yet. This guarantees that
2647 * nobody will actually run it, and a signal or other external
2648 * event cannot wake it up and insert it on the runqueue either.
2649 */
2650 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002651}
2652
2653/*
2654 * fork()/clone()-time setup:
2655 */
2656void sched_fork(struct task_struct *p, int clone_flags)
2657{
2658 int cpu = get_cpu();
2659
2660 __sched_fork(p);
2661
2662#ifdef CONFIG_SMP
2663 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2664#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002665 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002666
2667 /*
2668 * Make sure we do not leak PI boosting priority to the child:
2669 */
2670 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002671 if (!rt_prio(p->prio))
2672 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002673
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002674#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002675 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002676 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002678#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002679 p->oncpu = 0;
2680#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002682 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002683 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002685 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
2688/*
2689 * wake_up_new_task - wake up a newly created task for the first time.
2690 *
2691 * This function will do some initial scheduler statistics housekeeping
2692 * that must be done for every newly created context, then puts the task
2693 * on the runqueue and wakes it.
2694 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002695void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002698 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002702 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 p->prio = effective_prio(p);
2705
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002706 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002707 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002710 * Let the scheduling class do new task startup
2711 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002713 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002714 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002716 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002717#ifdef CONFIG_SMP
2718 if (p->sched_class->task_wake_up)
2719 p->sched_class->task_wake_up(rq, p);
2720#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002721 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Avi Kivitye107be32007-07-26 13:40:43 +02002724#ifdef CONFIG_PREEMPT_NOTIFIERS
2725
2726/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002727 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2728 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002729 */
2730void preempt_notifier_register(struct preempt_notifier *notifier)
2731{
2732 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2733}
2734EXPORT_SYMBOL_GPL(preempt_notifier_register);
2735
2736/**
2737 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002738 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002739 *
2740 * This is safe to call from within a preemption notifier.
2741 */
2742void preempt_notifier_unregister(struct preempt_notifier *notifier)
2743{
2744 hlist_del(&notifier->link);
2745}
2746EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2747
2748static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2749{
2750 struct preempt_notifier *notifier;
2751 struct hlist_node *node;
2752
2753 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2754 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2755}
2756
2757static void
2758fire_sched_out_preempt_notifiers(struct task_struct *curr,
2759 struct task_struct *next)
2760{
2761 struct preempt_notifier *notifier;
2762 struct hlist_node *node;
2763
2764 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2765 notifier->ops->sched_out(notifier, next);
2766}
2767
2768#else
2769
2770static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2771{
2772}
2773
2774static void
2775fire_sched_out_preempt_notifiers(struct task_struct *curr,
2776 struct task_struct *next)
2777{
2778}
2779
2780#endif
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002783 * prepare_task_switch - prepare to switch tasks
2784 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002785 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002786 * @next: the task we are going to switch to.
2787 *
2788 * This is called with the rq lock held and interrupts off. It must
2789 * be paired with a subsequent finish_task_switch after the context
2790 * switch.
2791 *
2792 * prepare_task_switch sets up locking and calls architecture specific
2793 * hooks.
2794 */
Avi Kivitye107be32007-07-26 13:40:43 +02002795static inline void
2796prepare_task_switch(struct rq *rq, struct task_struct *prev,
2797 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002798{
Avi Kivitye107be32007-07-26 13:40:43 +02002799 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002800 prepare_lock_switch(rq, next);
2801 prepare_arch_switch(next);
2802}
2803
2804/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002806 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 * @prev: the thread we just switched away from.
2808 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002809 * finish_task_switch must be called after the context switch, paired
2810 * with a prepare_task_switch call before the context switch.
2811 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2812 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *
2814 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002815 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * with the lock held can cause deadlocks; see schedule() for
2817 * details.)
2818 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002819static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 __releases(rq->lock)
2821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002823 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 rq->prev_mm = NULL;
2826
2827 /*
2828 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002829 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002830 * schedule one last time. The schedule call will never return, and
2831 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002832 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 * still held, otherwise prev could be scheduled on another cpu, die
2834 * there before we look at prev->state, and then the reference would
2835 * be dropped twice.
2836 * Manfred Spraul <manfred@colorfullife.com>
2837 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002838 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002839 finish_arch_switch(prev);
2840 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002841#ifdef CONFIG_SMP
2842 if (current->sched_class->post_schedule)
2843 current->sched_class->post_schedule(rq);
2844#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002845
Avi Kivitye107be32007-07-26 13:40:43 +02002846 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (mm)
2848 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002849 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002850 /*
2851 * Remove function-return probe instances associated with this
2852 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002853 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002854 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
2859/**
2860 * schedule_tail - first thing a freshly forked thread must call.
2861 * @prev: the thread we just switched away from.
2862 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002863asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 __releases(rq->lock)
2865{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002866 struct rq *rq = this_rq();
2867
Nick Piggin4866cde2005-06-25 14:57:23 -07002868 finish_task_switch(rq, prev);
2869#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2870 /* In this case, finish_task_switch does not reenable preemption */
2871 preempt_enable();
2872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002874 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877/*
2878 * context_switch - switch to the new MM and the new
2879 * thread's register state.
2880 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002881static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002882context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002883 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Ingo Molnardd41f592007-07-09 18:51:59 +02002885 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Avi Kivitye107be32007-07-26 13:40:43 +02002887 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002888 mm = next->mm;
2889 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002890 /*
2891 * For paravirt, this is coupled with an exit in switch_to to
2892 * combine the page table reload and the switch backend into
2893 * one hypercall.
2894 */
2895 arch_enter_lazy_cpu_mode();
2896
Ingo Molnardd41f592007-07-09 18:51:59 +02002897 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 next->active_mm = oldmm;
2899 atomic_inc(&oldmm->mm_count);
2900 enter_lazy_tlb(oldmm, next);
2901 } else
2902 switch_mm(oldmm, mm, next);
2903
Ingo Molnardd41f592007-07-09 18:51:59 +02002904 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 rq->prev_mm = oldmm;
2907 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002908 /*
2909 * Since the runqueue lock will be released by the next
2910 * task (which is an invalid locking op but in the case
2911 * of the scheduler it's an obvious special-case), so we
2912 * do an early lockdep release here:
2913 */
2914#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002915 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002916#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 /* Here we just switch the register state and the stack. */
2919 switch_to(prev, next, prev);
2920
Ingo Molnardd41f592007-07-09 18:51:59 +02002921 barrier();
2922 /*
2923 * this_rq must be evaluated again because prev may have moved
2924 * CPUs since it called schedule(), thus the 'rq' on its stack
2925 * frame will be invalid.
2926 */
2927 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930/*
2931 * nr_running, nr_uninterruptible and nr_context_switches:
2932 *
2933 * externally visible scheduler statistics: current number of runnable
2934 * threads, current number of uninterruptible-sleeping threads, total
2935 * number of context switches performed since bootup.
2936 */
2937unsigned long nr_running(void)
2938{
2939 unsigned long i, sum = 0;
2940
2941 for_each_online_cpu(i)
2942 sum += cpu_rq(i)->nr_running;
2943
2944 return sum;
2945}
2946
2947unsigned long nr_uninterruptible(void)
2948{
2949 unsigned long i, sum = 0;
2950
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002951 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 sum += cpu_rq(i)->nr_uninterruptible;
2953
2954 /*
2955 * Since we read the counters lockless, it might be slightly
2956 * inaccurate. Do not allow it to go below zero though:
2957 */
2958 if (unlikely((long)sum < 0))
2959 sum = 0;
2960
2961 return sum;
2962}
2963
2964unsigned long long nr_context_switches(void)
2965{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002966 int i;
2967 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002969 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 sum += cpu_rq(i)->nr_switches;
2971
2972 return sum;
2973}
2974
2975unsigned long nr_iowait(void)
2976{
2977 unsigned long i, sum = 0;
2978
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002979 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2981
2982 return sum;
2983}
2984
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002985unsigned long nr_active(void)
2986{
2987 unsigned long i, running = 0, uninterruptible = 0;
2988
2989 for_each_online_cpu(i) {
2990 running += cpu_rq(i)->nr_running;
2991 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2992 }
2993
2994 if (unlikely((long)uninterruptible < 0))
2995 uninterruptible = 0;
2996
2997 return running + uninterruptible;
2998}
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003001 * Update rq->cpu_load[] statistics. This function is usually called every
3002 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003003 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003004static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003005{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003006 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003007 int i, scale;
3008
3009 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003010
3011 /* Update our load: */
3012 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3013 unsigned long old_load, new_load;
3014
3015 /* scale is effectively 1 << i now, and >> i divides by scale */
3016
3017 old_load = this_rq->cpu_load[i];
3018 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003019 /*
3020 * Round up the averaging division if load is increasing. This
3021 * prevents us from getting stuck on 9 if the load is 10, for
3022 * example.
3023 */
3024 if (new_load > old_load)
3025 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003026 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3027 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003028}
3029
Ingo Molnardd41f592007-07-09 18:51:59 +02003030#ifdef CONFIG_SMP
3031
Ingo Molnar48f24c42006-07-03 00:25:40 -07003032/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 * double_rq_lock - safely lock two runqueues
3034 *
3035 * Note this does not disable interrupts like task_rq_lock,
3036 * you need to do so manually before calling.
3037 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003038static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 __acquires(rq1->lock)
3040 __acquires(rq2->lock)
3041{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003042 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (rq1 == rq2) {
3044 spin_lock(&rq1->lock);
3045 __acquire(rq2->lock); /* Fake it out ;) */
3046 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003047 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 spin_lock(&rq1->lock);
3049 spin_lock(&rq2->lock);
3050 } else {
3051 spin_lock(&rq2->lock);
3052 spin_lock(&rq1->lock);
3053 }
3054 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003055 update_rq_clock(rq1);
3056 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059/*
3060 * double_rq_unlock - safely unlock two runqueues
3061 *
3062 * Note this does not restore interrupts like task_rq_unlock,
3063 * you need to do so manually after calling.
3064 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003065static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 __releases(rq1->lock)
3067 __releases(rq2->lock)
3068{
3069 spin_unlock(&rq1->lock);
3070 if (rq1 != rq2)
3071 spin_unlock(&rq2->lock);
3072 else
3073 __release(rq2->lock);
3074}
3075
3076/*
3077 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3078 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003079static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 __releases(this_rq->lock)
3081 __acquires(busiest->lock)
3082 __acquires(this_rq->lock)
3083{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003084 int ret = 0;
3085
Kirill Korotaev054b9102006-12-10 02:20:11 -08003086 if (unlikely(!irqs_disabled())) {
3087 /* printk() doesn't work good under rq->lock */
3088 spin_unlock(&this_rq->lock);
3089 BUG_ON(1);
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003092 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 spin_unlock(&this_rq->lock);
3094 spin_lock(&busiest->lock);
3095 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003096 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 } else
3098 spin_lock(&busiest->lock);
3099 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
3103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 * If dest_cpu is allowed for this process, migrate the task to it.
3105 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003106 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 * the cpu_allowed mask is restored.
3108 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003109static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003111 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003113 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 rq = task_rq_lock(p, &flags);
3116 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3117 || unlikely(cpu_is_offline(dest_cpu)))
3118 goto out;
3119
3120 /* force the process onto the specified CPU */
3121 if (migrate_task(p, dest_cpu, &req)) {
3122 /* Need to wait for migration thread (might exit: take ref). */
3123 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 get_task_struct(mt);
3126 task_rq_unlock(rq, &flags);
3127 wake_up_process(mt);
3128 put_task_struct(mt);
3129 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return;
3132 }
3133out:
3134 task_rq_unlock(rq, &flags);
3135}
3136
3137/*
Nick Piggin476d1392005-06-25 14:57:29 -07003138 * sched_exec - execve() is a valuable balancing opportunity, because at
3139 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 */
3141void sched_exec(void)
3142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003144 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003146 if (new_cpu != this_cpu)
3147 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
3150/*
3151 * pull_task - move a task from a remote runqueue to the local runqueue.
3152 * Both runqueues must be locked.
3153 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003154static void pull_task(struct rq *src_rq, struct task_struct *p,
3155 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003157 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003159 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Note that idle threads have a prio of MAX_PRIO, for this test
3162 * to be always true for them.
3163 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003164 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
3167/*
3168 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3169 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003170static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003171int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003172 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003173 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175 /*
3176 * We do not migrate tasks that are:
3177 * 1) running (obviously), or
3178 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3179 * 3) are cache-hot on their current CPU.
3180 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003181 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3182 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003184 }
Nick Piggin81026792005-06-25 14:57:07 -07003185 *all_pinned = 0;
3186
Ingo Molnarcc367732007-10-15 17:00:18 +02003187 if (task_running(rq, p)) {
3188 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003189 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Ingo Molnarda84d962007-10-15 17:00:18 +02003192 /*
3193 * Aggressive migration if:
3194 * 1) task is cache cold, or
3195 * 2) too many balance attempts have failed.
3196 */
3197
Ingo Molnar6bc16652007-10-15 17:00:18 +02003198 if (!task_hot(p, rq->clock, sd) ||
3199 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003200#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003201 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003202 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003203 schedstat_inc(p, se.nr_forced_migrations);
3204 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003205#endif
3206 return 1;
3207 }
3208
Ingo Molnarcc367732007-10-15 17:00:18 +02003209 if (task_hot(p, rq->clock, sd)) {
3210 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003211 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return 1;
3214}
3215
Peter Williamse1d14842007-10-24 18:23:51 +02003216static unsigned long
3217balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3218 unsigned long max_load_move, struct sched_domain *sd,
3219 enum cpu_idle_type idle, int *all_pinned,
3220 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003221{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003222 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003223 struct task_struct *p;
3224 long rem_load_move = max_load_move;
3225
Peter Williamse1d14842007-10-24 18:23:51 +02003226 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003227 goto out;
3228
3229 pinned = 1;
3230
3231 /*
3232 * Start the load-balancing iterator:
3233 */
3234 p = iterator->start(iterator->arg);
3235next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003236 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003237 goto out;
3238 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003239 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003240 * skip a task if it will be the highest priority task (i.e. smallest
3241 * prio value) on its new queue regardless of its load weight
3242 */
3243 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3244 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003245 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003246 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003247 p = iterator->next(iterator->arg);
3248 goto next;
3249 }
3250
3251 pull_task(busiest, p, this_rq, this_cpu);
3252 pulled++;
3253 rem_load_move -= p->se.load.weight;
3254
3255 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003256 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003257 */
Peter Williamse1d14842007-10-24 18:23:51 +02003258 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003259 if (p->prio < *this_best_prio)
3260 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003261 p = iterator->next(iterator->arg);
3262 goto next;
3263 }
3264out:
3265 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003266 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003267 * so we can safely collect pull_task() stats here rather than
3268 * inside pull_task().
3269 */
3270 schedstat_add(sd, lb_gained[idle], pulled);
3271
3272 if (all_pinned)
3273 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003274
3275 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003276}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278/*
Peter Williams43010652007-08-09 11:16:46 +02003279 * move_tasks tries to move up to max_load_move weighted load from busiest to
3280 * this_rq, as part of a balancing operation within domain "sd".
3281 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 *
3283 * Called with both runqueues locked.
3284 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003285static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003286 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003287 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003288 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003290 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003291 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003292 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Ingo Molnardd41f592007-07-09 18:51:59 +02003294 do {
Peter Williams43010652007-08-09 11:16:46 +02003295 total_load_moved +=
3296 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003297 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003298 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003299 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003300 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Peter Williams43010652007-08-09 11:16:46 +02003302 return total_load_moved > 0;
3303}
3304
Peter Williamse1d14842007-10-24 18:23:51 +02003305static int
3306iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3307 struct sched_domain *sd, enum cpu_idle_type idle,
3308 struct rq_iterator *iterator)
3309{
3310 struct task_struct *p = iterator->start(iterator->arg);
3311 int pinned = 0;
3312
3313 while (p) {
3314 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3315 pull_task(busiest, p, this_rq, this_cpu);
3316 /*
3317 * Right now, this is only the second place pull_task()
3318 * is called, so we can safely collect pull_task()
3319 * stats here rather than inside pull_task().
3320 */
3321 schedstat_inc(sd, lb_gained[idle]);
3322
3323 return 1;
3324 }
3325 p = iterator->next(iterator->arg);
3326 }
3327
3328 return 0;
3329}
3330
Peter Williams43010652007-08-09 11:16:46 +02003331/*
3332 * move_one_task tries to move exactly one task from busiest to this_rq, as
3333 * part of active balancing operations within "domain".
3334 * Returns 1 if successful and 0 otherwise.
3335 *
3336 * Called with both runqueues locked.
3337 */
3338static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3339 struct sched_domain *sd, enum cpu_idle_type idle)
3340{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003341 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003342
3343 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003344 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003345 return 1;
3346
3347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
3350/*
3351 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003352 * domain. It calculates and returns the amount of weighted load which
3353 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 */
3355static struct sched_group *
3356find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003357 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003358 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3361 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003362 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003363 unsigned long busiest_load_per_task, busiest_nr_running;
3364 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003365 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003366#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3367 int power_savings_balance = 1;
3368 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3369 unsigned long min_nr_running = ULONG_MAX;
3370 struct sched_group *group_min = NULL, *group_leader = NULL;
3371#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003374 busiest_load_per_task = busiest_nr_running = 0;
3375 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003376 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003377 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003378 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003379 load_idx = sd->newidle_idx;
3380 else
3381 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003384 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 int local_group;
3386 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003387 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003388 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003389 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 local_group = cpu_isset(this_cpu, group->cpumask);
3392
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003393 if (local_group)
3394 balance_cpu = first_cpu(group->cpumask);
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003397 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003398 max_cpu_load = 0;
3399 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003402 struct rq *rq;
3403
3404 if (!cpu_isset(i, *cpus))
3405 continue;
3406
3407 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003408
Suresh Siddha9439aab2007-07-19 21:28:35 +02003409 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003410 *sd_idle = 0;
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003413 if (local_group) {
3414 if (idle_cpu(i) && !first_idle_cpu) {
3415 first_idle_cpu = 1;
3416 balance_cpu = i;
3417 }
3418
Nick Piggina2000572006-02-10 01:51:02 -08003419 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003420 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003421 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003422 if (load > max_cpu_load)
3423 max_cpu_load = load;
3424 if (min_cpu_load > load)
3425 min_cpu_load = load;
3426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003429 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003430 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003433 /*
3434 * First idle cpu or the first cpu(busiest) in this sched group
3435 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003436 * domains. In the newly idle case, we will allow all the cpu's
3437 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003438 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003439 if (idle != CPU_NEWLY_IDLE && local_group &&
3440 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003441 *balance = 0;
3442 goto ret;
3443 }
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003446 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003449 avg_load = sg_div_cpu_power(group,
3450 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Ken Chen908a7c12007-10-17 16:55:11 +02003452 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3453 __group_imb = 1;
3454
Eric Dumazet5517d862007-05-08 00:32:57 -07003455 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (local_group) {
3458 this_load = avg_load;
3459 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003460 this_nr_running = sum_nr_running;
3461 this_load_per_task = sum_weighted_load;
3462 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003463 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 max_load = avg_load;
3465 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003466 busiest_nr_running = sum_nr_running;
3467 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003468 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003470
3471#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3472 /*
3473 * Busy processors will not participate in power savings
3474 * balance.
3475 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003476 if (idle == CPU_NOT_IDLE ||
3477 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3478 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003479
3480 /*
3481 * If the local group is idle or completely loaded
3482 * no need to do power savings balance at this domain
3483 */
3484 if (local_group && (this_nr_running >= group_capacity ||
3485 !this_nr_running))
3486 power_savings_balance = 0;
3487
Ingo Molnardd41f592007-07-09 18:51:59 +02003488 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003489 * If a group is already running at full capacity or idle,
3490 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003491 */
3492 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003493 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003494 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003495
Ingo Molnardd41f592007-07-09 18:51:59 +02003496 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003497 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003498 * This is the group from where we need to pick up the load
3499 * for saving power
3500 */
3501 if ((sum_nr_running < min_nr_running) ||
3502 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003503 first_cpu(group->cpumask) <
3504 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003505 group_min = group;
3506 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003507 min_load_per_task = sum_weighted_load /
3508 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003509 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003510
Ingo Molnardd41f592007-07-09 18:51:59 +02003511 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003512 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003513 * capacity but still has some space to pick up some load
3514 * from other group and save more power
3515 */
3516 if (sum_nr_running <= group_capacity - 1) {
3517 if (sum_nr_running > leader_nr_running ||
3518 (sum_nr_running == leader_nr_running &&
3519 first_cpu(group->cpumask) >
3520 first_cpu(group_leader->cpumask))) {
3521 group_leader = group;
3522 leader_nr_running = sum_nr_running;
3523 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003524 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003525group_next:
3526#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 group = group->next;
3528 } while (group != sd->groups);
3529
Peter Williams2dd73a42006-06-27 02:54:34 -07003530 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 goto out_balanced;
3532
3533 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3534
3535 if (this_load >= avg_load ||
3536 100*max_load <= sd->imbalance_pct*this_load)
3537 goto out_balanced;
3538
Peter Williams2dd73a42006-06-27 02:54:34 -07003539 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003540 if (group_imb)
3541 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 /*
3544 * We're trying to get all the cpus to the average_load, so we don't
3545 * want to push ourselves above the average load, nor do we wish to
3546 * reduce the max loaded cpu below the average load, as either of these
3547 * actions would just result in more rebalancing later, and ping-pong
3548 * tasks around. Thus we look for the minimum possible imbalance.
3549 * Negative imbalances (*we* are more loaded than anyone else) will
3550 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003551 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 * appear as very large values with unsigned longs.
3553 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003554 if (max_load <= busiest_load_per_task)
3555 goto out_balanced;
3556
3557 /*
3558 * In the presence of smp nice balancing, certain scenarios can have
3559 * max load less than avg load(as we skip the groups at or below
3560 * its cpu_power, while calculating max_load..)
3561 */
3562 if (max_load < avg_load) {
3563 *imbalance = 0;
3564 goto small_imbalance;
3565 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003566
3567 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003568 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003571 *imbalance = min(max_pull * busiest->__cpu_power,
3572 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 / SCHED_LOAD_SCALE;
3574
Peter Williams2dd73a42006-06-27 02:54:34 -07003575 /*
3576 * if *imbalance is less than the average load per runnable task
3577 * there is no gaurantee that any tasks will be moved so we'll have
3578 * a think about bumping its value to force at least one task to be
3579 * moved
3580 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003581 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003582 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003583 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Peter Williams2dd73a42006-06-27 02:54:34 -07003585small_imbalance:
3586 pwr_move = pwr_now = 0;
3587 imbn = 2;
3588 if (this_nr_running) {
3589 this_load_per_task /= this_nr_running;
3590 if (busiest_load_per_task > this_load_per_task)
3591 imbn = 1;
3592 } else
3593 this_load_per_task = SCHED_LOAD_SCALE;
3594
Ingo Molnardd41f592007-07-09 18:51:59 +02003595 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3596 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003597 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return busiest;
3599 }
3600
3601 /*
3602 * OK, we don't have enough imbalance to justify moving tasks,
3603 * however we may be able to increase total CPU power used by
3604 * moving them.
3605 */
3606
Eric Dumazet5517d862007-05-08 00:32:57 -07003607 pwr_now += busiest->__cpu_power *
3608 min(busiest_load_per_task, max_load);
3609 pwr_now += this->__cpu_power *
3610 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 pwr_now /= SCHED_LOAD_SCALE;
3612
3613 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003614 tmp = sg_div_cpu_power(busiest,
3615 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003617 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003618 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003621 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003622 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003623 tmp = sg_div_cpu_power(this,
3624 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003626 tmp = sg_div_cpu_power(this,
3627 busiest_load_per_task * SCHED_LOAD_SCALE);
3628 pwr_move += this->__cpu_power *
3629 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 pwr_move /= SCHED_LOAD_SCALE;
3631
3632 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003633 if (pwr_move > pwr_now)
3634 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return busiest;
3638
3639out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003640#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003641 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003642 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003644 if (this == group_leader && group_leader != group_min) {
3645 *imbalance = min_load_per_task;
3646 return group_min;
3647 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003648#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003649ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 *imbalance = 0;
3651 return NULL;
3652}
3653
3654/*
3655 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3656 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003657static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003658find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003659 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003661 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003662 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 int i;
3664
3665 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003666 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003667
3668 if (!cpu_isset(i, *cpus))
3669 continue;
3670
Ingo Molnar48f24c42006-07-03 00:25:40 -07003671 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003672 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Ingo Molnardd41f592007-07-09 18:51:59 +02003674 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003675 continue;
3676
Ingo Molnardd41f592007-07-09 18:51:59 +02003677 if (wl > max_load) {
3678 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003679 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 }
3681 }
3682
3683 return busiest;
3684}
3685
3686/*
Nick Piggin77391d72005-06-25 14:57:30 -07003687 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3688 * so long as it is large enough.
3689 */
3690#define MAX_PINNED_INTERVAL 512
3691
3692/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3694 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003696static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003697 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003698 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Peter Williams43010652007-08-09 11:16:46 +02003700 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003703 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003704 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003705 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003706
Mike Travis7c16ec52008-04-04 18:11:11 -07003707 cpus_setall(*cpus);
3708
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003709 unlock_aggregate = get_aggregate(sd);
3710
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003711 /*
3712 * When power savings policy is enabled for the parent domain, idle
3713 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003714 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003715 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003716 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003717 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003718 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003719 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Ingo Molnar2d723762007-10-15 17:00:12 +02003721 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003723redo:
3724 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003725 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003726
Chen, Kenneth W06066712006-12-10 02:20:35 -08003727 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003728 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 if (!group) {
3731 schedstat_inc(sd, lb_nobusyg[idle]);
3732 goto out_balanced;
3733 }
3734
Mike Travis7c16ec52008-04-04 18:11:11 -07003735 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 if (!busiest) {
3737 schedstat_inc(sd, lb_nobusyq[idle]);
3738 goto out_balanced;
3739 }
3740
Nick Piggindb935db2005-06-25 14:57:11 -07003741 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 schedstat_add(sd, lb_imbalance[idle], imbalance);
3744
Peter Williams43010652007-08-09 11:16:46 +02003745 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 if (busiest->nr_running > 1) {
3747 /*
3748 * Attempt to move tasks. If find_busiest_group has found
3749 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003750 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 * correctly treated as an imbalance.
3752 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003753 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003754 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003755 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003756 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003757 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003758 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003759
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003760 /*
3761 * some other cpu did the load balance for us.
3762 */
Peter Williams43010652007-08-09 11:16:46 +02003763 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003764 resched_cpu(this_cpu);
3765
Nick Piggin81026792005-06-25 14:57:07 -07003766 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003767 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003768 cpu_clear(cpu_of(busiest), *cpus);
3769 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003770 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003771 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
Nick Piggin81026792005-06-25 14:57:07 -07003774
Peter Williams43010652007-08-09 11:16:46 +02003775 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 schedstat_inc(sd, lb_failed[idle]);
3777 sd->nr_balance_failed++;
3778
3779 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003781 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003782
3783 /* don't kick the migration_thread, if the curr
3784 * task on busiest cpu can't be moved to this_cpu
3785 */
3786 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003787 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003788 all_pinned = 1;
3789 goto out_one_pinned;
3790 }
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (!busiest->active_balance) {
3793 busiest->active_balance = 1;
3794 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003795 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003797 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003798 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 wake_up_process(busiest->migration_thread);
3800
3801 /*
3802 * We've kicked active balancing, reset the failure
3803 * counter.
3804 */
Nick Piggin39507452005-06-25 14:57:09 -07003805 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
Nick Piggin81026792005-06-25 14:57:07 -07003807 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 sd->nr_balance_failed = 0;
3809
Nick Piggin81026792005-06-25 14:57:07 -07003810 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 /* We were unbalanced, so reset the balancing interval */
3812 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003813 } else {
3814 /*
3815 * If we've begun active balancing, start to back off. This
3816 * case may not be covered by the all_pinned logic if there
3817 * is only 1 task on the busy runqueue (because we don't call
3818 * move_tasks).
3819 */
3820 if (sd->balance_interval < sd->max_interval)
3821 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823
Peter Williams43010652007-08-09 11:16:46 +02003824 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003825 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003826 ld_moved = -1;
3827
3828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 schedstat_inc(sd, lb_balanced[idle]);
3832
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003833 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003834
3835out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003837 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3838 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 sd->balance_interval *= 2;
3840
Ingo Molnar48f24c42006-07-03 00:25:40 -07003841 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003842 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003843 ld_moved = -1;
3844 else
3845 ld_moved = 0;
3846out:
3847 if (unlock_aggregate)
3848 put_aggregate(sd);
3849 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850}
3851
3852/*
3853 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3854 * tasks if there is an imbalance.
3855 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003856 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 * this_rq is locked.
3858 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003859static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003860load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3861 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
3863 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003864 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003866 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003867 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003868 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003869
3870 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003871
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003872 /*
3873 * When power savings policy is enabled for the parent domain, idle
3874 * sibling can pick up load irrespective of busy siblings. In this case,
3875 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003876 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003877 */
3878 if (sd->flags & SD_SHARE_CPUPOWER &&
3879 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003880 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Ingo Molnar2d723762007-10-15 17:00:12 +02003882 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003883redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003884 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003885 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003887 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003888 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
3890
Mike Travis7c16ec52008-04-04 18:11:11 -07003891 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003892 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003893 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003894 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
3896
Nick Piggindb935db2005-06-25 14:57:11 -07003897 BUG_ON(busiest == this_rq);
3898
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003899 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003900
Peter Williams43010652007-08-09 11:16:46 +02003901 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003902 if (busiest->nr_running > 1) {
3903 /* Attempt to move tasks */
3904 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003905 /* this_rq->clock is already updated */
3906 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003907 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003908 imbalance, sd, CPU_NEWLY_IDLE,
3909 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003910 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003911
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003912 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003913 cpu_clear(cpu_of(busiest), *cpus);
3914 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003915 goto redo;
3916 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003917 }
3918
Peter Williams43010652007-08-09 11:16:46 +02003919 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003920 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003921 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3922 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003923 return -1;
3924 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003925 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Peter Williams43010652007-08-09 11:16:46 +02003927 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003928
3929out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003930 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003931 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003932 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003933 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003934 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003935
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939/*
3940 * idle_balance is called by schedule() if this_cpu is about to become
3941 * idle. Attempts to pull tasks from other CPUs.
3942 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003943static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003946 int pulled_task = -1;
3947 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003948 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003951 unsigned long interval;
3952
3953 if (!(sd->flags & SD_LOAD_BALANCE))
3954 continue;
3955
3956 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003957 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003958 pulled_task = load_balance_newidle(this_cpu, this_rq,
3959 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003960
3961 interval = msecs_to_jiffies(sd->balance_interval);
3962 if (time_after(next_balance, sd->last_balance + interval))
3963 next_balance = sd->last_balance + interval;
3964 if (pulled_task)
3965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003967 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003968 /*
3969 * We are going idle. next_balance may be set based on
3970 * a busy processor. So reset next_balance.
3971 */
3972 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
3976/*
3977 * active_load_balance is run by migration threads. It pushes running tasks
3978 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3979 * running on each physical CPU where possible, and avoids physical /
3980 * logical imbalances.
3981 *
3982 * Called with busiest_rq locked.
3983 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003984static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Nick Piggin39507452005-06-25 14:57:09 -07003986 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003987 struct sched_domain *sd;
3988 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003989
Ingo Molnar48f24c42006-07-03 00:25:40 -07003990 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003991 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003992 return;
3993
3994 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 /*
Nick Piggin39507452005-06-25 14:57:09 -07003997 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003998 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003999 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 */
Nick Piggin39507452005-06-25 14:57:09 -07004001 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Nick Piggin39507452005-06-25 14:57:09 -07004003 /* move a task from busiest_rq to target_rq */
4004 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004005 update_rq_clock(busiest_rq);
4006 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Nick Piggin39507452005-06-25 14:57:09 -07004008 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004009 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004010 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004011 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07004012 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Ingo Molnar48f24c42006-07-03 00:25:40 -07004015 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004016 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Peter Williams43010652007-08-09 11:16:46 +02004018 if (move_one_task(target_rq, target_cpu, busiest_rq,
4019 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004020 schedstat_inc(sd, alb_pushed);
4021 else
4022 schedstat_inc(sd, alb_failed);
4023 }
Nick Piggin39507452005-06-25 14:57:09 -07004024 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004027#ifdef CONFIG_NO_HZ
4028static struct {
4029 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004030 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004031} nohz ____cacheline_aligned = {
4032 .load_balancer = ATOMIC_INIT(-1),
4033 .cpu_mask = CPU_MASK_NONE,
4034};
4035
Christoph Lameter7835b982006-12-10 02:20:22 -08004036/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004037 * This routine will try to nominate the ilb (idle load balancing)
4038 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4039 * load balancing on behalf of all those cpus. If all the cpus in the system
4040 * go into this tickless mode, then there will be no ilb owner (as there is
4041 * no need for one) and all the cpus will sleep till the next wakeup event
4042 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004043 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004044 * For the ilb owner, tick is not stopped. And this tick will be used
4045 * for idle load balancing. ilb owner will still be part of
4046 * nohz.cpu_mask..
4047 *
4048 * While stopping the tick, this cpu will become the ilb owner if there
4049 * is no other owner. And will be the owner till that cpu becomes busy
4050 * or if all cpus in the system stop their ticks at which point
4051 * there is no need for ilb owner.
4052 *
4053 * When the ilb owner becomes busy, it nominates another owner, during the
4054 * next busy scheduler_tick()
4055 */
4056int select_nohz_load_balancer(int stop_tick)
4057{
4058 int cpu = smp_processor_id();
4059
4060 if (stop_tick) {
4061 cpu_set(cpu, nohz.cpu_mask);
4062 cpu_rq(cpu)->in_nohz_recently = 1;
4063
4064 /*
4065 * If we are going offline and still the leader, give up!
4066 */
4067 if (cpu_is_offline(cpu) &&
4068 atomic_read(&nohz.load_balancer) == cpu) {
4069 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4070 BUG();
4071 return 0;
4072 }
4073
4074 /* time for ilb owner also to sleep */
4075 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4076 if (atomic_read(&nohz.load_balancer) == cpu)
4077 atomic_set(&nohz.load_balancer, -1);
4078 return 0;
4079 }
4080
4081 if (atomic_read(&nohz.load_balancer) == -1) {
4082 /* make me the ilb owner */
4083 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4084 return 1;
4085 } else if (atomic_read(&nohz.load_balancer) == cpu)
4086 return 1;
4087 } else {
4088 if (!cpu_isset(cpu, nohz.cpu_mask))
4089 return 0;
4090
4091 cpu_clear(cpu, nohz.cpu_mask);
4092
4093 if (atomic_read(&nohz.load_balancer) == cpu)
4094 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4095 BUG();
4096 }
4097 return 0;
4098}
4099#endif
4100
4101static DEFINE_SPINLOCK(balancing);
4102
4103/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004104 * It checks each scheduling domain to see if it is due to be balanced,
4105 * and initiates a balancing operation if so.
4106 *
4107 * Balancing parameters are set up in arch_init_sched_domains.
4108 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004109static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004110{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004111 int balance = 1;
4112 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004113 unsigned long interval;
4114 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004115 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004116 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004117 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004118 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004120 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 if (!(sd->flags & SD_LOAD_BALANCE))
4122 continue;
4123
4124 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004125 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 interval *= sd->busy_factor;
4127
4128 /* scale ms to jiffies */
4129 interval = msecs_to_jiffies(interval);
4130 if (unlikely(!interval))
4131 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004132 if (interval > HZ*NR_CPUS/10)
4133 interval = HZ*NR_CPUS/10;
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Christoph Lameter08c183f2006-12-10 02:20:29 -08004136 if (sd->flags & SD_SERIALIZE) {
4137 if (!spin_trylock(&balancing))
4138 goto out;
4139 }
4140
Christoph Lameterc9819f42006-12-10 02:20:25 -08004141 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004142 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004143 /*
4144 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004145 * longer idle, or one of our SMT siblings is
4146 * not idle.
4147 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004148 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004150 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004152 if (sd->flags & SD_SERIALIZE)
4153 spin_unlock(&balancing);
4154out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004155 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004156 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004157 update_next_balance = 1;
4158 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004159
4160 /*
4161 * Stop the load balance at this level. There is another
4162 * CPU in our sched group which is doing load balancing more
4163 * actively.
4164 */
4165 if (!balance)
4166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004168
4169 /*
4170 * next_balance will be updated only when there is a need.
4171 * When the cpu is attached to null domain for ex, it will not be
4172 * updated.
4173 */
4174 if (likely(update_next_balance))
4175 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004176}
4177
4178/*
4179 * run_rebalance_domains is triggered when needed from the scheduler tick.
4180 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4181 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4182 */
4183static void run_rebalance_domains(struct softirq_action *h)
4184{
Ingo Molnardd41f592007-07-09 18:51:59 +02004185 int this_cpu = smp_processor_id();
4186 struct rq *this_rq = cpu_rq(this_cpu);
4187 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4188 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004189
Ingo Molnardd41f592007-07-09 18:51:59 +02004190 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004191
4192#ifdef CONFIG_NO_HZ
4193 /*
4194 * If this cpu is the owner for idle load balancing, then do the
4195 * balancing on behalf of the other idle cpus whose ticks are
4196 * stopped.
4197 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004198 if (this_rq->idle_at_tick &&
4199 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004200 cpumask_t cpus = nohz.cpu_mask;
4201 struct rq *rq;
4202 int balance_cpu;
4203
Ingo Molnardd41f592007-07-09 18:51:59 +02004204 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004205 for_each_cpu_mask(balance_cpu, cpus) {
4206 /*
4207 * If this cpu gets work to do, stop the load balancing
4208 * work being done for other cpus. Next load
4209 * balancing owner will pick it up.
4210 */
4211 if (need_resched())
4212 break;
4213
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004214 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004215
4216 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004217 if (time_after(this_rq->next_balance, rq->next_balance))
4218 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004219 }
4220 }
4221#endif
4222}
4223
4224/*
4225 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4226 *
4227 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4228 * idle load balancing owner or decide to stop the periodic load balancing,
4229 * if the whole system is idle.
4230 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004231static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004232{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004233#ifdef CONFIG_NO_HZ
4234 /*
4235 * If we were in the nohz mode recently and busy at the current
4236 * scheduler tick, then check if we need to nominate new idle
4237 * load balancer.
4238 */
4239 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4240 rq->in_nohz_recently = 0;
4241
4242 if (atomic_read(&nohz.load_balancer) == cpu) {
4243 cpu_clear(cpu, nohz.cpu_mask);
4244 atomic_set(&nohz.load_balancer, -1);
4245 }
4246
4247 if (atomic_read(&nohz.load_balancer) == -1) {
4248 /*
4249 * simple selection for now: Nominate the
4250 * first cpu in the nohz list to be the next
4251 * ilb owner.
4252 *
4253 * TBD: Traverse the sched domains and nominate
4254 * the nearest cpu in the nohz.cpu_mask.
4255 */
4256 int ilb = first_cpu(nohz.cpu_mask);
4257
Mike Travis434d53b2008-04-04 18:11:04 -07004258 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004259 resched_cpu(ilb);
4260 }
4261 }
4262
4263 /*
4264 * If this cpu is idle and doing idle load balancing for all the
4265 * cpus with ticks stopped, is it time for that to stop?
4266 */
4267 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4268 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4269 resched_cpu(cpu);
4270 return;
4271 }
4272
4273 /*
4274 * If this cpu is idle and the idle load balancing is done by
4275 * someone else, then no need raise the SCHED_SOFTIRQ
4276 */
4277 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4278 cpu_isset(cpu, nohz.cpu_mask))
4279 return;
4280#endif
4281 if (time_after_eq(jiffies, rq->next_balance))
4282 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
Ingo Molnardd41f592007-07-09 18:51:59 +02004284
4285#else /* CONFIG_SMP */
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/*
4288 * on UP we do not need to balance between CPUs:
4289 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004290static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292}
Ingo Molnardd41f592007-07-09 18:51:59 +02004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294#endif
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296DEFINE_PER_CPU(struct kernel_stat, kstat);
4297
4298EXPORT_PER_CPU_SYMBOL(kstat);
4299
4300/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004301 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4302 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004304unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004307 u64 ns, delta_exec;
4308 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004309
Ingo Molnar41b86e92007-07-09 18:51:58 +02004310 rq = task_rq_lock(p, &flags);
4311 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004312 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004313 update_rq_clock(rq);
4314 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004315 if ((s64)delta_exec > 0)
4316 ns += delta_exec;
4317 }
4318 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return ns;
4321}
4322
4323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 * Account user cpu time to a process.
4325 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 * @cputime: the cpu time spent in user space since the last update
4327 */
4328void account_user_time(struct task_struct *p, cputime_t cputime)
4329{
4330 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4331 cputime64_t tmp;
4332
4333 p->utime = cputime_add(p->utime, cputime);
4334
4335 /* Add user time to cpustat. */
4336 tmp = cputime_to_cputime64(cputime);
4337 if (TASK_NICE(p) > 0)
4338 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4339 else
4340 cpustat->user = cputime64_add(cpustat->user, tmp);
4341}
4342
4343/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004344 * Account guest cpu time to a process.
4345 * @p: the process that the cpu time gets accounted to
4346 * @cputime: the cpu time spent in virtual machine since the last update
4347 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004348static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004349{
4350 cputime64_t tmp;
4351 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4352
4353 tmp = cputime_to_cputime64(cputime);
4354
4355 p->utime = cputime_add(p->utime, cputime);
4356 p->gtime = cputime_add(p->gtime, cputime);
4357
4358 cpustat->user = cputime64_add(cpustat->user, tmp);
4359 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4360}
4361
4362/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004363 * Account scaled user cpu time to a process.
4364 * @p: the process that the cpu time gets accounted to
4365 * @cputime: the cpu time spent in user space since the last update
4366 */
4367void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4368{
4369 p->utimescaled = cputime_add(p->utimescaled, cputime);
4370}
4371
4372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 * Account system cpu time to a process.
4374 * @p: the process that the cpu time gets accounted to
4375 * @hardirq_offset: the offset to subtract from hardirq_count()
4376 * @cputime: the cpu time spent in kernel space since the last update
4377 */
4378void account_system_time(struct task_struct *p, int hardirq_offset,
4379 cputime_t cputime)
4380{
4381 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004382 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 cputime64_t tmp;
4384
Christian Borntraeger97783852007-11-15 20:57:39 +01004385 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
4386 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 p->stime = cputime_add(p->stime, cputime);
4389
4390 /* Add system time to cpustat. */
4391 tmp = cputime_to_cputime64(cputime);
4392 if (hardirq_count() - hardirq_offset)
4393 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4394 else if (softirq_count())
4395 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004396 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004398 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4400 else
4401 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4402 /* Account for system time used */
4403 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
4406/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004407 * Account scaled system cpu time to a process.
4408 * @p: the process that the cpu time gets accounted to
4409 * @hardirq_offset: the offset to subtract from hardirq_count()
4410 * @cputime: the cpu time spent in kernel space since the last update
4411 */
4412void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4413{
4414 p->stimescaled = cputime_add(p->stimescaled, cputime);
4415}
4416
4417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 * Account for involuntary wait time.
4419 * @p: the process from which the cpu time has been stolen
4420 * @steal: the cpu time spent in involuntary wait
4421 */
4422void account_steal_time(struct task_struct *p, cputime_t steal)
4423{
4424 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4425 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004426 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 if (p == rq->idle) {
4429 p->stime = cputime_add(p->stime, steal);
4430 if (atomic_read(&rq->nr_iowait) > 0)
4431 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4432 else
4433 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004434 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4436}
4437
Christoph Lameter7835b982006-12-10 02:20:22 -08004438/*
4439 * This function gets called by the timer code, with HZ frequency.
4440 * We call it with interrupts disabled.
4441 *
4442 * It also gets called by the fork code, when changing the parent's
4443 * timeslices.
4444 */
4445void scheduler_tick(void)
4446{
Christoph Lameter7835b982006-12-10 02:20:22 -08004447 int cpu = smp_processor_id();
4448 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004449 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004450 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004451
Ingo Molnardd41f592007-07-09 18:51:59 +02004452 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004453 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004454 /*
4455 * Let rq->clock advance by at least TICK_NSEC:
4456 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004457 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004458 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004459 rq->clock_underflows++;
4460 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004461 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004462 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004463 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004464 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004465 spin_unlock(&rq->lock);
4466
Christoph Lametere418e1c2006-12-10 02:20:23 -08004467#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004468 rq->idle_at_tick = idle_cpu(cpu);
4469 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4474
Srinivasa Ds43627582008-02-23 15:24:04 -08004475void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
4477 /*
4478 * Underflow?
4479 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004480 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 preempt_count() += val;
4483 /*
4484 * Spinlock count overflowing soon?
4485 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004486 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4487 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489EXPORT_SYMBOL(add_preempt_count);
4490
Srinivasa Ds43627582008-02-23 15:24:04 -08004491void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 /*
4494 * Underflow?
4495 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004496 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 /*
4499 * Is the spinlock portion underflowing?
4500 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004501 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4502 !(preempt_count() & PREEMPT_MASK)))
4503 return;
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 preempt_count() -= val;
4506}
4507EXPORT_SYMBOL(sub_preempt_count);
4508
4509#endif
4510
4511/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004512 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004514static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515{
Satyam Sharma838225b2007-10-24 18:23:50 +02004516 struct pt_regs *regs = get_irq_regs();
4517
4518 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4519 prev->comm, prev->pid, preempt_count());
4520
Ingo Molnardd41f592007-07-09 18:51:59 +02004521 debug_show_held_locks(prev);
4522 if (irqs_disabled())
4523 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004524
4525 if (regs)
4526 show_regs(regs);
4527 else
4528 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Ingo Molnardd41f592007-07-09 18:51:59 +02004531/*
4532 * Various schedule()-time debugging checks and statistics:
4533 */
4534static inline void schedule_debug(struct task_struct *prev)
4535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004537 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 * schedule() atomically, we ignore that path for now.
4539 * Otherwise, whine if we are scheduling when we should not be.
4540 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004541 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4542 __schedule_bug(prev);
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4545
Ingo Molnar2d723762007-10-15 17:00:12 +02004546 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004547#ifdef CONFIG_SCHEDSTATS
4548 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004549 schedstat_inc(this_rq(), bkl_count);
4550 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004551 }
4552#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004553}
4554
4555/*
4556 * Pick up the highest-prio task:
4557 */
4558static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004559pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004560{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004561 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004562 struct task_struct *p;
4563
4564 /*
4565 * Optimization: we know that if all tasks are in
4566 * the fair class we can call that function directly:
4567 */
4568 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004569 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004570 if (likely(p))
4571 return p;
4572 }
4573
4574 class = sched_class_highest;
4575 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004576 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004577 if (p)
4578 return p;
4579 /*
4580 * Will never be NULL as the idle class always
4581 * returns a non-NULL p:
4582 */
4583 class = class->next;
4584 }
4585}
4586
4587/*
4588 * schedule() is the main scheduler function.
4589 */
4590asmlinkage void __sched schedule(void)
4591{
4592 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004593 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004594 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004595 int cpu;
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597need_resched:
4598 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004599 cpu = smp_processor_id();
4600 rq = cpu_rq(cpu);
4601 rcu_qsctr_inc(cpu);
4602 prev = rq->curr;
4603 switch_count = &prev->nivcsw;
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 release_kernel_lock(prev);
4606need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Ingo Molnardd41f592007-07-09 18:51:59 +02004608 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004610 hrtick_clear(rq);
4611
Ingo Molnar1e819952007-10-15 17:00:13 +02004612 /*
4613 * Do the rq-clock update outside the rq lock:
4614 */
4615 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004616 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004617 spin_lock(&rq->lock);
4618 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Ingo Molnardd41f592007-07-09 18:51:59 +02004620 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4621 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004622 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004623 prev->state = TASK_RUNNING;
4624 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004625 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004626 }
4627 switch_count = &prev->nvcsw;
4628 }
4629
Steven Rostedt9a897c52008-01-25 21:08:22 +01004630#ifdef CONFIG_SMP
4631 if (prev->sched_class->pre_schedule)
4632 prev->sched_class->pre_schedule(rq, prev);
4633#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004634
Ingo Molnardd41f592007-07-09 18:51:59 +02004635 if (unlikely(!rq->nr_running))
4636 idle_balance(cpu, rq);
4637
Ingo Molnar31ee5292007-08-09 11:16:49 +02004638 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004639 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 rq->nr_switches++;
4645 rq->curr = next;
4646 ++*switch_count;
4647
Ingo Molnardd41f592007-07-09 18:51:59 +02004648 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004649 /*
4650 * the context switch might have flipped the stack from under
4651 * us, hence refresh the local variables.
4652 */
4653 cpu = smp_processor_id();
4654 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 } else
4656 spin_unlock_irq(&rq->lock);
4657
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004658 hrtick_set(rq);
4659
4660 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 preempt_enable_no_resched();
4664 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4665 goto need_resched;
4666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667EXPORT_SYMBOL(schedule);
4668
4669#ifdef CONFIG_PREEMPT
4670/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004671 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004672 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 * occur there and call schedule directly.
4674 */
4675asmlinkage void __sched preempt_schedule(void)
4676{
4677 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 struct task_struct *task = current;
4679 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 /*
4682 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004683 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004685 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return;
4687
Andi Kleen3a5c3592007-10-15 17:00:14 +02004688 do {
4689 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Andi Kleen3a5c3592007-10-15 17:00:14 +02004691 /*
4692 * We keep the big kernel semaphore locked, but we
4693 * clear ->lock_depth so that schedule() doesnt
4694 * auto-release the semaphore:
4695 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004696 saved_lock_depth = task->lock_depth;
4697 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004698 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004699 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004700 sub_preempt_count(PREEMPT_ACTIVE);
4701
4702 /*
4703 * Check again in case we missed a preemption opportunity
4704 * between schedule and now.
4705 */
4706 barrier();
4707 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709EXPORT_SYMBOL(preempt_schedule);
4710
4711/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004712 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 * off of irq context.
4714 * Note, that this is called and return with irqs disabled. This will
4715 * protect us against recursive calling from irq.
4716 */
4717asmlinkage void __sched preempt_schedule_irq(void)
4718{
4719 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 struct task_struct *task = current;
4721 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004722
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004723 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 BUG_ON(ti->preempt_count || !irqs_disabled());
4725
Andi Kleen3a5c3592007-10-15 17:00:14 +02004726 do {
4727 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Andi Kleen3a5c3592007-10-15 17:00:14 +02004729 /*
4730 * We keep the big kernel semaphore locked, but we
4731 * clear ->lock_depth so that schedule() doesnt
4732 * auto-release the semaphore:
4733 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004734 saved_lock_depth = task->lock_depth;
4735 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004736 local_irq_enable();
4737 schedule();
4738 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004739 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004740 sub_preempt_count(PREEMPT_ACTIVE);
4741
4742 /*
4743 * Check again in case we missed a preemption opportunity
4744 * between schedule and now.
4745 */
4746 barrier();
4747 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
4750#endif /* CONFIG_PREEMPT */
4751
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004752int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4753 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004755 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757EXPORT_SYMBOL(default_wake_function);
4758
4759/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004760 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4761 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 * number) then we wake all the non-exclusive tasks and one exclusive task.
4763 *
4764 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004765 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4767 */
4768static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4769 int nr_exclusive, int sync, void *key)
4770{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004771 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004773 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004774 unsigned flags = curr->flags;
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004777 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 break;
4779 }
4780}
4781
4782/**
4783 * __wake_up - wake up threads blocked on a waitqueue.
4784 * @q: the waitqueue
4785 * @mode: which threads
4786 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004787 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004789void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004790 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
4792 unsigned long flags;
4793
4794 spin_lock_irqsave(&q->lock, flags);
4795 __wake_up_common(q, mode, nr_exclusive, 0, key);
4796 spin_unlock_irqrestore(&q->lock, flags);
4797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798EXPORT_SYMBOL(__wake_up);
4799
4800/*
4801 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4802 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004803void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
4805 __wake_up_common(q, mode, 1, 0, NULL);
4806}
4807
4808/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004809 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 * @q: the waitqueue
4811 * @mode: which threads
4812 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4813 *
4814 * The sync wakeup differs that the waker knows that it will schedule
4815 * away soon, so while the target thread will be woken up, it will not
4816 * be migrated to another CPU - ie. the two threads are 'synchronized'
4817 * with each other. This can prevent needless bouncing between CPUs.
4818 *
4819 * On UP it can prevent extra preemption.
4820 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004821void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004822__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
4824 unsigned long flags;
4825 int sync = 1;
4826
4827 if (unlikely(!q))
4828 return;
4829
4830 if (unlikely(!nr_exclusive))
4831 sync = 0;
4832
4833 spin_lock_irqsave(&q->lock, flags);
4834 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4835 spin_unlock_irqrestore(&q->lock, flags);
4836}
4837EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4838
Ingo Molnarb15136e2007-10-24 18:23:48 +02004839void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
4841 unsigned long flags;
4842
4843 spin_lock_irqsave(&x->wait.lock, flags);
4844 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004845 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 spin_unlock_irqrestore(&x->wait.lock, flags);
4847}
4848EXPORT_SYMBOL(complete);
4849
Ingo Molnarb15136e2007-10-24 18:23:48 +02004850void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 unsigned long flags;
4853
4854 spin_lock_irqsave(&x->wait.lock, flags);
4855 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004856 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 spin_unlock_irqrestore(&x->wait.lock, flags);
4858}
4859EXPORT_SYMBOL(complete_all);
4860
Andi Kleen8cbbe862007-10-15 17:00:14 +02004861static inline long __sched
4862do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 if (!x->done) {
4865 DECLARE_WAITQUEUE(wait, current);
4866
4867 wait.flags |= WQ_FLAG_EXCLUSIVE;
4868 __add_wait_queue_tail(&x->wait, &wait);
4869 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004870 if ((state == TASK_INTERRUPTIBLE &&
4871 signal_pending(current)) ||
4872 (state == TASK_KILLABLE &&
4873 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004874 __remove_wait_queue(&x->wait, &wait);
4875 return -ERESTARTSYS;
4876 }
4877 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004879 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004881 if (!timeout) {
4882 __remove_wait_queue(&x->wait, &wait);
4883 return timeout;
4884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 } while (!x->done);
4886 __remove_wait_queue(&x->wait, &wait);
4887 }
4888 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004889 return timeout;
4890}
4891
4892static long __sched
4893wait_for_common(struct completion *x, long timeout, int state)
4894{
4895 might_sleep();
4896
4897 spin_lock_irq(&x->wait.lock);
4898 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004900 return timeout;
4901}
4902
Ingo Molnarb15136e2007-10-24 18:23:48 +02004903void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004904{
4905 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907EXPORT_SYMBOL(wait_for_completion);
4908
Ingo Molnarb15136e2007-10-24 18:23:48 +02004909unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4911{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004912 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913}
4914EXPORT_SYMBOL(wait_for_completion_timeout);
4915
Andi Kleen8cbbe862007-10-15 17:00:14 +02004916int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Andi Kleen51e97992007-10-18 21:32:55 +02004918 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4919 if (t == -ERESTARTSYS)
4920 return t;
4921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923EXPORT_SYMBOL(wait_for_completion_interruptible);
4924
Ingo Molnarb15136e2007-10-24 18:23:48 +02004925unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926wait_for_completion_interruptible_timeout(struct completion *x,
4927 unsigned long timeout)
4928{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004929 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4932
Matthew Wilcox009e5772007-12-06 12:29:54 -05004933int __sched wait_for_completion_killable(struct completion *x)
4934{
4935 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4936 if (t == -ERESTARTSYS)
4937 return t;
4938 return 0;
4939}
4940EXPORT_SYMBOL(wait_for_completion_killable);
4941
Andi Kleen8cbbe862007-10-15 17:00:14 +02004942static long __sched
4943sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004944{
4945 unsigned long flags;
4946 wait_queue_t wait;
4947
4948 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Andi Kleen8cbbe862007-10-15 17:00:14 +02004950 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Andi Kleen8cbbe862007-10-15 17:00:14 +02004952 spin_lock_irqsave(&q->lock, flags);
4953 __add_wait_queue(q, &wait);
4954 spin_unlock(&q->lock);
4955 timeout = schedule_timeout(timeout);
4956 spin_lock_irq(&q->lock);
4957 __remove_wait_queue(q, &wait);
4958 spin_unlock_irqrestore(&q->lock, flags);
4959
4960 return timeout;
4961}
4962
4963void __sched interruptible_sleep_on(wait_queue_head_t *q)
4964{
4965 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967EXPORT_SYMBOL(interruptible_sleep_on);
4968
Ingo Molnar0fec1712007-07-09 18:52:01 +02004969long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004970interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004972 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4975
Ingo Molnar0fec1712007-07-09 18:52:01 +02004976void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004978 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980EXPORT_SYMBOL(sleep_on);
4981
Ingo Molnar0fec1712007-07-09 18:52:01 +02004982long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004984 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986EXPORT_SYMBOL(sleep_on_timeout);
4987
Ingo Molnarb29739f2006-06-27 02:54:51 -07004988#ifdef CONFIG_RT_MUTEXES
4989
4990/*
4991 * rt_mutex_setprio - set the current priority of a task
4992 * @p: task
4993 * @prio: prio value (kernel-internal form)
4994 *
4995 * This function changes the 'effective' priority of a task. It does
4996 * not touch ->normal_prio like __setscheduler().
4997 *
4998 * Used by the rt_mutex code to implement priority inheritance logic.
4999 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005000void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005001{
5002 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005003 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005004 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005005 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005006
5007 BUG_ON(prio < 0 || prio > MAX_PRIO);
5008
5009 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005010 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005011
Andrew Mortond5f9f942007-05-08 20:27:06 -07005012 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005013 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005014 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005015 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005016 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005017 if (running)
5018 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005019
5020 if (rt_prio(prio))
5021 p->sched_class = &rt_sched_class;
5022 else
5023 p->sched_class = &fair_sched_class;
5024
Ingo Molnarb29739f2006-06-27 02:54:51 -07005025 p->prio = prio;
5026
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005027 if (running)
5028 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005029 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005030 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005031
5032 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005033 }
5034 task_rq_unlock(rq, &flags);
5035}
5036
5037#endif
5038
Ingo Molnar36c8b582006-07-03 00:25:41 -07005039void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
Ingo Molnardd41f592007-07-09 18:51:59 +02005041 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005043 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5046 return;
5047 /*
5048 * We have to be careful, if called from sys_setpriority(),
5049 * the task might be in the middle of scheduling on another CPU.
5050 */
5051 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005052 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 /*
5054 * The RT priorities are set via sched_setscheduler(), but we still
5055 * allow the 'normal' nice value to be set - but as expected
5056 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005057 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005059 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 p->static_prio = NICE_TO_PRIO(nice);
5061 goto out_unlock;
5062 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005063 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005064 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005065 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005068 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005069 old_prio = p->prio;
5070 p->prio = effective_prio(p);
5071 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Ingo Molnardd41f592007-07-09 18:51:59 +02005073 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005074 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005076 * If the task increased its priority or is running and
5077 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005079 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 resched_task(rq->curr);
5081 }
5082out_unlock:
5083 task_rq_unlock(rq, &flags);
5084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085EXPORT_SYMBOL(set_user_nice);
5086
Matt Mackalle43379f2005-05-01 08:59:00 -07005087/*
5088 * can_nice - check if a task can reduce its nice value
5089 * @p: task
5090 * @nice: nice value
5091 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005092int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005093{
Matt Mackall024f4742005-08-18 11:24:19 -07005094 /* convert nice value [19,-20] to rlimit style value [1,40] */
5095 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005096
Matt Mackalle43379f2005-05-01 08:59:00 -07005097 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5098 capable(CAP_SYS_NICE));
5099}
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101#ifdef __ARCH_WANT_SYS_NICE
5102
5103/*
5104 * sys_nice - change the priority of the current process.
5105 * @increment: priority increment
5106 *
5107 * sys_setpriority is a more generic, but much slower function that
5108 * does similar things.
5109 */
5110asmlinkage long sys_nice(int increment)
5111{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005112 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 /*
5115 * Setpriority might change our priority at the same moment.
5116 * We don't have to worry. Conceptually one call occurs first
5117 * and we have a single winner.
5118 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005119 if (increment < -40)
5120 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 if (increment > 40)
5122 increment = 40;
5123
5124 nice = PRIO_TO_NICE(current->static_prio) + increment;
5125 if (nice < -20)
5126 nice = -20;
5127 if (nice > 19)
5128 nice = 19;
5129
Matt Mackalle43379f2005-05-01 08:59:00 -07005130 if (increment < 0 && !can_nice(current, nice))
5131 return -EPERM;
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 retval = security_task_setnice(current, nice);
5134 if (retval)
5135 return retval;
5136
5137 set_user_nice(current, nice);
5138 return 0;
5139}
5140
5141#endif
5142
5143/**
5144 * task_prio - return the priority value of a given task.
5145 * @p: the task in question.
5146 *
5147 * This is the priority value as seen by users in /proc.
5148 * RT tasks are offset by -200. Normal tasks are centered
5149 * around 0, value goes from -16 to +15.
5150 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005151int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
5153 return p->prio - MAX_RT_PRIO;
5154}
5155
5156/**
5157 * task_nice - return the nice value of a given task.
5158 * @p: the task in question.
5159 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005160int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161{
5162 return TASK_NICE(p);
5163}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005164EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166/**
5167 * idle_cpu - is a given cpu idle currently?
5168 * @cpu: the processor in question.
5169 */
5170int idle_cpu(int cpu)
5171{
5172 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5173}
5174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175/**
5176 * idle_task - return the idle task for a given cpu.
5177 * @cpu: the processor in question.
5178 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005179struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
5181 return cpu_rq(cpu)->idle;
5182}
5183
5184/**
5185 * find_process_by_pid - find a process with a matching PID value.
5186 * @pid: the pid in question.
5187 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005188static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005190 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
5193/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005194static void
5195__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196{
Ingo Molnardd41f592007-07-09 18:51:59 +02005197 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005200 switch (p->policy) {
5201 case SCHED_NORMAL:
5202 case SCHED_BATCH:
5203 case SCHED_IDLE:
5204 p->sched_class = &fair_sched_class;
5205 break;
5206 case SCHED_FIFO:
5207 case SCHED_RR:
5208 p->sched_class = &rt_sched_class;
5209 break;
5210 }
5211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005213 p->normal_prio = normal_prio(p);
5214 /* we are holding p->pi_lock already */
5215 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005216 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217}
5218
5219/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005220 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 * @p: the task in question.
5222 * @policy: new policy.
5223 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005224 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005225 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005227int sched_setscheduler(struct task_struct *p, int policy,
5228 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005230 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005232 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005233 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
Steven Rostedt66e53932006-06-27 02:54:44 -07005235 /* may grab non-irq protected spin_locks */
5236 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237recheck:
5238 /* double check policy once rq lock held */
5239 if (policy < 0)
5240 policy = oldpolicy = p->policy;
5241 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005242 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5243 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005244 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 /*
5246 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005247 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5248 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 */
5250 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005251 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005252 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005254 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return -EINVAL;
5256
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005257 /*
5258 * Allow unprivileged RT tasks to decrease priority:
5259 */
5260 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005261 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005262 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005263
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005264 if (!lock_task_sighand(p, &flags))
5265 return -ESRCH;
5266 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5267 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005268
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005269 /* can't set/change the rt policy */
5270 if (policy != p->policy && !rlim_rtprio)
5271 return -EPERM;
5272
5273 /* can't increase priority */
5274 if (param->sched_priority > p->rt_priority &&
5275 param->sched_priority > rlim_rtprio)
5276 return -EPERM;
5277 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005278 /*
5279 * Like positive nice levels, dont allow tasks to
5280 * move out of SCHED_IDLE either:
5281 */
5282 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5283 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005284
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005285 /* can't change other user's priorities */
5286 if ((current->euid != p->euid) &&
5287 (current->euid != p->uid))
5288 return -EPERM;
5289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005291#ifdef CONFIG_RT_GROUP_SCHED
5292 /*
5293 * Do not allow realtime tasks into groups that have no runtime
5294 * assigned.
5295 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005296 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005297 return -EPERM;
5298#endif
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 retval = security_task_setscheduler(p, policy, param);
5301 if (retval)
5302 return retval;
5303 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005304 * make sure no PI-waiters arrive (or leave) while we are
5305 * changing the priority of the task:
5306 */
5307 spin_lock_irqsave(&p->pi_lock, flags);
5308 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 * To be able to change p->policy safely, the apropriate
5310 * runqueue lock must be held.
5311 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005312 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 /* recheck policy now with rq lock held */
5314 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5315 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005316 __task_rq_unlock(rq);
5317 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 goto recheck;
5319 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005320 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005321 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005322 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005323 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005324 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005325 if (running)
5326 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005327
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005329 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005330
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005331 if (running)
5332 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005333 if (on_rq) {
5334 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005335
5336 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005338 __task_rq_unlock(rq);
5339 spin_unlock_irqrestore(&p->pi_lock, flags);
5340
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005341 rt_mutex_adjust_pi(p);
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 return 0;
5344}
5345EXPORT_SYMBOL_GPL(sched_setscheduler);
5346
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005347static int
5348do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 struct sched_param lparam;
5351 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005352 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
5354 if (!param || pid < 0)
5355 return -EINVAL;
5356 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5357 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005358
5359 rcu_read_lock();
5360 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005362 if (p != NULL)
5363 retval = sched_setscheduler(p, policy, &lparam);
5364 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return retval;
5367}
5368
5369/**
5370 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5371 * @pid: the pid in question.
5372 * @policy: new policy.
5373 * @param: structure containing the new RT priority.
5374 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005375asmlinkage long
5376sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377{
Jason Baronc21761f2006-01-18 17:43:03 -08005378 /* negative values for policy are not valid */
5379 if (policy < 0)
5380 return -EINVAL;
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return do_sched_setscheduler(pid, policy, param);
5383}
5384
5385/**
5386 * sys_sched_setparam - set/change the RT priority of a thread
5387 * @pid: the pid in question.
5388 * @param: structure containing the new RT priority.
5389 */
5390asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5391{
5392 return do_sched_setscheduler(pid, -1, param);
5393}
5394
5395/**
5396 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5397 * @pid: the pid in question.
5398 */
5399asmlinkage long sys_sched_getscheduler(pid_t pid)
5400{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005401 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005402 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
5404 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 retval = -ESRCH;
5408 read_lock(&tasklist_lock);
5409 p = find_process_by_pid(pid);
5410 if (p) {
5411 retval = security_task_getscheduler(p);
5412 if (!retval)
5413 retval = p->policy;
5414 }
5415 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return retval;
5417}
5418
5419/**
5420 * sys_sched_getscheduler - get the RT priority of a thread
5421 * @pid: the pid in question.
5422 * @param: structure containing the RT priority.
5423 */
5424asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5425{
5426 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005427 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005428 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 read_lock(&tasklist_lock);
5434 p = find_process_by_pid(pid);
5435 retval = -ESRCH;
5436 if (!p)
5437 goto out_unlock;
5438
5439 retval = security_task_getscheduler(p);
5440 if (retval)
5441 goto out_unlock;
5442
5443 lp.sched_priority = p->rt_priority;
5444 read_unlock(&tasklist_lock);
5445
5446 /*
5447 * This one might sleep, we cannot do it with a spinlock held ...
5448 */
5449 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 return retval;
5452
5453out_unlock:
5454 read_unlock(&tasklist_lock);
5455 return retval;
5456}
5457
Mike Travisb53e9212008-04-04 18:11:08 -07005458long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005461 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005462 struct task_struct *p;
5463 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005465 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 read_lock(&tasklist_lock);
5467
5468 p = find_process_by_pid(pid);
5469 if (!p) {
5470 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005471 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 return -ESRCH;
5473 }
5474
5475 /*
5476 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005477 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 * usage count and then drop tasklist_lock.
5479 */
5480 get_task_struct(p);
5481 read_unlock(&tasklist_lock);
5482
5483 retval = -EPERM;
5484 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5485 !capable(CAP_SYS_NICE))
5486 goto out_unlock;
5487
David Quigleye7834f82006-06-23 02:03:59 -07005488 retval = security_task_setscheduler(p, 0, NULL);
5489 if (retval)
5490 goto out_unlock;
5491
Mike Travisf9a86fc2008-04-04 18:11:07 -07005492 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005494 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005495 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Paul Menage8707d8b2007-10-18 23:40:22 -07005497 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005498 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005499 if (!cpus_subset(new_mask, cpus_allowed)) {
5500 /*
5501 * We must have raced with a concurrent cpuset
5502 * update. Just reset the cpus_allowed to the
5503 * cpuset's cpus_allowed
5504 */
5505 new_mask = cpus_allowed;
5506 goto again;
5507 }
5508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509out_unlock:
5510 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005511 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 return retval;
5513}
5514
5515static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5516 cpumask_t *new_mask)
5517{
5518 if (len < sizeof(cpumask_t)) {
5519 memset(new_mask, 0, sizeof(cpumask_t));
5520 } else if (len > sizeof(cpumask_t)) {
5521 len = sizeof(cpumask_t);
5522 }
5523 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5524}
5525
5526/**
5527 * sys_sched_setaffinity - set the cpu affinity of a process
5528 * @pid: pid of the process
5529 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5530 * @user_mask_ptr: user-space pointer to the new cpu mask
5531 */
5532asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5533 unsigned long __user *user_mask_ptr)
5534{
5535 cpumask_t new_mask;
5536 int retval;
5537
5538 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5539 if (retval)
5540 return retval;
5541
Mike Travisb53e9212008-04-04 18:11:08 -07005542 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
5545/*
5546 * Represents all cpu's present in the system
5547 * In systems capable of hotplug, this map could dynamically grow
5548 * as new cpu's are detected in the system via any platform specific
5549 * method, such as ACPI for e.g.
5550 */
5551
Andi Kleen4cef0c62006-01-11 22:44:57 +01005552cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553EXPORT_SYMBOL(cpu_present_map);
5554
5555#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005556cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005557EXPORT_SYMBOL(cpu_online_map);
5558
Andi Kleen4cef0c62006-01-11 22:44:57 +01005559cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005560EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561#endif
5562
5563long sched_getaffinity(pid_t pid, cpumask_t *mask)
5564{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005565 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005568 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 read_lock(&tasklist_lock);
5570
5571 retval = -ESRCH;
5572 p = find_process_by_pid(pid);
5573 if (!p)
5574 goto out_unlock;
5575
David Quigleye7834f82006-06-23 02:03:59 -07005576 retval = security_task_getscheduler(p);
5577 if (retval)
5578 goto out_unlock;
5579
Jack Steiner2f7016d2006-02-01 03:05:18 -08005580 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
5582out_unlock:
5583 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005584 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Ulrich Drepper9531b622007-08-09 11:16:46 +02005586 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
5589/**
5590 * sys_sched_getaffinity - get the cpu affinity of a process
5591 * @pid: pid of the process
5592 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5593 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5594 */
5595asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5596 unsigned long __user *user_mask_ptr)
5597{
5598 int ret;
5599 cpumask_t mask;
5600
5601 if (len < sizeof(cpumask_t))
5602 return -EINVAL;
5603
5604 ret = sched_getaffinity(pid, &mask);
5605 if (ret < 0)
5606 return ret;
5607
5608 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5609 return -EFAULT;
5610
5611 return sizeof(cpumask_t);
5612}
5613
5614/**
5615 * sys_sched_yield - yield the current processor to other threads.
5616 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005617 * This function yields the current CPU to other tasks. If there are no
5618 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 */
5620asmlinkage long sys_sched_yield(void)
5621{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005622 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Ingo Molnar2d723762007-10-15 17:00:12 +02005624 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005625 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 /*
5628 * Since we are going to call schedule() anyway, there's
5629 * no need to preempt or enable interrupts:
5630 */
5631 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005632 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 _raw_spin_unlock(&rq->lock);
5634 preempt_enable_no_resched();
5635
5636 schedule();
5637
5638 return 0;
5639}
5640
Andrew Mortone7b38402006-06-30 01:56:00 -07005641static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005643#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5644 __might_sleep(__FILE__, __LINE__);
5645#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005646 /*
5647 * The BKS might be reacquired before we have dropped
5648 * PREEMPT_ACTIVE, which could trigger a second
5649 * cond_resched() call.
5650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 do {
5652 add_preempt_count(PREEMPT_ACTIVE);
5653 schedule();
5654 sub_preempt_count(PREEMPT_ACTIVE);
5655 } while (need_resched());
5656}
5657
Herbert Xu02b67cc32008-01-25 21:08:28 +01005658#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5659int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660{
Ingo Molnar94142322006-12-29 16:48:13 -08005661 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5662 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 __cond_resched();
5664 return 1;
5665 }
5666 return 0;
5667}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005668EXPORT_SYMBOL(_cond_resched);
5669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671/*
5672 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5673 * call schedule, and on return reacquire the lock.
5674 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005675 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 * operations here to prevent schedule() from being called twice (once via
5677 * spin_unlock(), once by hand).
5678 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005679int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
Nick Piggin95c354f2008-01-30 13:31:20 +01005681 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005682 int ret = 0;
5683
Nick Piggin95c354f2008-01-30 13:31:20 +01005684 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005686 if (resched && need_resched())
5687 __cond_resched();
5688 else
5689 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005690 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005693 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695EXPORT_SYMBOL(cond_resched_lock);
5696
5697int __sched cond_resched_softirq(void)
5698{
5699 BUG_ON(!in_softirq());
5700
Ingo Molnar94142322006-12-29 16:48:13 -08005701 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005702 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 __cond_resched();
5704 local_bh_disable();
5705 return 1;
5706 }
5707 return 0;
5708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709EXPORT_SYMBOL(cond_resched_softirq);
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711/**
5712 * yield - yield the current processor to other threads.
5713 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005714 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 * thread runnable and calls sys_sched_yield().
5716 */
5717void __sched yield(void)
5718{
5719 set_current_state(TASK_RUNNING);
5720 sys_sched_yield();
5721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722EXPORT_SYMBOL(yield);
5723
5724/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005725 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 * that process accounting knows that this is a task in IO wait state.
5727 *
5728 * But don't do that if it is a deliberate, throttling IO wait (this task
5729 * has set its backing_dev_info: the queue against which it should throttle)
5730 */
5731void __sched io_schedule(void)
5732{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005733 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005735 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 atomic_inc(&rq->nr_iowait);
5737 schedule();
5738 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005739 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741EXPORT_SYMBOL(io_schedule);
5742
5743long __sched io_schedule_timeout(long timeout)
5744{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005745 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 long ret;
5747
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005748 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 atomic_inc(&rq->nr_iowait);
5750 ret = schedule_timeout(timeout);
5751 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005752 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 return ret;
5754}
5755
5756/**
5757 * sys_sched_get_priority_max - return maximum RT priority.
5758 * @policy: scheduling class.
5759 *
5760 * this syscall returns the maximum rt_priority that can be used
5761 * by a given scheduling class.
5762 */
5763asmlinkage long sys_sched_get_priority_max(int policy)
5764{
5765 int ret = -EINVAL;
5766
5767 switch (policy) {
5768 case SCHED_FIFO:
5769 case SCHED_RR:
5770 ret = MAX_USER_RT_PRIO-1;
5771 break;
5772 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005773 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005774 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 ret = 0;
5776 break;
5777 }
5778 return ret;
5779}
5780
5781/**
5782 * sys_sched_get_priority_min - return minimum RT priority.
5783 * @policy: scheduling class.
5784 *
5785 * this syscall returns the minimum rt_priority that can be used
5786 * by a given scheduling class.
5787 */
5788asmlinkage long sys_sched_get_priority_min(int policy)
5789{
5790 int ret = -EINVAL;
5791
5792 switch (policy) {
5793 case SCHED_FIFO:
5794 case SCHED_RR:
5795 ret = 1;
5796 break;
5797 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005798 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005799 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 ret = 0;
5801 }
5802 return ret;
5803}
5804
5805/**
5806 * sys_sched_rr_get_interval - return the default timeslice of a process.
5807 * @pid: pid of the process.
5808 * @interval: userspace pointer to the timeslice value.
5809 *
5810 * this syscall writes the default timeslice value of a given process
5811 * into the user-space timespec buffer. A value of '0' means infinity.
5812 */
5813asmlinkage
5814long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5815{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005816 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005817 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005818 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 retval = -ESRCH;
5825 read_lock(&tasklist_lock);
5826 p = find_process_by_pid(pid);
5827 if (!p)
5828 goto out_unlock;
5829
5830 retval = security_task_getscheduler(p);
5831 if (retval)
5832 goto out_unlock;
5833
Ingo Molnar77034932007-12-04 17:04:39 +01005834 /*
5835 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5836 * tasks that are on an otherwise idle runqueue:
5837 */
5838 time_slice = 0;
5839 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005840 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005841 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005842 struct sched_entity *se = &p->se;
5843 unsigned long flags;
5844 struct rq *rq;
5845
5846 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005847 if (rq->cfs.load.weight)
5848 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005849 task_rq_unlock(rq, &flags);
5850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005852 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856out_unlock:
5857 read_unlock(&tasklist_lock);
5858 return retval;
5859}
5860
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005861static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005862
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005863void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005866 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005869 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005870 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005871#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005873 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005875 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876#else
5877 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005878 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005880 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881#endif
5882#ifdef CONFIG_DEBUG_STACK_USAGE
5883 {
Al Viro10ebffd2005-11-13 16:06:56 -08005884 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 while (!*n)
5886 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005887 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
5889#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005890 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005891 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005893 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894}
5895
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005896void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005898 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Ingo Molnar4bd77322007-07-11 21:21:47 +02005900#if BITS_PER_LONG == 32
5901 printk(KERN_INFO
5902 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005904 printk(KERN_INFO
5905 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906#endif
5907 read_lock(&tasklist_lock);
5908 do_each_thread(g, p) {
5909 /*
5910 * reset the NMI-timeout, listing all files on a slow
5911 * console might take alot of time:
5912 */
5913 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005914 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005915 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 } while_each_thread(g, p);
5917
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005918 touch_all_softlockup_watchdogs();
5919
Ingo Molnardd41f592007-07-09 18:51:59 +02005920#ifdef CONFIG_SCHED_DEBUG
5921 sysrq_sched_debug_show();
5922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005924 /*
5925 * Only show locks if all tasks are dumped:
5926 */
5927 if (state_filter == -1)
5928 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929}
5930
Ingo Molnar1df21052007-07-09 18:51:58 +02005931void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5932{
Ingo Molnardd41f592007-07-09 18:51:59 +02005933 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005934}
5935
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005936/**
5937 * init_idle - set up an idle thread for a given CPU
5938 * @idle: task in question
5939 * @cpu: cpu the idle task belongs to
5940 *
5941 * NOTE: this function does not set the idle thread's NEED_RESCHED
5942 * flag, to make booting more robust.
5943 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005944void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005946 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 unsigned long flags;
5948
Ingo Molnardd41f592007-07-09 18:51:59 +02005949 __sched_fork(idle);
5950 idle->se.exec_start = sched_clock();
5951
Ingo Molnarb29739f2006-06-27 02:54:51 -07005952 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005954 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
5956 spin_lock_irqsave(&rq->lock, flags);
5957 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005958#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5959 idle->oncpu = 1;
5960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 spin_unlock_irqrestore(&rq->lock, flags);
5962
5963 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005964 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005965
Ingo Molnardd41f592007-07-09 18:51:59 +02005966 /*
5967 * The idle tasks have their own, simple scheduling class:
5968 */
5969 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
5972/*
5973 * In a system that switches off the HZ timer nohz_cpu_mask
5974 * indicates which cpus entered this state. This is used
5975 * in the rcu update to wait only for active cpus. For system
5976 * which do not switch off the HZ timer nohz_cpu_mask should
5977 * always be CPU_MASK_NONE.
5978 */
5979cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5980
Ingo Molnar19978ca2007-11-09 22:39:38 +01005981/*
5982 * Increase the granularity value when there are more CPUs,
5983 * because with more CPUs the 'effective latency' as visible
5984 * to users decreases. But the relationship is not linear,
5985 * so pick a second-best guess by going with the log2 of the
5986 * number of CPUs.
5987 *
5988 * This idea comes from the SD scheduler of Con Kolivas:
5989 */
5990static inline void sched_init_granularity(void)
5991{
5992 unsigned int factor = 1 + ilog2(num_online_cpus());
5993 const unsigned long limit = 200000000;
5994
5995 sysctl_sched_min_granularity *= factor;
5996 if (sysctl_sched_min_granularity > limit)
5997 sysctl_sched_min_granularity = limit;
5998
5999 sysctl_sched_latency *= factor;
6000 if (sysctl_sched_latency > limit)
6001 sysctl_sched_latency = limit;
6002
6003 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006004}
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006#ifdef CONFIG_SMP
6007/*
6008 * This is how migration works:
6009 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006010 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 * runqueue and wake up that CPU's migration thread.
6012 * 2) we down() the locked semaphore => thread blocks.
6013 * 3) migration thread wakes up (implicitly it forces the migrated
6014 * thread off the CPU)
6015 * 4) it gets the migration request and checks whether the migrated
6016 * task is still in the wrong runqueue.
6017 * 5) if it's in the wrong runqueue then the migration thread removes
6018 * it and puts it into the right queue.
6019 * 6) migration thread up()s the semaphore.
6020 * 7) we wake up and the migration is done.
6021 */
6022
6023/*
6024 * Change a given task's CPU affinity. Migrate the thread to a
6025 * proper CPU and schedule it away if the CPU it's executing on
6026 * is removed from the allowed bitmask.
6027 *
6028 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006029 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 * call is not atomic; no spinlocks may be held.
6031 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006032int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006034 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006036 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006037 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
6039 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006040 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 ret = -EINVAL;
6042 goto out;
6043 }
6044
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006045 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006046 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006047 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006048 p->cpus_allowed = *new_mask;
6049 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006050 }
6051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006053 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 goto out;
6055
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006056 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 /* Need help from migration thread: drop lock and wait. */
6058 task_rq_unlock(rq, &flags);
6059 wake_up_process(rq->migration_thread);
6060 wait_for_completion(&req.done);
6061 tlb_migrate_finish(p->mm);
6062 return 0;
6063 }
6064out:
6065 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 return ret;
6068}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006069EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006072 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 * this because either it can't run here any more (set_cpus_allowed()
6074 * away from this CPU, or CPU going down), or because we're
6075 * attempting to rebalance this task on exec (sched_exec).
6076 *
6077 * So we race with normal scheduler movements, but that's OK, as long
6078 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006079 *
6080 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006082static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006084 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006085 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
6090 rq_src = cpu_rq(src_cpu);
6091 rq_dest = cpu_rq(dest_cpu);
6092
6093 double_rq_lock(rq_src, rq_dest);
6094 /* Already moved. */
6095 if (task_cpu(p) != src_cpu)
6096 goto out;
6097 /* Affinity changed (again). */
6098 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6099 goto out;
6100
Ingo Molnardd41f592007-07-09 18:51:59 +02006101 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006102 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006103 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006106 if (on_rq) {
6107 activate_task(rq_dest, p, 0);
6108 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006110 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111out:
6112 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114}
6115
6116/*
6117 * migration_thread - this is a highprio system thread that performs
6118 * thread migration by bumping thread off CPU then 'pushing' onto
6119 * another runqueue.
6120 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006121static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006124 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
6126 rq = cpu_rq(cpu);
6127 BUG_ON(rq->migration_thread != current);
6128
6129 set_current_state(TASK_INTERRUPTIBLE);
6130 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006131 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 spin_lock_irq(&rq->lock);
6135
6136 if (cpu_is_offline(cpu)) {
6137 spin_unlock_irq(&rq->lock);
6138 goto wait_to_die;
6139 }
6140
6141 if (rq->active_balance) {
6142 active_load_balance(rq, cpu);
6143 rq->active_balance = 0;
6144 }
6145
6146 head = &rq->migration_queue;
6147
6148 if (list_empty(head)) {
6149 spin_unlock_irq(&rq->lock);
6150 schedule();
6151 set_current_state(TASK_INTERRUPTIBLE);
6152 continue;
6153 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006154 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 list_del_init(head->next);
6156
Nick Piggin674311d2005-06-25 14:57:27 -07006157 spin_unlock(&rq->lock);
6158 __migrate_task(req->task, cpu, req->dest_cpu);
6159 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 complete(&req->done);
6162 }
6163 __set_current_state(TASK_RUNNING);
6164 return 0;
6165
6166wait_to_die:
6167 /* Wait for kthread_stop */
6168 set_current_state(TASK_INTERRUPTIBLE);
6169 while (!kthread_should_stop()) {
6170 schedule();
6171 set_current_state(TASK_INTERRUPTIBLE);
6172 }
6173 __set_current_state(TASK_RUNNING);
6174 return 0;
6175}
6176
6177#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006178
6179static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6180{
6181 int ret;
6182
6183 local_irq_disable();
6184 ret = __migrate_task(p, src_cpu, dest_cpu);
6185 local_irq_enable();
6186 return ret;
6187}
6188
Kirill Korotaev054b9102006-12-10 02:20:11 -08006189/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006190 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006191 * NOTE: interrupts should be disabled by the caller
6192 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006193static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006195 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006197 struct rq *rq;
6198 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Andi Kleen3a5c3592007-10-15 17:00:14 +02006200 do {
6201 /* On same node? */
6202 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6203 cpus_and(mask, mask, p->cpus_allowed);
6204 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Andi Kleen3a5c3592007-10-15 17:00:14 +02006206 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006207 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006208 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Andi Kleen3a5c3592007-10-15 17:00:14 +02006210 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006211 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006212 cpumask_t cpus_allowed;
6213
6214 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006215 /*
6216 * Try to stay on the same cpuset, where the
6217 * current cpuset may be a subset of all cpus.
6218 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006219 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006220 * called within calls to cpuset_lock/cpuset_unlock.
6221 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006222 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006223 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006224 dest_cpu = any_online_cpu(p->cpus_allowed);
6225 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Andi Kleen3a5c3592007-10-15 17:00:14 +02006227 /*
6228 * Don't tell them about moving exiting tasks or
6229 * kernel threads (both mm NULL), since they never
6230 * leave kernel.
6231 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006232 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006233 printk(KERN_INFO "process %d (%s) no "
6234 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006235 task_pid_nr(p), p->comm, dead_cpu);
6236 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006237 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006238 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241/*
6242 * While a dead CPU has no uninterruptible tasks queued at this point,
6243 * it might still have a nonzero ->nr_uninterruptible counter, because
6244 * for performance reasons the counter is not stricly tracking tasks to
6245 * their home CPUs. So we just add the counter to another CPU's counter,
6246 * to keep the global sum constant after CPU-down:
6247 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006248static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
Mike Travis7c16ec52008-04-04 18:11:11 -07006250 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 unsigned long flags;
6252
6253 local_irq_save(flags);
6254 double_rq_lock(rq_src, rq_dest);
6255 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6256 rq_src->nr_uninterruptible = 0;
6257 double_rq_unlock(rq_src, rq_dest);
6258 local_irq_restore(flags);
6259}
6260
6261/* Run through task list and migrate tasks from the dead cpu. */
6262static void migrate_live_tasks(int src_cpu)
6263{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006264 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006266 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
Ingo Molnar48f24c42006-07-03 00:25:40 -07006268 do_each_thread(t, p) {
6269 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 continue;
6271
Ingo Molnar48f24c42006-07-03 00:25:40 -07006272 if (task_cpu(p) == src_cpu)
6273 move_task_off_dead_cpu(src_cpu, p);
6274 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006276 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277}
6278
Ingo Molnardd41f592007-07-09 18:51:59 +02006279/*
6280 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006281 * It does so by boosting its priority to highest possible.
6282 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 */
6284void sched_idle_next(void)
6285{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006286 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006287 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 struct task_struct *p = rq->idle;
6289 unsigned long flags;
6290
6291 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006292 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Ingo Molnar48f24c42006-07-03 00:25:40 -07006294 /*
6295 * Strictly not necessary since rest of the CPUs are stopped by now
6296 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 */
6298 spin_lock_irqsave(&rq->lock, flags);
6299
Ingo Molnardd41f592007-07-09 18:51:59 +02006300 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006301
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006302 update_rq_clock(rq);
6303 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304
6305 spin_unlock_irqrestore(&rq->lock, flags);
6306}
6307
Ingo Molnar48f24c42006-07-03 00:25:40 -07006308/*
6309 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 * offline.
6311 */
6312void idle_task_exit(void)
6313{
6314 struct mm_struct *mm = current->active_mm;
6315
6316 BUG_ON(cpu_online(smp_processor_id()));
6317
6318 if (mm != &init_mm)
6319 switch_mm(mm, &init_mm, current);
6320 mmdrop(mm);
6321}
6322
Kirill Korotaev054b9102006-12-10 02:20:11 -08006323/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006324static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006326 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
6328 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006329 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006332 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
Ingo Molnar48f24c42006-07-03 00:25:40 -07006334 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
6336 /*
6337 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006338 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 * fine.
6340 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006341 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006342 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006343 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Ingo Molnar48f24c42006-07-03 00:25:40 -07006345 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346}
6347
6348/* release_task() removes task from tasklist, so we won't find dead tasks. */
6349static void migrate_dead_tasks(unsigned int dead_cpu)
6350{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006351 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006352 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Ingo Molnardd41f592007-07-09 18:51:59 +02006354 for ( ; ; ) {
6355 if (!rq->nr_running)
6356 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006357 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006358 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006359 if (!next)
6360 break;
6361 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006362
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 }
6364}
6365#endif /* CONFIG_HOTPLUG_CPU */
6366
Nick Piggine692ab52007-07-26 13:40:43 +02006367#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6368
6369static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006370 {
6371 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006372 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006373 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006374 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006375};
6376
6377static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006378 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006379 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006380 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006381 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006382 .child = sd_ctl_dir,
6383 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006384 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006385};
6386
6387static struct ctl_table *sd_alloc_ctl_entry(int n)
6388{
6389 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006390 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006391
Nick Piggine692ab52007-07-26 13:40:43 +02006392 return entry;
6393}
6394
Milton Miller6382bc92007-10-15 17:00:19 +02006395static void sd_free_ctl_entry(struct ctl_table **tablep)
6396{
Milton Millercd7900762007-10-17 16:55:11 +02006397 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006398
Milton Millercd7900762007-10-17 16:55:11 +02006399 /*
6400 * In the intermediate directories, both the child directory and
6401 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006402 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006403 * static strings and all have proc handlers.
6404 */
6405 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006406 if (entry->child)
6407 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006408 if (entry->proc_handler == NULL)
6409 kfree(entry->procname);
6410 }
Milton Miller6382bc92007-10-15 17:00:19 +02006411
6412 kfree(*tablep);
6413 *tablep = NULL;
6414}
6415
Nick Piggine692ab52007-07-26 13:40:43 +02006416static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006417set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006418 const char *procname, void *data, int maxlen,
6419 mode_t mode, proc_handler *proc_handler)
6420{
Nick Piggine692ab52007-07-26 13:40:43 +02006421 entry->procname = procname;
6422 entry->data = data;
6423 entry->maxlen = maxlen;
6424 entry->mode = mode;
6425 entry->proc_handler = proc_handler;
6426}
6427
6428static struct ctl_table *
6429sd_alloc_ctl_domain_table(struct sched_domain *sd)
6430{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006431 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006432
Milton Millerad1cdc12007-10-15 17:00:19 +02006433 if (table == NULL)
6434 return NULL;
6435
Alexey Dobriyane0361852007-08-09 11:16:46 +02006436 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006437 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006438 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006439 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006440 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006441 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006442 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006443 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006444 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006445 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006446 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006447 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006448 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006449 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006450 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006451 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006452 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006453 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006454 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006455 &sd->cache_nice_tries,
6456 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006457 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006458 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006459 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006460
6461 return table;
6462}
6463
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006464static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006465{
6466 struct ctl_table *entry, *table;
6467 struct sched_domain *sd;
6468 int domain_num = 0, i;
6469 char buf[32];
6470
6471 for_each_domain(cpu, sd)
6472 domain_num++;
6473 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006474 if (table == NULL)
6475 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006476
6477 i = 0;
6478 for_each_domain(cpu, sd) {
6479 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006480 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006481 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006482 entry->child = sd_alloc_ctl_domain_table(sd);
6483 entry++;
6484 i++;
6485 }
6486 return table;
6487}
6488
6489static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006490static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006491{
6492 int i, cpu_num = num_online_cpus();
6493 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6494 char buf[32];
6495
Milton Miller73785472007-10-24 18:23:48 +02006496 WARN_ON(sd_ctl_dir[0].child);
6497 sd_ctl_dir[0].child = entry;
6498
Milton Millerad1cdc12007-10-15 17:00:19 +02006499 if (entry == NULL)
6500 return;
6501
Milton Miller97b6ea72007-10-15 17:00:19 +02006502 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006503 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006504 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006505 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006506 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006507 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006508 }
Milton Miller73785472007-10-24 18:23:48 +02006509
6510 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006511 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6512}
Milton Miller6382bc92007-10-15 17:00:19 +02006513
Milton Miller73785472007-10-24 18:23:48 +02006514/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006515static void unregister_sched_domain_sysctl(void)
6516{
Milton Miller73785472007-10-24 18:23:48 +02006517 if (sd_sysctl_header)
6518 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006519 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006520 if (sd_ctl_dir[0].child)
6521 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006522}
Nick Piggine692ab52007-07-26 13:40:43 +02006523#else
Milton Miller6382bc92007-10-15 17:00:19 +02006524static void register_sched_domain_sysctl(void)
6525{
6526}
6527static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006528{
6529}
6530#endif
6531
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532/*
6533 * migration_call - callback that gets triggered when a CPU is added.
6534 * Here we can start up the necessary migration thread for the new CPU.
6535 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006536static int __cpuinit
6537migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006540 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006542 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
6544 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006547 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006548 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 if (IS_ERR(p))
6550 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 kthread_bind(p, cpu);
6552 /* Must be high prio: stop_machine expects to yield to it. */
6553 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006554 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 task_rq_unlock(rq, &flags);
6556 cpu_rq(cpu)->migration_thread = p;
6557 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006558
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006560 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006561 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006563
6564 /* Update our root-domain */
6565 rq = cpu_rq(cpu);
6566 spin_lock_irqsave(&rq->lock, flags);
6567 if (rq->rd) {
6568 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6569 cpu_set(cpu, rq->rd->online);
6570 }
6571 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574#ifdef CONFIG_HOTPLUG_CPU
6575 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006576 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006577 if (!cpu_rq(cpu)->migration_thread)
6578 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006579 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006580 kthread_bind(cpu_rq(cpu)->migration_thread,
6581 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 kthread_stop(cpu_rq(cpu)->migration_thread);
6583 cpu_rq(cpu)->migration_thread = NULL;
6584 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006585
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006587 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006588 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 migrate_live_tasks(cpu);
6590 rq = cpu_rq(cpu);
6591 kthread_stop(rq->migration_thread);
6592 rq->migration_thread = NULL;
6593 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006594 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006595 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006596 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006598 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6599 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006601 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006602 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 migrate_nr_uninterruptible(rq);
6604 BUG_ON(rq->nr_running != 0);
6605
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006606 /*
6607 * No need to migrate the tasks: it was best-effort if
6608 * they didn't take sched_hotcpu_mutex. Just wake up
6609 * the requestors.
6610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 spin_lock_irq(&rq->lock);
6612 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006613 struct migration_req *req;
6614
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006616 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 list_del_init(&req->list);
6618 complete(&req->done);
6619 }
6620 spin_unlock_irq(&rq->lock);
6621 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006622
Gregory Haskins08f503b2008-03-10 17:59:11 -04006623 case CPU_DYING:
6624 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006625 /* Update our root-domain */
6626 rq = cpu_rq(cpu);
6627 spin_lock_irqsave(&rq->lock, flags);
6628 if (rq->rd) {
6629 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6630 cpu_clear(cpu, rq->rd->online);
6631 }
6632 spin_unlock_irqrestore(&rq->lock, flags);
6633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634#endif
6635 }
6636 return NOTIFY_OK;
6637}
6638
6639/* Register at highest priority so that task migration (migrate_all_tasks)
6640 * happens before everything else.
6641 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006642static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 .notifier_call = migration_call,
6644 .priority = 10
6645};
6646
Adrian Bunke6fe6642007-11-09 22:39:39 +01006647void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648{
6649 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006650 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006651
6652 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006653 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6654 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6656 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657}
6658#endif
6659
6660#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006661
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006662#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006663
Mike Travis7c16ec52008-04-04 18:11:11 -07006664static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6665 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006666{
6667 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006668 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006669
Mike Travis434d53b2008-04-04 18:11:04 -07006670 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006671 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006672
6673 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6674
6675 if (!(sd->flags & SD_LOAD_BALANCE)) {
6676 printk("does not load-balance\n");
6677 if (sd->parent)
6678 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6679 " has parent");
6680 return -1;
6681 }
6682
6683 printk(KERN_CONT "span %s\n", str);
6684
6685 if (!cpu_isset(cpu, sd->span)) {
6686 printk(KERN_ERR "ERROR: domain->span does not contain "
6687 "CPU%d\n", cpu);
6688 }
6689 if (!cpu_isset(cpu, group->cpumask)) {
6690 printk(KERN_ERR "ERROR: domain->groups does not contain"
6691 " CPU%d\n", cpu);
6692 }
6693
6694 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6695 do {
6696 if (!group) {
6697 printk("\n");
6698 printk(KERN_ERR "ERROR: group is NULL\n");
6699 break;
6700 }
6701
6702 if (!group->__cpu_power) {
6703 printk(KERN_CONT "\n");
6704 printk(KERN_ERR "ERROR: domain->cpu_power not "
6705 "set\n");
6706 break;
6707 }
6708
6709 if (!cpus_weight(group->cpumask)) {
6710 printk(KERN_CONT "\n");
6711 printk(KERN_ERR "ERROR: empty group\n");
6712 break;
6713 }
6714
Mike Travis7c16ec52008-04-04 18:11:11 -07006715 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006716 printk(KERN_CONT "\n");
6717 printk(KERN_ERR "ERROR: repeated CPUs\n");
6718 break;
6719 }
6720
Mike Travis7c16ec52008-04-04 18:11:11 -07006721 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006722
Mike Travis434d53b2008-04-04 18:11:04 -07006723 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006724 printk(KERN_CONT " %s", str);
6725
6726 group = group->next;
6727 } while (group != sd->groups);
6728 printk(KERN_CONT "\n");
6729
Mike Travis7c16ec52008-04-04 18:11:11 -07006730 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006731 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6732
Mike Travis7c16ec52008-04-04 18:11:11 -07006733 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006734 printk(KERN_ERR "ERROR: parent span is not a superset "
6735 "of domain->span\n");
6736 return 0;
6737}
6738
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739static void sched_domain_debug(struct sched_domain *sd, int cpu)
6740{
Mike Travis7c16ec52008-04-04 18:11:11 -07006741 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 int level = 0;
6743
Nick Piggin41c7ce92005-06-25 14:57:24 -07006744 if (!sd) {
6745 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6746 return;
6747 }
6748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6750
Mike Travis7c16ec52008-04-04 18:11:11 -07006751 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6752 if (!groupmask) {
6753 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6754 return;
6755 }
6756
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006757 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006758 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 level++;
6761 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006762 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006763 break;
6764 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006765 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766}
6767#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006768# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769#endif
6770
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006771static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006772{
6773 if (cpus_weight(sd->span) == 1)
6774 return 1;
6775
6776 /* Following flags need at least 2 groups */
6777 if (sd->flags & (SD_LOAD_BALANCE |
6778 SD_BALANCE_NEWIDLE |
6779 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006780 SD_BALANCE_EXEC |
6781 SD_SHARE_CPUPOWER |
6782 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006783 if (sd->groups != sd->groups->next)
6784 return 0;
6785 }
6786
6787 /* Following flags don't use groups */
6788 if (sd->flags & (SD_WAKE_IDLE |
6789 SD_WAKE_AFFINE |
6790 SD_WAKE_BALANCE))
6791 return 0;
6792
6793 return 1;
6794}
6795
Ingo Molnar48f24c42006-07-03 00:25:40 -07006796static int
6797sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006798{
6799 unsigned long cflags = sd->flags, pflags = parent->flags;
6800
6801 if (sd_degenerate(parent))
6802 return 1;
6803
6804 if (!cpus_equal(sd->span, parent->span))
6805 return 0;
6806
6807 /* Does parent contain flags not in child? */
6808 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6809 if (cflags & SD_WAKE_AFFINE)
6810 pflags &= ~SD_WAKE_BALANCE;
6811 /* Flags needing groups don't count if only 1 group in parent */
6812 if (parent->groups == parent->groups->next) {
6813 pflags &= ~(SD_LOAD_BALANCE |
6814 SD_BALANCE_NEWIDLE |
6815 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006816 SD_BALANCE_EXEC |
6817 SD_SHARE_CPUPOWER |
6818 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006819 }
6820 if (~cflags & pflags)
6821 return 0;
6822
6823 return 1;
6824}
6825
Gregory Haskins57d885f2008-01-25 21:08:18 +01006826static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6827{
6828 unsigned long flags;
6829 const struct sched_class *class;
6830
6831 spin_lock_irqsave(&rq->lock, flags);
6832
6833 if (rq->rd) {
6834 struct root_domain *old_rd = rq->rd;
6835
Ingo Molnar0eab9142008-01-25 21:08:19 +01006836 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006837 if (class->leave_domain)
6838 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006839 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006840
Gregory Haskinsdc938522008-01-25 21:08:26 +01006841 cpu_clear(rq->cpu, old_rd->span);
6842 cpu_clear(rq->cpu, old_rd->online);
6843
Gregory Haskins57d885f2008-01-25 21:08:18 +01006844 if (atomic_dec_and_test(&old_rd->refcount))
6845 kfree(old_rd);
6846 }
6847
6848 atomic_inc(&rd->refcount);
6849 rq->rd = rd;
6850
Gregory Haskinsdc938522008-01-25 21:08:26 +01006851 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006852 if (cpu_isset(rq->cpu, cpu_online_map))
6853 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006854
Ingo Molnar0eab9142008-01-25 21:08:19 +01006855 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006856 if (class->join_domain)
6857 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006858 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006859
6860 spin_unlock_irqrestore(&rq->lock, flags);
6861}
6862
Gregory Haskinsdc938522008-01-25 21:08:26 +01006863static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006864{
6865 memset(rd, 0, sizeof(*rd));
6866
Gregory Haskinsdc938522008-01-25 21:08:26 +01006867 cpus_clear(rd->span);
6868 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006869}
6870
6871static void init_defrootdomain(void)
6872{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006873 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006874 atomic_set(&def_root_domain.refcount, 1);
6875}
6876
Gregory Haskinsdc938522008-01-25 21:08:26 +01006877static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006878{
6879 struct root_domain *rd;
6880
6881 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6882 if (!rd)
6883 return NULL;
6884
Gregory Haskinsdc938522008-01-25 21:08:26 +01006885 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006886
6887 return rd;
6888}
6889
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006891 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 * hold the hotplug lock.
6893 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006894static void
6895cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006897 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006898 struct sched_domain *tmp;
6899
6900 /* Remove the sched domains which do not contribute to scheduling. */
6901 for (tmp = sd; tmp; tmp = tmp->parent) {
6902 struct sched_domain *parent = tmp->parent;
6903 if (!parent)
6904 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006905 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006906 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006907 if (parent->parent)
6908 parent->parent->child = tmp;
6909 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006910 }
6911
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006912 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006913 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006914 if (sd)
6915 sd->child = NULL;
6916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
6918 sched_domain_debug(sd, cpu);
6919
Gregory Haskins57d885f2008-01-25 21:08:18 +01006920 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006921 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922}
6923
6924/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006925static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
6927/* Setup the mask of cpus configured for isolated domains */
6928static int __init isolated_cpu_setup(char *str)
6929{
6930 int ints[NR_CPUS], i;
6931
6932 str = get_options(str, ARRAY_SIZE(ints), ints);
6933 cpus_clear(cpu_isolated_map);
6934 for (i = 1; i <= ints[0]; i++)
6935 if (ints[i] < NR_CPUS)
6936 cpu_set(ints[i], cpu_isolated_map);
6937 return 1;
6938}
6939
Ingo Molnar8927f492007-10-15 17:00:13 +02006940__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006943 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6944 * to a function which identifies what group(along with sched group) a CPU
6945 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6946 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 *
6948 * init_sched_build_groups will build a circular linked list of the groups
6949 * covered by the given span, and will set each group's ->cpumask correctly,
6950 * and ->cpu_power to 0.
6951 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006952static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006953init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006954 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006955 struct sched_group **sg,
6956 cpumask_t *tmpmask),
6957 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958{
6959 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 int i;
6961
Mike Travis7c16ec52008-04-04 18:11:11 -07006962 cpus_clear(*covered);
6963
6964 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006965 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006966 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 int j;
6968
Mike Travis7c16ec52008-04-04 18:11:11 -07006969 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 continue;
6971
Mike Travis7c16ec52008-04-04 18:11:11 -07006972 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006973 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
Mike Travis7c16ec52008-04-04 18:11:11 -07006975 for_each_cpu_mask(j, *span) {
6976 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 continue;
6978
Mike Travis7c16ec52008-04-04 18:11:11 -07006979 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 cpu_set(j, sg->cpumask);
6981 }
6982 if (!first)
6983 first = sg;
6984 if (last)
6985 last->next = sg;
6986 last = sg;
6987 }
6988 last->next = first;
6989}
6990
John Hawkes9c1cfda2005-09-06 15:18:14 -07006991#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
John Hawkes9c1cfda2005-09-06 15:18:14 -07006993#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006994
John Hawkes9c1cfda2005-09-06 15:18:14 -07006995/**
6996 * find_next_best_node - find the next node to include in a sched_domain
6997 * @node: node whose sched_domain we're building
6998 * @used_nodes: nodes already in the sched_domain
6999 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007000 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007001 * finds the closest node not already in the @used_nodes map.
7002 *
7003 * Should use nodemask_t.
7004 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007005static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007006{
7007 int i, n, val, min_val, best_node = 0;
7008
7009 min_val = INT_MAX;
7010
7011 for (i = 0; i < MAX_NUMNODES; i++) {
7012 /* Start at @node */
7013 n = (node + i) % MAX_NUMNODES;
7014
7015 if (!nr_cpus_node(n))
7016 continue;
7017
7018 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007019 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007020 continue;
7021
7022 /* Simple min distance search */
7023 val = node_distance(node, n);
7024
7025 if (val < min_val) {
7026 min_val = val;
7027 best_node = n;
7028 }
7029 }
7030
Mike Travisc5f59f02008-04-04 18:11:10 -07007031 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007032 return best_node;
7033}
7034
7035/**
7036 * sched_domain_node_span - get a cpumask for a node's sched_domain
7037 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007038 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007039 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007040 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007041 * should be one that prevents unnecessary balancing, but also spreads tasks
7042 * out optimally.
7043 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007044static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007045{
Mike Travisc5f59f02008-04-04 18:11:10 -07007046 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007047 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007048 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007049
Mike Travis4bdbaad32008-04-15 16:35:52 -07007050 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007051 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007052
Mike Travis4bdbaad32008-04-15 16:35:52 -07007053 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007054 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007055
7056 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007057 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007058
Mike Travisc5f59f02008-04-04 18:11:10 -07007059 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007060 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007061 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007062}
7063#endif
7064
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007065int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007066
John Hawkes9c1cfda2005-09-06 15:18:14 -07007067/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007068 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070#ifdef CONFIG_SCHED_SMT
7071static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007072static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007073
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007074static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007075cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7076 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007078 if (sg)
7079 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 return cpu;
7081}
7082#endif
7083
Ingo Molnar48f24c42006-07-03 00:25:40 -07007084/*
7085 * multi-core sched-domains:
7086 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007087#ifdef CONFIG_SCHED_MC
7088static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007089static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007090#endif
7091
7092#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007093static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007094cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7095 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007096{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007097 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007098
7099 *mask = per_cpu(cpu_sibling_map, cpu);
7100 cpus_and(*mask, *mask, *cpu_map);
7101 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007102 if (sg)
7103 *sg = &per_cpu(sched_group_core, group);
7104 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007105}
7106#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007107static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007108cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7109 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007110{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007111 if (sg)
7112 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007113 return cpu;
7114}
7115#endif
7116
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007118static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007119
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007120static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007121cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7122 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007124 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007125#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007126 *mask = cpu_coregroup_map(cpu);
7127 cpus_and(*mask, *mask, *cpu_map);
7128 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007129#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007130 *mask = per_cpu(cpu_sibling_map, cpu);
7131 cpus_and(*mask, *mask, *cpu_map);
7132 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007134 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007136 if (sg)
7137 *sg = &per_cpu(sched_group_phys, group);
7138 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139}
7140
7141#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007142/*
7143 * The init_sched_build_groups can't handle what we want to do with node
7144 * groups, so roll our own. Now each node has its own list of groups which
7145 * gets dynamically allocated.
7146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007148static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007149
7150static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007151static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007152
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007153static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007154 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007156 int group;
7157
Mike Travis7c16ec52008-04-04 18:11:11 -07007158 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7159 cpus_and(*nodemask, *nodemask, *cpu_map);
7160 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007161
7162 if (sg)
7163 *sg = &per_cpu(sched_group_allnodes, group);
7164 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007166
Siddha, Suresh B08069032006-03-27 01:15:23 -08007167static void init_numa_sched_groups_power(struct sched_group *group_head)
7168{
7169 struct sched_group *sg = group_head;
7170 int j;
7171
7172 if (!sg)
7173 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007174 do {
7175 for_each_cpu_mask(j, sg->cpumask) {
7176 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007177
Andi Kleen3a5c3592007-10-15 17:00:14 +02007178 sd = &per_cpu(phys_domains, j);
7179 if (j != first_cpu(sd->groups->cpumask)) {
7180 /*
7181 * Only add "power" once for each
7182 * physical package.
7183 */
7184 continue;
7185 }
7186
7187 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007188 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007189 sg = sg->next;
7190 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192#endif
7193
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007194#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007195/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007196static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007197{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007198 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007199
7200 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007201 struct sched_group **sched_group_nodes
7202 = sched_group_nodes_bycpu[cpu];
7203
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007204 if (!sched_group_nodes)
7205 continue;
7206
7207 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007208 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7209
Mike Travis7c16ec52008-04-04 18:11:11 -07007210 *nodemask = node_to_cpumask(i);
7211 cpus_and(*nodemask, *nodemask, *cpu_map);
7212 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007213 continue;
7214
7215 if (sg == NULL)
7216 continue;
7217 sg = sg->next;
7218next_sg:
7219 oldsg = sg;
7220 sg = sg->next;
7221 kfree(oldsg);
7222 if (oldsg != sched_group_nodes[i])
7223 goto next_sg;
7224 }
7225 kfree(sched_group_nodes);
7226 sched_group_nodes_bycpu[cpu] = NULL;
7227 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007228}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007229#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007230static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007231{
7232}
7233#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007236 * Initialize sched groups cpu_power.
7237 *
7238 * cpu_power indicates the capacity of sched group, which is used while
7239 * distributing the load between different sched groups in a sched domain.
7240 * Typically cpu_power for all the groups in a sched domain will be same unless
7241 * there are asymmetries in the topology. If there are asymmetries, group
7242 * having more cpu_power will pickup more load compared to the group having
7243 * less cpu_power.
7244 *
7245 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7246 * the maximum number of tasks a group can handle in the presence of other idle
7247 * or lightly loaded groups in the same sched domain.
7248 */
7249static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7250{
7251 struct sched_domain *child;
7252 struct sched_group *group;
7253
7254 WARN_ON(!sd || !sd->groups);
7255
7256 if (cpu != first_cpu(sd->groups->cpumask))
7257 return;
7258
7259 child = sd->child;
7260
Eric Dumazet5517d862007-05-08 00:32:57 -07007261 sd->groups->__cpu_power = 0;
7262
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007263 /*
7264 * For perf policy, if the groups in child domain share resources
7265 * (for example cores sharing some portions of the cache hierarchy
7266 * or SMT), then set this domain groups cpu_power such that each group
7267 * can handle only one task, when there are other idle groups in the
7268 * same sched domain.
7269 */
7270 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7271 (child->flags &
7272 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007273 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007274 return;
7275 }
7276
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007277 /*
7278 * add cpu_power of each child group to this groups cpu_power
7279 */
7280 group = child->groups;
7281 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007282 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007283 group = group->next;
7284 } while (group != child->groups);
7285}
7286
7287/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007288 * Initializers for schedule domains
7289 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7290 */
7291
7292#define SD_INIT(sd, type) sd_init_##type(sd)
7293#define SD_INIT_FUNC(type) \
7294static noinline void sd_init_##type(struct sched_domain *sd) \
7295{ \
7296 memset(sd, 0, sizeof(*sd)); \
7297 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007298 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007299}
7300
7301SD_INIT_FUNC(CPU)
7302#ifdef CONFIG_NUMA
7303 SD_INIT_FUNC(ALLNODES)
7304 SD_INIT_FUNC(NODE)
7305#endif
7306#ifdef CONFIG_SCHED_SMT
7307 SD_INIT_FUNC(SIBLING)
7308#endif
7309#ifdef CONFIG_SCHED_MC
7310 SD_INIT_FUNC(MC)
7311#endif
7312
7313/*
7314 * To minimize stack usage kmalloc room for cpumasks and share the
7315 * space as the usage in build_sched_domains() dictates. Used only
7316 * if the amount of space is significant.
7317 */
7318struct allmasks {
7319 cpumask_t tmpmask; /* make this one first */
7320 union {
7321 cpumask_t nodemask;
7322 cpumask_t this_sibling_map;
7323 cpumask_t this_core_map;
7324 };
7325 cpumask_t send_covered;
7326
7327#ifdef CONFIG_NUMA
7328 cpumask_t domainspan;
7329 cpumask_t covered;
7330 cpumask_t notcovered;
7331#endif
7332};
7333
7334#if NR_CPUS > 128
7335#define SCHED_CPUMASK_ALLOC 1
7336#define SCHED_CPUMASK_FREE(v) kfree(v)
7337#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7338#else
7339#define SCHED_CPUMASK_ALLOC 0
7340#define SCHED_CPUMASK_FREE(v)
7341#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7342#endif
7343
7344#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7345 ((unsigned long)(a) + offsetof(struct allmasks, v))
7346
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007347static int default_relax_domain_level = -1;
7348
7349static int __init setup_relax_domain_level(char *str)
7350{
7351 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7352 return 1;
7353}
7354__setup("relax_domain_level=", setup_relax_domain_level);
7355
7356static void set_domain_attribute(struct sched_domain *sd,
7357 struct sched_domain_attr *attr)
7358{
7359 int request;
7360
7361 if (!attr || attr->relax_domain_level < 0) {
7362 if (default_relax_domain_level < 0)
7363 return;
7364 else
7365 request = default_relax_domain_level;
7366 } else
7367 request = attr->relax_domain_level;
7368 if (request < sd->level) {
7369 /* turn off idle balance on this domain */
7370 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7371 } else {
7372 /* turn on idle balance on this domain */
7373 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7374 }
7375}
7376
Mike Travis7c16ec52008-04-04 18:11:11 -07007377/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007378 * Build sched domains for a given set of cpus and attach the sched domains
7379 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007381static int __build_sched_domains(const cpumask_t *cpu_map,
7382 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383{
7384 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007385 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007386 SCHED_CPUMASK_DECLARE(allmasks);
7387 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007388#ifdef CONFIG_NUMA
7389 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007390 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007391
7392 /*
7393 * Allocate the per-node list of sched groups
7394 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007395 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007396 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007397 if (!sched_group_nodes) {
7398 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007399 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007400 }
John Hawkesd1b55132005-09-06 15:18:14 -07007401#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
Gregory Haskinsdc938522008-01-25 21:08:26 +01007403 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007404 if (!rd) {
7405 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007406#ifdef CONFIG_NUMA
7407 kfree(sched_group_nodes);
7408#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007409 return -ENOMEM;
7410 }
7411
Mike Travis7c16ec52008-04-04 18:11:11 -07007412#if SCHED_CPUMASK_ALLOC
7413 /* get space for all scratch cpumask variables */
7414 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7415 if (!allmasks) {
7416 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7417 kfree(rd);
7418#ifdef CONFIG_NUMA
7419 kfree(sched_group_nodes);
7420#endif
7421 return -ENOMEM;
7422 }
7423#endif
7424 tmpmask = (cpumask_t *)allmasks;
7425
7426
7427#ifdef CONFIG_NUMA
7428 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7429#endif
7430
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007432 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007434 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
Mike Travis7c16ec52008-04-04 18:11:11 -07007438 *nodemask = node_to_cpumask(cpu_to_node(i));
7439 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
7441#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007442 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007443 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007444 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007445 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007446 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007447 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007448 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007449 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007450 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007451 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007452 } else
7453 p = NULL;
7454
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007456 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007457 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007458 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007459 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007460 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007461 if (p)
7462 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007463 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464#endif
7465
7466 p = sd;
7467 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007468 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007469 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007470 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007471 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007473 if (p)
7474 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007475 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007477#ifdef CONFIG_SCHED_MC
7478 p = sd;
7479 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007480 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007481 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007482 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007483 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007484 cpus_and(sd->span, sd->span, *cpu_map);
7485 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007486 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007487 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007488#endif
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490#ifdef CONFIG_SCHED_SMT
7491 p = sd;
7492 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007493 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007494 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007495 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007496 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007497 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007499 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007500 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501#endif
7502 }
7503
7504#ifdef CONFIG_SCHED_SMT
7505 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007506 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007507 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7508 SCHED_CPUMASK_VAR(send_covered, allmasks);
7509
7510 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7511 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7512 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 continue;
7514
Ingo Molnardd41f592007-07-09 18:51:59 +02007515 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007516 &cpu_to_cpu_group,
7517 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 }
7519#endif
7520
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007521#ifdef CONFIG_SCHED_MC
7522 /* Set up multi-core groups */
7523 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007524 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7525 SCHED_CPUMASK_VAR(send_covered, allmasks);
7526
7527 *this_core_map = cpu_coregroup_map(i);
7528 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7529 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007530 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007531
Ingo Molnardd41f592007-07-09 18:51:59 +02007532 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007533 &cpu_to_core_group,
7534 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007535 }
7536#endif
7537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 /* Set up physical groups */
7539 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007540 SCHED_CPUMASK_VAR(nodemask, allmasks);
7541 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542
Mike Travis7c16ec52008-04-04 18:11:11 -07007543 *nodemask = node_to_cpumask(i);
7544 cpus_and(*nodemask, *nodemask, *cpu_map);
7545 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 continue;
7547
Mike Travis7c16ec52008-04-04 18:11:11 -07007548 init_sched_build_groups(nodemask, cpu_map,
7549 &cpu_to_phys_group,
7550 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 }
7552
7553#ifdef CONFIG_NUMA
7554 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007555 if (sd_allnodes) {
7556 SCHED_CPUMASK_VAR(send_covered, allmasks);
7557
7558 init_sched_build_groups(cpu_map, cpu_map,
7559 &cpu_to_allnodes_group,
7560 send_covered, tmpmask);
7561 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007562
7563 for (i = 0; i < MAX_NUMNODES; i++) {
7564 /* Set up node groups */
7565 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007566 SCHED_CPUMASK_VAR(nodemask, allmasks);
7567 SCHED_CPUMASK_VAR(domainspan, allmasks);
7568 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007569 int j;
7570
Mike Travis7c16ec52008-04-04 18:11:11 -07007571 *nodemask = node_to_cpumask(i);
7572 cpus_clear(*covered);
7573
7574 cpus_and(*nodemask, *nodemask, *cpu_map);
7575 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007576 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007577 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007578 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007579
Mike Travis4bdbaad32008-04-15 16:35:52 -07007580 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007581 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007582
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007583 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007584 if (!sg) {
7585 printk(KERN_WARNING "Can not alloc domain group for "
7586 "node %d\n", i);
7587 goto error;
7588 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007589 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007590 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007591 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007592
John Hawkes9c1cfda2005-09-06 15:18:14 -07007593 sd = &per_cpu(node_domains, j);
7594 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007595 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007596 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007597 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007598 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007599 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007600 prev = sg;
7601
7602 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007603 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007604 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007605 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007606
Mike Travis7c16ec52008-04-04 18:11:11 -07007607 cpus_complement(*notcovered, *covered);
7608 cpus_and(*tmpmask, *notcovered, *cpu_map);
7609 cpus_and(*tmpmask, *tmpmask, *domainspan);
7610 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007611 break;
7612
Mike Travis7c16ec52008-04-04 18:11:11 -07007613 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7614 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007615 continue;
7616
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007617 sg = kmalloc_node(sizeof(struct sched_group),
7618 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007619 if (!sg) {
7620 printk(KERN_WARNING
7621 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007622 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007623 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007624 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007625 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007626 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007627 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628 prev->next = sg;
7629 prev = sg;
7630 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632#endif
7633
7634 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007635#ifdef CONFIG_SCHED_SMT
7636 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007637 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7638
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007639 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007640 }
7641#endif
7642#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007643 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007644 struct sched_domain *sd = &per_cpu(core_domains, i);
7645
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007646 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007647 }
7648#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007650 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007651 struct sched_domain *sd = &per_cpu(phys_domains, i);
7652
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007653 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 }
7655
John Hawkes9c1cfda2005-09-06 15:18:14 -07007656#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007657 for (i = 0; i < MAX_NUMNODES; i++)
7658 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007659
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007660 if (sd_allnodes) {
7661 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007662
Mike Travis7c16ec52008-04-04 18:11:11 -07007663 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7664 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007665 init_numa_sched_groups_power(sg);
7666 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007667#endif
7668
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007670 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 struct sched_domain *sd;
7672#ifdef CONFIG_SCHED_SMT
7673 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007674#elif defined(CONFIG_SCHED_MC)
7675 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676#else
7677 sd = &per_cpu(phys_domains, i);
7678#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007679 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007681
Mike Travis7c16ec52008-04-04 18:11:11 -07007682 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007683 return 0;
7684
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007685#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007686error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007687 free_sched_groups(cpu_map, tmpmask);
7688 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007689 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691}
Paul Jackson029190c2007-10-18 23:40:20 -07007692
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007693static int build_sched_domains(const cpumask_t *cpu_map)
7694{
7695 return __build_sched_domains(cpu_map, NULL);
7696}
7697
Paul Jackson029190c2007-10-18 23:40:20 -07007698static cpumask_t *doms_cur; /* current sched domains */
7699static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007700static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7701 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007702
7703/*
7704 * Special case: If a kmalloc of a doms_cur partition (array of
7705 * cpumask_t) fails, then fallback to a single sched domain,
7706 * as determined by the single cpumask_t fallback_doms.
7707 */
7708static cpumask_t fallback_doms;
7709
Heiko Carstens22e52b02008-03-12 18:31:59 +01007710void __attribute__((weak)) arch_update_cpu_topology(void)
7711{
7712}
7713
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007714/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007715 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007716 * For now this just excludes isolated cpus, but could be used to
7717 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007718 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007719static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007720{
Milton Miller73785472007-10-24 18:23:48 +02007721 int err;
7722
Heiko Carstens22e52b02008-03-12 18:31:59 +01007723 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007724 ndoms_cur = 1;
7725 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7726 if (!doms_cur)
7727 doms_cur = &fallback_doms;
7728 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007729 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007730 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007731 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007732
7733 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007734}
7735
Mike Travis7c16ec52008-04-04 18:11:11 -07007736static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7737 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738{
Mike Travis7c16ec52008-04-04 18:11:11 -07007739 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007742/*
7743 * Detach sched domains from a group of cpus specified in cpu_map
7744 * These cpus will now be attached to the NULL domain
7745 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007746static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007747{
Mike Travis7c16ec52008-04-04 18:11:11 -07007748 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007749 int i;
7750
Milton Miller6382bc92007-10-15 17:00:19 +02007751 unregister_sched_domain_sysctl();
7752
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007753 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007754 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007755 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007756 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007757}
7758
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007759/* handle null as "default" */
7760static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7761 struct sched_domain_attr *new, int idx_new)
7762{
7763 struct sched_domain_attr tmp;
7764
7765 /* fast path */
7766 if (!new && !cur)
7767 return 1;
7768
7769 tmp = SD_ATTR_INIT;
7770 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7771 new ? (new + idx_new) : &tmp,
7772 sizeof(struct sched_domain_attr));
7773}
7774
Paul Jackson029190c2007-10-18 23:40:20 -07007775/*
7776 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007777 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007778 * doms_new[] to the current sched domain partitioning, doms_cur[].
7779 * It destroys each deleted domain and builds each new domain.
7780 *
7781 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007782 * The masks don't intersect (don't overlap.) We should setup one
7783 * sched domain for each mask. CPUs not in any of the cpumasks will
7784 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007785 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7786 * it as it is.
7787 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007788 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7789 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007790 * failed the kmalloc call, then it can pass in doms_new == NULL,
7791 * and partition_sched_domains() will fallback to the single partition
7792 * 'fallback_doms'.
7793 *
7794 * Call with hotplug lock held
7795 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007796void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7797 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007798{
7799 int i, j;
7800
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007801 lock_doms_cur();
7802
Milton Miller73785472007-10-24 18:23:48 +02007803 /* always unregister in case we don't destroy any domains */
7804 unregister_sched_domain_sysctl();
7805
Paul Jackson029190c2007-10-18 23:40:20 -07007806 if (doms_new == NULL) {
7807 ndoms_new = 1;
7808 doms_new = &fallback_doms;
7809 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007810 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007811 }
7812
7813 /* Destroy deleted domains */
7814 for (i = 0; i < ndoms_cur; i++) {
7815 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007816 if (cpus_equal(doms_cur[i], doms_new[j])
7817 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007818 goto match1;
7819 }
7820 /* no match - a current sched domain not in new doms_new[] */
7821 detach_destroy_domains(doms_cur + i);
7822match1:
7823 ;
7824 }
7825
7826 /* Build new domains */
7827 for (i = 0; i < ndoms_new; i++) {
7828 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007829 if (cpus_equal(doms_new[i], doms_cur[j])
7830 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007831 goto match2;
7832 }
7833 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007834 __build_sched_domains(doms_new + i,
7835 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007836match2:
7837 ;
7838 }
7839
7840 /* Remember the new sched domains */
7841 if (doms_cur != &fallback_doms)
7842 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007843 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007844 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007845 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007846 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007847
7848 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007849
7850 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007851}
7852
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007853#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007854int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007855{
7856 int err;
7857
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007858 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007859 detach_destroy_domains(&cpu_online_map);
7860 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007861 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007862
7863 return err;
7864}
7865
7866static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7867{
7868 int ret;
7869
7870 if (buf[0] != '0' && buf[0] != '1')
7871 return -EINVAL;
7872
7873 if (smt)
7874 sched_smt_power_savings = (buf[0] == '1');
7875 else
7876 sched_mc_power_savings = (buf[0] == '1');
7877
7878 ret = arch_reinit_sched_domains();
7879
7880 return ret ? ret : count;
7881}
7882
Adrian Bunk6707de002007-08-12 18:08:19 +02007883#ifdef CONFIG_SCHED_MC
7884static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7885{
7886 return sprintf(page, "%u\n", sched_mc_power_savings);
7887}
7888static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7889 const char *buf, size_t count)
7890{
7891 return sched_power_savings_store(buf, count, 0);
7892}
7893static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7894 sched_mc_power_savings_store);
7895#endif
7896
7897#ifdef CONFIG_SCHED_SMT
7898static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7899{
7900 return sprintf(page, "%u\n", sched_smt_power_savings);
7901}
7902static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7903 const char *buf, size_t count)
7904{
7905 return sched_power_savings_store(buf, count, 1);
7906}
7907static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7908 sched_smt_power_savings_store);
7909#endif
7910
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007911int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7912{
7913 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007914
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007915#ifdef CONFIG_SCHED_SMT
7916 if (smt_capable())
7917 err = sysfs_create_file(&cls->kset.kobj,
7918 &attr_sched_smt_power_savings.attr);
7919#endif
7920#ifdef CONFIG_SCHED_MC
7921 if (!err && mc_capable())
7922 err = sysfs_create_file(&cls->kset.kobj,
7923 &attr_sched_mc_power_savings.attr);
7924#endif
7925 return err;
7926}
7927#endif
7928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007930 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007932 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933 * which will prevent rebalancing while the sched domains are recalculated.
7934 */
7935static int update_sched_domains(struct notifier_block *nfb,
7936 unsigned long action, void *hcpu)
7937{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 switch (action) {
7939 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007940 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007942 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007943 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 return NOTIFY_OK;
7945
7946 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007947 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007949 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007951 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007953 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954 /*
7955 * Fall through and re-initialise the domains.
7956 */
7957 break;
7958 default:
7959 return NOTIFY_DONE;
7960 }
7961
7962 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007963 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964
7965 return NOTIFY_OK;
7966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
7968void __init sched_init_smp(void)
7969{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007970 cpumask_t non_isolated_cpus;
7971
Mike Travis434d53b2008-04-04 18:11:04 -07007972#if defined(CONFIG_NUMA)
7973 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7974 GFP_KERNEL);
7975 BUG_ON(sched_group_nodes_bycpu == NULL);
7976#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007977 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007978 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007979 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007980 if (cpus_empty(non_isolated_cpus))
7981 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007982 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 /* XXX: Theoretical race here - CPU may be hotplugged now */
7984 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007985
7986 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007987 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007988 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007989 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990}
7991#else
7992void __init sched_init_smp(void)
7993{
Mike Travis434d53b2008-04-04 18:11:04 -07007994#if defined(CONFIG_NUMA)
7995 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7996 GFP_KERNEL);
7997 BUG_ON(sched_group_nodes_bycpu == NULL);
7998#endif
Ingo Molnar19978ca2007-11-09 22:39:38 +01007999 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000}
8001#endif /* CONFIG_SMP */
8002
8003int in_sched_functions(unsigned long addr)
8004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 return in_lock_functions(addr) ||
8006 (addr >= (unsigned long)__sched_text_start
8007 && addr < (unsigned long)__sched_text_end);
8008}
8009
Alexey Dobriyana9957442007-10-15 17:00:13 +02008010static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008011{
8012 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008013 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008014#ifdef CONFIG_FAIR_GROUP_SCHED
8015 cfs_rq->rq = rq;
8016#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008017 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008018}
8019
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008020static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8021{
8022 struct rt_prio_array *array;
8023 int i;
8024
8025 array = &rt_rq->active;
8026 for (i = 0; i < MAX_RT_PRIO; i++) {
8027 INIT_LIST_HEAD(array->queue + i);
8028 __clear_bit(i, array->bitmap);
8029 }
8030 /* delimiter for bitsearch: */
8031 __set_bit(MAX_RT_PRIO, array->bitmap);
8032
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008033#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008034 rt_rq->highest_prio = MAX_RT_PRIO;
8035#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008036#ifdef CONFIG_SMP
8037 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008038 rt_rq->overloaded = 0;
8039#endif
8040
8041 rt_rq->rt_time = 0;
8042 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008043 rt_rq->rt_runtime = 0;
8044 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008045
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008046#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008047 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008048 rt_rq->rq = rq;
8049#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008050}
8051
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008052#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008053static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8054 struct sched_entity *se, int cpu, int add,
8055 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008056{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008057 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008058 tg->cfs_rq[cpu] = cfs_rq;
8059 init_cfs_rq(cfs_rq, rq);
8060 cfs_rq->tg = tg;
8061 if (add)
8062 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8063
8064 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008065 /* se could be NULL for init_task_group */
8066 if (!se)
8067 return;
8068
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008069 if (!parent)
8070 se->cfs_rq = &rq->cfs;
8071 else
8072 se->cfs_rq = parent->my_q;
8073
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008074 se->my_q = cfs_rq;
8075 se->load.weight = tg->shares;
8076 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008077 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008078}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008079#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008080
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008081#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008082static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8083 struct sched_rt_entity *rt_se, int cpu, int add,
8084 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008085{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008086 struct rq *rq = cpu_rq(cpu);
8087
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008088 tg->rt_rq[cpu] = rt_rq;
8089 init_rt_rq(rt_rq, rq);
8090 rt_rq->tg = tg;
8091 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008092 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008093 if (add)
8094 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8095
8096 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008097 if (!rt_se)
8098 return;
8099
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008100 if (!parent)
8101 rt_se->rt_rq = &rq->rt;
8102 else
8103 rt_se->rt_rq = parent->my_q;
8104
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008105 rt_se->rt_rq = &rq->rt;
8106 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008107 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008108 INIT_LIST_HEAD(&rt_se->run_list);
8109}
8110#endif
8111
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112void __init sched_init(void)
8113{
Ingo Molnardd41f592007-07-09 18:51:59 +02008114 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008115 unsigned long alloc_size = 0, ptr;
8116
8117#ifdef CONFIG_FAIR_GROUP_SCHED
8118 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8119#endif
8120#ifdef CONFIG_RT_GROUP_SCHED
8121 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8122#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008123#ifdef CONFIG_USER_SCHED
8124 alloc_size *= 2;
8125#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008126 /*
8127 * As sched_init() is called before page_alloc is setup,
8128 * we use alloc_bootmem().
8129 */
8130 if (alloc_size) {
8131 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
8132
8133#ifdef CONFIG_FAIR_GROUP_SCHED
8134 init_task_group.se = (struct sched_entity **)ptr;
8135 ptr += nr_cpu_ids * sizeof(void **);
8136
8137 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8138 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008139
8140#ifdef CONFIG_USER_SCHED
8141 root_task_group.se = (struct sched_entity **)ptr;
8142 ptr += nr_cpu_ids * sizeof(void **);
8143
8144 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8145 ptr += nr_cpu_ids * sizeof(void **);
8146#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008147#endif
8148#ifdef CONFIG_RT_GROUP_SCHED
8149 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8150 ptr += nr_cpu_ids * sizeof(void **);
8151
8152 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008153 ptr += nr_cpu_ids * sizeof(void **);
8154
8155#ifdef CONFIG_USER_SCHED
8156 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8157 ptr += nr_cpu_ids * sizeof(void **);
8158
8159 root_task_group.rt_rq = (struct rt_rq **)ptr;
8160 ptr += nr_cpu_ids * sizeof(void **);
8161#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008162#endif
8163 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008164
Gregory Haskins57d885f2008-01-25 21:08:18 +01008165#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008166 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008167 init_defrootdomain();
8168#endif
8169
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008170 init_rt_bandwidth(&def_rt_bandwidth,
8171 global_rt_period(), global_rt_runtime());
8172
8173#ifdef CONFIG_RT_GROUP_SCHED
8174 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8175 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008176#ifdef CONFIG_USER_SCHED
8177 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8178 global_rt_period(), RUNTIME_INF);
8179#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008180#endif
8181
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008182#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008183 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008184 INIT_LIST_HEAD(&init_task_group.children);
8185
8186#ifdef CONFIG_USER_SCHED
8187 INIT_LIST_HEAD(&root_task_group.children);
8188 init_task_group.parent = &root_task_group;
8189 list_add(&init_task_group.siblings, &root_task_group.children);
8190#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008191#endif
8192
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008193 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008194 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
8196 rq = cpu_rq(i);
8197 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008198 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008199 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008200 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008201 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008202 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008203 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008204#ifdef CONFIG_FAIR_GROUP_SCHED
8205 init_task_group.shares = init_task_group_load;
8206 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008207#ifdef CONFIG_CGROUP_SCHED
8208 /*
8209 * How much cpu bandwidth does init_task_group get?
8210 *
8211 * In case of task-groups formed thr' the cgroup filesystem, it
8212 * gets 100% of the cpu resources in the system. This overall
8213 * system cpu resource is divided among the tasks of
8214 * init_task_group and its child task-groups in a fair manner,
8215 * based on each entity's (task or task-group's) weight
8216 * (se->load.weight).
8217 *
8218 * In other words, if init_task_group has 10 tasks of weight
8219 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8220 * then A0's share of the cpu resource is:
8221 *
8222 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8223 *
8224 * We achieve this by letting init_task_group's tasks sit
8225 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8226 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008227 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008228#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008229 root_task_group.shares = NICE_0_LOAD;
8230 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008231 /*
8232 * In case of task-groups formed thr' the user id of tasks,
8233 * init_task_group represents tasks belonging to root user.
8234 * Hence it forms a sibling of all subsequent groups formed.
8235 * In this case, init_task_group gets only a fraction of overall
8236 * system cpu resource, based on the weight assigned to root
8237 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8238 * by letting tasks of init_task_group sit in a separate cfs_rq
8239 * (init_cfs_rq) and having one entity represent this group of
8240 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8241 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008242 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008243 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008244 &per_cpu(init_sched_entity, i), i, 1,
8245 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008246
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008247#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008248#endif /* CONFIG_FAIR_GROUP_SCHED */
8249
8250 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008251#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008252 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008253#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008254 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008255#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008256 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008257 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008258 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008259 &per_cpu(init_sched_rt_entity, i), i, 1,
8260 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008261#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263
Ingo Molnardd41f592007-07-09 18:51:59 +02008264 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8265 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008267 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008268 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008270 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008272 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 rq->migration_thread = NULL;
8274 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008275 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008277 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 }
8280
Peter Williams2dd73a42006-06-27 02:54:34 -07008281 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008282
Avi Kivitye107be32007-07-26 13:40:43 +02008283#ifdef CONFIG_PREEMPT_NOTIFIERS
8284 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8285#endif
8286
Christoph Lameterc9819f42006-12-10 02:20:25 -08008287#ifdef CONFIG_SMP
8288 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8289#endif
8290
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008291#ifdef CONFIG_RT_MUTEXES
8292 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8293#endif
8294
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 /*
8296 * The boot idle thread does lazy MMU switching as well:
8297 */
8298 atomic_inc(&init_mm.mm_count);
8299 enter_lazy_tlb(&init_mm, current);
8300
8301 /*
8302 * Make us the idle thread. Technically, schedule() should not be
8303 * called from this thread, however somewhere below it might be,
8304 * but because we are the idle thread, we just pick up running again
8305 * when this runqueue becomes "idle".
8306 */
8307 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008308 /*
8309 * During early bootup we pretend to be a normal task:
8310 */
8311 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008312
8313 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314}
8315
8316#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8317void __might_sleep(char *file, int line)
8318{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008319#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 static unsigned long prev_jiffy; /* ratelimiting */
8321
8322 if ((in_atomic() || irqs_disabled()) &&
8323 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8324 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8325 return;
8326 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008327 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 " context at %s:%d\n", file, line);
8329 printk("in_atomic():%d, irqs_disabled():%d\n",
8330 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008331 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008332 if (irqs_disabled())
8333 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334 dump_stack();
8335 }
8336#endif
8337}
8338EXPORT_SYMBOL(__might_sleep);
8339#endif
8340
8341#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008342static void normalize_task(struct rq *rq, struct task_struct *p)
8343{
8344 int on_rq;
8345 update_rq_clock(rq);
8346 on_rq = p->se.on_rq;
8347 if (on_rq)
8348 deactivate_task(rq, p, 0);
8349 __setscheduler(rq, p, SCHED_NORMAL, 0);
8350 if (on_rq) {
8351 activate_task(rq, p, 0);
8352 resched_task(rq->curr);
8353 }
8354}
8355
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356void normalize_rt_tasks(void)
8357{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008358 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008360 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008362 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008363 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008364 /*
8365 * Only normalize user tasks:
8366 */
8367 if (!p->mm)
8368 continue;
8369
Ingo Molnardd41f592007-07-09 18:51:59 +02008370 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008371#ifdef CONFIG_SCHEDSTATS
8372 p->se.wait_start = 0;
8373 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008374 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008375#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008376 task_rq(p)->clock = 0;
8377
8378 if (!rt_task(p)) {
8379 /*
8380 * Renice negative nice level userspace
8381 * tasks back to 0:
8382 */
8383 if (TASK_NICE(p) < 0 && p->mm)
8384 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008388 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008389 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390
Ingo Molnar178be792007-10-15 17:00:18 +02008391 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008392
Ingo Molnarb29739f2006-06-27 02:54:51 -07008393 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008394 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008395 } while_each_thread(g, p);
8396
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008397 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398}
8399
8400#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008401
8402#ifdef CONFIG_IA64
8403/*
8404 * These functions are only useful for the IA64 MCA handling.
8405 *
8406 * They can only be called when the whole system has been
8407 * stopped - every CPU needs to be quiescent, and no scheduling
8408 * activity can take place. Using them for anything else would
8409 * be a serious bug, and as a result, they aren't even visible
8410 * under any other configuration.
8411 */
8412
8413/**
8414 * curr_task - return the current task for a given cpu.
8415 * @cpu: the processor in question.
8416 *
8417 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8418 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008419struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008420{
8421 return cpu_curr(cpu);
8422}
8423
8424/**
8425 * set_curr_task - set the current task for a given cpu.
8426 * @cpu: the processor in question.
8427 * @p: the task pointer to set.
8428 *
8429 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008430 * are serviced on a separate stack. It allows the architecture to switch the
8431 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008432 * must be called with all CPU's synchronized, and interrupts disabled, the
8433 * and caller must save the original value of the current task (see
8434 * curr_task() above) and restore that value before reenabling interrupts and
8435 * re-starting the system.
8436 *
8437 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8438 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008439void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008440{
8441 cpu_curr(cpu) = p;
8442}
8443
8444#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008445
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008446#ifdef CONFIG_FAIR_GROUP_SCHED
8447static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008448{
8449 int i;
8450
8451 for_each_possible_cpu(i) {
8452 if (tg->cfs_rq)
8453 kfree(tg->cfs_rq[i]);
8454 if (tg->se)
8455 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008456 }
8457
8458 kfree(tg->cfs_rq);
8459 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008460}
8461
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008462static
8463int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008464{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008465 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008466 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008467 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008468 int i;
8469
Mike Travis434d53b2008-04-04 18:11:04 -07008470 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008471 if (!tg->cfs_rq)
8472 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008473 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008474 if (!tg->se)
8475 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008476
8477 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008478
8479 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008480 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008481
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008482 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8483 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008484 if (!cfs_rq)
8485 goto err;
8486
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008487 se = kmalloc_node(sizeof(struct sched_entity),
8488 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008489 if (!se)
8490 goto err;
8491
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008492 parent_se = parent ? parent->se[i] : NULL;
8493 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008494 }
8495
8496 return 1;
8497
8498 err:
8499 return 0;
8500}
8501
8502static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8503{
8504 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8505 &cpu_rq(cpu)->leaf_cfs_rq_list);
8506}
8507
8508static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8509{
8510 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8511}
8512#else
8513static inline void free_fair_sched_group(struct task_group *tg)
8514{
8515}
8516
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008517static inline
8518int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008519{
8520 return 1;
8521}
8522
8523static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8524{
8525}
8526
8527static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8528{
8529}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008530#endif
8531
8532#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008533static void free_rt_sched_group(struct task_group *tg)
8534{
8535 int i;
8536
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008537 destroy_rt_bandwidth(&tg->rt_bandwidth);
8538
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008539 for_each_possible_cpu(i) {
8540 if (tg->rt_rq)
8541 kfree(tg->rt_rq[i]);
8542 if (tg->rt_se)
8543 kfree(tg->rt_se[i]);
8544 }
8545
8546 kfree(tg->rt_rq);
8547 kfree(tg->rt_se);
8548}
8549
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008550static
8551int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008552{
8553 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008554 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008555 struct rq *rq;
8556 int i;
8557
Mike Travis434d53b2008-04-04 18:11:04 -07008558 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008559 if (!tg->rt_rq)
8560 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008561 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008562 if (!tg->rt_se)
8563 goto err;
8564
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008565 init_rt_bandwidth(&tg->rt_bandwidth,
8566 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008567
8568 for_each_possible_cpu(i) {
8569 rq = cpu_rq(i);
8570
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008571 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8572 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8573 if (!rt_rq)
8574 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008575
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008576 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8577 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8578 if (!rt_se)
8579 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008580
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008581 parent_se = parent ? parent->rt_se[i] : NULL;
8582 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008583 }
8584
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008585 return 1;
8586
8587 err:
8588 return 0;
8589}
8590
8591static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8592{
8593 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8594 &cpu_rq(cpu)->leaf_rt_rq_list);
8595}
8596
8597static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8598{
8599 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8600}
8601#else
8602static inline void free_rt_sched_group(struct task_group *tg)
8603{
8604}
8605
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008606static inline
8607int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008608{
8609 return 1;
8610}
8611
8612static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8613{
8614}
8615
8616static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8617{
8618}
8619#endif
8620
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008621#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008622static void free_sched_group(struct task_group *tg)
8623{
8624 free_fair_sched_group(tg);
8625 free_rt_sched_group(tg);
8626 kfree(tg);
8627}
8628
8629/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008630struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008631{
8632 struct task_group *tg;
8633 unsigned long flags;
8634 int i;
8635
8636 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8637 if (!tg)
8638 return ERR_PTR(-ENOMEM);
8639
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008640 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008641 goto err;
8642
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008643 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008644 goto err;
8645
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008646 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008647 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008648 register_fair_sched_group(tg, i);
8649 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008650 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008651 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008652
8653 WARN_ON(!parent); /* root should already exist */
8654
8655 tg->parent = parent;
8656 list_add_rcu(&tg->siblings, &parent->children);
8657 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008658 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008659
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008660 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008661
8662err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008663 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008664 return ERR_PTR(-ENOMEM);
8665}
8666
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008667/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008668static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008669{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008670 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008671 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008672}
8673
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008674/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008675void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008676{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008677 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008678 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008679
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008680 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008681 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008682 unregister_fair_sched_group(tg, i);
8683 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008684 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008685 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008686 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008687 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008688
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008689 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008690 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008691}
8692
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008693/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008694 * The caller of this function should have put the task in its new group
8695 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8696 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008697 */
8698void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008699{
8700 int on_rq, running;
8701 unsigned long flags;
8702 struct rq *rq;
8703
8704 rq = task_rq_lock(tsk, &flags);
8705
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008706 update_rq_clock(rq);
8707
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008708 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008709 on_rq = tsk->se.on_rq;
8710
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008711 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008712 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008713 if (unlikely(running))
8714 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008715
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008716 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008717
Peter Zijlstra810b3812008-02-29 15:21:01 -05008718#ifdef CONFIG_FAIR_GROUP_SCHED
8719 if (tsk->sched_class->moved_group)
8720 tsk->sched_class->moved_group(tsk);
8721#endif
8722
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008723 if (unlikely(running))
8724 tsk->sched_class->set_curr_task(rq);
8725 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008726 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008727
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008728 task_rq_unlock(rq, &flags);
8729}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008730#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008731
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008732#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008733static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008734{
8735 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008736 int on_rq;
8737
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008738 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008739 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740 dequeue_entity(cfs_rq, se, 0);
8741
8742 se->load.weight = shares;
8743 se->load.inv_weight = div64_64((1ULL<<32), shares);
8744
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008745 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008746 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008747}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008748
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008749static void set_se_shares(struct sched_entity *se, unsigned long shares)
8750{
8751 struct cfs_rq *cfs_rq = se->cfs_rq;
8752 struct rq *rq = cfs_rq->rq;
8753 unsigned long flags;
8754
8755 spin_lock_irqsave(&rq->lock, flags);
8756 __set_se_shares(se, shares);
8757 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008758}
8759
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008760static DEFINE_MUTEX(shares_mutex);
8761
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008762int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008763{
8764 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008765 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008766
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008767 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008768 * We can't change the weight of the root cgroup.
8769 */
8770 if (!tg->se[0])
8771 return -EINVAL;
8772
8773 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008774 * A weight of 0 or 1 can cause arithmetics problems.
8775 * (The default weight is 1024 - so there's no practical
8776 * limitation from this.)
8777 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008778 if (shares < MIN_SHARES)
8779 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008780
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008781 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008782 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008783 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008784
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008785 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008786 for_each_possible_cpu(i)
8787 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008788 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008789 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008790
8791 /* wait for any ongoing reference to this group to finish */
8792 synchronize_sched();
8793
8794 /*
8795 * Now we are free to modify the group's share on each cpu
8796 * w/o tripping rebalance_share or load_balance_fair.
8797 */
8798 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008799 for_each_possible_cpu(i) {
8800 /*
8801 * force a rebalance
8802 */
8803 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8804 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8805 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008806
8807 /*
8808 * Enable load balance activity on this group, by inserting it back on
8809 * each cpu's rq->leaf_cfs_rq_list.
8810 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008811 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008812 for_each_possible_cpu(i)
8813 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008814 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008815 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008816done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008817 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008818 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008819}
8820
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008821unsigned long sched_group_shares(struct task_group *tg)
8822{
8823 return tg->shares;
8824}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008825#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008826
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008827#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008828/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008829 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008830 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008831static DEFINE_MUTEX(rt_constraints_mutex);
8832
8833static unsigned long to_ratio(u64 period, u64 runtime)
8834{
8835 if (runtime == RUNTIME_INF)
8836 return 1ULL << 16;
8837
Peter Zijlstra2692a242008-02-27 12:00:46 +01008838 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008839}
8840
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008841#ifdef CONFIG_CGROUP_SCHED
8842static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8843{
8844 struct task_group *tgi, *parent = tg->parent;
8845 unsigned long total = 0;
8846
8847 if (!parent) {
8848 if (global_rt_period() < period)
8849 return 0;
8850
8851 return to_ratio(period, runtime) <
8852 to_ratio(global_rt_period(), global_rt_runtime());
8853 }
8854
8855 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8856 return 0;
8857
8858 rcu_read_lock();
8859 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8860 if (tgi == tg)
8861 continue;
8862
8863 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8864 tgi->rt_bandwidth.rt_runtime);
8865 }
8866 rcu_read_unlock();
8867
8868 return total + to_ratio(period, runtime) <
8869 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8870 parent->rt_bandwidth.rt_runtime);
8871}
8872#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008873static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008874{
8875 struct task_group *tgi;
8876 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008877 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008878 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008879
8880 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008881 list_for_each_entry_rcu(tgi, &task_groups, list) {
8882 if (tgi == tg)
8883 continue;
8884
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008885 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8886 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008887 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008888 rcu_read_unlock();
8889
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008890 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008891}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008892#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008893
Dhaval Giani521f1a242008-02-28 15:21:56 +05308894/* Must be called with tasklist_lock held */
8895static inline int tg_has_rt_tasks(struct task_group *tg)
8896{
8897 struct task_struct *g, *p;
8898 do_each_thread(g, p) {
8899 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8900 return 1;
8901 } while_each_thread(g, p);
8902 return 0;
8903}
8904
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008905static int tg_set_bandwidth(struct task_group *tg,
8906 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008907{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008908 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008909
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008910 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308911 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008912 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308913 err = -EBUSY;
8914 goto unlock;
8915 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008916 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8917 err = -EINVAL;
8918 goto unlock;
8919 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008920
8921 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008922 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8923 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008924
8925 for_each_possible_cpu(i) {
8926 struct rt_rq *rt_rq = tg->rt_rq[i];
8927
8928 spin_lock(&rt_rq->rt_runtime_lock);
8929 rt_rq->rt_runtime = rt_runtime;
8930 spin_unlock(&rt_rq->rt_runtime_lock);
8931 }
8932 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008933 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308934 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008935 mutex_unlock(&rt_constraints_mutex);
8936
8937 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008938}
8939
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008940int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8941{
8942 u64 rt_runtime, rt_period;
8943
8944 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8945 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8946 if (rt_runtime_us < 0)
8947 rt_runtime = RUNTIME_INF;
8948
8949 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8950}
8951
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008952long sched_group_rt_runtime(struct task_group *tg)
8953{
8954 u64 rt_runtime_us;
8955
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008956 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008957 return -1;
8958
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008959 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008960 do_div(rt_runtime_us, NSEC_PER_USEC);
8961 return rt_runtime_us;
8962}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008963
8964int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8965{
8966 u64 rt_runtime, rt_period;
8967
8968 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8969 rt_runtime = tg->rt_bandwidth.rt_runtime;
8970
8971 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8972}
8973
8974long sched_group_rt_period(struct task_group *tg)
8975{
8976 u64 rt_period_us;
8977
8978 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8979 do_div(rt_period_us, NSEC_PER_USEC);
8980 return rt_period_us;
8981}
8982
8983static int sched_rt_global_constraints(void)
8984{
8985 int ret = 0;
8986
8987 mutex_lock(&rt_constraints_mutex);
8988 if (!__rt_schedulable(NULL, 1, 0))
8989 ret = -EINVAL;
8990 mutex_unlock(&rt_constraints_mutex);
8991
8992 return ret;
8993}
8994#else
8995static int sched_rt_global_constraints(void)
8996{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008997 unsigned long flags;
8998 int i;
8999
9000 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9001 for_each_possible_cpu(i) {
9002 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9003
9004 spin_lock(&rt_rq->rt_runtime_lock);
9005 rt_rq->rt_runtime = global_rt_runtime();
9006 spin_unlock(&rt_rq->rt_runtime_lock);
9007 }
9008 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9009
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009010 return 0;
9011}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009012#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009013
9014int sched_rt_handler(struct ctl_table *table, int write,
9015 struct file *filp, void __user *buffer, size_t *lenp,
9016 loff_t *ppos)
9017{
9018 int ret;
9019 int old_period, old_runtime;
9020 static DEFINE_MUTEX(mutex);
9021
9022 mutex_lock(&mutex);
9023 old_period = sysctl_sched_rt_period;
9024 old_runtime = sysctl_sched_rt_runtime;
9025
9026 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9027
9028 if (!ret && write) {
9029 ret = sched_rt_global_constraints();
9030 if (ret) {
9031 sysctl_sched_rt_period = old_period;
9032 sysctl_sched_rt_runtime = old_runtime;
9033 } else {
9034 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9035 def_rt_bandwidth.rt_period =
9036 ns_to_ktime(global_rt_period());
9037 }
9038 }
9039 mutex_unlock(&mutex);
9040
9041 return ret;
9042}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009043
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009044#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009045
9046/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009047static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009048{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009049 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9050 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009051}
9052
9053static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009054cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009055{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009056 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009057
Paul Menage2b01dfe2007-10-24 18:23:50 +02009058 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009059 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009060 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009061 return &init_task_group.css;
9062 }
9063
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009064 parent = cgroup_tg(cgrp->parent);
9065 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009066 if (IS_ERR(tg))
9067 return ERR_PTR(-ENOMEM);
9068
9069 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009070 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071
9072 return &tg->css;
9073}
9074
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009075static void
9076cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009078 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009079
9080 sched_destroy_group(tg);
9081}
9082
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009083static int
9084cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9085 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009086{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009087#ifdef CONFIG_RT_GROUP_SCHED
9088 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009089 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009090 return -EINVAL;
9091#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009092 /* We don't support RT-tasks being in separate groups */
9093 if (tsk->sched_class != &fair_sched_class)
9094 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009095#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009096
9097 return 0;
9098}
9099
9100static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009101cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009102 struct cgroup *old_cont, struct task_struct *tsk)
9103{
9104 sched_move_task(tsk);
9105}
9106
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009107#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02009108static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9109 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009110{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009111 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009112}
9113
Paul Menage2b01dfe2007-10-24 18:23:50 +02009114static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009115{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009116 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009117
9118 return (u64) tg->shares;
9119}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009120#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009121
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009122#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009123static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009124 struct file *file,
9125 const char __user *userbuf,
9126 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009127{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009128 char buffer[64];
9129 int retval = 0;
9130 s64 val;
9131 char *end;
9132
9133 if (!nbytes)
9134 return -EINVAL;
9135 if (nbytes >= sizeof(buffer))
9136 return -E2BIG;
9137 if (copy_from_user(buffer, userbuf, nbytes))
9138 return -EFAULT;
9139
9140 buffer[nbytes] = 0; /* nul-terminate */
9141
9142 /* strip newline if necessary */
9143 if (nbytes && (buffer[nbytes-1] == '\n'))
9144 buffer[nbytes-1] = 0;
9145 val = simple_strtoll(buffer, &end, 0);
9146 if (*end)
9147 return -EINVAL;
9148
9149 /* Pass to subsystem */
9150 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
9151 if (!retval)
9152 retval = nbytes;
9153 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009154}
9155
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009156static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
9157 struct file *file,
9158 char __user *buf, size_t nbytes,
9159 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009160{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009161 char tmp[64];
9162 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
9163 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009164
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009165 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009166}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009167
9168static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9169 u64 rt_period_us)
9170{
9171 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9172}
9173
9174static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9175{
9176 return sched_group_rt_period(cgroup_tg(cgrp));
9177}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009178#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009179
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009180static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009181#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009182 {
9183 .name = "shares",
9184 .read_uint = cpu_shares_read_uint,
9185 .write_uint = cpu_shares_write_uint,
9186 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009187#endif
9188#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009189 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009190 .name = "rt_runtime_us",
9191 .read = cpu_rt_runtime_read,
9192 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009193 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009194 {
9195 .name = "rt_period_us",
9196 .read_uint = cpu_rt_period_read_uint,
9197 .write_uint = cpu_rt_period_write_uint,
9198 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009199#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009200};
9201
9202static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9203{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009204 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009205}
9206
9207struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009208 .name = "cpu",
9209 .create = cpu_cgroup_create,
9210 .destroy = cpu_cgroup_destroy,
9211 .can_attach = cpu_cgroup_can_attach,
9212 .attach = cpu_cgroup_attach,
9213 .populate = cpu_cgroup_populate,
9214 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009215 .early_init = 1,
9216};
9217
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009218#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009219
9220#ifdef CONFIG_CGROUP_CPUACCT
9221
9222/*
9223 * CPU accounting code for task groups.
9224 *
9225 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9226 * (balbir@in.ibm.com).
9227 */
9228
9229/* track cpu usage of a group of tasks */
9230struct cpuacct {
9231 struct cgroup_subsys_state css;
9232 /* cpuusage holds pointer to a u64-type object on every cpu */
9233 u64 *cpuusage;
9234};
9235
9236struct cgroup_subsys cpuacct_subsys;
9237
9238/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309239static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009240{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309241 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009242 struct cpuacct, css);
9243}
9244
9245/* return cpu accounting group to which this task belongs */
9246static inline struct cpuacct *task_ca(struct task_struct *tsk)
9247{
9248 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9249 struct cpuacct, css);
9250}
9251
9252/* create a new cpu accounting group */
9253static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309254 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009255{
9256 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9257
9258 if (!ca)
9259 return ERR_PTR(-ENOMEM);
9260
9261 ca->cpuusage = alloc_percpu(u64);
9262 if (!ca->cpuusage) {
9263 kfree(ca);
9264 return ERR_PTR(-ENOMEM);
9265 }
9266
9267 return &ca->css;
9268}
9269
9270/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009271static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309272cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009273{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309274 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009275
9276 free_percpu(ca->cpuusage);
9277 kfree(ca);
9278}
9279
9280/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309281static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009282{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309283 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009284 u64 totalcpuusage = 0;
9285 int i;
9286
9287 for_each_possible_cpu(i) {
9288 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9289
9290 /*
9291 * Take rq->lock to make 64-bit addition safe on 32-bit
9292 * platforms.
9293 */
9294 spin_lock_irq(&cpu_rq(i)->lock);
9295 totalcpuusage += *cpuusage;
9296 spin_unlock_irq(&cpu_rq(i)->lock);
9297 }
9298
9299 return totalcpuusage;
9300}
9301
Dhaval Giani0297b802008-02-29 10:02:44 +05309302static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9303 u64 reset)
9304{
9305 struct cpuacct *ca = cgroup_ca(cgrp);
9306 int err = 0;
9307 int i;
9308
9309 if (reset) {
9310 err = -EINVAL;
9311 goto out;
9312 }
9313
9314 for_each_possible_cpu(i) {
9315 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9316
9317 spin_lock_irq(&cpu_rq(i)->lock);
9318 *cpuusage = 0;
9319 spin_unlock_irq(&cpu_rq(i)->lock);
9320 }
9321out:
9322 return err;
9323}
9324
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009325static struct cftype files[] = {
9326 {
9327 .name = "usage",
9328 .read_uint = cpuusage_read,
Dhaval Giani0297b802008-02-29 10:02:44 +05309329 .write_uint = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009330 },
9331};
9332
Dhaval Giani32cd7562008-02-29 10:02:43 +05309333static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009334{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309335 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009336}
9337
9338/*
9339 * charge this task's execution time to its accounting group.
9340 *
9341 * called with rq->lock held.
9342 */
9343static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9344{
9345 struct cpuacct *ca;
9346
9347 if (!cpuacct_subsys.active)
9348 return;
9349
9350 ca = task_ca(tsk);
9351 if (ca) {
9352 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9353
9354 *cpuusage += cputime;
9355 }
9356}
9357
9358struct cgroup_subsys cpuacct_subsys = {
9359 .name = "cpuacct",
9360 .create = cpuacct_create,
9361 .destroy = cpuacct_destroy,
9362 .populate = cpuacct_populate,
9363 .subsys_id = cpuacct_subsys_id,
9364};
9365#endif /* CONFIG_CGROUP_CPUACCT */