blob: bee9cbe13c1548aec429b298f41f448d3f3266c6 [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 Molnar62160e3f2007-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 Haskinsa22d7fc12008-01-25 21:08:12 +0100463 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100464#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100465 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100466 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200467 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100468 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200469 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100470
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100471#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100472 unsigned long rt_nr_boosted;
473
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100474 struct rq *rq;
475 struct list_head leaf_rt_rq_list;
476 struct task_group *tg;
477 struct sched_rt_entity *rt_se;
478#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200479};
480
Gregory Haskins57d885f2008-01-25 21:08:18 +0100481#ifdef CONFIG_SMP
482
483/*
484 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100485 * variables. Each exclusive cpuset essentially defines an island domain by
486 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100487 * exclusive cpuset is created, we also create and attach a new root-domain
488 * object.
489 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100490 */
491struct root_domain {
492 atomic_t refcount;
493 cpumask_t span;
494 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100495
Ingo Molnar0eab9142008-01-25 21:08:19 +0100496 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100497 * The "RT overload" flag: it gets set if a CPU has more than
498 * one runnable RT task.
499 */
500 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100501 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100502};
503
Gregory Haskinsdc938522008-01-25 21:08:26 +0100504/*
505 * By default the system creates a single root-domain with all cpus as
506 * members (mimicking the global state we have today).
507 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100508static struct root_domain def_root_domain;
509
510#endif
511
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * This is the main, per-CPU runqueue data structure.
514 *
515 * Locking rule: those places that want to lock multiple runqueues
516 * (such as the load balancing or the thread migration code), lock
517 * acquire operations must be ordered by ascending &runqueue.
518 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700519struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200520 /* runqueue lock: */
521 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * nr_running and cpu_load should be in the same cacheline because
525 * remote CPUs use both these fields when doing load calculation.
526 */
527 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200528 #define CPU_LOAD_IDX_MAX 5
529 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700530 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700531#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200532 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700533 unsigned char in_nohz_recently;
534#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200535 /* capture load from *all* tasks on this cpu: */
536 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537 unsigned long nr_load_updates;
538 u64 nr_switches;
539
540 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100541 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100542
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200543#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200544 /* list of leaf cfs_rq on this cpu: */
545 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100546#endif
547#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100548 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * This is part of a global counter where only the total sum
553 * over all CPUs matters. A task can increase this counter on
554 * one CPU and if it got migrated afterwards it may decrease
555 * it on another CPU. Always updated under the runqueue lock:
556 */
557 unsigned long nr_uninterruptible;
558
Ingo Molnar36c8b582006-07-03 00:25:41 -0700559 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800560 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200562
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200563 u64 clock, prev_clock_raw;
564 s64 clock_max_delta;
565
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100566 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200567 u64 idle_clock;
568 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200569 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 atomic_t nr_iowait;
572
573#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100574 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct sched_domain *sd;
576
577 /* For active balancing */
578 int active_balance;
579 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200580 /* cpu of this runqueue: */
581 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar36c8b582006-07-03 00:25:41 -0700583 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct list_head migration_queue;
585#endif
586
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100587#ifdef CONFIG_SCHED_HRTICK
588 unsigned long hrtick_flags;
589 ktime_t hrtick_expire;
590 struct hrtimer hrtick_timer;
591#endif
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#ifdef CONFIG_SCHEDSTATS
594 /* latency stats */
595 struct sched_info rq_sched_info;
596
597 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200598 unsigned int yld_exp_empty;
599 unsigned int yld_act_empty;
600 unsigned int yld_both_empty;
601 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200604 unsigned int sched_switch;
605 unsigned int sched_count;
606 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200609 unsigned int ttwu_count;
610 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200611
612 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200613 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700615 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616};
617
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700618static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ingo Molnardd41f592007-07-09 18:51:59 +0200620static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
621{
622 rq->curr->sched_class->check_preempt_curr(rq, p);
623}
624
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700625static inline int cpu_of(struct rq *rq)
626{
627#ifdef CONFIG_SMP
628 return rq->cpu;
629#else
630 return 0;
631#endif
632}
633
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200634#ifdef CONFIG_NO_HZ
635static inline bool nohz_on(int cpu)
636{
637 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
638}
639
640static inline u64 max_skipped_ticks(struct rq *rq)
641{
642 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
643}
644
645static inline void update_last_tick_seen(struct rq *rq)
646{
647 rq->last_tick_seen = jiffies;
648}
649#else
650static inline u64 max_skipped_ticks(struct rq *rq)
651{
652 return 1;
653}
654
655static inline void update_last_tick_seen(struct rq *rq)
656{
657}
658#endif
659
Nick Piggin674311d2005-06-25 14:57:27 -0700660/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200661 * Update the per-runqueue clock, as finegrained as the platform can give
662 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200663 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200664static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200665{
666 u64 prev_raw = rq->prev_clock_raw;
667 u64 now = sched_clock();
668 s64 delta = now - prev_raw;
669 u64 clock = rq->clock;
670
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200671#ifdef CONFIG_SCHED_DEBUG
672 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
673#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200674 /*
675 * Protect against sched_clock() occasionally going backwards:
676 */
677 if (unlikely(delta < 0)) {
678 clock++;
679 rq->clock_warps++;
680 } else {
681 /*
682 * Catch too large forward jumps too:
683 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200684 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
685 u64 max_time = rq->tick_timestamp + max_jump;
686
687 if (unlikely(clock + delta > max_time)) {
688 if (clock < max_time)
689 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200690 else
691 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200692 rq->clock_overflows++;
693 } else {
694 if (unlikely(delta > rq->clock_max_delta))
695 rq->clock_max_delta = delta;
696 clock += delta;
697 }
698 }
699
700 rq->prev_clock_raw = now;
701 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200702}
703
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200704static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200705{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200706 if (likely(smp_processor_id() == cpu_of(rq)))
707 __update_rq_clock(rq);
708}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200709
Ingo Molnar20d315d2007-07-09 18:51:58 +0200710/*
Nick Piggin674311d2005-06-25 14:57:27 -0700711 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700712 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700713 *
714 * The domain tree of any CPU may only be accessed from within
715 * preempt-disabled sections.
716 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700717#define for_each_domain(cpu, __sd) \
718 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
721#define this_rq() (&__get_cpu_var(runqueues))
722#define task_rq(p) cpu_rq(task_cpu(p))
723#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
724
Ingo Molnare436d802007-07-19 21:28:35 +0200725/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200726 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
727 */
728#ifdef CONFIG_SCHED_DEBUG
729# define const_debug __read_mostly
730#else
731# define const_debug static const
732#endif
733
734/*
735 * Debugging: various feature bits
736 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200737
738#define SCHED_FEAT(name, enabled) \
739 __SCHED_FEAT_##name ,
740
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200741enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200742#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200743};
744
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200745#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200746
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200747#define SCHED_FEAT(name, enabled) \
748 (1UL << __SCHED_FEAT_##name) * enabled |
749
750const_debug unsigned int sysctl_sched_features =
751#include "sched_features.h"
752 0;
753
754#undef SCHED_FEAT
755
756#ifdef CONFIG_SCHED_DEBUG
757#define SCHED_FEAT(name, enabled) \
758 #name ,
759
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700760static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200761#include "sched_features.h"
762 NULL
763};
764
765#undef SCHED_FEAT
766
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700767static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200768{
769 filp->private_data = inode->i_private;
770 return 0;
771}
772
773static ssize_t
774sched_feat_read(struct file *filp, char __user *ubuf,
775 size_t cnt, loff_t *ppos)
776{
777 char *buf;
778 int r = 0;
779 int len = 0;
780 int i;
781
782 for (i = 0; sched_feat_names[i]; i++) {
783 len += strlen(sched_feat_names[i]);
784 len += 4;
785 }
786
787 buf = kmalloc(len + 2, GFP_KERNEL);
788 if (!buf)
789 return -ENOMEM;
790
791 for (i = 0; sched_feat_names[i]; i++) {
792 if (sysctl_sched_features & (1UL << i))
793 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
794 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200795 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200796 }
797
798 r += sprintf(buf + r, "\n");
799 WARN_ON(r >= len + 2);
800
801 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
802
803 kfree(buf);
804
805 return r;
806}
807
808static ssize_t
809sched_feat_write(struct file *filp, const char __user *ubuf,
810 size_t cnt, loff_t *ppos)
811{
812 char buf[64];
813 char *cmp = buf;
814 int neg = 0;
815 int i;
816
817 if (cnt > 63)
818 cnt = 63;
819
820 if (copy_from_user(&buf, ubuf, cnt))
821 return -EFAULT;
822
823 buf[cnt] = 0;
824
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200825 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200826 neg = 1;
827 cmp += 3;
828 }
829
830 for (i = 0; sched_feat_names[i]; i++) {
831 int len = strlen(sched_feat_names[i]);
832
833 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
834 if (neg)
835 sysctl_sched_features &= ~(1UL << i);
836 else
837 sysctl_sched_features |= (1UL << i);
838 break;
839 }
840 }
841
842 if (!sched_feat_names[i])
843 return -EINVAL;
844
845 filp->f_pos += cnt;
846
847 return cnt;
848}
849
850static struct file_operations sched_feat_fops = {
851 .open = sched_feat_open,
852 .read = sched_feat_read,
853 .write = sched_feat_write,
854};
855
856static __init int sched_init_debug(void)
857{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200858 debugfs_create_file("sched_features", 0644, NULL, NULL,
859 &sched_feat_fops);
860
861 return 0;
862}
863late_initcall(sched_init_debug);
864
865#endif
866
867#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200868
869/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100870 * Number of tasks to iterate in a single balance run.
871 * Limited because this is done with IRQs disabled.
872 */
873const_debug unsigned int sysctl_sched_nr_migrate = 32;
874
875/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100876 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100877 * default: 1s
878 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100879unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100880
Ingo Molnar6892b752008-02-13 14:02:36 +0100881static __read_mostly int scheduler_running;
882
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100883/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100884 * part of the period that we allow rt tasks to run in us.
885 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100886 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100887int sysctl_sched_rt_runtime = 950000;
888
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200889static inline u64 global_rt_period(void)
890{
891 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
892}
893
894static inline u64 global_rt_runtime(void)
895{
896 if (sysctl_sched_rt_period < 0)
897 return RUNTIME_INF;
898
899 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
900}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100901
Ingo Molnar27ec4402008-02-28 21:00:21 +0100902static const unsigned long long time_sync_thresh = 100000;
903
904static DEFINE_PER_CPU(unsigned long long, time_offset);
905static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
906
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100907/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100908 * Global lock which we take every now and then to synchronize
909 * the CPUs time. This method is not warp-safe, but it's good
910 * enough to synchronize slowly diverging time sources and thus
911 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200912 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100913static DEFINE_SPINLOCK(time_sync_lock);
914static unsigned long long prev_global_time;
915
916static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
917{
918 unsigned long flags;
919
920 spin_lock_irqsave(&time_sync_lock, flags);
921
922 if (time < prev_global_time) {
923 per_cpu(time_offset, cpu) += prev_global_time - time;
924 time = prev_global_time;
925 } else {
926 prev_global_time = time;
927 }
928
929 spin_unlock_irqrestore(&time_sync_lock, flags);
930
931 return time;
932}
933
934static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200935{
Ingo Molnare436d802007-07-19 21:28:35 +0200936 unsigned long long now;
937 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200938 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200939
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100940 /*
941 * Only call sched_clock() if the scheduler has already been
942 * initialized (some code might call cpu_clock() very early):
943 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100944 if (unlikely(!scheduler_running))
945 return 0;
946
947 local_irq_save(flags);
948 rq = cpu_rq(cpu);
949 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200950 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200951 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200952
953 return now;
954}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100955
956/*
957 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
958 * clock constructed from sched_clock():
959 */
960unsigned long long cpu_clock(int cpu)
961{
962 unsigned long long prev_cpu_time, time, delta_time;
963
964 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
965 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
966 delta_time = time-prev_cpu_time;
967
968 if (unlikely(delta_time > time_sync_thresh))
969 time = __sync_cpu_clock(time, cpu);
970
971 return time;
972}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200973EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700976# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700978#ifndef finish_arch_switch
979# define finish_arch_switch(prev) do { } while (0)
980#endif
981
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100982static inline int task_current(struct rq *rq, struct task_struct *p)
983{
984 return rq->curr == p;
985}
986
Nick Piggin4866cde2005-06-25 14:57:23 -0700987#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700988static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700989{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100990 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700991}
992
Ingo Molnar70b97a72006-07-03 00:25:42 -0700993static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700994{
995}
996
Ingo Molnar70b97a72006-07-03 00:25:42 -0700997static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700998{
Ingo Molnarda04c032005-09-13 11:17:59 +0200999#ifdef CONFIG_DEBUG_SPINLOCK
1000 /* this is a valid case when another task releases the spinlock */
1001 rq->lock.owner = current;
1002#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001003 /*
1004 * If we are tracking spinlock dependencies then we have to
1005 * fix up the runqueue lock - which gets 'carried over' from
1006 * prev into current:
1007 */
1008 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1009
Nick Piggin4866cde2005-06-25 14:57:23 -07001010 spin_unlock_irq(&rq->lock);
1011}
1012
1013#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001014static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -07001015{
1016#ifdef CONFIG_SMP
1017 return p->oncpu;
1018#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01001019 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -07001020#endif
1021}
1022
Ingo Molnar70b97a72006-07-03 00:25:42 -07001023static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001024{
1025#ifdef CONFIG_SMP
1026 /*
1027 * We can optimise this out completely for !SMP, because the
1028 * SMP rebalancing from interrupt is the only thing that cares
1029 * here.
1030 */
1031 next->oncpu = 1;
1032#endif
1033#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1034 spin_unlock_irq(&rq->lock);
1035#else
1036 spin_unlock(&rq->lock);
1037#endif
1038}
1039
Ingo Molnar70b97a72006-07-03 00:25:42 -07001040static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -07001041{
1042#ifdef CONFIG_SMP
1043 /*
1044 * After ->oncpu is cleared, the task can be moved to a different CPU.
1045 * We must ensure this doesn't happen until the switch is completely
1046 * finished.
1047 */
1048 smp_wmb();
1049 prev->oncpu = 0;
1050#endif
1051#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1052 local_irq_enable();
1053#endif
1054}
1055#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001058 * __task_rq_lock - lock the runqueue a given task resides on.
1059 * Must be called interrupts disabled.
1060 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001061static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001062 __acquires(rq->lock)
1063{
Andi Kleen3a5c3592007-10-15 17:00:14 +02001064 for (;;) {
1065 struct rq *rq = task_rq(p);
1066 spin_lock(&rq->lock);
1067 if (likely(rq == task_rq(p)))
1068 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001069 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001070 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001071}
1072
1073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001075 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * explicitly disabling preemption.
1077 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001078static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 __acquires(rq->lock)
1080{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001081 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Andi Kleen3a5c3592007-10-15 17:00:14 +02001083 for (;;) {
1084 local_irq_save(*flags);
1085 rq = task_rq(p);
1086 spin_lock(&rq->lock);
1087 if (likely(rq == task_rq(p)))
1088 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Alexey Dobriyana9957442007-10-15 17:00:13 +02001093static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001094 __releases(rq->lock)
1095{
1096 spin_unlock(&rq->lock);
1097}
1098
Ingo Molnar70b97a72006-07-03 00:25:42 -07001099static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 __releases(rq->lock)
1101{
1102 spin_unlock_irqrestore(&rq->lock, *flags);
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001106 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001108static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 __acquires(rq->lock)
1110{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001111 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 local_irq_disable();
1114 rq = this_rq();
1115 spin_lock(&rq->lock);
1116
1117 return rq;
1118}
1119
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001120/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001121 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001122 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001123void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001124{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001125 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001126
Andrew Mortond478c2c2008-04-26 11:30:34 -07001127 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001128 spin_lock(&rq->lock);
1129 __update_rq_clock(rq);
1130 spin_unlock(&rq->lock);
1131 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001132}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001133EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1134
1135/*
1136 * We just idled delta nanoseconds (called with irqs disabled):
1137 */
1138void sched_clock_idle_wakeup_event(u64 delta_ns)
1139{
1140 struct rq *rq = cpu_rq(smp_processor_id());
1141 u64 now = sched_clock();
1142
Andrew Mortond478c2c2008-04-26 11:30:34 -07001143 WARN_ON(!irqs_disabled());
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001144 rq->idle_clock += delta_ns;
1145 /*
1146 * Override the previous timestamp and ignore all
1147 * sched_clock() deltas that occured while we idled,
1148 * and use the PM-provided delta_ns to advance the
1149 * rq clock:
1150 */
1151 spin_lock(&rq->lock);
1152 rq->prev_clock_raw = now;
1153 rq->clock += delta_ns;
1154 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001155 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001156}
1157EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001158
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001159static void __resched_task(struct task_struct *p, int tif_bit);
1160
1161static inline void resched_task(struct task_struct *p)
1162{
1163 __resched_task(p, TIF_NEED_RESCHED);
1164}
1165
1166#ifdef CONFIG_SCHED_HRTICK
1167/*
1168 * Use HR-timers to deliver accurate preemption points.
1169 *
1170 * Its all a bit involved since we cannot program an hrt while holding the
1171 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1172 * reschedule event.
1173 *
1174 * When we get rescheduled we reprogram the hrtick_timer outside of the
1175 * rq->lock.
1176 */
1177static inline void resched_hrt(struct task_struct *p)
1178{
1179 __resched_task(p, TIF_HRTICK_RESCHED);
1180}
1181
1182static inline void resched_rq(struct rq *rq)
1183{
1184 unsigned long flags;
1185
1186 spin_lock_irqsave(&rq->lock, flags);
1187 resched_task(rq->curr);
1188 spin_unlock_irqrestore(&rq->lock, flags);
1189}
1190
1191enum {
1192 HRTICK_SET, /* re-programm hrtick_timer */
1193 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001194 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001195};
1196
1197/*
1198 * Use hrtick when:
1199 * - enabled by features
1200 * - hrtimer is actually high res
1201 */
1202static inline int hrtick_enabled(struct rq *rq)
1203{
1204 if (!sched_feat(HRTICK))
1205 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001206 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1207 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001208 return hrtimer_is_hres_active(&rq->hrtick_timer);
1209}
1210
1211/*
1212 * Called to set the hrtick timer state.
1213 *
1214 * called with rq->lock held and irqs disabled
1215 */
1216static void hrtick_start(struct rq *rq, u64 delay, int reset)
1217{
1218 assert_spin_locked(&rq->lock);
1219
1220 /*
1221 * preempt at: now + delay
1222 */
1223 rq->hrtick_expire =
1224 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1225 /*
1226 * indicate we need to program the timer
1227 */
1228 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1229 if (reset)
1230 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1231
1232 /*
1233 * New slices are called from the schedule path and don't need a
1234 * forced reschedule.
1235 */
1236 if (reset)
1237 resched_hrt(rq->curr);
1238}
1239
1240static void hrtick_clear(struct rq *rq)
1241{
1242 if (hrtimer_active(&rq->hrtick_timer))
1243 hrtimer_cancel(&rq->hrtick_timer);
1244}
1245
1246/*
1247 * Update the timer from the possible pending state.
1248 */
1249static void hrtick_set(struct rq *rq)
1250{
1251 ktime_t time;
1252 int set, reset;
1253 unsigned long flags;
1254
1255 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1256
1257 spin_lock_irqsave(&rq->lock, flags);
1258 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1259 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1260 time = rq->hrtick_expire;
1261 clear_thread_flag(TIF_HRTICK_RESCHED);
1262 spin_unlock_irqrestore(&rq->lock, flags);
1263
1264 if (set) {
1265 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1266 if (reset && !hrtimer_active(&rq->hrtick_timer))
1267 resched_rq(rq);
1268 } else
1269 hrtick_clear(rq);
1270}
1271
1272/*
1273 * High-resolution timer tick.
1274 * Runs from hardirq context with interrupts disabled.
1275 */
1276static enum hrtimer_restart hrtick(struct hrtimer *timer)
1277{
1278 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1279
1280 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1281
1282 spin_lock(&rq->lock);
1283 __update_rq_clock(rq);
1284 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1285 spin_unlock(&rq->lock);
1286
1287 return HRTIMER_NORESTART;
1288}
1289
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001290static void hotplug_hrtick_disable(int cpu)
1291{
1292 struct rq *rq = cpu_rq(cpu);
1293 unsigned long flags;
1294
1295 spin_lock_irqsave(&rq->lock, flags);
1296 rq->hrtick_flags = 0;
1297 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1298 spin_unlock_irqrestore(&rq->lock, flags);
1299
1300 hrtick_clear(rq);
1301}
1302
1303static void hotplug_hrtick_enable(int cpu)
1304{
1305 struct rq *rq = cpu_rq(cpu);
1306 unsigned long flags;
1307
1308 spin_lock_irqsave(&rq->lock, flags);
1309 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1310 spin_unlock_irqrestore(&rq->lock, flags);
1311}
1312
1313static int
1314hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1315{
1316 int cpu = (int)(long)hcpu;
1317
1318 switch (action) {
1319 case CPU_UP_CANCELED:
1320 case CPU_UP_CANCELED_FROZEN:
1321 case CPU_DOWN_PREPARE:
1322 case CPU_DOWN_PREPARE_FROZEN:
1323 case CPU_DEAD:
1324 case CPU_DEAD_FROZEN:
1325 hotplug_hrtick_disable(cpu);
1326 return NOTIFY_OK;
1327
1328 case CPU_UP_PREPARE:
1329 case CPU_UP_PREPARE_FROZEN:
1330 case CPU_DOWN_FAILED:
1331 case CPU_DOWN_FAILED_FROZEN:
1332 case CPU_ONLINE:
1333 case CPU_ONLINE_FROZEN:
1334 hotplug_hrtick_enable(cpu);
1335 return NOTIFY_OK;
1336 }
1337
1338 return NOTIFY_DONE;
1339}
1340
1341static void init_hrtick(void)
1342{
1343 hotcpu_notifier(hotplug_hrtick, 0);
1344}
1345
1346static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001347{
1348 rq->hrtick_flags = 0;
1349 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1350 rq->hrtick_timer.function = hrtick;
1351 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1352}
1353
1354void hrtick_resched(void)
1355{
1356 struct rq *rq;
1357 unsigned long flags;
1358
1359 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1360 return;
1361
1362 local_irq_save(flags);
1363 rq = cpu_rq(smp_processor_id());
1364 hrtick_set(rq);
1365 local_irq_restore(flags);
1366}
1367#else
1368static inline void hrtick_clear(struct rq *rq)
1369{
1370}
1371
1372static inline void hrtick_set(struct rq *rq)
1373{
1374}
1375
1376static inline void init_rq_hrtick(struct rq *rq)
1377{
1378}
1379
1380void hrtick_resched(void)
1381{
1382}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001383
1384static inline void init_hrtick(void)
1385{
1386}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001387#endif
1388
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001389/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001390 * resched_task - mark a task 'to be rescheduled now'.
1391 *
1392 * On UP this means the setting of the need_resched flag, on SMP it
1393 * might also involve a cross-CPU call to trigger the scheduler on
1394 * the target CPU.
1395 */
1396#ifdef CONFIG_SMP
1397
1398#ifndef tsk_is_polling
1399#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1400#endif
1401
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001402static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001403{
1404 int cpu;
1405
1406 assert_spin_locked(&task_rq(p)->lock);
1407
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001408 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001409 return;
1410
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001411 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001412
1413 cpu = task_cpu(p);
1414 if (cpu == smp_processor_id())
1415 return;
1416
1417 /* NEED_RESCHED must be visible before we test polling */
1418 smp_mb();
1419 if (!tsk_is_polling(p))
1420 smp_send_reschedule(cpu);
1421}
1422
1423static void resched_cpu(int cpu)
1424{
1425 struct rq *rq = cpu_rq(cpu);
1426 unsigned long flags;
1427
1428 if (!spin_trylock_irqsave(&rq->lock, flags))
1429 return;
1430 resched_task(cpu_curr(cpu));
1431 spin_unlock_irqrestore(&rq->lock, flags);
1432}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001433
1434#ifdef CONFIG_NO_HZ
1435/*
1436 * When add_timer_on() enqueues a timer into the timer wheel of an
1437 * idle CPU then this timer might expire before the next timer event
1438 * which is scheduled to wake up that CPU. In case of a completely
1439 * idle system the next event might even be infinite time into the
1440 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1441 * leaves the inner idle loop so the newly added timer is taken into
1442 * account when the CPU goes back to idle and evaluates the timer
1443 * wheel for the next timer event.
1444 */
1445void wake_up_idle_cpu(int cpu)
1446{
1447 struct rq *rq = cpu_rq(cpu);
1448
1449 if (cpu == smp_processor_id())
1450 return;
1451
1452 /*
1453 * This is safe, as this function is called with the timer
1454 * wheel base lock of (cpu) held. When the CPU is on the way
1455 * to idle and has not yet set rq->curr to idle then it will
1456 * be serialized on the timer wheel base lock and take the new
1457 * timer into account automatically.
1458 */
1459 if (rq->curr != rq->idle)
1460 return;
1461
1462 /*
1463 * We can set TIF_RESCHED on the idle task of the other CPU
1464 * lockless. The worst case is that the other CPU runs the
1465 * idle task through an additional NOOP schedule()
1466 */
1467 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1468
1469 /* NEED_RESCHED must be visible before we test polling */
1470 smp_mb();
1471 if (!tsk_is_polling(rq->idle))
1472 smp_send_reschedule(cpu);
1473}
1474#endif
1475
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001476#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001477static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001478{
1479 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001480 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001481}
1482#endif
1483
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001484#if BITS_PER_LONG == 32
1485# define WMULT_CONST (~0UL)
1486#else
1487# define WMULT_CONST (1UL << 32)
1488#endif
1489
1490#define WMULT_SHIFT 32
1491
Ingo Molnar194081e2007-08-09 11:16:51 +02001492/*
1493 * Shift right and round:
1494 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001495#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001496
Peter Zijlstra8f1bc382008-04-19 19:45:00 +02001497/*
1498 * delta *= weight / lw
1499 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001500static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001501calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1502 struct load_weight *lw)
1503{
1504 u64 tmp;
1505
Peter Zijlstrae05510d2008-05-05 23:56:17 +02001506 if (!lw->inv_weight)
1507 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001508
1509 tmp = (u64)delta_exec * weight;
1510 /*
1511 * Check whether we'd overflow the 64-bit multiplication:
1512 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001513 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001514 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001515 WMULT_SHIFT/2);
1516 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001517 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001518
Ingo Molnarecf691d2007-08-02 17:41:40 +02001519 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001520}
1521
Ingo Molnar10919852007-10-15 17:00:04 +02001522static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001523{
1524 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001525 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001526}
1527
Ingo Molnar10919852007-10-15 17:00:04 +02001528static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001529{
1530 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001531 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001535 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1536 * of tasks with abnormal "nice" values across CPUs the contribution that
1537 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001538 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001539 * scaled version of the new time slice allocation that they receive on time
1540 * slice expiry etc.
1541 */
1542
Ingo Molnardd41f592007-07-09 18:51:59 +02001543#define WEIGHT_IDLEPRIO 2
1544#define WMULT_IDLEPRIO (1 << 31)
1545
1546/*
1547 * Nice levels are multiplicative, with a gentle 10% change for every
1548 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1549 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1550 * that remained on nice 0.
1551 *
1552 * The "10% effect" is relative and cumulative: from _any_ nice level,
1553 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001554 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1555 * If a task goes up by ~10% and another task goes down by ~10% then
1556 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001557 */
1558static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001559 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1560 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1561 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1562 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1563 /* 0 */ 1024, 820, 655, 526, 423,
1564 /* 5 */ 335, 272, 215, 172, 137,
1565 /* 10 */ 110, 87, 70, 56, 45,
1566 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001567};
1568
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001569/*
1570 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1571 *
1572 * In cases where the weight does not change often, we can use the
1573 * precalculated inverse to speed up arithmetics by turning divisions
1574 * into multiplications:
1575 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001576static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001577 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1578 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1579 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1580 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1581 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1582 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1583 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1584 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001585};
Peter Williams2dd73a42006-06-27 02:54:34 -07001586
Ingo Molnardd41f592007-07-09 18:51:59 +02001587static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1588
1589/*
1590 * runqueue iterator, to support SMP load-balancing between different
1591 * scheduling classes, without having to expose their internal data
1592 * structures to the load-balancing proper:
1593 */
1594struct rq_iterator {
1595 void *arg;
1596 struct task_struct *(*start)(void *);
1597 struct task_struct *(*next)(void *);
1598};
1599
Peter Williamse1d14842007-10-24 18:23:51 +02001600#ifdef CONFIG_SMP
1601static unsigned long
1602balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1603 unsigned long max_load_move, struct sched_domain *sd,
1604 enum cpu_idle_type idle, int *all_pinned,
1605 int *this_best_prio, struct rq_iterator *iterator);
1606
1607static int
1608iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1609 struct sched_domain *sd, enum cpu_idle_type idle,
1610 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001611#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001612
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001613#ifdef CONFIG_CGROUP_CPUACCT
1614static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1615#else
1616static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1617#endif
1618
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001619static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1620{
1621 update_load_add(&rq->load, load);
1622}
1623
1624static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1625{
1626 update_load_sub(&rq->load, load);
1627}
1628
Gregory Haskinse7693a32008-01-25 21:08:09 +01001629#ifdef CONFIG_SMP
1630static unsigned long source_load(int cpu, int type);
1631static unsigned long target_load(int cpu, int type);
1632static unsigned long cpu_avg_load_per_task(int cpu);
1633static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001634
1635#ifdef CONFIG_FAIR_GROUP_SCHED
1636
1637/*
1638 * Group load balancing.
1639 *
1640 * We calculate a few balance domain wide aggregate numbers; load and weight.
1641 * Given the pictures below, and assuming each item has equal weight:
1642 *
1643 * root 1 - thread
1644 * / | \ A - group
1645 * A 1 B
1646 * /|\ / \
1647 * C 2 D 3 4
1648 * | |
1649 * 5 6
1650 *
1651 * load:
1652 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1653 * which equals 1/9-th of the total load.
1654 *
1655 * shares:
1656 * The weight of this group on the selected cpus.
1657 *
1658 * rq_weight:
1659 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1660 * B would get 2.
1661 *
1662 * task_weight:
1663 * Part of the rq_weight contributed by tasks; all groups except B would
1664 * get 1, B gets 2.
1665 */
1666
1667static inline struct aggregate_struct *
1668aggregate(struct task_group *tg, struct sched_domain *sd)
1669{
1670 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1671}
1672
1673typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1674
1675/*
1676 * Iterate the full tree, calling @down when first entering a node and @up when
1677 * leaving it for the final time.
1678 */
1679static
1680void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1681 struct sched_domain *sd)
1682{
1683 struct task_group *parent, *child;
1684
1685 rcu_read_lock();
1686 parent = &root_task_group;
1687down:
1688 (*down)(parent, sd);
1689 list_for_each_entry_rcu(child, &parent->children, siblings) {
1690 parent = child;
1691 goto down;
1692
1693up:
1694 continue;
1695 }
1696 (*up)(parent, sd);
1697
1698 child = parent;
1699 parent = parent->parent;
1700 if (parent)
1701 goto up;
1702 rcu_read_unlock();
1703}
1704
1705/*
1706 * Calculate the aggregate runqueue weight.
1707 */
1708static
1709void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1710{
1711 unsigned long rq_weight = 0;
1712 unsigned long task_weight = 0;
1713 int i;
1714
1715 for_each_cpu_mask(i, sd->span) {
1716 rq_weight += tg->cfs_rq[i]->load.weight;
1717 task_weight += tg->cfs_rq[i]->task_weight;
1718 }
1719
1720 aggregate(tg, sd)->rq_weight = rq_weight;
1721 aggregate(tg, sd)->task_weight = task_weight;
1722}
1723
1724/*
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001725 * Compute the weight of this group on the given cpus.
1726 */
1727static
1728void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1729{
1730 unsigned long shares = 0;
1731 int i;
1732
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001733 for_each_cpu_mask(i, sd->span)
1734 shares += tg->cfs_rq[i]->shares;
1735
Peter Zijlstra3f5087a2008-04-25 00:25:08 +02001736 if ((!shares && aggregate(tg, sd)->rq_weight) || shares > tg->shares)
1737 shares = tg->shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001738
1739 aggregate(tg, sd)->shares = shares;
1740}
1741
1742/*
1743 * Compute the load fraction assigned to this group, relies on the aggregate
1744 * weight and this group's parent's load, i.e. top-down.
1745 */
1746static
1747void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1748{
1749 unsigned long load;
1750
1751 if (!tg->parent) {
1752 int i;
1753
1754 load = 0;
1755 for_each_cpu_mask(i, sd->span)
1756 load += cpu_rq(i)->load.weight;
1757
1758 } else {
1759 load = aggregate(tg->parent, sd)->load;
1760
1761 /*
1762 * shares is our weight in the parent's rq so
1763 * shares/parent->rq_weight gives our fraction of the load
1764 */
1765 load *= aggregate(tg, sd)->shares;
1766 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1767 }
1768
1769 aggregate(tg, sd)->load = load;
1770}
1771
1772static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1773
1774/*
1775 * Calculate and set the cpu's group shares.
1776 */
1777static void
1778__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1779 int tcpu)
1780{
1781 int boost = 0;
1782 unsigned long shares;
1783 unsigned long rq_weight;
1784
1785 if (!tg->se[tcpu])
1786 return;
1787
1788 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1789
1790 /*
1791 * If there are currently no tasks on the cpu pretend there is one of
1792 * average load so that when a new task gets to run here it will not
1793 * get delayed by group starvation.
1794 */
1795 if (!rq_weight) {
1796 boost = 1;
1797 rq_weight = NICE_0_LOAD;
1798 }
1799
1800 /*
1801 * \Sum shares * rq_weight
1802 * shares = -----------------------
1803 * \Sum rq_weight
1804 *
1805 */
1806 shares = aggregate(tg, sd)->shares * rq_weight;
1807 shares /= aggregate(tg, sd)->rq_weight + 1;
1808
1809 /*
1810 * record the actual number of shares, not the boosted amount.
1811 */
1812 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1813
1814 if (shares < MIN_SHARES)
1815 shares = MIN_SHARES;
1816
1817 __set_se_shares(tg->se[tcpu], shares);
1818}
1819
1820/*
1821 * Re-adjust the weights on the cpu the task came from and on the cpu the
1822 * task went to.
1823 */
1824static void
1825__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1826 int scpu, int dcpu)
1827{
1828 unsigned long shares;
1829
1830 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1831
1832 __update_group_shares_cpu(tg, sd, scpu);
1833 __update_group_shares_cpu(tg, sd, dcpu);
1834
1835 /*
1836 * ensure we never loose shares due to rounding errors in the
1837 * above redistribution.
1838 */
1839 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1840 if (shares)
1841 tg->cfs_rq[dcpu]->shares += shares;
1842}
1843
1844/*
1845 * Because changing a group's shares changes the weight of the super-group
1846 * we need to walk up the tree and change all shares until we hit the root.
1847 */
1848static void
1849move_group_shares(struct task_group *tg, struct sched_domain *sd,
1850 int scpu, int dcpu)
1851{
1852 while (tg) {
1853 __move_group_shares(tg, sd, scpu, dcpu);
1854 tg = tg->parent;
1855 }
1856}
1857
1858static
1859void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1860{
1861 unsigned long shares = aggregate(tg, sd)->shares;
1862 int i;
1863
1864 for_each_cpu_mask(i, sd->span) {
1865 struct rq *rq = cpu_rq(i);
1866 unsigned long flags;
1867
1868 spin_lock_irqsave(&rq->lock, flags);
1869 __update_group_shares_cpu(tg, sd, i);
1870 spin_unlock_irqrestore(&rq->lock, flags);
1871 }
1872
1873 aggregate_group_shares(tg, sd);
1874
1875 /*
1876 * ensure we never loose shares due to rounding errors in the
1877 * above redistribution.
1878 */
1879 shares -= aggregate(tg, sd)->shares;
1880 if (shares) {
1881 tg->cfs_rq[sd->first_cpu]->shares += shares;
1882 aggregate(tg, sd)->shares += shares;
1883 }
1884}
1885
1886/*
1887 * Calculate the accumulative weight and recursive load of each task group
1888 * while walking down the tree.
1889 */
1890static
1891void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1892{
1893 aggregate_group_weight(tg, sd);
1894 aggregate_group_shares(tg, sd);
1895 aggregate_group_load(tg, sd);
1896}
1897
1898/*
1899 * Rebalance the cpu shares while walking back up the tree.
1900 */
1901static
1902void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1903{
1904 aggregate_group_set_shares(tg, sd);
1905}
1906
1907static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1908
1909static void __init init_aggregate(void)
1910{
1911 int i;
1912
1913 for_each_possible_cpu(i)
1914 spin_lock_init(&per_cpu(aggregate_lock, i));
1915}
1916
1917static int get_aggregate(struct sched_domain *sd)
1918{
1919 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1920 return 0;
1921
1922 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1923 return 1;
1924}
1925
1926static void put_aggregate(struct sched_domain *sd)
1927{
1928 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1929}
1930
1931static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1932{
1933 cfs_rq->shares = shares;
1934}
1935
1936#else
1937
1938static inline void init_aggregate(void)
1939{
1940}
1941
1942static inline int get_aggregate(struct sched_domain *sd)
1943{
1944 return 0;
1945}
1946
1947static inline void put_aggregate(struct sched_domain *sd)
1948{
1949}
1950#endif
1951
1952#else /* CONFIG_SMP */
1953
1954#ifdef CONFIG_FAIR_GROUP_SCHED
1955static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1956{
1957}
1958#endif
1959
Gregory Haskinse7693a32008-01-25 21:08:09 +01001960#endif /* CONFIG_SMP */
1961
Ingo Molnardd41f592007-07-09 18:51:59 +02001962#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001963#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001964#include "sched_fair.c"
1965#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001966#ifdef CONFIG_SCHED_DEBUG
1967# include "sched_debug.c"
1968#endif
1969
1970#define sched_class_highest (&rt_sched_class)
1971
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001972static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001973{
1974 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001975}
1976
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001977static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001978{
1979 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001980}
1981
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001982static void set_load_weight(struct task_struct *p)
1983{
1984 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001985 p->se.load.weight = prio_to_weight[0] * 2;
1986 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1987 return;
1988 }
1989
1990 /*
1991 * SCHED_IDLE tasks get minimal weight:
1992 */
1993 if (p->policy == SCHED_IDLE) {
1994 p->se.load.weight = WEIGHT_IDLEPRIO;
1995 p->se.load.inv_weight = WMULT_IDLEPRIO;
1996 return;
1997 }
1998
1999 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
2000 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02002001}
2002
Ingo Molnar8159f872007-08-09 11:16:49 +02002003static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002004{
2005 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02002006 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02002007 p->se.on_rq = 1;
2008}
2009
Ingo Molnar69be72c2007-08-09 11:16:49 +02002010static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02002011{
Ingo Molnarf02231e2007-08-09 11:16:48 +02002012 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02002013 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002014}
2015
2016/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002017 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002018 */
Ingo Molnar14531182007-07-09 18:51:59 +02002019static inline int __normal_prio(struct task_struct *p)
2020{
Ingo Molnardd41f592007-07-09 18:51:59 +02002021 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002022}
2023
2024/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002025 * Calculate the expected normal priority: i.e. priority
2026 * without taking RT-inheritance into account. Might be
2027 * boosted by interactivity modifiers. Changes upon fork,
2028 * setprio syscalls, and whenever the interactivity
2029 * estimator recalculates.
2030 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002031static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002032{
2033 int prio;
2034
Ingo Molnare05606d2007-07-09 18:51:59 +02002035 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002036 prio = MAX_RT_PRIO-1 - p->rt_priority;
2037 else
2038 prio = __normal_prio(p);
2039 return prio;
2040}
2041
2042/*
2043 * Calculate the current priority, i.e. the priority
2044 * taken into account by the scheduler. This value might
2045 * be boosted by RT tasks, or might be boosted by
2046 * interactivity modifiers. Will be RT if the task got
2047 * RT-boosted. If not then it returns p->normal_prio.
2048 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002049static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002050{
2051 p->normal_prio = normal_prio(p);
2052 /*
2053 * If we are RT tasks or we were boosted to RT priority,
2054 * keep the priority unchanged. Otherwise, update priority
2055 * to the normal priority:
2056 */
2057 if (!rt_prio(p->prio))
2058 return p->normal_prio;
2059 return p->prio;
2060}
2061
2062/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002063 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002065static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002067 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002068 rq->nr_uninterruptible--;
2069
Ingo Molnar8159f872007-08-09 11:16:49 +02002070 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002071 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * deactivate_task - remove a task from the runqueue.
2076 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002077static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002079 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02002080 rq->nr_uninterruptible++;
2081
Ingo Molnar69be72c2007-08-09 11:16:49 +02002082 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002083 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/**
2087 * task_curr - is this task currently executing on a CPU?
2088 * @p: the task in question.
2089 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002090inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 return cpu_curr(task_cpu(p)) == p;
2093}
2094
Peter Williams2dd73a42006-06-27 02:54:34 -07002095/* Used instead of source_load when we know the type == 0 */
2096unsigned long weighted_cpuload(const int cpu)
2097{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002098 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002099}
2100
2101static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2102{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01002103 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002104#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01002105 /*
2106 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2107 * successfuly executed on another CPU. We must ensure that updates of
2108 * per-task data have been completed by this moment.
2109 */
2110 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02002111 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002112#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07002113}
2114
Steven Rostedtcb469842008-01-25 21:08:22 +01002115static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2116 const struct sched_class *prev_class,
2117 int oldprio, int running)
2118{
2119 if (prev_class != p->sched_class) {
2120 if (prev_class->switched_from)
2121 prev_class->switched_from(rq, p, running);
2122 p->sched_class->switched_to(rq, p, running);
2123 } else
2124 p->sched_class->prio_changed(rq, p, oldprio, running);
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02002128
Ingo Molnarcc367732007-10-15 17:00:18 +02002129/*
2130 * Is this task likely cache-hot:
2131 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002132static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002133task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2134{
2135 s64 delta;
2136
Ingo Molnarf540a602008-03-15 17:10:34 +01002137 /*
2138 * Buddy candidates are cache hot:
2139 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002140 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002141 return 1;
2142
Ingo Molnarcc367732007-10-15 17:00:18 +02002143 if (p->sched_class != &fair_sched_class)
2144 return 0;
2145
Ingo Molnar6bc16652007-10-15 17:00:18 +02002146 if (sysctl_sched_migration_cost == -1)
2147 return 1;
2148 if (sysctl_sched_migration_cost == 0)
2149 return 0;
2150
Ingo Molnarcc367732007-10-15 17:00:18 +02002151 delta = now - p->se.exec_start;
2152
2153 return delta < (s64)sysctl_sched_migration_cost;
2154}
2155
2156
Ingo Molnardd41f592007-07-09 18:51:59 +02002157void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002158{
Ingo Molnardd41f592007-07-09 18:51:59 +02002159 int old_cpu = task_cpu(p);
2160 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002161 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2162 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002163 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002164
2165 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002166
2167#ifdef CONFIG_SCHEDSTATS
2168 if (p->se.wait_start)
2169 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002170 if (p->se.sleep_start)
2171 p->se.sleep_start -= clock_offset;
2172 if (p->se.block_start)
2173 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002174 if (old_cpu != new_cpu) {
2175 schedstat_inc(p, se.nr_migrations);
2176 if (task_hot(p, old_rq->clock, NULL))
2177 schedstat_inc(p, se.nr_forced2_migrations);
2178 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002179#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002180 p->se.vruntime -= old_cfsrq->min_vruntime -
2181 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002182
2183 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002184}
2185
Ingo Molnar70b97a72006-07-03 00:25:42 -07002186struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Ingo Molnar36c8b582006-07-03 00:25:41 -07002189 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int dest_cpu;
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002193};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195/*
2196 * The task's runqueue lock must be held.
2197 * Returns true if you have to wait for migration thread.
2198 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002199static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002200migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002202 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /*
2205 * If the task is not on a runqueue (and not running), then
2206 * it is sufficient to simply update the task's cpu field.
2207 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002208 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 set_task_cpu(p, dest_cpu);
2210 return 0;
2211 }
2212
2213 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 req->task = p;
2215 req->dest_cpu = dest_cpu;
2216 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 1;
2219}
2220
2221/*
2222 * wait_task_inactive - wait for a thread to unschedule.
2223 *
2224 * The caller must ensure that the task *will* unschedule sometime soon,
2225 * else this function might spin for a *long* time. This function can't
2226 * be called with interrupts off, or it may introduce deadlock with
2227 * smp_call_function() if an IPI is sent by the same process we are
2228 * waiting to become inactive.
2229 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002230void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002233 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002234 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Andi Kleen3a5c3592007-10-15 17:00:14 +02002236 for (;;) {
2237 /*
2238 * We do the initial early heuristics without holding
2239 * any task-queue locks at all. We'll only try to get
2240 * the runqueue lock when things look like they will
2241 * work out!
2242 */
2243 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002244
Andi Kleen3a5c3592007-10-15 17:00:14 +02002245 /*
2246 * If the task is actively running on another CPU
2247 * still, just relax and busy-wait without holding
2248 * any locks.
2249 *
2250 * NOTE! Since we don't hold any locks, it's not
2251 * even sure that "rq" stays as the right runqueue!
2252 * But we don't care, since "task_running()" will
2253 * return false if the runqueue has changed and p
2254 * is actually now running somewhere else!
2255 */
2256 while (task_running(rq, p))
2257 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002258
Andi Kleen3a5c3592007-10-15 17:00:14 +02002259 /*
2260 * Ok, time to look more closely! We need the rq
2261 * lock now, to be *sure*. If we're wrong, we'll
2262 * just go back and repeat.
2263 */
2264 rq = task_rq_lock(p, &flags);
2265 running = task_running(rq, p);
2266 on_rq = p->se.on_rq;
2267 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002268
Andi Kleen3a5c3592007-10-15 17:00:14 +02002269 /*
2270 * Was it really running after all now that we
2271 * checked with the proper locks actually held?
2272 *
2273 * Oops. Go back and try again..
2274 */
2275 if (unlikely(running)) {
2276 cpu_relax();
2277 continue;
2278 }
2279
2280 /*
2281 * It's not enough that it's not actively running,
2282 * it must be off the runqueue _entirely_, and not
2283 * preempted!
2284 *
2285 * So if it wa still runnable (but just not actively
2286 * running right now), it's preempted, and we should
2287 * yield - it could be a while.
2288 */
2289 if (unlikely(on_rq)) {
2290 schedule_timeout_uninterruptible(1);
2291 continue;
2292 }
2293
2294 /*
2295 * Ahh, all good. It wasn't running, and it wasn't
2296 * runnable, which means that it will never become
2297 * running in the future either. We're all done!
2298 */
2299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/***
2304 * kick_process - kick a running thread to enter/exit the kernel
2305 * @p: the to-be-kicked thread
2306 *
2307 * Cause a process which is running on another CPU to enter
2308 * kernel-mode, without any delay. (to get signals handled.)
2309 *
2310 * NOTE: this function doesnt have to take the runqueue lock,
2311 * because all it wants to ensure is that the remote task enters
2312 * the kernel. If the IPI races and the task has been migrated
2313 * to another CPU then no harm is done and the purpose has been
2314 * achieved as well.
2315 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002316void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 int cpu;
2319
2320 preempt_disable();
2321 cpu = task_cpu(p);
2322 if ((cpu != smp_processor_id()) && task_curr(p))
2323 smp_send_reschedule(cpu);
2324 preempt_enable();
2325}
2326
2327/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002328 * Return a low guess at the load of a migration-source cpu weighted
2329 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 *
2331 * We want to under-estimate the load of migration sources, to
2332 * balance conservatively.
2333 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002334static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002335{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002336 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002337 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002338
Peter Williams2dd73a42006-06-27 02:54:34 -07002339 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002340 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002341
Ingo Molnardd41f592007-07-09 18:51:59 +02002342 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002346 * Return a high guess at the load of a migration-target cpu weighted
2347 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002349static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002350{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002351 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002352 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002353
Peter Williams2dd73a42006-06-27 02:54:34 -07002354 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002355 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002356
Ingo Molnardd41f592007-07-09 18:51:59 +02002357 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002358}
2359
2360/*
2361 * Return the average load per task on the cpu's run queue
2362 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002363static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002364{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002365 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002367 unsigned long n = rq->nr_running;
2368
Ingo Molnardd41f592007-07-09 18:51:59 +02002369 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Nick Piggin147cbb42005-06-25 14:57:19 -07002372/*
2373 * find_idlest_group finds and returns the least busy CPU group within the
2374 * domain.
2375 */
2376static struct sched_group *
2377find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2378{
2379 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2380 unsigned long min_load = ULONG_MAX, this_load = 0;
2381 int load_idx = sd->forkexec_idx;
2382 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2383
2384 do {
2385 unsigned long load, avg_load;
2386 int local_group;
2387 int i;
2388
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002389 /* Skip over this group if it has no CPUs allowed */
2390 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002391 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002392
Nick Piggin147cbb42005-06-25 14:57:19 -07002393 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002394
2395 /* Tally up the load of all CPUs in the group */
2396 avg_load = 0;
2397
2398 for_each_cpu_mask(i, group->cpumask) {
2399 /* Bias balancing toward cpus of our domain */
2400 if (local_group)
2401 load = source_load(i, load_idx);
2402 else
2403 load = target_load(i, load_idx);
2404
2405 avg_load += load;
2406 }
2407
2408 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002409 avg_load = sg_div_cpu_power(group,
2410 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002411
2412 if (local_group) {
2413 this_load = avg_load;
2414 this = group;
2415 } else if (avg_load < min_load) {
2416 min_load = avg_load;
2417 idlest = group;
2418 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002419 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002420
2421 if (!idlest || 100*this_load < imbalance*min_load)
2422 return NULL;
2423 return idlest;
2424}
2425
2426/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002427 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002428 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002429static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002430find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2431 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002432{
2433 unsigned long load, min_load = ULONG_MAX;
2434 int idlest = -1;
2435 int i;
2436
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002437 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002438 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002439
Mike Travis7c16ec52008-04-04 18:11:11 -07002440 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002441 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002442
2443 if (load < min_load || (load == min_load && i == this_cpu)) {
2444 min_load = load;
2445 idlest = i;
2446 }
2447 }
2448
2449 return idlest;
2450}
2451
Nick Piggin476d1392005-06-25 14:57:29 -07002452/*
2453 * sched_balance_self: balance the current task (running on cpu) in domains
2454 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2455 * SD_BALANCE_EXEC.
2456 *
2457 * Balance, ie. select the least loaded group.
2458 *
2459 * Returns the target CPU number, or the same CPU if no balancing is needed.
2460 *
2461 * preempt must be disabled.
2462 */
2463static int sched_balance_self(int cpu, int flag)
2464{
2465 struct task_struct *t = current;
2466 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002467
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002468 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002469 /*
2470 * If power savings logic is enabled for a domain, stop there.
2471 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002472 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2473 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002474 if (tmp->flags & flag)
2475 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002476 }
Nick Piggin476d1392005-06-25 14:57:29 -07002477
2478 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002479 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002480 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002481 int new_cpu, weight;
2482
2483 if (!(sd->flags & flag)) {
2484 sd = sd->child;
2485 continue;
2486 }
Nick Piggin476d1392005-06-25 14:57:29 -07002487
2488 span = sd->span;
2489 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002490 if (!group) {
2491 sd = sd->child;
2492 continue;
2493 }
Nick Piggin476d1392005-06-25 14:57:29 -07002494
Mike Travis7c16ec52008-04-04 18:11:11 -07002495 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002496 if (new_cpu == -1 || new_cpu == cpu) {
2497 /* Now try balancing at a lower domain level of cpu */
2498 sd = sd->child;
2499 continue;
2500 }
Nick Piggin476d1392005-06-25 14:57:29 -07002501
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002502 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002503 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002504 sd = NULL;
2505 weight = cpus_weight(span);
2506 for_each_domain(cpu, tmp) {
2507 if (weight <= cpus_weight(tmp->span))
2508 break;
2509 if (tmp->flags & flag)
2510 sd = tmp;
2511 }
2512 /* while loop will break here if sd == NULL */
2513 }
2514
2515 return cpu;
2516}
2517
2518#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/***
2521 * try_to_wake_up - wake up a thread
2522 * @p: the to-be-woken-up thread
2523 * @state: the mask of task states that can be woken
2524 * @sync: do a synchronous wakeup?
2525 *
2526 * Put it on the run-queue if it's not already there. The "current"
2527 * thread is always on the run-queue (except when the actual
2528 * re-schedule is in progress), and as such you're allowed to do
2529 * the simpler "current->state = TASK_RUNNING" to mark yourself
2530 * runnable without the overhead of this.
2531 *
2532 * returns failure only if the task is already active.
2533 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002534static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Ingo Molnarcc367732007-10-15 17:00:18 +02002536 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 unsigned long flags;
2538 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002539 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Ingo Molnarb85d0662008-03-16 20:03:22 +01002541 if (!sched_feat(SYNC_WAKEUPS))
2542 sync = 0;
2543
Linus Torvalds04e2f172008-02-23 18:05:03 -08002544 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 rq = task_rq_lock(p, &flags);
2546 old_state = p->state;
2547 if (!(old_state & state))
2548 goto out;
2549
Ingo Molnardd41f592007-07-09 18:51:59 +02002550 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 goto out_running;
2552
2553 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002554 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 this_cpu = smp_processor_id();
2556
2557#ifdef CONFIG_SMP
2558 if (unlikely(task_running(rq, p)))
2559 goto out_activate;
2560
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002561 cpu = p->sched_class->select_task_rq(p, sync);
2562 if (cpu != orig_cpu) {
2563 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 task_rq_unlock(rq, &flags);
2565 /* might preempt at this point */
2566 rq = task_rq_lock(p, &flags);
2567 old_state = p->state;
2568 if (!(old_state & state))
2569 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002570 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 goto out_running;
2572
2573 this_cpu = smp_processor_id();
2574 cpu = task_cpu(p);
2575 }
2576
Gregory Haskinse7693a32008-01-25 21:08:09 +01002577#ifdef CONFIG_SCHEDSTATS
2578 schedstat_inc(rq, ttwu_count);
2579 if (cpu == this_cpu)
2580 schedstat_inc(rq, ttwu_local);
2581 else {
2582 struct sched_domain *sd;
2583 for_each_domain(this_cpu, sd) {
2584 if (cpu_isset(cpu, sd->span)) {
2585 schedstat_inc(sd, ttwu_wake_remote);
2586 break;
2587 }
2588 }
2589 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002590#endif
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592out_activate:
2593#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002594 schedstat_inc(p, se.nr_wakeups);
2595 if (sync)
2596 schedstat_inc(p, se.nr_wakeups_sync);
2597 if (orig_cpu != cpu)
2598 schedstat_inc(p, se.nr_wakeups_migrate);
2599 if (cpu == this_cpu)
2600 schedstat_inc(p, se.nr_wakeups_local);
2601 else
2602 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002603 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002604 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 success = 1;
2606
2607out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002608 check_preempt_curr(rq, p);
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002611#ifdef CONFIG_SMP
2612 if (p->sched_class->task_wake_up)
2613 p->sched_class->task_wake_up(rq, p);
2614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615out:
2616 task_rq_unlock(rq, &flags);
2617
2618 return success;
2619}
2620
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002621int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002623 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625EXPORT_SYMBOL(wake_up_process);
2626
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002627int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 return try_to_wake_up(p, state, 0);
2630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632/*
2633 * Perform scheduler related setup for a newly forked process p.
2634 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002635 *
2636 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002638static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Ingo Molnardd41f592007-07-09 18:51:59 +02002640 p->se.exec_start = 0;
2641 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002642 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002643 p->se.last_wakeup = 0;
2644 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002645
2646#ifdef CONFIG_SCHEDSTATS
2647 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002648 p->se.sum_sleep_runtime = 0;
2649 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002650 p->se.block_start = 0;
2651 p->se.sleep_max = 0;
2652 p->se.block_max = 0;
2653 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002654 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002655 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002656#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002657
Peter Zijlstrafa717062008-01-25 21:08:27 +01002658 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002659 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002660 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002661
Avi Kivitye107be32007-07-26 13:40:43 +02002662#ifdef CONFIG_PREEMPT_NOTIFIERS
2663 INIT_HLIST_HEAD(&p->preempt_notifiers);
2664#endif
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 /*
2667 * We mark the process as running here, but have not actually
2668 * inserted it onto the runqueue yet. This guarantees that
2669 * nobody will actually run it, and a signal or other external
2670 * event cannot wake it up and insert it on the runqueue either.
2671 */
2672 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002673}
2674
2675/*
2676 * fork()/clone()-time setup:
2677 */
2678void sched_fork(struct task_struct *p, int clone_flags)
2679{
2680 int cpu = get_cpu();
2681
2682 __sched_fork(p);
2683
2684#ifdef CONFIG_SMP
2685 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2686#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002687 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002688
2689 /*
2690 * Make sure we do not leak PI boosting priority to the child:
2691 */
2692 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002693 if (!rt_prio(p->prio))
2694 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002695
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002696#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002697 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002698 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002700#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002701 p->oncpu = 0;
2702#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002704 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002705 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002707 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710/*
2711 * wake_up_new_task - wake up a newly created task for the first time.
2712 *
2713 * This function will do some initial scheduler statistics housekeeping
2714 * that must be done for every newly created context, then puts the task
2715 * on the runqueue and wakes it.
2716 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002717void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002720 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002724 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 p->prio = effective_prio(p);
2727
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002728 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002729 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002732 * Let the scheduling class do new task startup
2733 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002735 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002736 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002738 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002739#ifdef CONFIG_SMP
2740 if (p->sched_class->task_wake_up)
2741 p->sched_class->task_wake_up(rq, p);
2742#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002743 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
Avi Kivitye107be32007-07-26 13:40:43 +02002746#ifdef CONFIG_PREEMPT_NOTIFIERS
2747
2748/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002749 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2750 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002751 */
2752void preempt_notifier_register(struct preempt_notifier *notifier)
2753{
2754 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2755}
2756EXPORT_SYMBOL_GPL(preempt_notifier_register);
2757
2758/**
2759 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002760 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002761 *
2762 * This is safe to call from within a preemption notifier.
2763 */
2764void preempt_notifier_unregister(struct preempt_notifier *notifier)
2765{
2766 hlist_del(&notifier->link);
2767}
2768EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2769
2770static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2771{
2772 struct preempt_notifier *notifier;
2773 struct hlist_node *node;
2774
2775 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2776 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2777}
2778
2779static void
2780fire_sched_out_preempt_notifiers(struct task_struct *curr,
2781 struct task_struct *next)
2782{
2783 struct preempt_notifier *notifier;
2784 struct hlist_node *node;
2785
2786 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2787 notifier->ops->sched_out(notifier, next);
2788}
2789
2790#else
2791
2792static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2793{
2794}
2795
2796static void
2797fire_sched_out_preempt_notifiers(struct task_struct *curr,
2798 struct task_struct *next)
2799{
2800}
2801
2802#endif
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002805 * prepare_task_switch - prepare to switch tasks
2806 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002807 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002808 * @next: the task we are going to switch to.
2809 *
2810 * This is called with the rq lock held and interrupts off. It must
2811 * be paired with a subsequent finish_task_switch after the context
2812 * switch.
2813 *
2814 * prepare_task_switch sets up locking and calls architecture specific
2815 * hooks.
2816 */
Avi Kivitye107be32007-07-26 13:40:43 +02002817static inline void
2818prepare_task_switch(struct rq *rq, struct task_struct *prev,
2819 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002820{
Avi Kivitye107be32007-07-26 13:40:43 +02002821 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002822 prepare_lock_switch(rq, next);
2823 prepare_arch_switch(next);
2824}
2825
2826/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002828 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * @prev: the thread we just switched away from.
2830 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002831 * finish_task_switch must be called after the context switch, paired
2832 * with a prepare_task_switch call before the context switch.
2833 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2834 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 *
2836 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002837 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 * with the lock held can cause deadlocks; see schedule() for
2839 * details.)
2840 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002841static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 __releases(rq->lock)
2843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002845 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 rq->prev_mm = NULL;
2848
2849 /*
2850 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002851 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002852 * schedule one last time. The schedule call will never return, and
2853 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002854 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 * still held, otherwise prev could be scheduled on another cpu, die
2856 * there before we look at prev->state, and then the reference would
2857 * be dropped twice.
2858 * Manfred Spraul <manfred@colorfullife.com>
2859 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002860 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002861 finish_arch_switch(prev);
2862 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002863#ifdef CONFIG_SMP
2864 if (current->sched_class->post_schedule)
2865 current->sched_class->post_schedule(rq);
2866#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002867
Avi Kivitye107be32007-07-26 13:40:43 +02002868 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (mm)
2870 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002871 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002872 /*
2873 * Remove function-return probe instances associated with this
2874 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002875 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002876 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/**
2882 * schedule_tail - first thing a freshly forked thread must call.
2883 * @prev: the thread we just switched away from.
2884 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002885asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 __releases(rq->lock)
2887{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002888 struct rq *rq = this_rq();
2889
Nick Piggin4866cde2005-06-25 14:57:23 -07002890 finish_task_switch(rq, prev);
2891#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2892 /* In this case, finish_task_switch does not reenable preemption */
2893 preempt_enable();
2894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002896 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899/*
2900 * context_switch - switch to the new MM and the new
2901 * thread's register state.
2902 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002903static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002904context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002905 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Ingo Molnardd41f592007-07-09 18:51:59 +02002907 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Avi Kivitye107be32007-07-26 13:40:43 +02002909 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002910 mm = next->mm;
2911 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002912 /*
2913 * For paravirt, this is coupled with an exit in switch_to to
2914 * combine the page table reload and the switch backend into
2915 * one hypercall.
2916 */
2917 arch_enter_lazy_cpu_mode();
2918
Ingo Molnardd41f592007-07-09 18:51:59 +02002919 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 next->active_mm = oldmm;
2921 atomic_inc(&oldmm->mm_count);
2922 enter_lazy_tlb(oldmm, next);
2923 } else
2924 switch_mm(oldmm, mm, next);
2925
Ingo Molnardd41f592007-07-09 18:51:59 +02002926 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 rq->prev_mm = oldmm;
2929 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002930 /*
2931 * Since the runqueue lock will be released by the next
2932 * task (which is an invalid locking op but in the case
2933 * of the scheduler it's an obvious special-case), so we
2934 * do an early lockdep release here:
2935 */
2936#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002937 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /* Here we just switch the register state and the stack. */
2941 switch_to(prev, next, prev);
2942
Ingo Molnardd41f592007-07-09 18:51:59 +02002943 barrier();
2944 /*
2945 * this_rq must be evaluated again because prev may have moved
2946 * CPUs since it called schedule(), thus the 'rq' on its stack
2947 * frame will be invalid.
2948 */
2949 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952/*
2953 * nr_running, nr_uninterruptible and nr_context_switches:
2954 *
2955 * externally visible scheduler statistics: current number of runnable
2956 * threads, current number of uninterruptible-sleeping threads, total
2957 * number of context switches performed since bootup.
2958 */
2959unsigned long nr_running(void)
2960{
2961 unsigned long i, sum = 0;
2962
2963 for_each_online_cpu(i)
2964 sum += cpu_rq(i)->nr_running;
2965
2966 return sum;
2967}
2968
2969unsigned long nr_uninterruptible(void)
2970{
2971 unsigned long i, sum = 0;
2972
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002973 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 sum += cpu_rq(i)->nr_uninterruptible;
2975
2976 /*
2977 * Since we read the counters lockless, it might be slightly
2978 * inaccurate. Do not allow it to go below zero though:
2979 */
2980 if (unlikely((long)sum < 0))
2981 sum = 0;
2982
2983 return sum;
2984}
2985
2986unsigned long long nr_context_switches(void)
2987{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002988 int i;
2989 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002991 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 sum += cpu_rq(i)->nr_switches;
2993
2994 return sum;
2995}
2996
2997unsigned long nr_iowait(void)
2998{
2999 unsigned long i, sum = 0;
3000
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003001 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3003
3004 return sum;
3005}
3006
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003007unsigned long nr_active(void)
3008{
3009 unsigned long i, running = 0, uninterruptible = 0;
3010
3011 for_each_online_cpu(i) {
3012 running += cpu_rq(i)->nr_running;
3013 uninterruptible += cpu_rq(i)->nr_uninterruptible;
3014 }
3015
3016 if (unlikely((long)uninterruptible < 0))
3017 uninterruptible = 0;
3018
3019 return running + uninterruptible;
3020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003023 * Update rq->cpu_load[] statistics. This function is usually called every
3024 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003025 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003026static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003027{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003028 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003029 int i, scale;
3030
3031 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003032
3033 /* Update our load: */
3034 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3035 unsigned long old_load, new_load;
3036
3037 /* scale is effectively 1 << i now, and >> i divides by scale */
3038
3039 old_load = this_rq->cpu_load[i];
3040 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003041 /*
3042 * Round up the averaging division if load is increasing. This
3043 * prevents us from getting stuck on 9 if the load is 10, for
3044 * example.
3045 */
3046 if (new_load > old_load)
3047 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003048 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3049 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003050}
3051
Ingo Molnardd41f592007-07-09 18:51:59 +02003052#ifdef CONFIG_SMP
3053
Ingo Molnar48f24c42006-07-03 00:25:40 -07003054/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 * double_rq_lock - safely lock two runqueues
3056 *
3057 * Note this does not disable interrupts like task_rq_lock,
3058 * you need to do so manually before calling.
3059 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003060static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 __acquires(rq1->lock)
3062 __acquires(rq2->lock)
3063{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003064 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (rq1 == rq2) {
3066 spin_lock(&rq1->lock);
3067 __acquire(rq2->lock); /* Fake it out ;) */
3068 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003069 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 spin_lock(&rq1->lock);
3071 spin_lock(&rq2->lock);
3072 } else {
3073 spin_lock(&rq2->lock);
3074 spin_lock(&rq1->lock);
3075 }
3076 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003077 update_rq_clock(rq1);
3078 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/*
3082 * double_rq_unlock - safely unlock two runqueues
3083 *
3084 * Note this does not restore interrupts like task_rq_unlock,
3085 * you need to do so manually after calling.
3086 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003087static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 __releases(rq1->lock)
3089 __releases(rq2->lock)
3090{
3091 spin_unlock(&rq1->lock);
3092 if (rq1 != rq2)
3093 spin_unlock(&rq2->lock);
3094 else
3095 __release(rq2->lock);
3096}
3097
3098/*
3099 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3100 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01003101static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 __releases(this_rq->lock)
3103 __acquires(busiest->lock)
3104 __acquires(this_rq->lock)
3105{
Steven Rostedte8fa1362008-01-25 21:08:05 +01003106 int ret = 0;
3107
Kirill Korotaev054b9102006-12-10 02:20:11 -08003108 if (unlikely(!irqs_disabled())) {
3109 /* printk() doesn't work good under rq->lock */
3110 spin_unlock(&this_rq->lock);
3111 BUG_ON(1);
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003114 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 spin_unlock(&this_rq->lock);
3116 spin_lock(&busiest->lock);
3117 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003118 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 } else
3120 spin_lock(&busiest->lock);
3121 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01003122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 * If dest_cpu is allowed for this process, migrate the task to it.
3127 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003128 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 * the cpu_allowed mask is restored.
3130 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003131static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003133 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003135 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 rq = task_rq_lock(p, &flags);
3138 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3139 || unlikely(cpu_is_offline(dest_cpu)))
3140 goto out;
3141
3142 /* force the process onto the specified CPU */
3143 if (migrate_task(p, dest_cpu, &req)) {
3144 /* Need to wait for migration thread (might exit: take ref). */
3145 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 get_task_struct(mt);
3148 task_rq_unlock(rq, &flags);
3149 wake_up_process(mt);
3150 put_task_struct(mt);
3151 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return;
3154 }
3155out:
3156 task_rq_unlock(rq, &flags);
3157}
3158
3159/*
Nick Piggin476d1392005-06-25 14:57:29 -07003160 * sched_exec - execve() is a valuable balancing opportunity, because at
3161 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 */
3163void sched_exec(void)
3164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003166 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003168 if (new_cpu != this_cpu)
3169 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
3172/*
3173 * pull_task - move a task from a remote runqueue to the local runqueue.
3174 * Both runqueues must be locked.
3175 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003176static void pull_task(struct rq *src_rq, struct task_struct *p,
3177 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003179 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003181 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /*
3183 * Note that idle threads have a prio of MAX_PRIO, for this test
3184 * to be always true for them.
3185 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003186 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
3189/*
3190 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3191 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003192static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003193int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003194 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003195 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 /*
3198 * We do not migrate tasks that are:
3199 * 1) running (obviously), or
3200 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3201 * 3) are cache-hot on their current CPU.
3202 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003203 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3204 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003206 }
Nick Piggin81026792005-06-25 14:57:07 -07003207 *all_pinned = 0;
3208
Ingo Molnarcc367732007-10-15 17:00:18 +02003209 if (task_running(rq, p)) {
3210 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003211 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Ingo Molnarda84d962007-10-15 17:00:18 +02003214 /*
3215 * Aggressive migration if:
3216 * 1) task is cache cold, or
3217 * 2) too many balance attempts have failed.
3218 */
3219
Ingo Molnar6bc16652007-10-15 17:00:18 +02003220 if (!task_hot(p, rq->clock, sd) ||
3221 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003222#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003223 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003224 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003225 schedstat_inc(p, se.nr_forced_migrations);
3226 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003227#endif
3228 return 1;
3229 }
3230
Ingo Molnarcc367732007-10-15 17:00:18 +02003231 if (task_hot(p, rq->clock, sd)) {
3232 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003233 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 1;
3236}
3237
Peter Williamse1d14842007-10-24 18:23:51 +02003238static unsigned long
3239balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3240 unsigned long max_load_move, struct sched_domain *sd,
3241 enum cpu_idle_type idle, int *all_pinned,
3242 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003243{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003244 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003245 struct task_struct *p;
3246 long rem_load_move = max_load_move;
3247
Peter Williamse1d14842007-10-24 18:23:51 +02003248 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003249 goto out;
3250
3251 pinned = 1;
3252
3253 /*
3254 * Start the load-balancing iterator:
3255 */
3256 p = iterator->start(iterator->arg);
3257next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003258 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003259 goto out;
3260 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003261 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003262 * skip a task if it will be the highest priority task (i.e. smallest
3263 * prio value) on its new queue regardless of its load weight
3264 */
3265 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3266 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003267 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003268 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003269 p = iterator->next(iterator->arg);
3270 goto next;
3271 }
3272
3273 pull_task(busiest, p, this_rq, this_cpu);
3274 pulled++;
3275 rem_load_move -= p->se.load.weight;
3276
3277 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003278 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003279 */
Peter Williamse1d14842007-10-24 18:23:51 +02003280 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003281 if (p->prio < *this_best_prio)
3282 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003283 p = iterator->next(iterator->arg);
3284 goto next;
3285 }
3286out:
3287 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003288 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003289 * so we can safely collect pull_task() stats here rather than
3290 * inside pull_task().
3291 */
3292 schedstat_add(sd, lb_gained[idle], pulled);
3293
3294 if (all_pinned)
3295 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003296
3297 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003298}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300/*
Peter Williams43010652007-08-09 11:16:46 +02003301 * move_tasks tries to move up to max_load_move weighted load from busiest to
3302 * this_rq, as part of a balancing operation within domain "sd".
3303 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *
3305 * Called with both runqueues locked.
3306 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003307static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003308 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003309 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003310 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003312 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003313 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003314 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Ingo Molnardd41f592007-07-09 18:51:59 +02003316 do {
Peter Williams43010652007-08-09 11:16:46 +02003317 total_load_moved +=
3318 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003319 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003320 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003321 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003322 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Peter Williams43010652007-08-09 11:16:46 +02003324 return total_load_moved > 0;
3325}
3326
Peter Williamse1d14842007-10-24 18:23:51 +02003327static int
3328iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3329 struct sched_domain *sd, enum cpu_idle_type idle,
3330 struct rq_iterator *iterator)
3331{
3332 struct task_struct *p = iterator->start(iterator->arg);
3333 int pinned = 0;
3334
3335 while (p) {
3336 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3337 pull_task(busiest, p, this_rq, this_cpu);
3338 /*
3339 * Right now, this is only the second place pull_task()
3340 * is called, so we can safely collect pull_task()
3341 * stats here rather than inside pull_task().
3342 */
3343 schedstat_inc(sd, lb_gained[idle]);
3344
3345 return 1;
3346 }
3347 p = iterator->next(iterator->arg);
3348 }
3349
3350 return 0;
3351}
3352
Peter Williams43010652007-08-09 11:16:46 +02003353/*
3354 * move_one_task tries to move exactly one task from busiest to this_rq, as
3355 * part of active balancing operations within "domain".
3356 * Returns 1 if successful and 0 otherwise.
3357 *
3358 * Called with both runqueues locked.
3359 */
3360static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3361 struct sched_domain *sd, enum cpu_idle_type idle)
3362{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003363 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003364
3365 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003366 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003367 return 1;
3368
3369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
3372/*
3373 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003374 * domain. It calculates and returns the amount of weighted load which
3375 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 */
3377static struct sched_group *
3378find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003379 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003380 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3383 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003384 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003385 unsigned long busiest_load_per_task, busiest_nr_running;
3386 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003387 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003388#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3389 int power_savings_balance = 1;
3390 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3391 unsigned long min_nr_running = ULONG_MAX;
3392 struct sched_group *group_min = NULL, *group_leader = NULL;
3393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003396 busiest_load_per_task = busiest_nr_running = 0;
3397 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003398 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003399 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003400 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003401 load_idx = sd->newidle_idx;
3402 else
3403 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003406 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 int local_group;
3408 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003409 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003410 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003411 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413 local_group = cpu_isset(this_cpu, group->cpumask);
3414
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003415 if (local_group)
3416 balance_cpu = first_cpu(group->cpumask);
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003419 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003420 max_cpu_load = 0;
3421 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003424 struct rq *rq;
3425
3426 if (!cpu_isset(i, *cpus))
3427 continue;
3428
3429 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003430
Suresh Siddha9439aab2007-07-19 21:28:35 +02003431 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003432 *sd_idle = 0;
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003435 if (local_group) {
3436 if (idle_cpu(i) && !first_idle_cpu) {
3437 first_idle_cpu = 1;
3438 balance_cpu = i;
3439 }
3440
Nick Piggina2000572006-02-10 01:51:02 -08003441 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003442 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003443 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003444 if (load > max_cpu_load)
3445 max_cpu_load = load;
3446 if (min_cpu_load > load)
3447 min_cpu_load = load;
3448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003451 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003452 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003455 /*
3456 * First idle cpu or the first cpu(busiest) in this sched group
3457 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003458 * domains. In the newly idle case, we will allow all the cpu's
3459 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003460 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003461 if (idle != CPU_NEWLY_IDLE && local_group &&
3462 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003463 *balance = 0;
3464 goto ret;
3465 }
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003468 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003471 avg_load = sg_div_cpu_power(group,
3472 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Ken Chen908a7c12007-10-17 16:55:11 +02003474 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3475 __group_imb = 1;
3476
Eric Dumazet5517d862007-05-08 00:32:57 -07003477 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (local_group) {
3480 this_load = avg_load;
3481 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003482 this_nr_running = sum_nr_running;
3483 this_load_per_task = sum_weighted_load;
3484 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003485 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 max_load = avg_load;
3487 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003488 busiest_nr_running = sum_nr_running;
3489 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003490 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003492
3493#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3494 /*
3495 * Busy processors will not participate in power savings
3496 * balance.
3497 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003498 if (idle == CPU_NOT_IDLE ||
3499 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3500 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003501
3502 /*
3503 * If the local group is idle or completely loaded
3504 * no need to do power savings balance at this domain
3505 */
3506 if (local_group && (this_nr_running >= group_capacity ||
3507 !this_nr_running))
3508 power_savings_balance = 0;
3509
Ingo Molnardd41f592007-07-09 18:51:59 +02003510 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003511 * If a group is already running at full capacity or idle,
3512 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003513 */
3514 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003515 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003516 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003517
Ingo Molnardd41f592007-07-09 18:51:59 +02003518 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003519 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003520 * This is the group from where we need to pick up the load
3521 * for saving power
3522 */
3523 if ((sum_nr_running < min_nr_running) ||
3524 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003525 first_cpu(group->cpumask) <
3526 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003527 group_min = group;
3528 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003529 min_load_per_task = sum_weighted_load /
3530 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003531 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003532
Ingo Molnardd41f592007-07-09 18:51:59 +02003533 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003534 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003535 * capacity but still has some space to pick up some load
3536 * from other group and save more power
3537 */
3538 if (sum_nr_running <= group_capacity - 1) {
3539 if (sum_nr_running > leader_nr_running ||
3540 (sum_nr_running == leader_nr_running &&
3541 first_cpu(group->cpumask) >
3542 first_cpu(group_leader->cpumask))) {
3543 group_leader = group;
3544 leader_nr_running = sum_nr_running;
3545 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003546 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003547group_next:
3548#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 group = group->next;
3550 } while (group != sd->groups);
3551
Peter Williams2dd73a42006-06-27 02:54:34 -07003552 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 goto out_balanced;
3554
3555 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3556
3557 if (this_load >= avg_load ||
3558 100*max_load <= sd->imbalance_pct*this_load)
3559 goto out_balanced;
3560
Peter Williams2dd73a42006-06-27 02:54:34 -07003561 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003562 if (group_imb)
3563 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 /*
3566 * We're trying to get all the cpus to the average_load, so we don't
3567 * want to push ourselves above the average load, nor do we wish to
3568 * reduce the max loaded cpu below the average load, as either of these
3569 * actions would just result in more rebalancing later, and ping-pong
3570 * tasks around. Thus we look for the minimum possible imbalance.
3571 * Negative imbalances (*we* are more loaded than anyone else) will
3572 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003573 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 * appear as very large values with unsigned longs.
3575 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003576 if (max_load <= busiest_load_per_task)
3577 goto out_balanced;
3578
3579 /*
3580 * In the presence of smp nice balancing, certain scenarios can have
3581 * max load less than avg load(as we skip the groups at or below
3582 * its cpu_power, while calculating max_load..)
3583 */
3584 if (max_load < avg_load) {
3585 *imbalance = 0;
3586 goto small_imbalance;
3587 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003588
3589 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003590 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003593 *imbalance = min(max_pull * busiest->__cpu_power,
3594 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 / SCHED_LOAD_SCALE;
3596
Peter Williams2dd73a42006-06-27 02:54:34 -07003597 /*
3598 * if *imbalance is less than the average load per runnable task
3599 * there is no gaurantee that any tasks will be moved so we'll have
3600 * a think about bumping its value to force at least one task to be
3601 * moved
3602 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003603 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003604 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003605 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Peter Williams2dd73a42006-06-27 02:54:34 -07003607small_imbalance:
3608 pwr_move = pwr_now = 0;
3609 imbn = 2;
3610 if (this_nr_running) {
3611 this_load_per_task /= this_nr_running;
3612 if (busiest_load_per_task > this_load_per_task)
3613 imbn = 1;
3614 } else
3615 this_load_per_task = SCHED_LOAD_SCALE;
3616
Ingo Molnardd41f592007-07-09 18:51:59 +02003617 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3618 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003619 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return busiest;
3621 }
3622
3623 /*
3624 * OK, we don't have enough imbalance to justify moving tasks,
3625 * however we may be able to increase total CPU power used by
3626 * moving them.
3627 */
3628
Eric Dumazet5517d862007-05-08 00:32:57 -07003629 pwr_now += busiest->__cpu_power *
3630 min(busiest_load_per_task, max_load);
3631 pwr_now += this->__cpu_power *
3632 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 pwr_now /= SCHED_LOAD_SCALE;
3634
3635 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003636 tmp = sg_div_cpu_power(busiest,
3637 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003639 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003640 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003643 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003644 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003645 tmp = sg_div_cpu_power(this,
3646 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003648 tmp = sg_div_cpu_power(this,
3649 busiest_load_per_task * SCHED_LOAD_SCALE);
3650 pwr_move += this->__cpu_power *
3651 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 pwr_move /= SCHED_LOAD_SCALE;
3653
3654 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003655 if (pwr_move > pwr_now)
3656 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return busiest;
3660
3661out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003662#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003663 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003664 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003666 if (this == group_leader && group_leader != group_min) {
3667 *imbalance = min_load_per_task;
3668 return group_min;
3669 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003670#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003671ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 *imbalance = 0;
3673 return NULL;
3674}
3675
3676/*
3677 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3678 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003679static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003680find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003681 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003683 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003684 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 int i;
3686
3687 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003688 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003689
3690 if (!cpu_isset(i, *cpus))
3691 continue;
3692
Ingo Molnar48f24c42006-07-03 00:25:40 -07003693 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003694 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Ingo Molnardd41f592007-07-09 18:51:59 +02003696 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003697 continue;
3698
Ingo Molnardd41f592007-07-09 18:51:59 +02003699 if (wl > max_load) {
3700 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003701 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
3703 }
3704
3705 return busiest;
3706}
3707
3708/*
Nick Piggin77391d72005-06-25 14:57:30 -07003709 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3710 * so long as it is large enough.
3711 */
3712#define MAX_PINNED_INTERVAL 512
3713
3714/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3716 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003718static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003719 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003720 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Peter Williams43010652007-08-09 11:16:46 +02003722 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003725 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003726 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003727 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003728
Mike Travis7c16ec52008-04-04 18:11:11 -07003729 cpus_setall(*cpus);
3730
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003731 unlock_aggregate = get_aggregate(sd);
3732
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003733 /*
3734 * When power savings policy is enabled for the parent domain, idle
3735 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003736 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003737 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003738 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003739 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003740 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003741 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Ingo Molnar2d723762007-10-15 17:00:12 +02003743 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003745redo:
3746 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003747 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003748
Chen, Kenneth W06066712006-12-10 02:20:35 -08003749 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003750 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (!group) {
3753 schedstat_inc(sd, lb_nobusyg[idle]);
3754 goto out_balanced;
3755 }
3756
Mike Travis7c16ec52008-04-04 18:11:11 -07003757 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (!busiest) {
3759 schedstat_inc(sd, lb_nobusyq[idle]);
3760 goto out_balanced;
3761 }
3762
Nick Piggindb935db2005-06-25 14:57:11 -07003763 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 schedstat_add(sd, lb_imbalance[idle], imbalance);
3766
Peter Williams43010652007-08-09 11:16:46 +02003767 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if (busiest->nr_running > 1) {
3769 /*
3770 * Attempt to move tasks. If find_busiest_group has found
3771 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003772 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 * correctly treated as an imbalance.
3774 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003775 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003776 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003777 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003778 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003779 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003780 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003781
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003782 /*
3783 * some other cpu did the load balance for us.
3784 */
Peter Williams43010652007-08-09 11:16:46 +02003785 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003786 resched_cpu(this_cpu);
3787
Nick Piggin81026792005-06-25 14:57:07 -07003788 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003789 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003790 cpu_clear(cpu_of(busiest), *cpus);
3791 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003792 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003793 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 }
Nick Piggin81026792005-06-25 14:57:07 -07003796
Peter Williams43010652007-08-09 11:16:46 +02003797 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 schedstat_inc(sd, lb_failed[idle]);
3799 sd->nr_balance_failed++;
3800
3801 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003803 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003804
3805 /* don't kick the migration_thread, if the curr
3806 * task on busiest cpu can't be moved to this_cpu
3807 */
3808 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003809 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003810 all_pinned = 1;
3811 goto out_one_pinned;
3812 }
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (!busiest->active_balance) {
3815 busiest->active_balance = 1;
3816 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003817 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003819 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003820 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 wake_up_process(busiest->migration_thread);
3822
3823 /*
3824 * We've kicked active balancing, reset the failure
3825 * counter.
3826 */
Nick Piggin39507452005-06-25 14:57:09 -07003827 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
Nick Piggin81026792005-06-25 14:57:07 -07003829 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 sd->nr_balance_failed = 0;
3831
Nick Piggin81026792005-06-25 14:57:07 -07003832 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 /* We were unbalanced, so reset the balancing interval */
3834 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003835 } else {
3836 /*
3837 * If we've begun active balancing, start to back off. This
3838 * case may not be covered by the all_pinned logic if there
3839 * is only 1 task on the busy runqueue (because we don't call
3840 * move_tasks).
3841 */
3842 if (sd->balance_interval < sd->max_interval)
3843 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 }
3845
Peter Williams43010652007-08-09 11:16:46 +02003846 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003847 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003848 ld_moved = -1;
3849
3850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
3852out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 schedstat_inc(sd, lb_balanced[idle]);
3854
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003855 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003856
3857out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003859 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3860 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 sd->balance_interval *= 2;
3862
Ingo Molnar48f24c42006-07-03 00:25:40 -07003863 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003864 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003865 ld_moved = -1;
3866 else
3867 ld_moved = 0;
3868out:
3869 if (unlock_aggregate)
3870 put_aggregate(sd);
3871 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873
3874/*
3875 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3876 * tasks if there is an imbalance.
3877 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003878 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 * this_rq is locked.
3880 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003881static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003882load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3883 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
3885 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003886 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003888 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003889 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003890 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003891
3892 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003893
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003894 /*
3895 * When power savings policy is enabled for the parent domain, idle
3896 * sibling can pick up load irrespective of busy siblings. In this case,
3897 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003898 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003899 */
3900 if (sd->flags & SD_SHARE_CPUPOWER &&
3901 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003902 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Ingo Molnar2d723762007-10-15 17:00:12 +02003904 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003905redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003906 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003907 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003909 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003910 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
3912
Mike Travis7c16ec52008-04-04 18:11:11 -07003913 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003914 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003915 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003916 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
3918
Nick Piggindb935db2005-06-25 14:57:11 -07003919 BUG_ON(busiest == this_rq);
3920
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003921 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003922
Peter Williams43010652007-08-09 11:16:46 +02003923 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003924 if (busiest->nr_running > 1) {
3925 /* Attempt to move tasks */
3926 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003927 /* this_rq->clock is already updated */
3928 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003929 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003930 imbalance, sd, CPU_NEWLY_IDLE,
3931 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003932 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003933
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003934 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003935 cpu_clear(cpu_of(busiest), *cpus);
3936 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003937 goto redo;
3938 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003939 }
3940
Peter Williams43010652007-08-09 11:16:46 +02003941 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003942 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003943 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3944 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003945 return -1;
3946 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003947 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Peter Williams43010652007-08-09 11:16:46 +02003949 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003950
3951out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003952 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003953 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003954 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003955 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003956 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003957
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
3961/*
3962 * idle_balance is called by schedule() if this_cpu is about to become
3963 * idle. Attempts to pull tasks from other CPUs.
3964 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003965static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
3967 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003968 int pulled_task = -1;
3969 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003970 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003973 unsigned long interval;
3974
3975 if (!(sd->flags & SD_LOAD_BALANCE))
3976 continue;
3977
3978 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003979 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003980 pulled_task = load_balance_newidle(this_cpu, this_rq,
3981 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003982
3983 interval = msecs_to_jiffies(sd->balance_interval);
3984 if (time_after(next_balance, sd->last_balance + interval))
3985 next_balance = sd->last_balance + interval;
3986 if (pulled_task)
3987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003989 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003990 /*
3991 * We are going idle. next_balance may be set based on
3992 * a busy processor. So reset next_balance.
3993 */
3994 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998/*
3999 * active_load_balance is run by migration threads. It pushes running tasks
4000 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4001 * running on each physical CPU where possible, and avoids physical /
4002 * logical imbalances.
4003 *
4004 * Called with busiest_rq locked.
4005 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004006static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Nick Piggin39507452005-06-25 14:57:09 -07004008 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004009 struct sched_domain *sd;
4010 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07004011
Ingo Molnar48f24c42006-07-03 00:25:40 -07004012 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07004013 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07004014 return;
4015
4016 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 /*
Nick Piggin39507452005-06-25 14:57:09 -07004019 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004020 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07004021 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 */
Nick Piggin39507452005-06-25 14:57:09 -07004023 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Nick Piggin39507452005-06-25 14:57:09 -07004025 /* move a task from busiest_rq to target_rq */
4026 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004027 update_rq_clock(busiest_rq);
4028 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Nick Piggin39507452005-06-25 14:57:09 -07004030 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004031 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004032 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004033 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07004034 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Ingo Molnar48f24c42006-07-03 00:25:40 -07004037 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004038 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Peter Williams43010652007-08-09 11:16:46 +02004040 if (move_one_task(target_rq, target_cpu, busiest_rq,
4041 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004042 schedstat_inc(sd, alb_pushed);
4043 else
4044 schedstat_inc(sd, alb_failed);
4045 }
Nick Piggin39507452005-06-25 14:57:09 -07004046 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004049#ifdef CONFIG_NO_HZ
4050static struct {
4051 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004052 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004053} nohz ____cacheline_aligned = {
4054 .load_balancer = ATOMIC_INIT(-1),
4055 .cpu_mask = CPU_MASK_NONE,
4056};
4057
Christoph Lameter7835b982006-12-10 02:20:22 -08004058/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004059 * This routine will try to nominate the ilb (idle load balancing)
4060 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4061 * load balancing on behalf of all those cpus. If all the cpus in the system
4062 * go into this tickless mode, then there will be no ilb owner (as there is
4063 * no need for one) and all the cpus will sleep till the next wakeup event
4064 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004065 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004066 * For the ilb owner, tick is not stopped. And this tick will be used
4067 * for idle load balancing. ilb owner will still be part of
4068 * nohz.cpu_mask..
4069 *
4070 * While stopping the tick, this cpu will become the ilb owner if there
4071 * is no other owner. And will be the owner till that cpu becomes busy
4072 * or if all cpus in the system stop their ticks at which point
4073 * there is no need for ilb owner.
4074 *
4075 * When the ilb owner becomes busy, it nominates another owner, during the
4076 * next busy scheduler_tick()
4077 */
4078int select_nohz_load_balancer(int stop_tick)
4079{
4080 int cpu = smp_processor_id();
4081
4082 if (stop_tick) {
4083 cpu_set(cpu, nohz.cpu_mask);
4084 cpu_rq(cpu)->in_nohz_recently = 1;
4085
4086 /*
4087 * If we are going offline and still the leader, give up!
4088 */
4089 if (cpu_is_offline(cpu) &&
4090 atomic_read(&nohz.load_balancer) == cpu) {
4091 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4092 BUG();
4093 return 0;
4094 }
4095
4096 /* time for ilb owner also to sleep */
4097 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4098 if (atomic_read(&nohz.load_balancer) == cpu)
4099 atomic_set(&nohz.load_balancer, -1);
4100 return 0;
4101 }
4102
4103 if (atomic_read(&nohz.load_balancer) == -1) {
4104 /* make me the ilb owner */
4105 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4106 return 1;
4107 } else if (atomic_read(&nohz.load_balancer) == cpu)
4108 return 1;
4109 } else {
4110 if (!cpu_isset(cpu, nohz.cpu_mask))
4111 return 0;
4112
4113 cpu_clear(cpu, nohz.cpu_mask);
4114
4115 if (atomic_read(&nohz.load_balancer) == cpu)
4116 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4117 BUG();
4118 }
4119 return 0;
4120}
4121#endif
4122
4123static DEFINE_SPINLOCK(balancing);
4124
4125/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004126 * It checks each scheduling domain to see if it is due to be balanced,
4127 * and initiates a balancing operation if so.
4128 *
4129 * Balancing parameters are set up in arch_init_sched_domains.
4130 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004131static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004132{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004133 int balance = 1;
4134 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004135 unsigned long interval;
4136 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004137 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004138 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004139 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004140 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004142 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (!(sd->flags & SD_LOAD_BALANCE))
4144 continue;
4145
4146 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004147 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 interval *= sd->busy_factor;
4149
4150 /* scale ms to jiffies */
4151 interval = msecs_to_jiffies(interval);
4152 if (unlikely(!interval))
4153 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004154 if (interval > HZ*NR_CPUS/10)
4155 interval = HZ*NR_CPUS/10;
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Christoph Lameter08c183f2006-12-10 02:20:29 -08004158 if (sd->flags & SD_SERIALIZE) {
4159 if (!spin_trylock(&balancing))
4160 goto out;
4161 }
4162
Christoph Lameterc9819f42006-12-10 02:20:25 -08004163 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004164 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004165 /*
4166 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004167 * longer idle, or one of our SMT siblings is
4168 * not idle.
4169 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004170 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004172 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004174 if (sd->flags & SD_SERIALIZE)
4175 spin_unlock(&balancing);
4176out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004177 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004178 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004179 update_next_balance = 1;
4180 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004181
4182 /*
4183 * Stop the load balance at this level. There is another
4184 * CPU in our sched group which is doing load balancing more
4185 * actively.
4186 */
4187 if (!balance)
4188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004190
4191 /*
4192 * next_balance will be updated only when there is a need.
4193 * When the cpu is attached to null domain for ex, it will not be
4194 * updated.
4195 */
4196 if (likely(update_next_balance))
4197 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004198}
4199
4200/*
4201 * run_rebalance_domains is triggered when needed from the scheduler tick.
4202 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4203 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4204 */
4205static void run_rebalance_domains(struct softirq_action *h)
4206{
Ingo Molnardd41f592007-07-09 18:51:59 +02004207 int this_cpu = smp_processor_id();
4208 struct rq *this_rq = cpu_rq(this_cpu);
4209 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4210 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004211
Ingo Molnardd41f592007-07-09 18:51:59 +02004212 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004213
4214#ifdef CONFIG_NO_HZ
4215 /*
4216 * If this cpu is the owner for idle load balancing, then do the
4217 * balancing on behalf of the other idle cpus whose ticks are
4218 * stopped.
4219 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004220 if (this_rq->idle_at_tick &&
4221 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004222 cpumask_t cpus = nohz.cpu_mask;
4223 struct rq *rq;
4224 int balance_cpu;
4225
Ingo Molnardd41f592007-07-09 18:51:59 +02004226 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004227 for_each_cpu_mask(balance_cpu, cpus) {
4228 /*
4229 * If this cpu gets work to do, stop the load balancing
4230 * work being done for other cpus. Next load
4231 * balancing owner will pick it up.
4232 */
4233 if (need_resched())
4234 break;
4235
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004236 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004237
4238 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004239 if (time_after(this_rq->next_balance, rq->next_balance))
4240 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004241 }
4242 }
4243#endif
4244}
4245
4246/*
4247 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4248 *
4249 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4250 * idle load balancing owner or decide to stop the periodic load balancing,
4251 * if the whole system is idle.
4252 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004253static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004254{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004255#ifdef CONFIG_NO_HZ
4256 /*
4257 * If we were in the nohz mode recently and busy at the current
4258 * scheduler tick, then check if we need to nominate new idle
4259 * load balancer.
4260 */
4261 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4262 rq->in_nohz_recently = 0;
4263
4264 if (atomic_read(&nohz.load_balancer) == cpu) {
4265 cpu_clear(cpu, nohz.cpu_mask);
4266 atomic_set(&nohz.load_balancer, -1);
4267 }
4268
4269 if (atomic_read(&nohz.load_balancer) == -1) {
4270 /*
4271 * simple selection for now: Nominate the
4272 * first cpu in the nohz list to be the next
4273 * ilb owner.
4274 *
4275 * TBD: Traverse the sched domains and nominate
4276 * the nearest cpu in the nohz.cpu_mask.
4277 */
4278 int ilb = first_cpu(nohz.cpu_mask);
4279
Mike Travis434d53b2008-04-04 18:11:04 -07004280 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004281 resched_cpu(ilb);
4282 }
4283 }
4284
4285 /*
4286 * If this cpu is idle and doing idle load balancing for all the
4287 * cpus with ticks stopped, is it time for that to stop?
4288 */
4289 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4290 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4291 resched_cpu(cpu);
4292 return;
4293 }
4294
4295 /*
4296 * If this cpu is idle and the idle load balancing is done by
4297 * someone else, then no need raise the SCHED_SOFTIRQ
4298 */
4299 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4300 cpu_isset(cpu, nohz.cpu_mask))
4301 return;
4302#endif
4303 if (time_after_eq(jiffies, rq->next_balance))
4304 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
Ingo Molnardd41f592007-07-09 18:51:59 +02004306
4307#else /* CONFIG_SMP */
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309/*
4310 * on UP we do not need to balance between CPUs:
4311 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004312static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314}
Ingo Molnardd41f592007-07-09 18:51:59 +02004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316#endif
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318DEFINE_PER_CPU(struct kernel_stat, kstat);
4319
4320EXPORT_PER_CPU_SYMBOL(kstat);
4321
4322/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004323 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4324 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004326unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004329 u64 ns, delta_exec;
4330 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004331
Ingo Molnar41b86e92007-07-09 18:51:58 +02004332 rq = task_rq_lock(p, &flags);
4333 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004334 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004335 update_rq_clock(rq);
4336 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004337 if ((s64)delta_exec > 0)
4338 ns += delta_exec;
4339 }
4340 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return ns;
4343}
4344
4345/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 * Account user cpu time to a process.
4347 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 * @cputime: the cpu time spent in user space since the last update
4349 */
4350void account_user_time(struct task_struct *p, cputime_t cputime)
4351{
4352 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4353 cputime64_t tmp;
4354
4355 p->utime = cputime_add(p->utime, cputime);
4356
4357 /* Add user time to cpustat. */
4358 tmp = cputime_to_cputime64(cputime);
4359 if (TASK_NICE(p) > 0)
4360 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4361 else
4362 cpustat->user = cputime64_add(cpustat->user, tmp);
4363}
4364
4365/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004366 * Account guest cpu time to a process.
4367 * @p: the process that the cpu time gets accounted to
4368 * @cputime: the cpu time spent in virtual machine since the last update
4369 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004370static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004371{
4372 cputime64_t tmp;
4373 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4374
4375 tmp = cputime_to_cputime64(cputime);
4376
4377 p->utime = cputime_add(p->utime, cputime);
4378 p->gtime = cputime_add(p->gtime, cputime);
4379
4380 cpustat->user = cputime64_add(cpustat->user, tmp);
4381 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4382}
4383
4384/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004385 * Account scaled user cpu time to a process.
4386 * @p: the process that the cpu time gets accounted to
4387 * @cputime: the cpu time spent in user space since the last update
4388 */
4389void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4390{
4391 p->utimescaled = cputime_add(p->utimescaled, cputime);
4392}
4393
4394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 * Account system cpu time to a process.
4396 * @p: the process that the cpu time gets accounted to
4397 * @hardirq_offset: the offset to subtract from hardirq_count()
4398 * @cputime: the cpu time spent in kernel space since the last update
4399 */
4400void account_system_time(struct task_struct *p, int hardirq_offset,
4401 cputime_t cputime)
4402{
4403 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004404 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 cputime64_t tmp;
4406
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004407 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4408 account_guest_time(p, cputime);
4409 return;
4410 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 p->stime = cputime_add(p->stime, cputime);
4413
4414 /* Add system time to cpustat. */
4415 tmp = cputime_to_cputime64(cputime);
4416 if (hardirq_count() - hardirq_offset)
4417 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4418 else if (softirq_count())
4419 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004420 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004422 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4424 else
4425 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4426 /* Account for system time used */
4427 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004431 * Account scaled system cpu time to a process.
4432 * @p: the process that the cpu time gets accounted to
4433 * @hardirq_offset: the offset to subtract from hardirq_count()
4434 * @cputime: the cpu time spent in kernel space since the last update
4435 */
4436void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4437{
4438 p->stimescaled = cputime_add(p->stimescaled, cputime);
4439}
4440
4441/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 * Account for involuntary wait time.
4443 * @p: the process from which the cpu time has been stolen
4444 * @steal: the cpu time spent in involuntary wait
4445 */
4446void account_steal_time(struct task_struct *p, cputime_t steal)
4447{
4448 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4449 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004450 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
4452 if (p == rq->idle) {
4453 p->stime = cputime_add(p->stime, steal);
4454 if (atomic_read(&rq->nr_iowait) > 0)
4455 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4456 else
4457 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004458 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4460}
4461
Christoph Lameter7835b982006-12-10 02:20:22 -08004462/*
4463 * This function gets called by the timer code, with HZ frequency.
4464 * We call it with interrupts disabled.
4465 *
4466 * It also gets called by the fork code, when changing the parent's
4467 * timeslices.
4468 */
4469void scheduler_tick(void)
4470{
Christoph Lameter7835b982006-12-10 02:20:22 -08004471 int cpu = smp_processor_id();
4472 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004473 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004474 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004475
Ingo Molnardd41f592007-07-09 18:51:59 +02004476 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004477 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004478 /*
4479 * Let rq->clock advance by at least TICK_NSEC:
4480 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004481 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004482 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004483 rq->clock_underflows++;
4484 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004485 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004486 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004487 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004488 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004489 spin_unlock(&rq->lock);
4490
Christoph Lametere418e1c2006-12-10 02:20:23 -08004491#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004492 rq->idle_at_tick = idle_cpu(cpu);
4493 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495}
4496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4498
Srinivasa Ds43627582008-02-23 15:24:04 -08004499void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
4501 /*
4502 * Underflow?
4503 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004504 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 preempt_count() += val;
4507 /*
4508 * Spinlock count overflowing soon?
4509 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004510 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4511 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513EXPORT_SYMBOL(add_preempt_count);
4514
Srinivasa Ds43627582008-02-23 15:24:04 -08004515void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
4517 /*
4518 * Underflow?
4519 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004520 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4521 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /*
4523 * Is the spinlock portion underflowing?
4524 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004525 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4526 !(preempt_count() & PREEMPT_MASK)))
4527 return;
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 preempt_count() -= val;
4530}
4531EXPORT_SYMBOL(sub_preempt_count);
4532
4533#endif
4534
4535/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004536 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004538static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Satyam Sharma838225b2007-10-24 18:23:50 +02004540 struct pt_regs *regs = get_irq_regs();
4541
4542 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4543 prev->comm, prev->pid, preempt_count());
4544
Ingo Molnardd41f592007-07-09 18:51:59 +02004545 debug_show_held_locks(prev);
4546 if (irqs_disabled())
4547 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004548
4549 if (regs)
4550 show_regs(regs);
4551 else
4552 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Ingo Molnardd41f592007-07-09 18:51:59 +02004555/*
4556 * Various schedule()-time debugging checks and statistics:
4557 */
4558static inline void schedule_debug(struct task_struct *prev)
4559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004561 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 * schedule() atomically, we ignore that path for now.
4563 * Otherwise, whine if we are scheduling when we should not be.
4564 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004565 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4566 __schedule_bug(prev);
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4569
Ingo Molnar2d723762007-10-15 17:00:12 +02004570 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004571#ifdef CONFIG_SCHEDSTATS
4572 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004573 schedstat_inc(this_rq(), bkl_count);
4574 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004575 }
4576#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004577}
4578
4579/*
4580 * Pick up the highest-prio task:
4581 */
4582static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004583pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004584{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004585 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004586 struct task_struct *p;
4587
4588 /*
4589 * Optimization: we know that if all tasks are in
4590 * the fair class we can call that function directly:
4591 */
4592 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004593 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004594 if (likely(p))
4595 return p;
4596 }
4597
4598 class = sched_class_highest;
4599 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004600 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004601 if (p)
4602 return p;
4603 /*
4604 * Will never be NULL as the idle class always
4605 * returns a non-NULL p:
4606 */
4607 class = class->next;
4608 }
4609}
4610
4611/*
4612 * schedule() is the main scheduler function.
4613 */
4614asmlinkage void __sched schedule(void)
4615{
4616 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004617 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004618 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004619 int cpu;
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621need_resched:
4622 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004623 cpu = smp_processor_id();
4624 rq = cpu_rq(cpu);
4625 rcu_qsctr_inc(cpu);
4626 prev = rq->curr;
4627 switch_count = &prev->nivcsw;
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 release_kernel_lock(prev);
4630need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Ingo Molnardd41f592007-07-09 18:51:59 +02004632 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004634 hrtick_clear(rq);
4635
Ingo Molnar1e819952007-10-15 17:00:13 +02004636 /*
4637 * Do the rq-clock update outside the rq lock:
4638 */
4639 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004640 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004641 spin_lock(&rq->lock);
4642 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Ingo Molnardd41f592007-07-09 18:51:59 +02004644 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4645 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004646 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004647 prev->state = TASK_RUNNING;
4648 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004649 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004650 }
4651 switch_count = &prev->nvcsw;
4652 }
4653
Steven Rostedt9a897c52008-01-25 21:08:22 +01004654#ifdef CONFIG_SMP
4655 if (prev->sched_class->pre_schedule)
4656 prev->sched_class->pre_schedule(rq, prev);
4657#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004658
Ingo Molnardd41f592007-07-09 18:51:59 +02004659 if (unlikely(!rq->nr_running))
4660 idle_balance(cpu, rq);
4661
Ingo Molnar31ee5292007-08-09 11:16:49 +02004662 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004663 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 rq->nr_switches++;
4669 rq->curr = next;
4670 ++*switch_count;
4671
Ingo Molnardd41f592007-07-09 18:51:59 +02004672 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004673 /*
4674 * the context switch might have flipped the stack from under
4675 * us, hence refresh the local variables.
4676 */
4677 cpu = smp_processor_id();
4678 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else
4680 spin_unlock_irq(&rq->lock);
4681
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004682 hrtick_set(rq);
4683
4684 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 preempt_enable_no_resched();
4688 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4689 goto need_resched;
4690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691EXPORT_SYMBOL(schedule);
4692
4693#ifdef CONFIG_PREEMPT
4694/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004695 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004696 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 * occur there and call schedule directly.
4698 */
4699asmlinkage void __sched preempt_schedule(void)
4700{
4701 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 struct task_struct *task = current;
4703 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 /*
4706 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004707 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004709 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 return;
4711
Andi Kleen3a5c3592007-10-15 17:00:14 +02004712 do {
4713 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Andi Kleen3a5c3592007-10-15 17:00:14 +02004715 /*
4716 * We keep the big kernel semaphore locked, but we
4717 * clear ->lock_depth so that schedule() doesnt
4718 * auto-release the semaphore:
4719 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004720 saved_lock_depth = task->lock_depth;
4721 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004722 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004723 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004724 sub_preempt_count(PREEMPT_ACTIVE);
4725
4726 /*
4727 * Check again in case we missed a preemption opportunity
4728 * between schedule and now.
4729 */
4730 barrier();
4731 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733EXPORT_SYMBOL(preempt_schedule);
4734
4735/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004736 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 * off of irq context.
4738 * Note, that this is called and return with irqs disabled. This will
4739 * protect us against recursive calling from irq.
4740 */
4741asmlinkage void __sched preempt_schedule_irq(void)
4742{
4743 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct task_struct *task = current;
4745 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004746
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004747 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 BUG_ON(ti->preempt_count || !irqs_disabled());
4749
Andi Kleen3a5c3592007-10-15 17:00:14 +02004750 do {
4751 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Andi Kleen3a5c3592007-10-15 17:00:14 +02004753 /*
4754 * We keep the big kernel semaphore locked, but we
4755 * clear ->lock_depth so that schedule() doesnt
4756 * auto-release the semaphore:
4757 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004758 saved_lock_depth = task->lock_depth;
4759 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004760 local_irq_enable();
4761 schedule();
4762 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004763 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004764 sub_preempt_count(PREEMPT_ACTIVE);
4765
4766 /*
4767 * Check again in case we missed a preemption opportunity
4768 * between schedule and now.
4769 */
4770 barrier();
4771 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
4773
4774#endif /* CONFIG_PREEMPT */
4775
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004776int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4777 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004779 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781EXPORT_SYMBOL(default_wake_function);
4782
4783/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004784 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4785 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 * number) then we wake all the non-exclusive tasks and one exclusive task.
4787 *
4788 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004789 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4791 */
4792static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4793 int nr_exclusive, int sync, void *key)
4794{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004795 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004797 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004798 unsigned flags = curr->flags;
4799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004801 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 break;
4803 }
4804}
4805
4806/**
4807 * __wake_up - wake up threads blocked on a waitqueue.
4808 * @q: the waitqueue
4809 * @mode: which threads
4810 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004811 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004813void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004814 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
4816 unsigned long flags;
4817
4818 spin_lock_irqsave(&q->lock, flags);
4819 __wake_up_common(q, mode, nr_exclusive, 0, key);
4820 spin_unlock_irqrestore(&q->lock, flags);
4821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822EXPORT_SYMBOL(__wake_up);
4823
4824/*
4825 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4826 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004827void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
4829 __wake_up_common(q, mode, 1, 0, NULL);
4830}
4831
4832/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004833 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 * @q: the waitqueue
4835 * @mode: which threads
4836 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4837 *
4838 * The sync wakeup differs that the waker knows that it will schedule
4839 * away soon, so while the target thread will be woken up, it will not
4840 * be migrated to another CPU - ie. the two threads are 'synchronized'
4841 * with each other. This can prevent needless bouncing between CPUs.
4842 *
4843 * On UP it can prevent extra preemption.
4844 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004845void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004846__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
4848 unsigned long flags;
4849 int sync = 1;
4850
4851 if (unlikely(!q))
4852 return;
4853
4854 if (unlikely(!nr_exclusive))
4855 sync = 0;
4856
4857 spin_lock_irqsave(&q->lock, flags);
4858 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4859 spin_unlock_irqrestore(&q->lock, flags);
4860}
4861EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4862
Ingo Molnarb15136e2007-10-24 18:23:48 +02004863void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
4865 unsigned long flags;
4866
4867 spin_lock_irqsave(&x->wait.lock, flags);
4868 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004869 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 spin_unlock_irqrestore(&x->wait.lock, flags);
4871}
4872EXPORT_SYMBOL(complete);
4873
Ingo Molnarb15136e2007-10-24 18:23:48 +02004874void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
4876 unsigned long flags;
4877
4878 spin_lock_irqsave(&x->wait.lock, flags);
4879 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004880 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 spin_unlock_irqrestore(&x->wait.lock, flags);
4882}
4883EXPORT_SYMBOL(complete_all);
4884
Andi Kleen8cbbe862007-10-15 17:00:14 +02004885static inline long __sched
4886do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (!x->done) {
4889 DECLARE_WAITQUEUE(wait, current);
4890
4891 wait.flags |= WQ_FLAG_EXCLUSIVE;
4892 __add_wait_queue_tail(&x->wait, &wait);
4893 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004894 if ((state == TASK_INTERRUPTIBLE &&
4895 signal_pending(current)) ||
4896 (state == TASK_KILLABLE &&
4897 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004898 __remove_wait_queue(&x->wait, &wait);
4899 return -ERESTARTSYS;
4900 }
4901 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004903 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004905 if (!timeout) {
4906 __remove_wait_queue(&x->wait, &wait);
4907 return timeout;
4908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 } while (!x->done);
4910 __remove_wait_queue(&x->wait, &wait);
4911 }
4912 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004913 return timeout;
4914}
4915
4916static long __sched
4917wait_for_common(struct completion *x, long timeout, int state)
4918{
4919 might_sleep();
4920
4921 spin_lock_irq(&x->wait.lock);
4922 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004924 return timeout;
4925}
4926
Ingo Molnarb15136e2007-10-24 18:23:48 +02004927void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004928{
4929 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931EXPORT_SYMBOL(wait_for_completion);
4932
Ingo Molnarb15136e2007-10-24 18:23:48 +02004933unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4935{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004936 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938EXPORT_SYMBOL(wait_for_completion_timeout);
4939
Andi Kleen8cbbe862007-10-15 17:00:14 +02004940int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
Andi Kleen51e97992007-10-18 21:32:55 +02004942 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4943 if (t == -ERESTARTSYS)
4944 return t;
4945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947EXPORT_SYMBOL(wait_for_completion_interruptible);
4948
Ingo Molnarb15136e2007-10-24 18:23:48 +02004949unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950wait_for_completion_interruptible_timeout(struct completion *x,
4951 unsigned long timeout)
4952{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004953 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4956
Matthew Wilcox009e5772007-12-06 12:29:54 -05004957int __sched wait_for_completion_killable(struct completion *x)
4958{
4959 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4960 if (t == -ERESTARTSYS)
4961 return t;
4962 return 0;
4963}
4964EXPORT_SYMBOL(wait_for_completion_killable);
4965
Andi Kleen8cbbe862007-10-15 17:00:14 +02004966static long __sched
4967sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004968{
4969 unsigned long flags;
4970 wait_queue_t wait;
4971
4972 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Andi Kleen8cbbe862007-10-15 17:00:14 +02004974 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Andi Kleen8cbbe862007-10-15 17:00:14 +02004976 spin_lock_irqsave(&q->lock, flags);
4977 __add_wait_queue(q, &wait);
4978 spin_unlock(&q->lock);
4979 timeout = schedule_timeout(timeout);
4980 spin_lock_irq(&q->lock);
4981 __remove_wait_queue(q, &wait);
4982 spin_unlock_irqrestore(&q->lock, flags);
4983
4984 return timeout;
4985}
4986
4987void __sched interruptible_sleep_on(wait_queue_head_t *q)
4988{
4989 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991EXPORT_SYMBOL(interruptible_sleep_on);
4992
Ingo Molnar0fec1712007-07-09 18:52:01 +02004993long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004994interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004996 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4999
Ingo Molnar0fec1712007-07-09 18:52:01 +02005000void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005002 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004EXPORT_SYMBOL(sleep_on);
5005
Ingo Molnar0fec1712007-07-09 18:52:01 +02005006long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005008 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010EXPORT_SYMBOL(sleep_on_timeout);
5011
Ingo Molnarb29739f2006-06-27 02:54:51 -07005012#ifdef CONFIG_RT_MUTEXES
5013
5014/*
5015 * rt_mutex_setprio - set the current priority of a task
5016 * @p: task
5017 * @prio: prio value (kernel-internal form)
5018 *
5019 * This function changes the 'effective' priority of a task. It does
5020 * not touch ->normal_prio like __setscheduler().
5021 *
5022 * Used by the rt_mutex code to implement priority inheritance logic.
5023 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005024void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005025{
5026 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005027 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005028 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005029 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005030
5031 BUG_ON(prio < 0 || prio > MAX_PRIO);
5032
5033 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005034 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005035
Andrew Mortond5f9f942007-05-08 20:27:06 -07005036 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005037 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005038 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005039 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005040 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005041 if (running)
5042 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005043
5044 if (rt_prio(prio))
5045 p->sched_class = &rt_sched_class;
5046 else
5047 p->sched_class = &fair_sched_class;
5048
Ingo Molnarb29739f2006-06-27 02:54:51 -07005049 p->prio = prio;
5050
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005051 if (running)
5052 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005053 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005054 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005055
5056 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005057 }
5058 task_rq_unlock(rq, &flags);
5059}
5060
5061#endif
5062
Ingo Molnar36c8b582006-07-03 00:25:41 -07005063void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
Ingo Molnardd41f592007-07-09 18:51:59 +02005065 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005067 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5070 return;
5071 /*
5072 * We have to be careful, if called from sys_setpriority(),
5073 * the task might be in the middle of scheduling on another CPU.
5074 */
5075 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005076 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 /*
5078 * The RT priorities are set via sched_setscheduler(), but we still
5079 * allow the 'normal' nice value to be set - but as expected
5080 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005081 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005083 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 p->static_prio = NICE_TO_PRIO(nice);
5085 goto out_unlock;
5086 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005087 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02005088 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005089 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005092 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005093 old_prio = p->prio;
5094 p->prio = effective_prio(p);
5095 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Ingo Molnardd41f592007-07-09 18:51:59 +02005097 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005098 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005100 * If the task increased its priority or is running and
5101 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005103 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 resched_task(rq->curr);
5105 }
5106out_unlock:
5107 task_rq_unlock(rq, &flags);
5108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109EXPORT_SYMBOL(set_user_nice);
5110
Matt Mackalle43379f2005-05-01 08:59:00 -07005111/*
5112 * can_nice - check if a task can reduce its nice value
5113 * @p: task
5114 * @nice: nice value
5115 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005116int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005117{
Matt Mackall024f4742005-08-18 11:24:19 -07005118 /* convert nice value [19,-20] to rlimit style value [1,40] */
5119 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005120
Matt Mackalle43379f2005-05-01 08:59:00 -07005121 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5122 capable(CAP_SYS_NICE));
5123}
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125#ifdef __ARCH_WANT_SYS_NICE
5126
5127/*
5128 * sys_nice - change the priority of the current process.
5129 * @increment: priority increment
5130 *
5131 * sys_setpriority is a more generic, but much slower function that
5132 * does similar things.
5133 */
5134asmlinkage long sys_nice(int increment)
5135{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005136 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 /*
5139 * Setpriority might change our priority at the same moment.
5140 * We don't have to worry. Conceptually one call occurs first
5141 * and we have a single winner.
5142 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005143 if (increment < -40)
5144 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (increment > 40)
5146 increment = 40;
5147
5148 nice = PRIO_TO_NICE(current->static_prio) + increment;
5149 if (nice < -20)
5150 nice = -20;
5151 if (nice > 19)
5152 nice = 19;
5153
Matt Mackalle43379f2005-05-01 08:59:00 -07005154 if (increment < 0 && !can_nice(current, nice))
5155 return -EPERM;
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 retval = security_task_setnice(current, nice);
5158 if (retval)
5159 return retval;
5160
5161 set_user_nice(current, nice);
5162 return 0;
5163}
5164
5165#endif
5166
5167/**
5168 * task_prio - return the priority value of a given task.
5169 * @p: the task in question.
5170 *
5171 * This is the priority value as seen by users in /proc.
5172 * RT tasks are offset by -200. Normal tasks are centered
5173 * around 0, value goes from -16 to +15.
5174 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005175int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
5177 return p->prio - MAX_RT_PRIO;
5178}
5179
5180/**
5181 * task_nice - return the nice value of a given task.
5182 * @p: the task in question.
5183 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005184int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
5186 return TASK_NICE(p);
5187}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005188EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
5190/**
5191 * idle_cpu - is a given cpu idle currently?
5192 * @cpu: the processor in question.
5193 */
5194int idle_cpu(int cpu)
5195{
5196 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5197}
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199/**
5200 * idle_task - return the idle task for a given cpu.
5201 * @cpu: the processor in question.
5202 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005203struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
5205 return cpu_rq(cpu)->idle;
5206}
5207
5208/**
5209 * find_process_by_pid - find a process with a matching PID value.
5210 * @pid: the pid in question.
5211 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005212static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005214 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216
5217/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005218static void
5219__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
Ingo Molnardd41f592007-07-09 18:51:59 +02005221 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005224 switch (p->policy) {
5225 case SCHED_NORMAL:
5226 case SCHED_BATCH:
5227 case SCHED_IDLE:
5228 p->sched_class = &fair_sched_class;
5229 break;
5230 case SCHED_FIFO:
5231 case SCHED_RR:
5232 p->sched_class = &rt_sched_class;
5233 break;
5234 }
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005237 p->normal_prio = normal_prio(p);
5238 /* we are holding p->pi_lock already */
5239 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005240 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
5243/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005244 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 * @p: the task in question.
5246 * @policy: new policy.
5247 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005248 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005249 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005251int sched_setscheduler(struct task_struct *p, int policy,
5252 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005254 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005256 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005257 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
Steven Rostedt66e53932006-06-27 02:54:44 -07005259 /* may grab non-irq protected spin_locks */
5260 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261recheck:
5262 /* double check policy once rq lock held */
5263 if (policy < 0)
5264 policy = oldpolicy = p->policy;
5265 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005266 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5267 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 /*
5270 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005271 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5272 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 */
5274 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005275 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005276 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005278 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 return -EINVAL;
5280
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005281 /*
5282 * Allow unprivileged RT tasks to decrease priority:
5283 */
5284 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005285 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005286 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005287
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005288 if (!lock_task_sighand(p, &flags))
5289 return -ESRCH;
5290 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5291 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005292
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005293 /* can't set/change the rt policy */
5294 if (policy != p->policy && !rlim_rtprio)
5295 return -EPERM;
5296
5297 /* can't increase priority */
5298 if (param->sched_priority > p->rt_priority &&
5299 param->sched_priority > rlim_rtprio)
5300 return -EPERM;
5301 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005302 /*
5303 * Like positive nice levels, dont allow tasks to
5304 * move out of SCHED_IDLE either:
5305 */
5306 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5307 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005308
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005309 /* can't change other user's priorities */
5310 if ((current->euid != p->euid) &&
5311 (current->euid != p->uid))
5312 return -EPERM;
5313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005315#ifdef CONFIG_RT_GROUP_SCHED
5316 /*
5317 * Do not allow realtime tasks into groups that have no runtime
5318 * assigned.
5319 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005320 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005321 return -EPERM;
5322#endif
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 retval = security_task_setscheduler(p, policy, param);
5325 if (retval)
5326 return retval;
5327 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005328 * make sure no PI-waiters arrive (or leave) while we are
5329 * changing the priority of the task:
5330 */
5331 spin_lock_irqsave(&p->pi_lock, flags);
5332 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 * To be able to change p->policy safely, the apropriate
5334 * runqueue lock must be held.
5335 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005336 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 /* recheck policy now with rq lock held */
5338 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5339 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005340 __task_rq_unlock(rq);
5341 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 goto recheck;
5343 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005344 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005345 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005346 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005347 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005348 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005349 if (running)
5350 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005353 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005354
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005355 if (running)
5356 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005357 if (on_rq) {
5358 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005359
5360 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005362 __task_rq_unlock(rq);
5363 spin_unlock_irqrestore(&p->pi_lock, flags);
5364
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005365 rt_mutex_adjust_pi(p);
5366
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 return 0;
5368}
5369EXPORT_SYMBOL_GPL(sched_setscheduler);
5370
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005371static int
5372do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 struct sched_param lparam;
5375 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005376 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 if (!param || pid < 0)
5379 return -EINVAL;
5380 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5381 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005382
5383 rcu_read_lock();
5384 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005386 if (p != NULL)
5387 retval = sched_setscheduler(p, policy, &lparam);
5388 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return retval;
5391}
5392
5393/**
5394 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5395 * @pid: the pid in question.
5396 * @policy: new policy.
5397 * @param: structure containing the new RT priority.
5398 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005399asmlinkage long
5400sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Jason Baronc21761f2006-01-18 17:43:03 -08005402 /* negative values for policy are not valid */
5403 if (policy < 0)
5404 return -EINVAL;
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return do_sched_setscheduler(pid, policy, param);
5407}
5408
5409/**
5410 * sys_sched_setparam - set/change the RT priority of a thread
5411 * @pid: the pid in question.
5412 * @param: structure containing the new RT priority.
5413 */
5414asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5415{
5416 return do_sched_setscheduler(pid, -1, param);
5417}
5418
5419/**
5420 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5421 * @pid: the pid in question.
5422 */
5423asmlinkage long sys_sched_getscheduler(pid_t pid)
5424{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005425 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005426 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
5431 retval = -ESRCH;
5432 read_lock(&tasklist_lock);
5433 p = find_process_by_pid(pid);
5434 if (p) {
5435 retval = security_task_getscheduler(p);
5436 if (!retval)
5437 retval = p->policy;
5438 }
5439 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 return retval;
5441}
5442
5443/**
5444 * sys_sched_getscheduler - get the RT priority of a thread
5445 * @pid: the pid in question.
5446 * @param: structure containing the RT priority.
5447 */
5448asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5449{
5450 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005451 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005452 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
5454 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 read_lock(&tasklist_lock);
5458 p = find_process_by_pid(pid);
5459 retval = -ESRCH;
5460 if (!p)
5461 goto out_unlock;
5462
5463 retval = security_task_getscheduler(p);
5464 if (retval)
5465 goto out_unlock;
5466
5467 lp.sched_priority = p->rt_priority;
5468 read_unlock(&tasklist_lock);
5469
5470 /*
5471 * This one might sleep, we cannot do it with a spinlock held ...
5472 */
5473 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 return retval;
5476
5477out_unlock:
5478 read_unlock(&tasklist_lock);
5479 return retval;
5480}
5481
Mike Travisb53e9212008-04-04 18:11:08 -07005482long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005485 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005486 struct task_struct *p;
5487 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005489 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 read_lock(&tasklist_lock);
5491
5492 p = find_process_by_pid(pid);
5493 if (!p) {
5494 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005495 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 return -ESRCH;
5497 }
5498
5499 /*
5500 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005501 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 * usage count and then drop tasklist_lock.
5503 */
5504 get_task_struct(p);
5505 read_unlock(&tasklist_lock);
5506
5507 retval = -EPERM;
5508 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5509 !capable(CAP_SYS_NICE))
5510 goto out_unlock;
5511
David Quigleye7834f82006-06-23 02:03:59 -07005512 retval = security_task_setscheduler(p, 0, NULL);
5513 if (retval)
5514 goto out_unlock;
5515
Mike Travisf9a86fc2008-04-04 18:11:07 -07005516 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005518 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005519 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Paul Menage8707d8b2007-10-18 23:40:22 -07005521 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005522 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005523 if (!cpus_subset(new_mask, cpus_allowed)) {
5524 /*
5525 * We must have raced with a concurrent cpuset
5526 * update. Just reset the cpus_allowed to the
5527 * cpuset's cpus_allowed
5528 */
5529 new_mask = cpus_allowed;
5530 goto again;
5531 }
5532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533out_unlock:
5534 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005535 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return retval;
5537}
5538
5539static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5540 cpumask_t *new_mask)
5541{
5542 if (len < sizeof(cpumask_t)) {
5543 memset(new_mask, 0, sizeof(cpumask_t));
5544 } else if (len > sizeof(cpumask_t)) {
5545 len = sizeof(cpumask_t);
5546 }
5547 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5548}
5549
5550/**
5551 * sys_sched_setaffinity - set the cpu affinity of a process
5552 * @pid: pid of the process
5553 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5554 * @user_mask_ptr: user-space pointer to the new cpu mask
5555 */
5556asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5557 unsigned long __user *user_mask_ptr)
5558{
5559 cpumask_t new_mask;
5560 int retval;
5561
5562 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5563 if (retval)
5564 return retval;
5565
Mike Travisb53e9212008-04-04 18:11:08 -07005566 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567}
5568
5569/*
5570 * Represents all cpu's present in the system
5571 * In systems capable of hotplug, this map could dynamically grow
5572 * as new cpu's are detected in the system via any platform specific
5573 * method, such as ACPI for e.g.
5574 */
5575
Andi Kleen4cef0c62006-01-11 22:44:57 +01005576cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577EXPORT_SYMBOL(cpu_present_map);
5578
5579#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005580cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005581EXPORT_SYMBOL(cpu_online_map);
5582
Andi Kleen4cef0c62006-01-11 22:44:57 +01005583cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005584EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585#endif
5586
5587long sched_getaffinity(pid_t pid, cpumask_t *mask)
5588{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005589 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005592 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 read_lock(&tasklist_lock);
5594
5595 retval = -ESRCH;
5596 p = find_process_by_pid(pid);
5597 if (!p)
5598 goto out_unlock;
5599
David Quigleye7834f82006-06-23 02:03:59 -07005600 retval = security_task_getscheduler(p);
5601 if (retval)
5602 goto out_unlock;
5603
Jack Steiner2f7016d2006-02-01 03:05:18 -08005604 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606out_unlock:
5607 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005608 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Ulrich Drepper9531b622007-08-09 11:16:46 +02005610 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
5613/**
5614 * sys_sched_getaffinity - get the cpu affinity of a process
5615 * @pid: pid of the process
5616 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5617 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5618 */
5619asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5620 unsigned long __user *user_mask_ptr)
5621{
5622 int ret;
5623 cpumask_t mask;
5624
5625 if (len < sizeof(cpumask_t))
5626 return -EINVAL;
5627
5628 ret = sched_getaffinity(pid, &mask);
5629 if (ret < 0)
5630 return ret;
5631
5632 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5633 return -EFAULT;
5634
5635 return sizeof(cpumask_t);
5636}
5637
5638/**
5639 * sys_sched_yield - yield the current processor to other threads.
5640 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005641 * This function yields the current CPU to other tasks. If there are no
5642 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 */
5644asmlinkage long sys_sched_yield(void)
5645{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005646 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Ingo Molnar2d723762007-10-15 17:00:12 +02005648 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005649 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
5651 /*
5652 * Since we are going to call schedule() anyway, there's
5653 * no need to preempt or enable interrupts:
5654 */
5655 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005656 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 _raw_spin_unlock(&rq->lock);
5658 preempt_enable_no_resched();
5659
5660 schedule();
5661
5662 return 0;
5663}
5664
Andrew Mortone7b38402006-06-30 01:56:00 -07005665static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005667#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5668 __might_sleep(__FILE__, __LINE__);
5669#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005670 /*
5671 * The BKS might be reacquired before we have dropped
5672 * PREEMPT_ACTIVE, which could trigger a second
5673 * cond_resched() call.
5674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 do {
5676 add_preempt_count(PREEMPT_ACTIVE);
5677 schedule();
5678 sub_preempt_count(PREEMPT_ACTIVE);
5679 } while (need_resched());
5680}
5681
Herbert Xu02b67cc32008-01-25 21:08:28 +01005682#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5683int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684{
Ingo Molnar94142322006-12-29 16:48:13 -08005685 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5686 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 __cond_resched();
5688 return 1;
5689 }
5690 return 0;
5691}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005692EXPORT_SYMBOL(_cond_resched);
5693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
5695/*
5696 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5697 * call schedule, and on return reacquire the lock.
5698 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005699 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 * operations here to prevent schedule() from being called twice (once via
5701 * spin_unlock(), once by hand).
5702 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005703int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
Nick Piggin95c354f2008-01-30 13:31:20 +01005705 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005706 int ret = 0;
5707
Nick Piggin95c354f2008-01-30 13:31:20 +01005708 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005710 if (resched && need_resched())
5711 __cond_resched();
5712 else
5713 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005714 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005717 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719EXPORT_SYMBOL(cond_resched_lock);
5720
5721int __sched cond_resched_softirq(void)
5722{
5723 BUG_ON(!in_softirq());
5724
Ingo Molnar94142322006-12-29 16:48:13 -08005725 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005726 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 __cond_resched();
5728 local_bh_disable();
5729 return 1;
5730 }
5731 return 0;
5732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733EXPORT_SYMBOL(cond_resched_softirq);
5734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735/**
5736 * yield - yield the current processor to other threads.
5737 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005738 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 * thread runnable and calls sys_sched_yield().
5740 */
5741void __sched yield(void)
5742{
5743 set_current_state(TASK_RUNNING);
5744 sys_sched_yield();
5745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746EXPORT_SYMBOL(yield);
5747
5748/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005749 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 * that process accounting knows that this is a task in IO wait state.
5751 *
5752 * But don't do that if it is a deliberate, throttling IO wait (this task
5753 * has set its backing_dev_info: the queue against which it should throttle)
5754 */
5755void __sched io_schedule(void)
5756{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005757 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005759 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 atomic_inc(&rq->nr_iowait);
5761 schedule();
5762 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005763 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765EXPORT_SYMBOL(io_schedule);
5766
5767long __sched io_schedule_timeout(long timeout)
5768{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005769 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 long ret;
5771
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005772 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 atomic_inc(&rq->nr_iowait);
5774 ret = schedule_timeout(timeout);
5775 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005776 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 return ret;
5778}
5779
5780/**
5781 * sys_sched_get_priority_max - return maximum RT priority.
5782 * @policy: scheduling class.
5783 *
5784 * this syscall returns the maximum rt_priority that can be used
5785 * by a given scheduling class.
5786 */
5787asmlinkage long sys_sched_get_priority_max(int policy)
5788{
5789 int ret = -EINVAL;
5790
5791 switch (policy) {
5792 case SCHED_FIFO:
5793 case SCHED_RR:
5794 ret = MAX_USER_RT_PRIO-1;
5795 break;
5796 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005797 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005798 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 ret = 0;
5800 break;
5801 }
5802 return ret;
5803}
5804
5805/**
5806 * sys_sched_get_priority_min - return minimum RT priority.
5807 * @policy: scheduling class.
5808 *
5809 * this syscall returns the minimum rt_priority that can be used
5810 * by a given scheduling class.
5811 */
5812asmlinkage long sys_sched_get_priority_min(int policy)
5813{
5814 int ret = -EINVAL;
5815
5816 switch (policy) {
5817 case SCHED_FIFO:
5818 case SCHED_RR:
5819 ret = 1;
5820 break;
5821 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005822 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005823 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 ret = 0;
5825 }
5826 return ret;
5827}
5828
5829/**
5830 * sys_sched_rr_get_interval - return the default timeslice of a process.
5831 * @pid: pid of the process.
5832 * @interval: userspace pointer to the timeslice value.
5833 *
5834 * this syscall writes the default timeslice value of a given process
5835 * into the user-space timespec buffer. A value of '0' means infinity.
5836 */
5837asmlinkage
5838long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5839{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005840 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005841 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005842 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
5848 retval = -ESRCH;
5849 read_lock(&tasklist_lock);
5850 p = find_process_by_pid(pid);
5851 if (!p)
5852 goto out_unlock;
5853
5854 retval = security_task_getscheduler(p);
5855 if (retval)
5856 goto out_unlock;
5857
Ingo Molnar77034932007-12-04 17:04:39 +01005858 /*
5859 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5860 * tasks that are on an otherwise idle runqueue:
5861 */
5862 time_slice = 0;
5863 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005864 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005865 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005866 struct sched_entity *se = &p->se;
5867 unsigned long flags;
5868 struct rq *rq;
5869
5870 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005871 if (rq->cfs.load.weight)
5872 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005873 task_rq_unlock(rq, &flags);
5874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005876 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880out_unlock:
5881 read_unlock(&tasklist_lock);
5882 return retval;
5883}
5884
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005885static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005886
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005887void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005890 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005893 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005894 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005895#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005897 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005899 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900#else
5901 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005902 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005904 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905#endif
5906#ifdef CONFIG_DEBUG_STACK_USAGE
5907 {
Al Viro10ebffd2005-11-13 16:06:56 -08005908 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 while (!*n)
5910 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005911 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 }
5913#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005914 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005915 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005917 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918}
5919
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005920void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005922 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
Ingo Molnar4bd77322007-07-11 21:21:47 +02005924#if BITS_PER_LONG == 32
5925 printk(KERN_INFO
5926 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005928 printk(KERN_INFO
5929 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930#endif
5931 read_lock(&tasklist_lock);
5932 do_each_thread(g, p) {
5933 /*
5934 * reset the NMI-timeout, listing all files on a slow
5935 * console might take alot of time:
5936 */
5937 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005938 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005939 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 } while_each_thread(g, p);
5941
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005942 touch_all_softlockup_watchdogs();
5943
Ingo Molnardd41f592007-07-09 18:51:59 +02005944#ifdef CONFIG_SCHED_DEBUG
5945 sysrq_sched_debug_show();
5946#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005948 /*
5949 * Only show locks if all tasks are dumped:
5950 */
5951 if (state_filter == -1)
5952 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
5954
Ingo Molnar1df21052007-07-09 18:51:58 +02005955void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5956{
Ingo Molnardd41f592007-07-09 18:51:59 +02005957 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005958}
5959
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005960/**
5961 * init_idle - set up an idle thread for a given CPU
5962 * @idle: task in question
5963 * @cpu: cpu the idle task belongs to
5964 *
5965 * NOTE: this function does not set the idle thread's NEED_RESCHED
5966 * flag, to make booting more robust.
5967 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005968void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005970 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 unsigned long flags;
5972
Ingo Molnardd41f592007-07-09 18:51:59 +02005973 __sched_fork(idle);
5974 idle->se.exec_start = sched_clock();
5975
Ingo Molnarb29739f2006-06-27 02:54:51 -07005976 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005978 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 spin_lock_irqsave(&rq->lock, flags);
5981 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005982#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5983 idle->oncpu = 1;
5984#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 spin_unlock_irqrestore(&rq->lock, flags);
5986
5987 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005988 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005989
Ingo Molnardd41f592007-07-09 18:51:59 +02005990 /*
5991 * The idle tasks have their own, simple scheduling class:
5992 */
5993 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994}
5995
5996/*
5997 * In a system that switches off the HZ timer nohz_cpu_mask
5998 * indicates which cpus entered this state. This is used
5999 * in the rcu update to wait only for active cpus. For system
6000 * which do not switch off the HZ timer nohz_cpu_mask should
6001 * always be CPU_MASK_NONE.
6002 */
6003cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
6004
Ingo Molnar19978ca2007-11-09 22:39:38 +01006005/*
6006 * Increase the granularity value when there are more CPUs,
6007 * because with more CPUs the 'effective latency' as visible
6008 * to users decreases. But the relationship is not linear,
6009 * so pick a second-best guess by going with the log2 of the
6010 * number of CPUs.
6011 *
6012 * This idea comes from the SD scheduler of Con Kolivas:
6013 */
6014static inline void sched_init_granularity(void)
6015{
6016 unsigned int factor = 1 + ilog2(num_online_cpus());
6017 const unsigned long limit = 200000000;
6018
6019 sysctl_sched_min_granularity *= factor;
6020 if (sysctl_sched_min_granularity > limit)
6021 sysctl_sched_min_granularity = limit;
6022
6023 sysctl_sched_latency *= factor;
6024 if (sysctl_sched_latency > limit)
6025 sysctl_sched_latency = limit;
6026
6027 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006028}
6029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030#ifdef CONFIG_SMP
6031/*
6032 * This is how migration works:
6033 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006034 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 * runqueue and wake up that CPU's migration thread.
6036 * 2) we down() the locked semaphore => thread blocks.
6037 * 3) migration thread wakes up (implicitly it forces the migrated
6038 * thread off the CPU)
6039 * 4) it gets the migration request and checks whether the migrated
6040 * task is still in the wrong runqueue.
6041 * 5) if it's in the wrong runqueue then the migration thread removes
6042 * it and puts it into the right queue.
6043 * 6) migration thread up()s the semaphore.
6044 * 7) we wake up and the migration is done.
6045 */
6046
6047/*
6048 * Change a given task's CPU affinity. Migrate the thread to a
6049 * proper CPU and schedule it away if the CPU it's executing on
6050 * is removed from the allowed bitmask.
6051 *
6052 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006053 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 * call is not atomic; no spinlocks may be held.
6055 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006056int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006058 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006060 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006061 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
6063 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006064 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 ret = -EINVAL;
6066 goto out;
6067 }
6068
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006069 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006070 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006071 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006072 p->cpus_allowed = *new_mask;
6073 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006074 }
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006077 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 goto out;
6079
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006080 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 /* Need help from migration thread: drop lock and wait. */
6082 task_rq_unlock(rq, &flags);
6083 wake_up_process(rq->migration_thread);
6084 wait_for_completion(&req.done);
6085 tlb_migrate_finish(p->mm);
6086 return 0;
6087 }
6088out:
6089 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006090
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return ret;
6092}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006093EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
6095/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006096 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 * this because either it can't run here any more (set_cpus_allowed()
6098 * away from this CPU, or CPU going down), or because we're
6099 * attempting to rebalance this task on exec (sched_exec).
6100 *
6101 * So we race with normal scheduler movements, but that's OK, as long
6102 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006103 *
6104 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006106static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006108 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006109 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 rq_src = cpu_rq(src_cpu);
6115 rq_dest = cpu_rq(dest_cpu);
6116
6117 double_rq_lock(rq_src, rq_dest);
6118 /* Already moved. */
6119 if (task_cpu(p) != src_cpu)
6120 goto out;
6121 /* Affinity changed (again). */
6122 if (!cpu_isset(dest_cpu, p->cpus_allowed))
6123 goto out;
6124
Ingo Molnardd41f592007-07-09 18:51:59 +02006125 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006126 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006127 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006128
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006130 if (on_rq) {
6131 activate_task(rq_dest, p, 0);
6132 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07006134 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135out:
6136 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006137 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138}
6139
6140/*
6141 * migration_thread - this is a highprio system thread that performs
6142 * thread migration by bumping thread off CPU then 'pushing' onto
6143 * another runqueue.
6144 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006145static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006148 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
6150 rq = cpu_rq(cpu);
6151 BUG_ON(rq->migration_thread != current);
6152
6153 set_current_state(TASK_INTERRUPTIBLE);
6154 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006155 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 spin_lock_irq(&rq->lock);
6159
6160 if (cpu_is_offline(cpu)) {
6161 spin_unlock_irq(&rq->lock);
6162 goto wait_to_die;
6163 }
6164
6165 if (rq->active_balance) {
6166 active_load_balance(rq, cpu);
6167 rq->active_balance = 0;
6168 }
6169
6170 head = &rq->migration_queue;
6171
6172 if (list_empty(head)) {
6173 spin_unlock_irq(&rq->lock);
6174 schedule();
6175 set_current_state(TASK_INTERRUPTIBLE);
6176 continue;
6177 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006178 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 list_del_init(head->next);
6180
Nick Piggin674311d2005-06-25 14:57:27 -07006181 spin_unlock(&rq->lock);
6182 __migrate_task(req->task, cpu, req->dest_cpu);
6183 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 complete(&req->done);
6186 }
6187 __set_current_state(TASK_RUNNING);
6188 return 0;
6189
6190wait_to_die:
6191 /* Wait for kthread_stop */
6192 set_current_state(TASK_INTERRUPTIBLE);
6193 while (!kthread_should_stop()) {
6194 schedule();
6195 set_current_state(TASK_INTERRUPTIBLE);
6196 }
6197 __set_current_state(TASK_RUNNING);
6198 return 0;
6199}
6200
6201#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006202
6203static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6204{
6205 int ret;
6206
6207 local_irq_disable();
6208 ret = __migrate_task(p, src_cpu, dest_cpu);
6209 local_irq_enable();
6210 return ret;
6211}
6212
Kirill Korotaev054b9102006-12-10 02:20:11 -08006213/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006214 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006215 * NOTE: interrupts should be disabled by the caller
6216 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006217static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006219 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006221 struct rq *rq;
6222 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Andi Kleen3a5c3592007-10-15 17:00:14 +02006224 do {
6225 /* On same node? */
6226 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6227 cpus_and(mask, mask, p->cpus_allowed);
6228 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Andi Kleen3a5c3592007-10-15 17:00:14 +02006230 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006231 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006232 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Andi Kleen3a5c3592007-10-15 17:00:14 +02006234 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006235 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006236 cpumask_t cpus_allowed;
6237
6238 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006239 /*
6240 * Try to stay on the same cpuset, where the
6241 * current cpuset may be a subset of all cpus.
6242 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006243 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006244 * called within calls to cpuset_lock/cpuset_unlock.
6245 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006246 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006247 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006248 dest_cpu = any_online_cpu(p->cpus_allowed);
6249 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Andi Kleen3a5c3592007-10-15 17:00:14 +02006251 /*
6252 * Don't tell them about moving exiting tasks or
6253 * kernel threads (both mm NULL), since they never
6254 * leave kernel.
6255 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006256 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006257 printk(KERN_INFO "process %d (%s) no "
6258 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006259 task_pid_nr(p), p->comm, dead_cpu);
6260 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006261 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006262 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263}
6264
6265/*
6266 * While a dead CPU has no uninterruptible tasks queued at this point,
6267 * it might still have a nonzero ->nr_uninterruptible counter, because
6268 * for performance reasons the counter is not stricly tracking tasks to
6269 * their home CPUs. So we just add the counter to another CPU's counter,
6270 * to keep the global sum constant after CPU-down:
6271 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006272static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273{
Mike Travis7c16ec52008-04-04 18:11:11 -07006274 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 unsigned long flags;
6276
6277 local_irq_save(flags);
6278 double_rq_lock(rq_src, rq_dest);
6279 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6280 rq_src->nr_uninterruptible = 0;
6281 double_rq_unlock(rq_src, rq_dest);
6282 local_irq_restore(flags);
6283}
6284
6285/* Run through task list and migrate tasks from the dead cpu. */
6286static void migrate_live_tasks(int src_cpu)
6287{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006288 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006290 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Ingo Molnar48f24c42006-07-03 00:25:40 -07006292 do_each_thread(t, p) {
6293 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 continue;
6295
Ingo Molnar48f24c42006-07-03 00:25:40 -07006296 if (task_cpu(p) == src_cpu)
6297 move_task_off_dead_cpu(src_cpu, p);
6298 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006300 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
Ingo Molnardd41f592007-07-09 18:51:59 +02006303/*
6304 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006305 * It does so by boosting its priority to highest possible.
6306 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 */
6308void sched_idle_next(void)
6309{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006310 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006311 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 struct task_struct *p = rq->idle;
6313 unsigned long flags;
6314
6315 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006316 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Ingo Molnar48f24c42006-07-03 00:25:40 -07006318 /*
6319 * Strictly not necessary since rest of the CPUs are stopped by now
6320 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 */
6322 spin_lock_irqsave(&rq->lock, flags);
6323
Ingo Molnardd41f592007-07-09 18:51:59 +02006324 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006325
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006326 update_rq_clock(rq);
6327 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
6329 spin_unlock_irqrestore(&rq->lock, flags);
6330}
6331
Ingo Molnar48f24c42006-07-03 00:25:40 -07006332/*
6333 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 * offline.
6335 */
6336void idle_task_exit(void)
6337{
6338 struct mm_struct *mm = current->active_mm;
6339
6340 BUG_ON(cpu_online(smp_processor_id()));
6341
6342 if (mm != &init_mm)
6343 switch_mm(mm, &init_mm, current);
6344 mmdrop(mm);
6345}
6346
Kirill Korotaev054b9102006-12-10 02:20:11 -08006347/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006348static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006350 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
6352 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006353 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
6355 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006356 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Ingo Molnar48f24c42006-07-03 00:25:40 -07006358 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360 /*
6361 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006362 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 * fine.
6364 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006365 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006366 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006367 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Ingo Molnar48f24c42006-07-03 00:25:40 -07006369 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370}
6371
6372/* release_task() removes task from tasklist, so we won't find dead tasks. */
6373static void migrate_dead_tasks(unsigned int dead_cpu)
6374{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006375 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006376 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Ingo Molnardd41f592007-07-09 18:51:59 +02006378 for ( ; ; ) {
6379 if (!rq->nr_running)
6380 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006381 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006382 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006383 if (!next)
6384 break;
6385 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 }
6388}
6389#endif /* CONFIG_HOTPLUG_CPU */
6390
Nick Piggine692ab52007-07-26 13:40:43 +02006391#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6392
6393static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006394 {
6395 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006396 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006397 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006398 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006399};
6400
6401static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006402 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006403 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006404 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006405 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006406 .child = sd_ctl_dir,
6407 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006408 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006409};
6410
6411static struct ctl_table *sd_alloc_ctl_entry(int n)
6412{
6413 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006414 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006415
Nick Piggine692ab52007-07-26 13:40:43 +02006416 return entry;
6417}
6418
Milton Miller6382bc92007-10-15 17:00:19 +02006419static void sd_free_ctl_entry(struct ctl_table **tablep)
6420{
Milton Millercd7900762007-10-17 16:55:11 +02006421 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006422
Milton Millercd7900762007-10-17 16:55:11 +02006423 /*
6424 * In the intermediate directories, both the child directory and
6425 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006426 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006427 * static strings and all have proc handlers.
6428 */
6429 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006430 if (entry->child)
6431 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006432 if (entry->proc_handler == NULL)
6433 kfree(entry->procname);
6434 }
Milton Miller6382bc92007-10-15 17:00:19 +02006435
6436 kfree(*tablep);
6437 *tablep = NULL;
6438}
6439
Nick Piggine692ab52007-07-26 13:40:43 +02006440static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006441set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006442 const char *procname, void *data, int maxlen,
6443 mode_t mode, proc_handler *proc_handler)
6444{
Nick Piggine692ab52007-07-26 13:40:43 +02006445 entry->procname = procname;
6446 entry->data = data;
6447 entry->maxlen = maxlen;
6448 entry->mode = mode;
6449 entry->proc_handler = proc_handler;
6450}
6451
6452static struct ctl_table *
6453sd_alloc_ctl_domain_table(struct sched_domain *sd)
6454{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006455 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006456
Milton Millerad1cdc12007-10-15 17:00:19 +02006457 if (table == NULL)
6458 return NULL;
6459
Alexey Dobriyane0361852007-08-09 11:16:46 +02006460 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006461 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006462 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006463 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006464 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006465 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006466 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006467 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006468 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006469 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006470 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006471 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006472 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006473 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006474 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006475 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006476 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006477 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006478 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006479 &sd->cache_nice_tries,
6480 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006481 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006482 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006483 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006484
6485 return table;
6486}
6487
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006488static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006489{
6490 struct ctl_table *entry, *table;
6491 struct sched_domain *sd;
6492 int domain_num = 0, i;
6493 char buf[32];
6494
6495 for_each_domain(cpu, sd)
6496 domain_num++;
6497 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006498 if (table == NULL)
6499 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006500
6501 i = 0;
6502 for_each_domain(cpu, sd) {
6503 snprintf(buf, 32, "domain%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_domain_table(sd);
6507 entry++;
6508 i++;
6509 }
6510 return table;
6511}
6512
6513static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006514static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006515{
6516 int i, cpu_num = num_online_cpus();
6517 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6518 char buf[32];
6519
Milton Miller73785472007-10-24 18:23:48 +02006520 WARN_ON(sd_ctl_dir[0].child);
6521 sd_ctl_dir[0].child = entry;
6522
Milton Millerad1cdc12007-10-15 17:00:19 +02006523 if (entry == NULL)
6524 return;
6525
Milton Miller97b6ea72007-10-15 17:00:19 +02006526 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006527 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006528 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006529 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006530 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006531 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006532 }
Milton Miller73785472007-10-24 18:23:48 +02006533
6534 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006535 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6536}
Milton Miller6382bc92007-10-15 17:00:19 +02006537
Milton Miller73785472007-10-24 18:23:48 +02006538/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006539static void unregister_sched_domain_sysctl(void)
6540{
Milton Miller73785472007-10-24 18:23:48 +02006541 if (sd_sysctl_header)
6542 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006543 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006544 if (sd_ctl_dir[0].child)
6545 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006546}
Nick Piggine692ab52007-07-26 13:40:43 +02006547#else
Milton Miller6382bc92007-10-15 17:00:19 +02006548static void register_sched_domain_sysctl(void)
6549{
6550}
6551static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006552{
6553}
6554#endif
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556/*
6557 * migration_call - callback that gets triggered when a CPU is added.
6558 * Here we can start up the necessary migration thread for the new CPU.
6559 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006560static int __cpuinit
6561migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006564 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006566 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
6568 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006569
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006571 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006572 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 if (IS_ERR(p))
6574 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 kthread_bind(p, cpu);
6576 /* Must be high prio: stop_machine expects to yield to it. */
6577 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006578 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 task_rq_unlock(rq, &flags);
6580 cpu_rq(cpu)->migration_thread = p;
6581 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006582
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006584 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006585 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006587
6588 /* Update our root-domain */
6589 rq = cpu_rq(cpu);
6590 spin_lock_irqsave(&rq->lock, flags);
6591 if (rq->rd) {
6592 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6593 cpu_set(cpu, rq->rd->online);
6594 }
6595 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598#ifdef CONFIG_HOTPLUG_CPU
6599 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006600 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006601 if (!cpu_rq(cpu)->migration_thread)
6602 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006603 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006604 kthread_bind(cpu_rq(cpu)->migration_thread,
6605 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 kthread_stop(cpu_rq(cpu)->migration_thread);
6607 cpu_rq(cpu)->migration_thread = NULL;
6608 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006611 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006612 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 migrate_live_tasks(cpu);
6614 rq = cpu_rq(cpu);
6615 kthread_stop(rq->migration_thread);
6616 rq->migration_thread = NULL;
6617 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006618 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006619 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006620 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006622 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6623 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006625 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006626 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 migrate_nr_uninterruptible(rq);
6628 BUG_ON(rq->nr_running != 0);
6629
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006630 /*
6631 * No need to migrate the tasks: it was best-effort if
6632 * they didn't take sched_hotcpu_mutex. Just wake up
6633 * the requestors.
6634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 spin_lock_irq(&rq->lock);
6636 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006637 struct migration_req *req;
6638
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006640 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 list_del_init(&req->list);
6642 complete(&req->done);
6643 }
6644 spin_unlock_irq(&rq->lock);
6645 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006646
Gregory Haskins08f503b2008-03-10 17:59:11 -04006647 case CPU_DYING:
6648 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006649 /* Update our root-domain */
6650 rq = cpu_rq(cpu);
6651 spin_lock_irqsave(&rq->lock, flags);
6652 if (rq->rd) {
6653 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6654 cpu_clear(cpu, rq->rd->online);
6655 }
6656 spin_unlock_irqrestore(&rq->lock, flags);
6657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658#endif
6659 }
6660 return NOTIFY_OK;
6661}
6662
6663/* Register at highest priority so that task migration (migrate_all_tasks)
6664 * happens before everything else.
6665 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006666static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 .notifier_call = migration_call,
6668 .priority = 10
6669};
6670
Adrian Bunke6fe6642007-11-09 22:39:39 +01006671void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672{
6673 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006674 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006675
6676 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006677 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6678 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6680 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681}
6682#endif
6683
6684#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006685
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006686#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006687
Mike Travis7c16ec52008-04-04 18:11:11 -07006688static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6689 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006690{
6691 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006692 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006693
Mike Travis434d53b2008-04-04 18:11:04 -07006694 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006695 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006696
6697 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6698
6699 if (!(sd->flags & SD_LOAD_BALANCE)) {
6700 printk("does not load-balance\n");
6701 if (sd->parent)
6702 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6703 " has parent");
6704 return -1;
6705 }
6706
6707 printk(KERN_CONT "span %s\n", str);
6708
6709 if (!cpu_isset(cpu, sd->span)) {
6710 printk(KERN_ERR "ERROR: domain->span does not contain "
6711 "CPU%d\n", cpu);
6712 }
6713 if (!cpu_isset(cpu, group->cpumask)) {
6714 printk(KERN_ERR "ERROR: domain->groups does not contain"
6715 " CPU%d\n", cpu);
6716 }
6717
6718 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6719 do {
6720 if (!group) {
6721 printk("\n");
6722 printk(KERN_ERR "ERROR: group is NULL\n");
6723 break;
6724 }
6725
6726 if (!group->__cpu_power) {
6727 printk(KERN_CONT "\n");
6728 printk(KERN_ERR "ERROR: domain->cpu_power not "
6729 "set\n");
6730 break;
6731 }
6732
6733 if (!cpus_weight(group->cpumask)) {
6734 printk(KERN_CONT "\n");
6735 printk(KERN_ERR "ERROR: empty group\n");
6736 break;
6737 }
6738
Mike Travis7c16ec52008-04-04 18:11:11 -07006739 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006740 printk(KERN_CONT "\n");
6741 printk(KERN_ERR "ERROR: repeated CPUs\n");
6742 break;
6743 }
6744
Mike Travis7c16ec52008-04-04 18:11:11 -07006745 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006746
Mike Travis434d53b2008-04-04 18:11:04 -07006747 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006748 printk(KERN_CONT " %s", str);
6749
6750 group = group->next;
6751 } while (group != sd->groups);
6752 printk(KERN_CONT "\n");
6753
Mike Travis7c16ec52008-04-04 18:11:11 -07006754 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006755 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6756
Mike Travis7c16ec52008-04-04 18:11:11 -07006757 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006758 printk(KERN_ERR "ERROR: parent span is not a superset "
6759 "of domain->span\n");
6760 return 0;
6761}
6762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763static void sched_domain_debug(struct sched_domain *sd, int cpu)
6764{
Mike Travis7c16ec52008-04-04 18:11:11 -07006765 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 int level = 0;
6767
Nick Piggin41c7ce92005-06-25 14:57:24 -07006768 if (!sd) {
6769 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6770 return;
6771 }
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6774
Mike Travis7c16ec52008-04-04 18:11:11 -07006775 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6776 if (!groupmask) {
6777 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6778 return;
6779 }
6780
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006781 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006782 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 level++;
6785 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006786 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006787 break;
6788 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006789 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790}
6791#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006792# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793#endif
6794
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006795static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006796{
6797 if (cpus_weight(sd->span) == 1)
6798 return 1;
6799
6800 /* Following flags need at least 2 groups */
6801 if (sd->flags & (SD_LOAD_BALANCE |
6802 SD_BALANCE_NEWIDLE |
6803 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006804 SD_BALANCE_EXEC |
6805 SD_SHARE_CPUPOWER |
6806 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006807 if (sd->groups != sd->groups->next)
6808 return 0;
6809 }
6810
6811 /* Following flags don't use groups */
6812 if (sd->flags & (SD_WAKE_IDLE |
6813 SD_WAKE_AFFINE |
6814 SD_WAKE_BALANCE))
6815 return 0;
6816
6817 return 1;
6818}
6819
Ingo Molnar48f24c42006-07-03 00:25:40 -07006820static int
6821sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006822{
6823 unsigned long cflags = sd->flags, pflags = parent->flags;
6824
6825 if (sd_degenerate(parent))
6826 return 1;
6827
6828 if (!cpus_equal(sd->span, parent->span))
6829 return 0;
6830
6831 /* Does parent contain flags not in child? */
6832 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6833 if (cflags & SD_WAKE_AFFINE)
6834 pflags &= ~SD_WAKE_BALANCE;
6835 /* Flags needing groups don't count if only 1 group in parent */
6836 if (parent->groups == parent->groups->next) {
6837 pflags &= ~(SD_LOAD_BALANCE |
6838 SD_BALANCE_NEWIDLE |
6839 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006840 SD_BALANCE_EXEC |
6841 SD_SHARE_CPUPOWER |
6842 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006843 }
6844 if (~cflags & pflags)
6845 return 0;
6846
6847 return 1;
6848}
6849
Gregory Haskins57d885f2008-01-25 21:08:18 +01006850static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6851{
6852 unsigned long flags;
6853 const struct sched_class *class;
6854
6855 spin_lock_irqsave(&rq->lock, flags);
6856
6857 if (rq->rd) {
6858 struct root_domain *old_rd = rq->rd;
6859
Ingo Molnar0eab9142008-01-25 21:08:19 +01006860 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006861 if (class->leave_domain)
6862 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006863 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006864
Gregory Haskinsdc938522008-01-25 21:08:26 +01006865 cpu_clear(rq->cpu, old_rd->span);
6866 cpu_clear(rq->cpu, old_rd->online);
6867
Gregory Haskins57d885f2008-01-25 21:08:18 +01006868 if (atomic_dec_and_test(&old_rd->refcount))
6869 kfree(old_rd);
6870 }
6871
6872 atomic_inc(&rd->refcount);
6873 rq->rd = rd;
6874
Gregory Haskinsdc938522008-01-25 21:08:26 +01006875 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006876 if (cpu_isset(rq->cpu, cpu_online_map))
6877 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006878
Ingo Molnar0eab9142008-01-25 21:08:19 +01006879 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006880 if (class->join_domain)
6881 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006882 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006883
6884 spin_unlock_irqrestore(&rq->lock, flags);
6885}
6886
Gregory Haskinsdc938522008-01-25 21:08:26 +01006887static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006888{
6889 memset(rd, 0, sizeof(*rd));
6890
Gregory Haskinsdc938522008-01-25 21:08:26 +01006891 cpus_clear(rd->span);
6892 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006893}
6894
6895static void init_defrootdomain(void)
6896{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006897 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006898 atomic_set(&def_root_domain.refcount, 1);
6899}
6900
Gregory Haskinsdc938522008-01-25 21:08:26 +01006901static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006902{
6903 struct root_domain *rd;
6904
6905 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6906 if (!rd)
6907 return NULL;
6908
Gregory Haskinsdc938522008-01-25 21:08:26 +01006909 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006910
6911 return rd;
6912}
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006915 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 * hold the hotplug lock.
6917 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006918static void
6919cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006921 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006922 struct sched_domain *tmp;
6923
6924 /* Remove the sched domains which do not contribute to scheduling. */
6925 for (tmp = sd; tmp; tmp = tmp->parent) {
6926 struct sched_domain *parent = tmp->parent;
6927 if (!parent)
6928 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006929 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006930 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006931 if (parent->parent)
6932 parent->parent->child = tmp;
6933 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006934 }
6935
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006936 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006937 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006938 if (sd)
6939 sd->child = NULL;
6940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 sched_domain_debug(sd, cpu);
6943
Gregory Haskins57d885f2008-01-25 21:08:18 +01006944 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006945 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
6948/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006949static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
6951/* Setup the mask of cpus configured for isolated domains */
6952static int __init isolated_cpu_setup(char *str)
6953{
6954 int ints[NR_CPUS], i;
6955
6956 str = get_options(str, ARRAY_SIZE(ints), ints);
6957 cpus_clear(cpu_isolated_map);
6958 for (i = 1; i <= ints[0]; i++)
6959 if (ints[i] < NR_CPUS)
6960 cpu_set(ints[i], cpu_isolated_map);
6961 return 1;
6962}
6963
Ingo Molnar8927f492007-10-15 17:00:13 +02006964__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006967 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6968 * to a function which identifies what group(along with sched group) a CPU
6969 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6970 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 *
6972 * init_sched_build_groups will build a circular linked list of the groups
6973 * covered by the given span, and will set each group's ->cpumask correctly,
6974 * and ->cpu_power to 0.
6975 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006976static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006977init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006978 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006979 struct sched_group **sg,
6980 cpumask_t *tmpmask),
6981 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982{
6983 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 int i;
6985
Mike Travis7c16ec52008-04-04 18:11:11 -07006986 cpus_clear(*covered);
6987
6988 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006989 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006990 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 int j;
6992
Mike Travis7c16ec52008-04-04 18:11:11 -07006993 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 continue;
6995
Mike Travis7c16ec52008-04-04 18:11:11 -07006996 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006997 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Mike Travis7c16ec52008-04-04 18:11:11 -07006999 for_each_cpu_mask(j, *span) {
7000 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 continue;
7002
Mike Travis7c16ec52008-04-04 18:11:11 -07007003 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 cpu_set(j, sg->cpumask);
7005 }
7006 if (!first)
7007 first = sg;
7008 if (last)
7009 last->next = sg;
7010 last = sg;
7011 }
7012 last->next = first;
7013}
7014
John Hawkes9c1cfda2005-09-06 15:18:14 -07007015#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
John Hawkes9c1cfda2005-09-06 15:18:14 -07007017#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007018
John Hawkes9c1cfda2005-09-06 15:18:14 -07007019/**
7020 * find_next_best_node - find the next node to include in a sched_domain
7021 * @node: node whose sched_domain we're building
7022 * @used_nodes: nodes already in the sched_domain
7023 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007024 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007025 * finds the closest node not already in the @used_nodes map.
7026 *
7027 * Should use nodemask_t.
7028 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007029static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007030{
7031 int i, n, val, min_val, best_node = 0;
7032
7033 min_val = INT_MAX;
7034
7035 for (i = 0; i < MAX_NUMNODES; i++) {
7036 /* Start at @node */
7037 n = (node + i) % MAX_NUMNODES;
7038
7039 if (!nr_cpus_node(n))
7040 continue;
7041
7042 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007043 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007044 continue;
7045
7046 /* Simple min distance search */
7047 val = node_distance(node, n);
7048
7049 if (val < min_val) {
7050 min_val = val;
7051 best_node = n;
7052 }
7053 }
7054
Mike Travisc5f59f02008-04-04 18:11:10 -07007055 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007056 return best_node;
7057}
7058
7059/**
7060 * sched_domain_node_span - get a cpumask for a node's sched_domain
7061 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007062 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007063 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007064 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007065 * should be one that prevents unnecessary balancing, but also spreads tasks
7066 * out optimally.
7067 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007068static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007069{
Mike Travisc5f59f02008-04-04 18:11:10 -07007070 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007071 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007072 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007073
Mike Travis4bdbaad32008-04-15 16:35:52 -07007074 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007075 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007076
Mike Travis4bdbaad32008-04-15 16:35:52 -07007077 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007078 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007079
7080 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007081 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007082
Mike Travisc5f59f02008-04-04 18:11:10 -07007083 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007084 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007085 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007086}
7087#endif
7088
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007089int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007090
John Hawkes9c1cfda2005-09-06 15:18:14 -07007091/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007092 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094#ifdef CONFIG_SCHED_SMT
7095static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007096static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007097
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007098static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007099cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7100 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007102 if (sg)
7103 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 return cpu;
7105}
7106#endif
7107
Ingo Molnar48f24c42006-07-03 00:25:40 -07007108/*
7109 * multi-core sched-domains:
7110 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007111#ifdef CONFIG_SCHED_MC
7112static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007113static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007114#endif
7115
7116#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007117static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007118cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7119 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007120{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007121 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007122
7123 *mask = per_cpu(cpu_sibling_map, cpu);
7124 cpus_and(*mask, *mask, *cpu_map);
7125 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007126 if (sg)
7127 *sg = &per_cpu(sched_group_core, group);
7128 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007129}
7130#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007131static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007132cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7133 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007134{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007135 if (sg)
7136 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007137 return cpu;
7138}
7139#endif
7140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007142static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007143
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007144static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007145cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7146 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007148 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007149#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007150 *mask = cpu_coregroup_map(cpu);
7151 cpus_and(*mask, *mask, *cpu_map);
7152 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007153#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007154 *mask = per_cpu(cpu_sibling_map, cpu);
7155 cpus_and(*mask, *mask, *cpu_map);
7156 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007158 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007160 if (sg)
7161 *sg = &per_cpu(sched_group_phys, group);
7162 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163}
7164
7165#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007166/*
7167 * The init_sched_build_groups can't handle what we want to do with node
7168 * groups, so roll our own. Now each node has its own list of groups which
7169 * gets dynamically allocated.
7170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007172static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007173
7174static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007175static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007176
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007177static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007178 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007180 int group;
7181
Mike Travis7c16ec52008-04-04 18:11:11 -07007182 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7183 cpus_and(*nodemask, *nodemask, *cpu_map);
7184 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007185
7186 if (sg)
7187 *sg = &per_cpu(sched_group_allnodes, group);
7188 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007190
Siddha, Suresh B08069032006-03-27 01:15:23 -08007191static void init_numa_sched_groups_power(struct sched_group *group_head)
7192{
7193 struct sched_group *sg = group_head;
7194 int j;
7195
7196 if (!sg)
7197 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007198 do {
7199 for_each_cpu_mask(j, sg->cpumask) {
7200 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007201
Andi Kleen3a5c3592007-10-15 17:00:14 +02007202 sd = &per_cpu(phys_domains, j);
7203 if (j != first_cpu(sd->groups->cpumask)) {
7204 /*
7205 * Only add "power" once for each
7206 * physical package.
7207 */
7208 continue;
7209 }
7210
7211 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007212 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007213 sg = sg->next;
7214 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216#endif
7217
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007218#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007219/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007220static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007221{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007222 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007223
7224 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007225 struct sched_group **sched_group_nodes
7226 = sched_group_nodes_bycpu[cpu];
7227
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007228 if (!sched_group_nodes)
7229 continue;
7230
7231 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007232 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7233
Mike Travis7c16ec52008-04-04 18:11:11 -07007234 *nodemask = node_to_cpumask(i);
7235 cpus_and(*nodemask, *nodemask, *cpu_map);
7236 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007237 continue;
7238
7239 if (sg == NULL)
7240 continue;
7241 sg = sg->next;
7242next_sg:
7243 oldsg = sg;
7244 sg = sg->next;
7245 kfree(oldsg);
7246 if (oldsg != sched_group_nodes[i])
7247 goto next_sg;
7248 }
7249 kfree(sched_group_nodes);
7250 sched_group_nodes_bycpu[cpu] = NULL;
7251 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007252}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007253#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007254static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007255{
7256}
7257#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007258
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007260 * Initialize sched groups cpu_power.
7261 *
7262 * cpu_power indicates the capacity of sched group, which is used while
7263 * distributing the load between different sched groups in a sched domain.
7264 * Typically cpu_power for all the groups in a sched domain will be same unless
7265 * there are asymmetries in the topology. If there are asymmetries, group
7266 * having more cpu_power will pickup more load compared to the group having
7267 * less cpu_power.
7268 *
7269 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7270 * the maximum number of tasks a group can handle in the presence of other idle
7271 * or lightly loaded groups in the same sched domain.
7272 */
7273static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7274{
7275 struct sched_domain *child;
7276 struct sched_group *group;
7277
7278 WARN_ON(!sd || !sd->groups);
7279
7280 if (cpu != first_cpu(sd->groups->cpumask))
7281 return;
7282
7283 child = sd->child;
7284
Eric Dumazet5517d862007-05-08 00:32:57 -07007285 sd->groups->__cpu_power = 0;
7286
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007287 /*
7288 * For perf policy, if the groups in child domain share resources
7289 * (for example cores sharing some portions of the cache hierarchy
7290 * or SMT), then set this domain groups cpu_power such that each group
7291 * can handle only one task, when there are other idle groups in the
7292 * same sched domain.
7293 */
7294 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7295 (child->flags &
7296 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007297 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007298 return;
7299 }
7300
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007301 /*
7302 * add cpu_power of each child group to this groups cpu_power
7303 */
7304 group = child->groups;
7305 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007306 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007307 group = group->next;
7308 } while (group != child->groups);
7309}
7310
7311/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007312 * Initializers for schedule domains
7313 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7314 */
7315
7316#define SD_INIT(sd, type) sd_init_##type(sd)
7317#define SD_INIT_FUNC(type) \
7318static noinline void sd_init_##type(struct sched_domain *sd) \
7319{ \
7320 memset(sd, 0, sizeof(*sd)); \
7321 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007322 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007323}
7324
7325SD_INIT_FUNC(CPU)
7326#ifdef CONFIG_NUMA
7327 SD_INIT_FUNC(ALLNODES)
7328 SD_INIT_FUNC(NODE)
7329#endif
7330#ifdef CONFIG_SCHED_SMT
7331 SD_INIT_FUNC(SIBLING)
7332#endif
7333#ifdef CONFIG_SCHED_MC
7334 SD_INIT_FUNC(MC)
7335#endif
7336
7337/*
7338 * To minimize stack usage kmalloc room for cpumasks and share the
7339 * space as the usage in build_sched_domains() dictates. Used only
7340 * if the amount of space is significant.
7341 */
7342struct allmasks {
7343 cpumask_t tmpmask; /* make this one first */
7344 union {
7345 cpumask_t nodemask;
7346 cpumask_t this_sibling_map;
7347 cpumask_t this_core_map;
7348 };
7349 cpumask_t send_covered;
7350
7351#ifdef CONFIG_NUMA
7352 cpumask_t domainspan;
7353 cpumask_t covered;
7354 cpumask_t notcovered;
7355#endif
7356};
7357
7358#if NR_CPUS > 128
7359#define SCHED_CPUMASK_ALLOC 1
7360#define SCHED_CPUMASK_FREE(v) kfree(v)
7361#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7362#else
7363#define SCHED_CPUMASK_ALLOC 0
7364#define SCHED_CPUMASK_FREE(v)
7365#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7366#endif
7367
7368#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7369 ((unsigned long)(a) + offsetof(struct allmasks, v))
7370
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007371static int default_relax_domain_level = -1;
7372
7373static int __init setup_relax_domain_level(char *str)
7374{
7375 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7376 return 1;
7377}
7378__setup("relax_domain_level=", setup_relax_domain_level);
7379
7380static void set_domain_attribute(struct sched_domain *sd,
7381 struct sched_domain_attr *attr)
7382{
7383 int request;
7384
7385 if (!attr || attr->relax_domain_level < 0) {
7386 if (default_relax_domain_level < 0)
7387 return;
7388 else
7389 request = default_relax_domain_level;
7390 } else
7391 request = attr->relax_domain_level;
7392 if (request < sd->level) {
7393 /* turn off idle balance on this domain */
7394 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7395 } else {
7396 /* turn on idle balance on this domain */
7397 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7398 }
7399}
7400
Mike Travis7c16ec52008-04-04 18:11:11 -07007401/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007402 * Build sched domains for a given set of cpus and attach the sched domains
7403 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007405static int __build_sched_domains(const cpumask_t *cpu_map,
7406 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407{
7408 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007409 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007410 SCHED_CPUMASK_DECLARE(allmasks);
7411 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007412#ifdef CONFIG_NUMA
7413 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007414 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007415
7416 /*
7417 * Allocate the per-node list of sched groups
7418 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007419 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007420 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007421 if (!sched_group_nodes) {
7422 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007423 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007424 }
John Hawkesd1b55132005-09-06 15:18:14 -07007425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
Gregory Haskinsdc938522008-01-25 21:08:26 +01007427 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007428 if (!rd) {
7429 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007430#ifdef CONFIG_NUMA
7431 kfree(sched_group_nodes);
7432#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007433 return -ENOMEM;
7434 }
7435
Mike Travis7c16ec52008-04-04 18:11:11 -07007436#if SCHED_CPUMASK_ALLOC
7437 /* get space for all scratch cpumask variables */
7438 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7439 if (!allmasks) {
7440 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7441 kfree(rd);
7442#ifdef CONFIG_NUMA
7443 kfree(sched_group_nodes);
7444#endif
7445 return -ENOMEM;
7446 }
7447#endif
7448 tmpmask = (cpumask_t *)allmasks;
7449
7450
7451#ifdef CONFIG_NUMA
7452 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7453#endif
7454
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007456 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007458 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007460 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461
Mike Travis7c16ec52008-04-04 18:11:11 -07007462 *nodemask = node_to_cpumask(cpu_to_node(i));
7463 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
7465#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007466 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007467 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007468 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007469 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007470 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007471 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007472 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007473 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007474 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007475 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007476 } else
7477 p = NULL;
7478
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007480 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007481 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007482 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007483 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007484 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007485 if (p)
7486 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007487 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488#endif
7489
7490 p = sd;
7491 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007492 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007493 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007494 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007495 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007497 if (p)
7498 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007499 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007501#ifdef CONFIG_SCHED_MC
7502 p = sd;
7503 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007504 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007505 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007506 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007507 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007508 cpus_and(sd->span, sd->span, *cpu_map);
7509 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007510 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007511 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007512#endif
7513
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514#ifdef CONFIG_SCHED_SMT
7515 p = sd;
7516 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007517 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007518 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007519 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007520 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007521 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007523 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007524 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525#endif
7526 }
7527
7528#ifdef CONFIG_SCHED_SMT
7529 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007530 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007531 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7532 SCHED_CPUMASK_VAR(send_covered, allmasks);
7533
7534 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7535 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7536 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 continue;
7538
Ingo Molnardd41f592007-07-09 18:51:59 +02007539 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007540 &cpu_to_cpu_group,
7541 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 }
7543#endif
7544
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007545#ifdef CONFIG_SCHED_MC
7546 /* Set up multi-core groups */
7547 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007548 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7549 SCHED_CPUMASK_VAR(send_covered, allmasks);
7550
7551 *this_core_map = cpu_coregroup_map(i);
7552 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7553 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007554 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007555
Ingo Molnardd41f592007-07-09 18:51:59 +02007556 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007557 &cpu_to_core_group,
7558 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007559 }
7560#endif
7561
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 /* Set up physical groups */
7563 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007564 SCHED_CPUMASK_VAR(nodemask, allmasks);
7565 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
Mike Travis7c16ec52008-04-04 18:11:11 -07007567 *nodemask = node_to_cpumask(i);
7568 cpus_and(*nodemask, *nodemask, *cpu_map);
7569 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 continue;
7571
Mike Travis7c16ec52008-04-04 18:11:11 -07007572 init_sched_build_groups(nodemask, cpu_map,
7573 &cpu_to_phys_group,
7574 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 }
7576
7577#ifdef CONFIG_NUMA
7578 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007579 if (sd_allnodes) {
7580 SCHED_CPUMASK_VAR(send_covered, allmasks);
7581
7582 init_sched_build_groups(cpu_map, cpu_map,
7583 &cpu_to_allnodes_group,
7584 send_covered, tmpmask);
7585 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007586
7587 for (i = 0; i < MAX_NUMNODES; i++) {
7588 /* Set up node groups */
7589 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007590 SCHED_CPUMASK_VAR(nodemask, allmasks);
7591 SCHED_CPUMASK_VAR(domainspan, allmasks);
7592 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007593 int j;
7594
Mike Travis7c16ec52008-04-04 18:11:11 -07007595 *nodemask = node_to_cpumask(i);
7596 cpus_clear(*covered);
7597
7598 cpus_and(*nodemask, *nodemask, *cpu_map);
7599 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007600 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007601 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007602 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007603
Mike Travis4bdbaad32008-04-15 16:35:52 -07007604 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007605 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007606
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007607 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007608 if (!sg) {
7609 printk(KERN_WARNING "Can not alloc domain group for "
7610 "node %d\n", i);
7611 goto error;
7612 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007613 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007614 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007615 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007616
John Hawkes9c1cfda2005-09-06 15:18:14 -07007617 sd = &per_cpu(node_domains, j);
7618 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007619 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007620 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007621 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007622 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007623 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007624 prev = sg;
7625
7626 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007627 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007629 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007630
Mike Travis7c16ec52008-04-04 18:11:11 -07007631 cpus_complement(*notcovered, *covered);
7632 cpus_and(*tmpmask, *notcovered, *cpu_map);
7633 cpus_and(*tmpmask, *tmpmask, *domainspan);
7634 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007635 break;
7636
Mike Travis7c16ec52008-04-04 18:11:11 -07007637 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7638 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007639 continue;
7640
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007641 sg = kmalloc_node(sizeof(struct sched_group),
7642 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007643 if (!sg) {
7644 printk(KERN_WARNING
7645 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007646 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007647 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007648 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007649 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007650 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007651 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007652 prev->next = sg;
7653 prev = sg;
7654 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656#endif
7657
7658 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007659#ifdef CONFIG_SCHED_SMT
7660 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007661 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7662
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007663 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007664 }
7665#endif
7666#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007667 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007668 struct sched_domain *sd = &per_cpu(core_domains, i);
7669
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007670 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007671 }
7672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007674 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007675 struct sched_domain *sd = &per_cpu(phys_domains, i);
7676
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007677 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 }
7679
John Hawkes9c1cfda2005-09-06 15:18:14 -07007680#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007681 for (i = 0; i < MAX_NUMNODES; i++)
7682 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007683
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007684 if (sd_allnodes) {
7685 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007686
Mike Travis7c16ec52008-04-04 18:11:11 -07007687 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7688 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007689 init_numa_sched_groups_power(sg);
7690 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007691#endif
7692
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007694 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695 struct sched_domain *sd;
7696#ifdef CONFIG_SCHED_SMT
7697 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007698#elif defined(CONFIG_SCHED_MC)
7699 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700#else
7701 sd = &per_cpu(phys_domains, i);
7702#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007703 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007705
Mike Travis7c16ec52008-04-04 18:11:11 -07007706 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007707 return 0;
7708
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007709#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007710error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007711 free_sched_groups(cpu_map, tmpmask);
7712 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007713 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715}
Paul Jackson029190c2007-10-18 23:40:20 -07007716
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007717static int build_sched_domains(const cpumask_t *cpu_map)
7718{
7719 return __build_sched_domains(cpu_map, NULL);
7720}
7721
Paul Jackson029190c2007-10-18 23:40:20 -07007722static cpumask_t *doms_cur; /* current sched domains */
7723static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007724static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7725 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007726
7727/*
7728 * Special case: If a kmalloc of a doms_cur partition (array of
7729 * cpumask_t) fails, then fallback to a single sched domain,
7730 * as determined by the single cpumask_t fallback_doms.
7731 */
7732static cpumask_t fallback_doms;
7733
Heiko Carstens22e52b02008-03-12 18:31:59 +01007734void __attribute__((weak)) arch_update_cpu_topology(void)
7735{
7736}
7737
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007738/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007739 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007740 * For now this just excludes isolated cpus, but could be used to
7741 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007742 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007743static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007744{
Milton Miller73785472007-10-24 18:23:48 +02007745 int err;
7746
Heiko Carstens22e52b02008-03-12 18:31:59 +01007747 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007748 ndoms_cur = 1;
7749 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7750 if (!doms_cur)
7751 doms_cur = &fallback_doms;
7752 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007753 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007754 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007755 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007756
7757 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007758}
7759
Mike Travis7c16ec52008-04-04 18:11:11 -07007760static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7761 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762{
Mike Travis7c16ec52008-04-04 18:11:11 -07007763 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007766/*
7767 * Detach sched domains from a group of cpus specified in cpu_map
7768 * These cpus will now be attached to the NULL domain
7769 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007770static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007771{
Mike Travis7c16ec52008-04-04 18:11:11 -07007772 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007773 int i;
7774
Milton Miller6382bc92007-10-15 17:00:19 +02007775 unregister_sched_domain_sysctl();
7776
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007777 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007778 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007779 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007780 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007781}
7782
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007783/* handle null as "default" */
7784static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7785 struct sched_domain_attr *new, int idx_new)
7786{
7787 struct sched_domain_attr tmp;
7788
7789 /* fast path */
7790 if (!new && !cur)
7791 return 1;
7792
7793 tmp = SD_ATTR_INIT;
7794 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7795 new ? (new + idx_new) : &tmp,
7796 sizeof(struct sched_domain_attr));
7797}
7798
Paul Jackson029190c2007-10-18 23:40:20 -07007799/*
7800 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007801 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007802 * doms_new[] to the current sched domain partitioning, doms_cur[].
7803 * It destroys each deleted domain and builds each new domain.
7804 *
7805 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007806 * The masks don't intersect (don't overlap.) We should setup one
7807 * sched domain for each mask. CPUs not in any of the cpumasks will
7808 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007809 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7810 * it as it is.
7811 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007812 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7813 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007814 * failed the kmalloc call, then it can pass in doms_new == NULL,
7815 * and partition_sched_domains() will fallback to the single partition
7816 * 'fallback_doms'.
7817 *
7818 * Call with hotplug lock held
7819 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007820void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7821 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007822{
7823 int i, j;
7824
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007825 lock_doms_cur();
7826
Milton Miller73785472007-10-24 18:23:48 +02007827 /* always unregister in case we don't destroy any domains */
7828 unregister_sched_domain_sysctl();
7829
Paul Jackson029190c2007-10-18 23:40:20 -07007830 if (doms_new == NULL) {
7831 ndoms_new = 1;
7832 doms_new = &fallback_doms;
7833 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007834 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007835 }
7836
7837 /* Destroy deleted domains */
7838 for (i = 0; i < ndoms_cur; i++) {
7839 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007840 if (cpus_equal(doms_cur[i], doms_new[j])
7841 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007842 goto match1;
7843 }
7844 /* no match - a current sched domain not in new doms_new[] */
7845 detach_destroy_domains(doms_cur + i);
7846match1:
7847 ;
7848 }
7849
7850 /* Build new domains */
7851 for (i = 0; i < ndoms_new; i++) {
7852 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007853 if (cpus_equal(doms_new[i], doms_cur[j])
7854 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007855 goto match2;
7856 }
7857 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007858 __build_sched_domains(doms_new + i,
7859 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007860match2:
7861 ;
7862 }
7863
7864 /* Remember the new sched domains */
7865 if (doms_cur != &fallback_doms)
7866 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007867 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007868 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007869 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007870 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007871
7872 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007873
7874 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007875}
7876
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007877#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007878int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007879{
7880 int err;
7881
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007882 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007883 detach_destroy_domains(&cpu_online_map);
7884 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007885 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007886
7887 return err;
7888}
7889
7890static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7891{
7892 int ret;
7893
7894 if (buf[0] != '0' && buf[0] != '1')
7895 return -EINVAL;
7896
7897 if (smt)
7898 sched_smt_power_savings = (buf[0] == '1');
7899 else
7900 sched_mc_power_savings = (buf[0] == '1');
7901
7902 ret = arch_reinit_sched_domains();
7903
7904 return ret ? ret : count;
7905}
7906
Adrian Bunk6707de002007-08-12 18:08:19 +02007907#ifdef CONFIG_SCHED_MC
7908static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7909{
7910 return sprintf(page, "%u\n", sched_mc_power_savings);
7911}
7912static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7913 const char *buf, size_t count)
7914{
7915 return sched_power_savings_store(buf, count, 0);
7916}
7917static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7918 sched_mc_power_savings_store);
7919#endif
7920
7921#ifdef CONFIG_SCHED_SMT
7922static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7923{
7924 return sprintf(page, "%u\n", sched_smt_power_savings);
7925}
7926static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7927 const char *buf, size_t count)
7928{
7929 return sched_power_savings_store(buf, count, 1);
7930}
7931static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7932 sched_smt_power_savings_store);
7933#endif
7934
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007935int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7936{
7937 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007938
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007939#ifdef CONFIG_SCHED_SMT
7940 if (smt_capable())
7941 err = sysfs_create_file(&cls->kset.kobj,
7942 &attr_sched_smt_power_savings.attr);
7943#endif
7944#ifdef CONFIG_SCHED_MC
7945 if (!err && mc_capable())
7946 err = sysfs_create_file(&cls->kset.kobj,
7947 &attr_sched_mc_power_savings.attr);
7948#endif
7949 return err;
7950}
7951#endif
7952
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007954 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007956 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 * which will prevent rebalancing while the sched domains are recalculated.
7958 */
7959static int update_sched_domains(struct notifier_block *nfb,
7960 unsigned long action, void *hcpu)
7961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962 switch (action) {
7963 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007964 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007966 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007967 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968 return NOTIFY_OK;
7969
7970 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007971 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007973 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007975 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007977 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 /*
7979 * Fall through and re-initialise the domains.
7980 */
7981 break;
7982 default:
7983 return NOTIFY_DONE;
7984 }
7985
7986 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007987 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988
7989 return NOTIFY_OK;
7990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
7992void __init sched_init_smp(void)
7993{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007994 cpumask_t non_isolated_cpus;
7995
Mike Travis434d53b2008-04-04 18:11:04 -07007996#if defined(CONFIG_NUMA)
7997 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7998 GFP_KERNEL);
7999 BUG_ON(sched_group_nodes_bycpu == NULL);
8000#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008001 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008002 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08008003 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008004 if (cpus_empty(non_isolated_cpus))
8005 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008006 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 /* XXX: Theoretical race here - CPU may be hotplugged now */
8008 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008009 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008010
8011 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07008012 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008013 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008014 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015}
8016#else
8017void __init sched_init_smp(void)
8018{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008019 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020}
8021#endif /* CONFIG_SMP */
8022
8023int in_sched_functions(unsigned long addr)
8024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025 return in_lock_functions(addr) ||
8026 (addr >= (unsigned long)__sched_text_start
8027 && addr < (unsigned long)__sched_text_end);
8028}
8029
Alexey Dobriyana9957442007-10-15 17:00:13 +02008030static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008031{
8032 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008033 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008034#ifdef CONFIG_FAIR_GROUP_SCHED
8035 cfs_rq->rq = rq;
8036#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008037 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008038}
8039
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008040static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8041{
8042 struct rt_prio_array *array;
8043 int i;
8044
8045 array = &rt_rq->active;
8046 for (i = 0; i < MAX_RT_PRIO; i++) {
8047 INIT_LIST_HEAD(array->queue + i);
8048 __clear_bit(i, array->bitmap);
8049 }
8050 /* delimiter for bitsearch: */
8051 __set_bit(MAX_RT_PRIO, array->bitmap);
8052
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008053#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008054 rt_rq->highest_prio = MAX_RT_PRIO;
8055#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008056#ifdef CONFIG_SMP
8057 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008058 rt_rq->overloaded = 0;
8059#endif
8060
8061 rt_rq->rt_time = 0;
8062 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008063 rt_rq->rt_runtime = 0;
8064 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008065
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008066#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008067 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008068 rt_rq->rq = rq;
8069#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008070}
8071
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008072#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008073static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8074 struct sched_entity *se, int cpu, int add,
8075 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008076{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008077 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008078 tg->cfs_rq[cpu] = cfs_rq;
8079 init_cfs_rq(cfs_rq, rq);
8080 cfs_rq->tg = tg;
8081 if (add)
8082 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8083
8084 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008085 /* se could be NULL for init_task_group */
8086 if (!se)
8087 return;
8088
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008089 if (!parent)
8090 se->cfs_rq = &rq->cfs;
8091 else
8092 se->cfs_rq = parent->my_q;
8093
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008094 se->my_q = cfs_rq;
8095 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008096 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008097 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008098}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008099#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008100
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008101#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008102static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8103 struct sched_rt_entity *rt_se, int cpu, int add,
8104 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008105{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008106 struct rq *rq = cpu_rq(cpu);
8107
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008108 tg->rt_rq[cpu] = rt_rq;
8109 init_rt_rq(rt_rq, rq);
8110 rt_rq->tg = tg;
8111 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008112 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008113 if (add)
8114 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8115
8116 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008117 if (!rt_se)
8118 return;
8119
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008120 if (!parent)
8121 rt_se->rt_rq = &rq->rt;
8122 else
8123 rt_se->rt_rq = parent->my_q;
8124
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008125 rt_se->rt_rq = &rq->rt;
8126 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008127 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008128 INIT_LIST_HEAD(&rt_se->run_list);
8129}
8130#endif
8131
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132void __init sched_init(void)
8133{
Ingo Molnardd41f592007-07-09 18:51:59 +02008134 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008135 unsigned long alloc_size = 0, ptr;
8136
8137#ifdef CONFIG_FAIR_GROUP_SCHED
8138 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8139#endif
8140#ifdef CONFIG_RT_GROUP_SCHED
8141 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8142#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008143#ifdef CONFIG_USER_SCHED
8144 alloc_size *= 2;
8145#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008146 /*
8147 * As sched_init() is called before page_alloc is setup,
8148 * we use alloc_bootmem().
8149 */
8150 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008151 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008152
8153#ifdef CONFIG_FAIR_GROUP_SCHED
8154 init_task_group.se = (struct sched_entity **)ptr;
8155 ptr += nr_cpu_ids * sizeof(void **);
8156
8157 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8158 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008159
8160#ifdef CONFIG_USER_SCHED
8161 root_task_group.se = (struct sched_entity **)ptr;
8162 ptr += nr_cpu_ids * sizeof(void **);
8163
8164 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8165 ptr += nr_cpu_ids * sizeof(void **);
8166#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008167#endif
8168#ifdef CONFIG_RT_GROUP_SCHED
8169 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8170 ptr += nr_cpu_ids * sizeof(void **);
8171
8172 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008173 ptr += nr_cpu_ids * sizeof(void **);
8174
8175#ifdef CONFIG_USER_SCHED
8176 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8177 ptr += nr_cpu_ids * sizeof(void **);
8178
8179 root_task_group.rt_rq = (struct rt_rq **)ptr;
8180 ptr += nr_cpu_ids * sizeof(void **);
8181#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008182#endif
8183 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008184
Gregory Haskins57d885f2008-01-25 21:08:18 +01008185#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008186 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008187 init_defrootdomain();
8188#endif
8189
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008190 init_rt_bandwidth(&def_rt_bandwidth,
8191 global_rt_period(), global_rt_runtime());
8192
8193#ifdef CONFIG_RT_GROUP_SCHED
8194 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8195 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008196#ifdef CONFIG_USER_SCHED
8197 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8198 global_rt_period(), RUNTIME_INF);
8199#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008200#endif
8201
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008202#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008203 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008204 INIT_LIST_HEAD(&init_task_group.children);
8205
8206#ifdef CONFIG_USER_SCHED
8207 INIT_LIST_HEAD(&root_task_group.children);
8208 init_task_group.parent = &root_task_group;
8209 list_add(&init_task_group.siblings, &root_task_group.children);
8210#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008211#endif
8212
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008213 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008214 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215
8216 rq = cpu_rq(i);
8217 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008218 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008219 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008220 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008221 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008222 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008223 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008224#ifdef CONFIG_FAIR_GROUP_SCHED
8225 init_task_group.shares = init_task_group_load;
8226 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008227#ifdef CONFIG_CGROUP_SCHED
8228 /*
8229 * How much cpu bandwidth does init_task_group get?
8230 *
8231 * In case of task-groups formed thr' the cgroup filesystem, it
8232 * gets 100% of the cpu resources in the system. This overall
8233 * system cpu resource is divided among the tasks of
8234 * init_task_group and its child task-groups in a fair manner,
8235 * based on each entity's (task or task-group's) weight
8236 * (se->load.weight).
8237 *
8238 * In other words, if init_task_group has 10 tasks of weight
8239 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8240 * then A0's share of the cpu resource is:
8241 *
8242 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8243 *
8244 * We achieve this by letting init_task_group's tasks sit
8245 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8246 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008247 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008248#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008249 root_task_group.shares = NICE_0_LOAD;
8250 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008251 /*
8252 * In case of task-groups formed thr' the user id of tasks,
8253 * init_task_group represents tasks belonging to root user.
8254 * Hence it forms a sibling of all subsequent groups formed.
8255 * In this case, init_task_group gets only a fraction of overall
8256 * system cpu resource, based on the weight assigned to root
8257 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8258 * by letting tasks of init_task_group sit in a separate cfs_rq
8259 * (init_cfs_rq) and having one entity represent this group of
8260 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8261 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008262 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008263 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008264 &per_cpu(init_sched_entity, i), i, 1,
8265 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008266
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008267#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008268#endif /* CONFIG_FAIR_GROUP_SCHED */
8269
8270 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008271#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008272 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008273#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008274 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008275#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008276 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008277 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008278 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008279 &per_cpu(init_sched_rt_entity, i), i, 1,
8280 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008281#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283
Ingo Molnardd41f592007-07-09 18:51:59 +02008284 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8285 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008287 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008288 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008290 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008292 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 rq->migration_thread = NULL;
8294 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008295 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008297 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 }
8300
Peter Williams2dd73a42006-06-27 02:54:34 -07008301 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008302
Avi Kivitye107be32007-07-26 13:40:43 +02008303#ifdef CONFIG_PREEMPT_NOTIFIERS
8304 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8305#endif
8306
Christoph Lameterc9819f42006-12-10 02:20:25 -08008307#ifdef CONFIG_SMP
8308 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8309#endif
8310
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008311#ifdef CONFIG_RT_MUTEXES
8312 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8313#endif
8314
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 /*
8316 * The boot idle thread does lazy MMU switching as well:
8317 */
8318 atomic_inc(&init_mm.mm_count);
8319 enter_lazy_tlb(&init_mm, current);
8320
8321 /*
8322 * Make us the idle thread. Technically, schedule() should not be
8323 * called from this thread, however somewhere below it might be,
8324 * but because we are the idle thread, we just pick up running again
8325 * when this runqueue becomes "idle".
8326 */
8327 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008328 /*
8329 * During early bootup we pretend to be a normal task:
8330 */
8331 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008332
8333 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334}
8335
8336#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8337void __might_sleep(char *file, int line)
8338{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008339#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 static unsigned long prev_jiffy; /* ratelimiting */
8341
8342 if ((in_atomic() || irqs_disabled()) &&
8343 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8344 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8345 return;
8346 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008347 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 " context at %s:%d\n", file, line);
8349 printk("in_atomic():%d, irqs_disabled():%d\n",
8350 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008351 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008352 if (irqs_disabled())
8353 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 dump_stack();
8355 }
8356#endif
8357}
8358EXPORT_SYMBOL(__might_sleep);
8359#endif
8360
8361#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008362static void normalize_task(struct rq *rq, struct task_struct *p)
8363{
8364 int on_rq;
8365 update_rq_clock(rq);
8366 on_rq = p->se.on_rq;
8367 if (on_rq)
8368 deactivate_task(rq, p, 0);
8369 __setscheduler(rq, p, SCHED_NORMAL, 0);
8370 if (on_rq) {
8371 activate_task(rq, p, 0);
8372 resched_task(rq->curr);
8373 }
8374}
8375
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376void normalize_rt_tasks(void)
8377{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008378 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008380 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008382 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008383 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008384 /*
8385 * Only normalize user tasks:
8386 */
8387 if (!p->mm)
8388 continue;
8389
Ingo Molnardd41f592007-07-09 18:51:59 +02008390 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008391#ifdef CONFIG_SCHEDSTATS
8392 p->se.wait_start = 0;
8393 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008394 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008395#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008396 task_rq(p)->clock = 0;
8397
8398 if (!rt_task(p)) {
8399 /*
8400 * Renice negative nice level userspace
8401 * tasks back to 0:
8402 */
8403 if (TASK_NICE(p) < 0 && p->mm)
8404 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008408 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008409 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410
Ingo Molnar178be792007-10-15 17:00:18 +02008411 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008412
Ingo Molnarb29739f2006-06-27 02:54:51 -07008413 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008414 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008415 } while_each_thread(g, p);
8416
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008417 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418}
8419
8420#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008421
8422#ifdef CONFIG_IA64
8423/*
8424 * These functions are only useful for the IA64 MCA handling.
8425 *
8426 * They can only be called when the whole system has been
8427 * stopped - every CPU needs to be quiescent, and no scheduling
8428 * activity can take place. Using them for anything else would
8429 * be a serious bug, and as a result, they aren't even visible
8430 * under any other configuration.
8431 */
8432
8433/**
8434 * curr_task - return the current task for a given cpu.
8435 * @cpu: the processor in question.
8436 *
8437 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8438 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008439struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008440{
8441 return cpu_curr(cpu);
8442}
8443
8444/**
8445 * set_curr_task - set the current task for a given cpu.
8446 * @cpu: the processor in question.
8447 * @p: the task pointer to set.
8448 *
8449 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008450 * are serviced on a separate stack. It allows the architecture to switch the
8451 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008452 * must be called with all CPU's synchronized, and interrupts disabled, the
8453 * and caller must save the original value of the current task (see
8454 * curr_task() above) and restore that value before reenabling interrupts and
8455 * re-starting the system.
8456 *
8457 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8458 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008459void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008460{
8461 cpu_curr(cpu) = p;
8462}
8463
8464#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008465
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008466#ifdef CONFIG_FAIR_GROUP_SCHED
8467static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008468{
8469 int i;
8470
8471 for_each_possible_cpu(i) {
8472 if (tg->cfs_rq)
8473 kfree(tg->cfs_rq[i]);
8474 if (tg->se)
8475 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008476 }
8477
8478 kfree(tg->cfs_rq);
8479 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008480}
8481
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008482static
8483int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008484{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008485 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008486 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008487 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008488 int i;
8489
Mike Travis434d53b2008-04-04 18:11:04 -07008490 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008491 if (!tg->cfs_rq)
8492 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008493 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008494 if (!tg->se)
8495 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008496
8497 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008498
8499 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008500 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008501
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008502 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8503 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008504 if (!cfs_rq)
8505 goto err;
8506
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008507 se = kmalloc_node(sizeof(struct sched_entity),
8508 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008509 if (!se)
8510 goto err;
8511
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008512 parent_se = parent ? parent->se[i] : NULL;
8513 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008514 }
8515
8516 return 1;
8517
8518 err:
8519 return 0;
8520}
8521
8522static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8523{
8524 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8525 &cpu_rq(cpu)->leaf_cfs_rq_list);
8526}
8527
8528static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8529{
8530 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8531}
8532#else
8533static inline void free_fair_sched_group(struct task_group *tg)
8534{
8535}
8536
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008537static inline
8538int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008539{
8540 return 1;
8541}
8542
8543static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8544{
8545}
8546
8547static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8548{
8549}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008550#endif
8551
8552#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008553static void free_rt_sched_group(struct task_group *tg)
8554{
8555 int i;
8556
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008557 destroy_rt_bandwidth(&tg->rt_bandwidth);
8558
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008559 for_each_possible_cpu(i) {
8560 if (tg->rt_rq)
8561 kfree(tg->rt_rq[i]);
8562 if (tg->rt_se)
8563 kfree(tg->rt_se[i]);
8564 }
8565
8566 kfree(tg->rt_rq);
8567 kfree(tg->rt_se);
8568}
8569
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008570static
8571int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008572{
8573 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008574 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008575 struct rq *rq;
8576 int i;
8577
Mike Travis434d53b2008-04-04 18:11:04 -07008578 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008579 if (!tg->rt_rq)
8580 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008581 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008582 if (!tg->rt_se)
8583 goto err;
8584
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008585 init_rt_bandwidth(&tg->rt_bandwidth,
8586 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008587
8588 for_each_possible_cpu(i) {
8589 rq = cpu_rq(i);
8590
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008591 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8592 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8593 if (!rt_rq)
8594 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008595
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008596 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8597 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8598 if (!rt_se)
8599 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008600
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008601 parent_se = parent ? parent->rt_se[i] : NULL;
8602 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008603 }
8604
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008605 return 1;
8606
8607 err:
8608 return 0;
8609}
8610
8611static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8612{
8613 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8614 &cpu_rq(cpu)->leaf_rt_rq_list);
8615}
8616
8617static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8618{
8619 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8620}
8621#else
8622static inline void free_rt_sched_group(struct task_group *tg)
8623{
8624}
8625
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008626static inline
8627int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008628{
8629 return 1;
8630}
8631
8632static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8633{
8634}
8635
8636static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8637{
8638}
8639#endif
8640
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008641#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008642static void free_sched_group(struct task_group *tg)
8643{
8644 free_fair_sched_group(tg);
8645 free_rt_sched_group(tg);
8646 kfree(tg);
8647}
8648
8649/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008650struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008651{
8652 struct task_group *tg;
8653 unsigned long flags;
8654 int i;
8655
8656 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8657 if (!tg)
8658 return ERR_PTR(-ENOMEM);
8659
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008660 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008661 goto err;
8662
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008663 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008664 goto err;
8665
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008666 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008667 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008668 register_fair_sched_group(tg, i);
8669 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008670 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008671 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008672
8673 WARN_ON(!parent); /* root should already exist */
8674
8675 tg->parent = parent;
8676 list_add_rcu(&tg->siblings, &parent->children);
8677 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008678 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008679
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008680 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008681
8682err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008683 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008684 return ERR_PTR(-ENOMEM);
8685}
8686
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008687/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008688static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008689{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008690 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008691 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008692}
8693
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008694/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008695void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008696{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008697 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008698 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008699
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008700 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008701 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008702 unregister_fair_sched_group(tg, i);
8703 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008704 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008705 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008706 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008707 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008708
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008709 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008710 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008711}
8712
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008713/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008714 * The caller of this function should have put the task in its new group
8715 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8716 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008717 */
8718void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008719{
8720 int on_rq, running;
8721 unsigned long flags;
8722 struct rq *rq;
8723
8724 rq = task_rq_lock(tsk, &flags);
8725
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008726 update_rq_clock(rq);
8727
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008728 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008729 on_rq = tsk->se.on_rq;
8730
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008731 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008732 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008733 if (unlikely(running))
8734 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008735
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008736 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008737
Peter Zijlstra810b3812008-02-29 15:21:01 -05008738#ifdef CONFIG_FAIR_GROUP_SCHED
8739 if (tsk->sched_class->moved_group)
8740 tsk->sched_class->moved_group(tsk);
8741#endif
8742
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008743 if (unlikely(running))
8744 tsk->sched_class->set_curr_task(rq);
8745 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008746 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008747
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008748 task_rq_unlock(rq, &flags);
8749}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008750#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008751
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008752#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008753static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008754{
8755 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008756 int on_rq;
8757
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008758 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008759 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008760 dequeue_entity(cfs_rq, se, 0);
8761
8762 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008763 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008764
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008765 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008766 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008767}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008768
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008769static void set_se_shares(struct sched_entity *se, unsigned long shares)
8770{
8771 struct cfs_rq *cfs_rq = se->cfs_rq;
8772 struct rq *rq = cfs_rq->rq;
8773 unsigned long flags;
8774
8775 spin_lock_irqsave(&rq->lock, flags);
8776 __set_se_shares(se, shares);
8777 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008778}
8779
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008780static DEFINE_MUTEX(shares_mutex);
8781
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008782int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008783{
8784 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008785 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008786
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008787 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008788 * We can't change the weight of the root cgroup.
8789 */
8790 if (!tg->se[0])
8791 return -EINVAL;
8792
8793 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008794 * A weight of 0 or 1 can cause arithmetics problems.
8795 * (The default weight is 1024 - so there's no practical
8796 * limitation from this.)
8797 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008798 if (shares < MIN_SHARES)
8799 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008800
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008801 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008802 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008803 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008804
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008805 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008806 for_each_possible_cpu(i)
8807 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008808 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008809 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008810
8811 /* wait for any ongoing reference to this group to finish */
8812 synchronize_sched();
8813
8814 /*
8815 * Now we are free to modify the group's share on each cpu
8816 * w/o tripping rebalance_share or load_balance_fair.
8817 */
8818 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008819 for_each_possible_cpu(i) {
8820 /*
8821 * force a rebalance
8822 */
8823 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8824 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8825 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008826
8827 /*
8828 * Enable load balance activity on this group, by inserting it back on
8829 * each cpu's rq->leaf_cfs_rq_list.
8830 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008831 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008832 for_each_possible_cpu(i)
8833 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008834 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008835 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008836done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008837 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008838 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008839}
8840
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008841unsigned long sched_group_shares(struct task_group *tg)
8842{
8843 return tg->shares;
8844}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008845#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008846
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008847#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008848/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008849 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008850 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008851static DEFINE_MUTEX(rt_constraints_mutex);
8852
8853static unsigned long to_ratio(u64 period, u64 runtime)
8854{
8855 if (runtime == RUNTIME_INF)
8856 return 1ULL << 16;
8857
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008858 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008859}
8860
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008861#ifdef CONFIG_CGROUP_SCHED
8862static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8863{
8864 struct task_group *tgi, *parent = tg->parent;
8865 unsigned long total = 0;
8866
8867 if (!parent) {
8868 if (global_rt_period() < period)
8869 return 0;
8870
8871 return to_ratio(period, runtime) <
8872 to_ratio(global_rt_period(), global_rt_runtime());
8873 }
8874
8875 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8876 return 0;
8877
8878 rcu_read_lock();
8879 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8880 if (tgi == tg)
8881 continue;
8882
8883 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8884 tgi->rt_bandwidth.rt_runtime);
8885 }
8886 rcu_read_unlock();
8887
8888 return total + to_ratio(period, runtime) <
8889 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8890 parent->rt_bandwidth.rt_runtime);
8891}
8892#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008893static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008894{
8895 struct task_group *tgi;
8896 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008897 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008898 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008899
8900 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008901 list_for_each_entry_rcu(tgi, &task_groups, list) {
8902 if (tgi == tg)
8903 continue;
8904
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008905 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8906 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008907 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008908 rcu_read_unlock();
8909
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008910 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008911}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008912#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008913
Dhaval Giani521f1a242008-02-28 15:21:56 +05308914/* Must be called with tasklist_lock held */
8915static inline int tg_has_rt_tasks(struct task_group *tg)
8916{
8917 struct task_struct *g, *p;
8918 do_each_thread(g, p) {
8919 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8920 return 1;
8921 } while_each_thread(g, p);
8922 return 0;
8923}
8924
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008925static int tg_set_bandwidth(struct task_group *tg,
8926 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008927{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008928 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008929
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008930 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308931 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008932 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308933 err = -EBUSY;
8934 goto unlock;
8935 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008936 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8937 err = -EINVAL;
8938 goto unlock;
8939 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008940
8941 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008942 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8943 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008944
8945 for_each_possible_cpu(i) {
8946 struct rt_rq *rt_rq = tg->rt_rq[i];
8947
8948 spin_lock(&rt_rq->rt_runtime_lock);
8949 rt_rq->rt_runtime = rt_runtime;
8950 spin_unlock(&rt_rq->rt_runtime_lock);
8951 }
8952 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008953 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308954 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008955 mutex_unlock(&rt_constraints_mutex);
8956
8957 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008958}
8959
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008960int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8961{
8962 u64 rt_runtime, rt_period;
8963
8964 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8965 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8966 if (rt_runtime_us < 0)
8967 rt_runtime = RUNTIME_INF;
8968
8969 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8970}
8971
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008972long sched_group_rt_runtime(struct task_group *tg)
8973{
8974 u64 rt_runtime_us;
8975
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008976 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008977 return -1;
8978
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008979 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008980 do_div(rt_runtime_us, NSEC_PER_USEC);
8981 return rt_runtime_us;
8982}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008983
8984int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8985{
8986 u64 rt_runtime, rt_period;
8987
8988 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8989 rt_runtime = tg->rt_bandwidth.rt_runtime;
8990
8991 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8992}
8993
8994long sched_group_rt_period(struct task_group *tg)
8995{
8996 u64 rt_period_us;
8997
8998 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8999 do_div(rt_period_us, NSEC_PER_USEC);
9000 return rt_period_us;
9001}
9002
9003static int sched_rt_global_constraints(void)
9004{
9005 int ret = 0;
9006
9007 mutex_lock(&rt_constraints_mutex);
9008 if (!__rt_schedulable(NULL, 1, 0))
9009 ret = -EINVAL;
9010 mutex_unlock(&rt_constraints_mutex);
9011
9012 return ret;
9013}
9014#else
9015static int sched_rt_global_constraints(void)
9016{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009017 unsigned long flags;
9018 int i;
9019
9020 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9021 for_each_possible_cpu(i) {
9022 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9023
9024 spin_lock(&rt_rq->rt_runtime_lock);
9025 rt_rq->rt_runtime = global_rt_runtime();
9026 spin_unlock(&rt_rq->rt_runtime_lock);
9027 }
9028 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9029
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009030 return 0;
9031}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009032#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009033
9034int sched_rt_handler(struct ctl_table *table, int write,
9035 struct file *filp, void __user *buffer, size_t *lenp,
9036 loff_t *ppos)
9037{
9038 int ret;
9039 int old_period, old_runtime;
9040 static DEFINE_MUTEX(mutex);
9041
9042 mutex_lock(&mutex);
9043 old_period = sysctl_sched_rt_period;
9044 old_runtime = sysctl_sched_rt_runtime;
9045
9046 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9047
9048 if (!ret && write) {
9049 ret = sched_rt_global_constraints();
9050 if (ret) {
9051 sysctl_sched_rt_period = old_period;
9052 sysctl_sched_rt_runtime = old_runtime;
9053 } else {
9054 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9055 def_rt_bandwidth.rt_period =
9056 ns_to_ktime(global_rt_period());
9057 }
9058 }
9059 mutex_unlock(&mutex);
9060
9061 return ret;
9062}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009063
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009064#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009065
9066/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009067static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009068{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009069 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9070 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009071}
9072
9073static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009074cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009075{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009076 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009077
Paul Menage2b01dfe2007-10-24 18:23:50 +02009078 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009079 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009080 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009081 return &init_task_group.css;
9082 }
9083
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009084 parent = cgroup_tg(cgrp->parent);
9085 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009086 if (IS_ERR(tg))
9087 return ERR_PTR(-ENOMEM);
9088
9089 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009090 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009091
9092 return &tg->css;
9093}
9094
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009095static void
9096cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009097{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009098 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009099
9100 sched_destroy_group(tg);
9101}
9102
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009103static int
9104cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9105 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009106{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009107#ifdef CONFIG_RT_GROUP_SCHED
9108 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009109 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009110 return -EINVAL;
9111#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009112 /* We don't support RT-tasks being in separate groups */
9113 if (tsk->sched_class != &fair_sched_class)
9114 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009115#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009116
9117 return 0;
9118}
9119
9120static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009121cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009122 struct cgroup *old_cont, struct task_struct *tsk)
9123{
9124 sched_move_task(tsk);
9125}
9126
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009127#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009128static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009129 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009130{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009131 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009132}
9133
Paul Menagef4c753b2008-04-29 00:59:56 -07009134static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009135{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009136 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009137
9138 return (u64) tg->shares;
9139}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009140#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009141
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009142#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009143static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009144 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009145{
Paul Menage06ecb272008-04-29 01:00:06 -07009146 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009147}
9148
Paul Menage06ecb272008-04-29 01:00:06 -07009149static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009150{
Paul Menage06ecb272008-04-29 01:00:06 -07009151 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009152}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009153
9154static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9155 u64 rt_period_us)
9156{
9157 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9158}
9159
9160static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9161{
9162 return sched_group_rt_period(cgroup_tg(cgrp));
9163}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009164#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009165
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009166static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009167#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009168 {
9169 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009170 .read_u64 = cpu_shares_read_u64,
9171 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009172 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009173#endif
9174#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009175 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009176 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009177 .read_s64 = cpu_rt_runtime_read,
9178 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009179 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009180 {
9181 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009182 .read_u64 = cpu_rt_period_read_uint,
9183 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009184 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009185#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009186};
9187
9188static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9189{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009190 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009191}
9192
9193struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009194 .name = "cpu",
9195 .create = cpu_cgroup_create,
9196 .destroy = cpu_cgroup_destroy,
9197 .can_attach = cpu_cgroup_can_attach,
9198 .attach = cpu_cgroup_attach,
9199 .populate = cpu_cgroup_populate,
9200 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009201 .early_init = 1,
9202};
9203
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009204#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009205
9206#ifdef CONFIG_CGROUP_CPUACCT
9207
9208/*
9209 * CPU accounting code for task groups.
9210 *
9211 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9212 * (balbir@in.ibm.com).
9213 */
9214
9215/* track cpu usage of a group of tasks */
9216struct cpuacct {
9217 struct cgroup_subsys_state css;
9218 /* cpuusage holds pointer to a u64-type object on every cpu */
9219 u64 *cpuusage;
9220};
9221
9222struct cgroup_subsys cpuacct_subsys;
9223
9224/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309225static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009226{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309227 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009228 struct cpuacct, css);
9229}
9230
9231/* return cpu accounting group to which this task belongs */
9232static inline struct cpuacct *task_ca(struct task_struct *tsk)
9233{
9234 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9235 struct cpuacct, css);
9236}
9237
9238/* create a new cpu accounting group */
9239static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309240 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009241{
9242 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9243
9244 if (!ca)
9245 return ERR_PTR(-ENOMEM);
9246
9247 ca->cpuusage = alloc_percpu(u64);
9248 if (!ca->cpuusage) {
9249 kfree(ca);
9250 return ERR_PTR(-ENOMEM);
9251 }
9252
9253 return &ca->css;
9254}
9255
9256/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009257static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309258cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009259{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309260 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009261
9262 free_percpu(ca->cpuusage);
9263 kfree(ca);
9264}
9265
9266/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309267static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009268{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309269 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009270 u64 totalcpuusage = 0;
9271 int i;
9272
9273 for_each_possible_cpu(i) {
9274 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9275
9276 /*
9277 * Take rq->lock to make 64-bit addition safe on 32-bit
9278 * platforms.
9279 */
9280 spin_lock_irq(&cpu_rq(i)->lock);
9281 totalcpuusage += *cpuusage;
9282 spin_unlock_irq(&cpu_rq(i)->lock);
9283 }
9284
9285 return totalcpuusage;
9286}
9287
Dhaval Giani0297b802008-02-29 10:02:44 +05309288static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9289 u64 reset)
9290{
9291 struct cpuacct *ca = cgroup_ca(cgrp);
9292 int err = 0;
9293 int i;
9294
9295 if (reset) {
9296 err = -EINVAL;
9297 goto out;
9298 }
9299
9300 for_each_possible_cpu(i) {
9301 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9302
9303 spin_lock_irq(&cpu_rq(i)->lock);
9304 *cpuusage = 0;
9305 spin_unlock_irq(&cpu_rq(i)->lock);
9306 }
9307out:
9308 return err;
9309}
9310
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009311static struct cftype files[] = {
9312 {
9313 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009314 .read_u64 = cpuusage_read,
9315 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009316 },
9317};
9318
Dhaval Giani32cd7562008-02-29 10:02:43 +05309319static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009320{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309321 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009322}
9323
9324/*
9325 * charge this task's execution time to its accounting group.
9326 *
9327 * called with rq->lock held.
9328 */
9329static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9330{
9331 struct cpuacct *ca;
9332
9333 if (!cpuacct_subsys.active)
9334 return;
9335
9336 ca = task_ca(tsk);
9337 if (ca) {
9338 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9339
9340 *cpuusage += cputime;
9341 }
9342}
9343
9344struct cgroup_subsys cpuacct_subsys = {
9345 .name = "cpuacct",
9346 .create = cpuacct_create,
9347 .destroy = cpuacct_destroy,
9348 .populate = cpuacct_populate,
9349 .subsys_id = cpuacct_subsys_id,
9350};
9351#endif /* CONFIG_CGROUP_CPUACCT */