blob: ae1a3e936d28ca97d4dda8a4f9d54c4524d20e5d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Dumazet5517d862007-05-08 00:32:57 -070072#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020073#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080076 * Scheduler clock - returns current time in nanosec units.
77 * This is default implementation.
78 * Architectures and sub-architectures can override this.
79 */
80unsigned long long __attribute__((weak)) sched_clock(void)
81{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010082 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080083}
84
85/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Convert user-nice values [ -20 ... 0 ... 19 ]
87 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
88 * and back.
89 */
90#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
91#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
92#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
93
94/*
95 * 'User priority' is the nice value converted to something we
96 * can work with better when scaling various scheduler parameters,
97 * it's a [ 0 ... 39 ] range.
98 */
99#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
100#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
101#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
102
103/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100104 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100106#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200108#define NICE_0_LOAD SCHED_LOAD_SCALE
109#define NICE_0_SHIFT SCHED_LOAD_SHIFT
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * These are the 'tuning knobs' of the scheduler:
113 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200114 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * Timeslices get refilled after they expire.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700118
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200119/*
120 * single value that denotes runtime == period, ie unlimited time.
121 */
122#define RUNTIME_INF ((u64)~0ULL)
123
Eric Dumazet5517d862007-05-08 00:32:57 -0700124#ifdef CONFIG_SMP
125/*
126 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
127 * Since cpu_power is a 'constant', we can use a reciprocal divide.
128 */
129static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
130{
131 return reciprocal_divide(load, sg->reciprocal_cpu_power);
132}
133
134/*
135 * Each time a sched group cpu_power is changed,
136 * we must compute its reciprocal value
137 */
138static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
139{
140 sg->__cpu_power += val;
141 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
142}
143#endif
144
Ingo Molnare05606d2007-07-09 18:51:59 +0200145static inline int rt_policy(int policy)
146{
147 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
148 return 1;
149 return 0;
150}
151
152static inline int task_has_rt_policy(struct task_struct *p)
153{
154 return rt_policy(p->policy);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200158 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200160struct rt_prio_array {
161 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
162 struct list_head queue[MAX_RT_PRIO];
163};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200165struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100166 /* nests inside the rq lock: */
167 spinlock_t rt_runtime_lock;
168 ktime_t rt_period;
169 u64 rt_runtime;
170 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200171};
172
173static struct rt_bandwidth def_rt_bandwidth;
174
175static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
176
177static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
178{
179 struct rt_bandwidth *rt_b =
180 container_of(timer, struct rt_bandwidth, rt_period_timer);
181 ktime_t now;
182 int overrun;
183 int idle = 0;
184
185 for (;;) {
186 now = hrtimer_cb_get_time(timer);
187 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
188
189 if (!overrun)
190 break;
191
192 idle = do_sched_rt_period_timer(rt_b, overrun);
193 }
194
195 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
196}
197
198static
199void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
200{
201 rt_b->rt_period = ns_to_ktime(period);
202 rt_b->rt_runtime = runtime;
203
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200204 spin_lock_init(&rt_b->rt_runtime_lock);
205
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200206 hrtimer_init(&rt_b->rt_period_timer,
207 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
208 rt_b->rt_period_timer.function = sched_rt_period_timer;
209 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
210}
211
212static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
213{
214 ktime_t now;
215
216 if (rt_b->rt_runtime == RUNTIME_INF)
217 return;
218
219 if (hrtimer_active(&rt_b->rt_period_timer))
220 return;
221
222 spin_lock(&rt_b->rt_runtime_lock);
223 for (;;) {
224 if (hrtimer_active(&rt_b->rt_period_timer))
225 break;
226
227 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
228 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
229 hrtimer_start(&rt_b->rt_period_timer,
230 rt_b->rt_period_timer.expires,
231 HRTIMER_MODE_ABS);
232 }
233 spin_unlock(&rt_b->rt_runtime_lock);
234}
235
236#ifdef CONFIG_RT_GROUP_SCHED
237static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
238{
239 hrtimer_cancel(&rt_b->rt_period_timer);
240}
241#endif
242
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100243#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200244
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700245#include <linux/cgroup.h>
246
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200247struct cfs_rq;
248
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100249static LIST_HEAD(task_groups);
250
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200252struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100253#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700254 struct cgroup_subsys_state css;
255#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100256
257#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200258 /* schedulable entities of this group on each cpu */
259 struct sched_entity **se;
260 /* runqueue "owned" by this group on each cpu */
261 struct cfs_rq **cfs_rq;
262 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100263#endif
264
265#ifdef CONFIG_RT_GROUP_SCHED
266 struct sched_rt_entity **rt_se;
267 struct rt_rq **rt_rq;
268
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200269 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100271
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100272 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100273 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200274
275 struct task_group *parent;
276 struct list_head siblings;
277 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200278};
279
Dhaval Giani354d60c2008-04-19 19:44:59 +0200280#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200281
282/*
283 * Root task group.
284 * Every UID task group (including init_task_group aka UID-0) will
285 * be a child to this group.
286 */
287struct task_group root_task_group;
288
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290/* Default task group's sched entity on each cpu */
291static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
292/* Default task group's cfs_rq on each cpu */
293static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100294#endif
295
296#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100297static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
298static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100299#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200300#else
301#define root_task_group init_task_group
Dhaval Giani354d60c2008-04-19 19:44:59 +0200302#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100303
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100304/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100305 * a task group's cpu shares.
306 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100307static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100308
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100309/* doms_cur_mutex serializes access to doms_cur[] array */
310static DEFINE_MUTEX(doms_cur_mutex);
311
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100312#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100313#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100314# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200315#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100316# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200317#endif
318
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200319#define MIN_SHARES 2
320
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100321static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100322#endif
323
324/* Default task group.
325 * Every task in system belong to this group at bootup.
326 */
Mike Travis434d53b2008-04-04 18:11:04 -0700327struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200328
329/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200330static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200331{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200332 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200333
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100334#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200335 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100336#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700337 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
338 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200339#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100340 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200342 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200343}
344
345/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100346static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200347{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100348#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100349 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
350 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100351#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100352
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100353#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100354 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
355 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100356#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200357}
358
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100359static inline void lock_doms_cur(void)
360{
361 mutex_lock(&doms_cur_mutex);
362}
363
364static inline void unlock_doms_cur(void)
365{
366 mutex_unlock(&doms_cur_mutex);
367}
368
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200369#else
370
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100371static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100372static inline void lock_doms_cur(void) { }
373static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200374
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100375#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200376
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200377/* CFS-related fields in a runqueue */
378struct cfs_rq {
379 struct load_weight load;
380 unsigned long nr_running;
381
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200382 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200383 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200384
385 struct rb_root tasks_timeline;
386 struct rb_node *rb_leftmost;
387 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200388 /* 'curr' points to currently running entity on this cfs_rq.
389 * It is set to NULL otherwise (i.e when none are currently running).
390 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100391 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200392
393 unsigned long nr_spread_over;
394
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200395#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200396 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
397
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100398 /*
399 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200400 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
401 * (like users, containers etc.)
402 *
403 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
404 * list is used during load balance.
405 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100406 struct list_head leaf_cfs_rq_list;
407 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200408
409#ifdef CONFIG_SMP
410 unsigned long task_weight;
411 unsigned long shares;
412 /*
413 * We need space to build a sched_domain wide view of the full task
414 * group tree, in order to avoid depending on dynamic memory allocation
415 * during the load balancing we place this in the per cpu task group
416 * hierarchy. This limits the load balancing to one instance per cpu,
417 * but more should not be needed anyway.
418 */
419 struct aggregate_struct {
420 /*
421 * load = weight(cpus) * f(tg)
422 *
423 * Where f(tg) is the recursive weight fraction assigned to
424 * this group.
425 */
426 unsigned long load;
427
428 /*
429 * part of the group weight distributed to this span.
430 */
431 unsigned long shares;
432
433 /*
434 * The sum of all runqueue weights within this span.
435 */
436 unsigned long rq_weight;
437
438 /*
439 * Weight contributed by tasks; this is the part we can
440 * influence by moving tasks around.
441 */
442 unsigned long task_weight;
443 } aggregate;
444#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200445#endif
446};
447
448/* Real-Time classes' related field in a runqueue: */
449struct rt_rq {
450 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100451 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100452#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100453 int highest_prio; /* highest queued rt task prio */
454#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100455#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100456 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100457 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100458#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100459 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100460 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200461 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100462 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200463 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100464
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100465#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100466 unsigned long rt_nr_boosted;
467
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100468 struct rq *rq;
469 struct list_head leaf_rt_rq_list;
470 struct task_group *tg;
471 struct sched_rt_entity *rt_se;
472#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200473};
474
Gregory Haskins57d885f2008-01-25 21:08:18 +0100475#ifdef CONFIG_SMP
476
477/*
478 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100479 * variables. Each exclusive cpuset essentially defines an island domain by
480 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100481 * exclusive cpuset is created, we also create and attach a new root-domain
482 * object.
483 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100484 */
485struct root_domain {
486 atomic_t refcount;
487 cpumask_t span;
488 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100489
Ingo Molnar0eab9142008-01-25 21:08:19 +0100490 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100491 * The "RT overload" flag: it gets set if a CPU has more than
492 * one runnable RT task.
493 */
494 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100495 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100496};
497
Gregory Haskinsdc938522008-01-25 21:08:26 +0100498/*
499 * By default the system creates a single root-domain with all cpus as
500 * members (mimicking the global state we have today).
501 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100502static struct root_domain def_root_domain;
503
504#endif
505
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200506/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * This is the main, per-CPU runqueue data structure.
508 *
509 * Locking rule: those places that want to lock multiple runqueues
510 * (such as the load balancing or the thread migration code), lock
511 * acquire operations must be ordered by ascending &runqueue.
512 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700513struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200514 /* runqueue lock: */
515 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * nr_running and cpu_load should be in the same cacheline because
519 * remote CPUs use both these fields when doing load calculation.
520 */
521 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200522 #define CPU_LOAD_IDX_MAX 5
523 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700524 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700525#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200526 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700527 unsigned char in_nohz_recently;
528#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200529 /* capture load from *all* tasks on this cpu: */
530 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200531 unsigned long nr_load_updates;
532 u64 nr_switches;
533
534 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100535 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100536
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200538 /* list of leaf cfs_rq on this cpu: */
539 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100540#endif
541#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100542 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * This is part of a global counter where only the total sum
547 * over all CPUs matters. A task can increase this counter on
548 * one CPU and if it got migrated afterwards it may decrease
549 * it on another CPU. Always updated under the runqueue lock:
550 */
551 unsigned long nr_uninterruptible;
552
Ingo Molnar36c8b582006-07-03 00:25:41 -0700553 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800554 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200556
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200557 u64 clock, prev_clock_raw;
558 s64 clock_max_delta;
559
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100560 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200561 u64 idle_clock;
562 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200563 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 atomic_t nr_iowait;
566
567#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100568 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct sched_domain *sd;
570
571 /* For active balancing */
572 int active_balance;
573 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200574 /* cpu of this runqueue: */
575 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Ingo Molnar36c8b582006-07-03 00:25:41 -0700577 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct list_head migration_queue;
579#endif
580
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100581#ifdef CONFIG_SCHED_HRTICK
582 unsigned long hrtick_flags;
583 ktime_t hrtick_expire;
584 struct hrtimer hrtick_timer;
585#endif
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#ifdef CONFIG_SCHEDSTATS
588 /* latency stats */
589 struct sched_info rq_sched_info;
590
591 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200592 unsigned int yld_exp_empty;
593 unsigned int yld_act_empty;
594 unsigned int yld_both_empty;
595 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200598 unsigned int sched_switch;
599 unsigned int sched_count;
600 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200603 unsigned int ttwu_count;
604 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200605
606 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200607 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700609 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610};
611
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700612static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Ingo Molnardd41f592007-07-09 18:51:59 +0200614static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
615{
616 rq->curr->sched_class->check_preempt_curr(rq, p);
617}
618
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700619static inline int cpu_of(struct rq *rq)
620{
621#ifdef CONFIG_SMP
622 return rq->cpu;
623#else
624 return 0;
625#endif
626}
627
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200628#ifdef CONFIG_NO_HZ
629static inline bool nohz_on(int cpu)
630{
631 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
632}
633
634static inline u64 max_skipped_ticks(struct rq *rq)
635{
636 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
637}
638
639static inline void update_last_tick_seen(struct rq *rq)
640{
641 rq->last_tick_seen = jiffies;
642}
643#else
644static inline u64 max_skipped_ticks(struct rq *rq)
645{
646 return 1;
647}
648
649static inline void update_last_tick_seen(struct rq *rq)
650{
651}
652#endif
653
Nick Piggin674311d2005-06-25 14:57:27 -0700654/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200655 * Update the per-runqueue clock, as finegrained as the platform can give
656 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200657 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200658static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200659{
660 u64 prev_raw = rq->prev_clock_raw;
661 u64 now = sched_clock();
662 s64 delta = now - prev_raw;
663 u64 clock = rq->clock;
664
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200665#ifdef CONFIG_SCHED_DEBUG
666 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
667#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200668 /*
669 * Protect against sched_clock() occasionally going backwards:
670 */
671 if (unlikely(delta < 0)) {
672 clock++;
673 rq->clock_warps++;
674 } else {
675 /*
676 * Catch too large forward jumps too:
677 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200678 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
679 u64 max_time = rq->tick_timestamp + max_jump;
680
681 if (unlikely(clock + delta > max_time)) {
682 if (clock < max_time)
683 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200684 else
685 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200686 rq->clock_overflows++;
687 } else {
688 if (unlikely(delta > rq->clock_max_delta))
689 rq->clock_max_delta = delta;
690 clock += delta;
691 }
692 }
693
694 rq->prev_clock_raw = now;
695 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200696}
697
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200698static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200699{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200700 if (likely(smp_processor_id() == cpu_of(rq)))
701 __update_rq_clock(rq);
702}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200703
Ingo Molnar20d315d2007-07-09 18:51:58 +0200704/*
Nick Piggin674311d2005-06-25 14:57:27 -0700705 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700706 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700707 *
708 * The domain tree of any CPU may only be accessed from within
709 * preempt-disabled sections.
710 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700711#define for_each_domain(cpu, __sd) \
712 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
715#define this_rq() (&__get_cpu_var(runqueues))
716#define task_rq(p) cpu_rq(task_cpu(p))
717#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
718
Ingo Molnare436d802007-07-19 21:28:35 +0200719/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200720 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
721 */
722#ifdef CONFIG_SCHED_DEBUG
723# define const_debug __read_mostly
724#else
725# define const_debug static const
726#endif
727
728/*
729 * Debugging: various feature bits
730 */
731enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200732 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100733 SCHED_FEAT_WAKEUP_PREEMPT = 2,
734 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100735 SCHED_FEAT_AFFINE_WAKEUPS = 8,
736 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100737 SCHED_FEAT_SYNC_WAKEUPS = 32,
738 SCHED_FEAT_HRTICK = 64,
739 SCHED_FEAT_DOUBLE_TICK = 128,
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100740 SCHED_FEAT_NORMALIZED_SLEEPER = 256,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200741};
742
743const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200744 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100745 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200746 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100747 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
748 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100749 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100750 SCHED_FEAT_HRTICK * 1 |
Peter Zijlstra112f53f2008-03-19 11:43:36 +0100751 SCHED_FEAT_DOUBLE_TICK * 0 |
752 SCHED_FEAT_NORMALIZED_SLEEPER * 1;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200753
754#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
755
756/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100757 * Number of tasks to iterate in a single balance run.
758 * Limited because this is done with IRQs disabled.
759 */
760const_debug unsigned int sysctl_sched_nr_migrate = 32;
761
762/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100763 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100764 * default: 1s
765 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100766unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100767
Ingo Molnar6892b752008-02-13 14:02:36 +0100768static __read_mostly int scheduler_running;
769
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100770/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100771 * part of the period that we allow rt tasks to run in us.
772 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100773 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100774int sysctl_sched_rt_runtime = 950000;
775
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200776static inline u64 global_rt_period(void)
777{
778 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
779}
780
781static inline u64 global_rt_runtime(void)
782{
783 if (sysctl_sched_rt_period < 0)
784 return RUNTIME_INF;
785
786 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
787}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100788
Ingo Molnar27ec4402008-02-28 21:00:21 +0100789static const unsigned long long time_sync_thresh = 100000;
790
791static DEFINE_PER_CPU(unsigned long long, time_offset);
792static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
793
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100794/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100795 * Global lock which we take every now and then to synchronize
796 * the CPUs time. This method is not warp-safe, but it's good
797 * enough to synchronize slowly diverging time sources and thus
798 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200799 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100800static DEFINE_SPINLOCK(time_sync_lock);
801static unsigned long long prev_global_time;
802
803static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
804{
805 unsigned long flags;
806
807 spin_lock_irqsave(&time_sync_lock, flags);
808
809 if (time < prev_global_time) {
810 per_cpu(time_offset, cpu) += prev_global_time - time;
811 time = prev_global_time;
812 } else {
813 prev_global_time = time;
814 }
815
816 spin_unlock_irqrestore(&time_sync_lock, flags);
817
818 return time;
819}
820
821static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200822{
Ingo Molnare436d802007-07-19 21:28:35 +0200823 unsigned long long now;
824 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200825 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200826
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100827 /*
828 * Only call sched_clock() if the scheduler has already been
829 * initialized (some code might call cpu_clock() very early):
830 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100831 if (unlikely(!scheduler_running))
832 return 0;
833
834 local_irq_save(flags);
835 rq = cpu_rq(cpu);
836 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200837 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200838 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200839
840 return now;
841}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100842
843/*
844 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
845 * clock constructed from sched_clock():
846 */
847unsigned long long cpu_clock(int cpu)
848{
849 unsigned long long prev_cpu_time, time, delta_time;
850
851 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
852 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
853 delta_time = time-prev_cpu_time;
854
855 if (unlikely(delta_time > time_sync_thresh))
856 time = __sync_cpu_clock(time, cpu);
857
858 return time;
859}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200860EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700863# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700865#ifndef finish_arch_switch
866# define finish_arch_switch(prev) do { } while (0)
867#endif
868
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100869static inline int task_current(struct rq *rq, struct task_struct *p)
870{
871 return rq->curr == p;
872}
873
Nick Piggin4866cde2005-06-25 14:57:23 -0700874#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700875static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700876{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100877 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700878}
879
Ingo Molnar70b97a72006-07-03 00:25:42 -0700880static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700881{
882}
883
Ingo Molnar70b97a72006-07-03 00:25:42 -0700884static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700885{
Ingo Molnarda04c032005-09-13 11:17:59 +0200886#ifdef CONFIG_DEBUG_SPINLOCK
887 /* this is a valid case when another task releases the spinlock */
888 rq->lock.owner = current;
889#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700890 /*
891 * If we are tracking spinlock dependencies then we have to
892 * fix up the runqueue lock - which gets 'carried over' from
893 * prev into current:
894 */
895 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
896
Nick Piggin4866cde2005-06-25 14:57:23 -0700897 spin_unlock_irq(&rq->lock);
898}
899
900#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700901static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700902{
903#ifdef CONFIG_SMP
904 return p->oncpu;
905#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100906 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700907#endif
908}
909
Ingo Molnar70b97a72006-07-03 00:25:42 -0700910static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700911{
912#ifdef CONFIG_SMP
913 /*
914 * We can optimise this out completely for !SMP, because the
915 * SMP rebalancing from interrupt is the only thing that cares
916 * here.
917 */
918 next->oncpu = 1;
919#endif
920#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
921 spin_unlock_irq(&rq->lock);
922#else
923 spin_unlock(&rq->lock);
924#endif
925}
926
Ingo Molnar70b97a72006-07-03 00:25:42 -0700927static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700928{
929#ifdef CONFIG_SMP
930 /*
931 * After ->oncpu is cleared, the task can be moved to a different CPU.
932 * We must ensure this doesn't happen until the switch is completely
933 * finished.
934 */
935 smp_wmb();
936 prev->oncpu = 0;
937#endif
938#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
939 local_irq_enable();
940#endif
941}
942#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700945 * __task_rq_lock - lock the runqueue a given task resides on.
946 * Must be called interrupts disabled.
947 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700948static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700949 __acquires(rq->lock)
950{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200951 for (;;) {
952 struct rq *rq = task_rq(p);
953 spin_lock(&rq->lock);
954 if (likely(rq == task_rq(p)))
955 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700956 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700957 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700958}
959
960/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100962 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * explicitly disabling preemption.
964 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700965static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 __acquires(rq->lock)
967{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700968 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Andi Kleen3a5c3592007-10-15 17:00:14 +0200970 for (;;) {
971 local_irq_save(*flags);
972 rq = task_rq(p);
973 spin_lock(&rq->lock);
974 if (likely(rq == task_rq(p)))
975 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Alexey Dobriyana9957442007-10-15 17:00:13 +0200980static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700981 __releases(rq->lock)
982{
983 spin_unlock(&rq->lock);
984}
985
Ingo Molnar70b97a72006-07-03 00:25:42 -0700986static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 __releases(rq->lock)
988{
989 spin_unlock_irqrestore(&rq->lock, *flags);
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800993 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200995static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 __acquires(rq->lock)
997{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700998 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 local_irq_disable();
1001 rq = this_rq();
1002 spin_lock(&rq->lock);
1003
1004 return rq;
1005}
1006
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001007/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001008 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001009 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001010void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001011{
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001012 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001013
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001014 spin_lock(&rq->lock);
1015 __update_rq_clock(rq);
1016 spin_unlock(&rq->lock);
1017 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001018}
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001019EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
1020
1021/*
1022 * We just idled delta nanoseconds (called with irqs disabled):
1023 */
1024void sched_clock_idle_wakeup_event(u64 delta_ns)
1025{
1026 struct rq *rq = cpu_rq(smp_processor_id());
1027 u64 now = sched_clock();
1028
1029 rq->idle_clock += delta_ns;
1030 /*
1031 * Override the previous timestamp and ignore all
1032 * sched_clock() deltas that occured while we idled,
1033 * and use the PM-provided delta_ns to advance the
1034 * rq clock:
1035 */
1036 spin_lock(&rq->lock);
1037 rq->prev_clock_raw = now;
1038 rq->clock += delta_ns;
1039 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +01001040 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +02001041}
1042EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001043
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001044static void __resched_task(struct task_struct *p, int tif_bit);
1045
1046static inline void resched_task(struct task_struct *p)
1047{
1048 __resched_task(p, TIF_NEED_RESCHED);
1049}
1050
1051#ifdef CONFIG_SCHED_HRTICK
1052/*
1053 * Use HR-timers to deliver accurate preemption points.
1054 *
1055 * Its all a bit involved since we cannot program an hrt while holding the
1056 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1057 * reschedule event.
1058 *
1059 * When we get rescheduled we reprogram the hrtick_timer outside of the
1060 * rq->lock.
1061 */
1062static inline void resched_hrt(struct task_struct *p)
1063{
1064 __resched_task(p, TIF_HRTICK_RESCHED);
1065}
1066
1067static inline void resched_rq(struct rq *rq)
1068{
1069 unsigned long flags;
1070
1071 spin_lock_irqsave(&rq->lock, flags);
1072 resched_task(rq->curr);
1073 spin_unlock_irqrestore(&rq->lock, flags);
1074}
1075
1076enum {
1077 HRTICK_SET, /* re-programm hrtick_timer */
1078 HRTICK_RESET, /* not a new slice */
1079};
1080
1081/*
1082 * Use hrtick when:
1083 * - enabled by features
1084 * - hrtimer is actually high res
1085 */
1086static inline int hrtick_enabled(struct rq *rq)
1087{
1088 if (!sched_feat(HRTICK))
1089 return 0;
1090 return hrtimer_is_hres_active(&rq->hrtick_timer);
1091}
1092
1093/*
1094 * Called to set the hrtick timer state.
1095 *
1096 * called with rq->lock held and irqs disabled
1097 */
1098static void hrtick_start(struct rq *rq, u64 delay, int reset)
1099{
1100 assert_spin_locked(&rq->lock);
1101
1102 /*
1103 * preempt at: now + delay
1104 */
1105 rq->hrtick_expire =
1106 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1107 /*
1108 * indicate we need to program the timer
1109 */
1110 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1111 if (reset)
1112 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1113
1114 /*
1115 * New slices are called from the schedule path and don't need a
1116 * forced reschedule.
1117 */
1118 if (reset)
1119 resched_hrt(rq->curr);
1120}
1121
1122static void hrtick_clear(struct rq *rq)
1123{
1124 if (hrtimer_active(&rq->hrtick_timer))
1125 hrtimer_cancel(&rq->hrtick_timer);
1126}
1127
1128/*
1129 * Update the timer from the possible pending state.
1130 */
1131static void hrtick_set(struct rq *rq)
1132{
1133 ktime_t time;
1134 int set, reset;
1135 unsigned long flags;
1136
1137 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1138
1139 spin_lock_irqsave(&rq->lock, flags);
1140 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1141 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1142 time = rq->hrtick_expire;
1143 clear_thread_flag(TIF_HRTICK_RESCHED);
1144 spin_unlock_irqrestore(&rq->lock, flags);
1145
1146 if (set) {
1147 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1148 if (reset && !hrtimer_active(&rq->hrtick_timer))
1149 resched_rq(rq);
1150 } else
1151 hrtick_clear(rq);
1152}
1153
1154/*
1155 * High-resolution timer tick.
1156 * Runs from hardirq context with interrupts disabled.
1157 */
1158static enum hrtimer_restart hrtick(struct hrtimer *timer)
1159{
1160 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1161
1162 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1163
1164 spin_lock(&rq->lock);
1165 __update_rq_clock(rq);
1166 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1167 spin_unlock(&rq->lock);
1168
1169 return HRTIMER_NORESTART;
1170}
1171
1172static inline void init_rq_hrtick(struct rq *rq)
1173{
1174 rq->hrtick_flags = 0;
1175 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1176 rq->hrtick_timer.function = hrtick;
1177 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1178}
1179
1180void hrtick_resched(void)
1181{
1182 struct rq *rq;
1183 unsigned long flags;
1184
1185 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1186 return;
1187
1188 local_irq_save(flags);
1189 rq = cpu_rq(smp_processor_id());
1190 hrtick_set(rq);
1191 local_irq_restore(flags);
1192}
1193#else
1194static inline void hrtick_clear(struct rq *rq)
1195{
1196}
1197
1198static inline void hrtick_set(struct rq *rq)
1199{
1200}
1201
1202static inline void init_rq_hrtick(struct rq *rq)
1203{
1204}
1205
1206void hrtick_resched(void)
1207{
1208}
1209#endif
1210
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001211/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001212 * resched_task - mark a task 'to be rescheduled now'.
1213 *
1214 * On UP this means the setting of the need_resched flag, on SMP it
1215 * might also involve a cross-CPU call to trigger the scheduler on
1216 * the target CPU.
1217 */
1218#ifdef CONFIG_SMP
1219
1220#ifndef tsk_is_polling
1221#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1222#endif
1223
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001224static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001225{
1226 int cpu;
1227
1228 assert_spin_locked(&task_rq(p)->lock);
1229
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001230 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001231 return;
1232
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001233 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001234
1235 cpu = task_cpu(p);
1236 if (cpu == smp_processor_id())
1237 return;
1238
1239 /* NEED_RESCHED must be visible before we test polling */
1240 smp_mb();
1241 if (!tsk_is_polling(p))
1242 smp_send_reschedule(cpu);
1243}
1244
1245static void resched_cpu(int cpu)
1246{
1247 struct rq *rq = cpu_rq(cpu);
1248 unsigned long flags;
1249
1250 if (!spin_trylock_irqsave(&rq->lock, flags))
1251 return;
1252 resched_task(cpu_curr(cpu));
1253 spin_unlock_irqrestore(&rq->lock, flags);
1254}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001255
1256#ifdef CONFIG_NO_HZ
1257/*
1258 * When add_timer_on() enqueues a timer into the timer wheel of an
1259 * idle CPU then this timer might expire before the next timer event
1260 * which is scheduled to wake up that CPU. In case of a completely
1261 * idle system the next event might even be infinite time into the
1262 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1263 * leaves the inner idle loop so the newly added timer is taken into
1264 * account when the CPU goes back to idle and evaluates the timer
1265 * wheel for the next timer event.
1266 */
1267void wake_up_idle_cpu(int cpu)
1268{
1269 struct rq *rq = cpu_rq(cpu);
1270
1271 if (cpu == smp_processor_id())
1272 return;
1273
1274 /*
1275 * This is safe, as this function is called with the timer
1276 * wheel base lock of (cpu) held. When the CPU is on the way
1277 * to idle and has not yet set rq->curr to idle then it will
1278 * be serialized on the timer wheel base lock and take the new
1279 * timer into account automatically.
1280 */
1281 if (rq->curr != rq->idle)
1282 return;
1283
1284 /*
1285 * We can set TIF_RESCHED on the idle task of the other CPU
1286 * lockless. The worst case is that the other CPU runs the
1287 * idle task through an additional NOOP schedule()
1288 */
1289 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1290
1291 /* NEED_RESCHED must be visible before we test polling */
1292 smp_mb();
1293 if (!tsk_is_polling(rq->idle))
1294 smp_send_reschedule(cpu);
1295}
1296#endif
1297
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001298#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001299static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001300{
1301 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001302 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001303}
1304#endif
1305
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001306#if BITS_PER_LONG == 32
1307# define WMULT_CONST (~0UL)
1308#else
1309# define WMULT_CONST (1UL << 32)
1310#endif
1311
1312#define WMULT_SHIFT 32
1313
Ingo Molnar194081e2007-08-09 11:16:51 +02001314/*
1315 * Shift right and round:
1316 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001317#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001318
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001319static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001320calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1321 struct load_weight *lw)
1322{
1323 u64 tmp;
1324
1325 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001326 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001327
1328 tmp = (u64)delta_exec * weight;
1329 /*
1330 * Check whether we'd overflow the 64-bit multiplication:
1331 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001332 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001333 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001334 WMULT_SHIFT/2);
1335 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001336 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001337
Ingo Molnarecf691d2007-08-02 17:41:40 +02001338 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001339}
1340
1341static inline unsigned long
1342calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1343{
1344 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1345}
1346
Ingo Molnar10919852007-10-15 17:00:04 +02001347static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001348{
1349 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001350 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001351}
1352
Ingo Molnar10919852007-10-15 17:00:04 +02001353static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001354{
1355 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001356 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001360 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1361 * of tasks with abnormal "nice" values across CPUs the contribution that
1362 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001363 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001364 * scaled version of the new time slice allocation that they receive on time
1365 * slice expiry etc.
1366 */
1367
Ingo Molnardd41f592007-07-09 18:51:59 +02001368#define WEIGHT_IDLEPRIO 2
1369#define WMULT_IDLEPRIO (1 << 31)
1370
1371/*
1372 * Nice levels are multiplicative, with a gentle 10% change for every
1373 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1374 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1375 * that remained on nice 0.
1376 *
1377 * The "10% effect" is relative and cumulative: from _any_ nice level,
1378 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001379 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1380 * If a task goes up by ~10% and another task goes down by ~10% then
1381 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001382 */
1383static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001384 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1385 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1386 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1387 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1388 /* 0 */ 1024, 820, 655, 526, 423,
1389 /* 5 */ 335, 272, 215, 172, 137,
1390 /* 10 */ 110, 87, 70, 56, 45,
1391 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001392};
1393
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001394/*
1395 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1396 *
1397 * In cases where the weight does not change often, we can use the
1398 * precalculated inverse to speed up arithmetics by turning divisions
1399 * into multiplications:
1400 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001401static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001402 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1403 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1404 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1405 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1406 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1407 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1408 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1409 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001410};
Peter Williams2dd73a42006-06-27 02:54:34 -07001411
Ingo Molnardd41f592007-07-09 18:51:59 +02001412static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1413
1414/*
1415 * runqueue iterator, to support SMP load-balancing between different
1416 * scheduling classes, without having to expose their internal data
1417 * structures to the load-balancing proper:
1418 */
1419struct rq_iterator {
1420 void *arg;
1421 struct task_struct *(*start)(void *);
1422 struct task_struct *(*next)(void *);
1423};
1424
Peter Williamse1d14842007-10-24 18:23:51 +02001425#ifdef CONFIG_SMP
1426static unsigned long
1427balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1428 unsigned long max_load_move, struct sched_domain *sd,
1429 enum cpu_idle_type idle, int *all_pinned,
1430 int *this_best_prio, struct rq_iterator *iterator);
1431
1432static int
1433iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1434 struct sched_domain *sd, enum cpu_idle_type idle,
1435 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001436#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001437
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001438#ifdef CONFIG_CGROUP_CPUACCT
1439static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1440#else
1441static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1442#endif
1443
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001444static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1445{
1446 update_load_add(&rq->load, load);
1447}
1448
1449static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1450{
1451 update_load_sub(&rq->load, load);
1452}
1453
Gregory Haskinse7693a32008-01-25 21:08:09 +01001454#ifdef CONFIG_SMP
1455static unsigned long source_load(int cpu, int type);
1456static unsigned long target_load(int cpu, int type);
1457static unsigned long cpu_avg_load_per_task(int cpu);
1458static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001459
1460#ifdef CONFIG_FAIR_GROUP_SCHED
1461
1462/*
1463 * Group load balancing.
1464 *
1465 * We calculate a few balance domain wide aggregate numbers; load and weight.
1466 * Given the pictures below, and assuming each item has equal weight:
1467 *
1468 * root 1 - thread
1469 * / | \ A - group
1470 * A 1 B
1471 * /|\ / \
1472 * C 2 D 3 4
1473 * | |
1474 * 5 6
1475 *
1476 * load:
1477 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1478 * which equals 1/9-th of the total load.
1479 *
1480 * shares:
1481 * The weight of this group on the selected cpus.
1482 *
1483 * rq_weight:
1484 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1485 * B would get 2.
1486 *
1487 * task_weight:
1488 * Part of the rq_weight contributed by tasks; all groups except B would
1489 * get 1, B gets 2.
1490 */
1491
1492static inline struct aggregate_struct *
1493aggregate(struct task_group *tg, struct sched_domain *sd)
1494{
1495 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1496}
1497
1498typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1499
1500/*
1501 * Iterate the full tree, calling @down when first entering a node and @up when
1502 * leaving it for the final time.
1503 */
1504static
1505void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1506 struct sched_domain *sd)
1507{
1508 struct task_group *parent, *child;
1509
1510 rcu_read_lock();
1511 parent = &root_task_group;
1512down:
1513 (*down)(parent, sd);
1514 list_for_each_entry_rcu(child, &parent->children, siblings) {
1515 parent = child;
1516 goto down;
1517
1518up:
1519 continue;
1520 }
1521 (*up)(parent, sd);
1522
1523 child = parent;
1524 parent = parent->parent;
1525 if (parent)
1526 goto up;
1527 rcu_read_unlock();
1528}
1529
1530/*
1531 * Calculate the aggregate runqueue weight.
1532 */
1533static
1534void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1535{
1536 unsigned long rq_weight = 0;
1537 unsigned long task_weight = 0;
1538 int i;
1539
1540 for_each_cpu_mask(i, sd->span) {
1541 rq_weight += tg->cfs_rq[i]->load.weight;
1542 task_weight += tg->cfs_rq[i]->task_weight;
1543 }
1544
1545 aggregate(tg, sd)->rq_weight = rq_weight;
1546 aggregate(tg, sd)->task_weight = task_weight;
1547}
1548
1549/*
1550 * Redistribute tg->shares amongst all tg->cfs_rq[]s.
1551 */
1552static void __aggregate_redistribute_shares(struct task_group *tg)
1553{
1554 int i, max_cpu = smp_processor_id();
1555 unsigned long rq_weight = 0;
1556 unsigned long shares, max_shares = 0, shares_rem = tg->shares;
1557
1558 for_each_possible_cpu(i)
1559 rq_weight += tg->cfs_rq[i]->load.weight;
1560
1561 for_each_possible_cpu(i) {
1562 /*
1563 * divide shares proportional to the rq_weights.
1564 */
1565 shares = tg->shares * tg->cfs_rq[i]->load.weight;
1566 shares /= rq_weight + 1;
1567
1568 tg->cfs_rq[i]->shares = shares;
1569
1570 if (shares > max_shares) {
1571 max_shares = shares;
1572 max_cpu = i;
1573 }
1574 shares_rem -= shares;
1575 }
1576
1577 /*
1578 * Ensure it all adds up to tg->shares; we can loose a few
1579 * due to rounding down when computing the per-cpu shares.
1580 */
1581 if (shares_rem)
1582 tg->cfs_rq[max_cpu]->shares += shares_rem;
1583}
1584
1585/*
1586 * Compute the weight of this group on the given cpus.
1587 */
1588static
1589void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1590{
1591 unsigned long shares = 0;
1592 int i;
1593
1594again:
1595 for_each_cpu_mask(i, sd->span)
1596 shares += tg->cfs_rq[i]->shares;
1597
1598 /*
1599 * When the span doesn't have any shares assigned, but does have
1600 * tasks to run do a machine wide rebalance (should be rare).
1601 */
1602 if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) {
1603 __aggregate_redistribute_shares(tg);
1604 goto again;
1605 }
1606
1607 aggregate(tg, sd)->shares = shares;
1608}
1609
1610/*
1611 * Compute the load fraction assigned to this group, relies on the aggregate
1612 * weight and this group's parent's load, i.e. top-down.
1613 */
1614static
1615void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1616{
1617 unsigned long load;
1618
1619 if (!tg->parent) {
1620 int i;
1621
1622 load = 0;
1623 for_each_cpu_mask(i, sd->span)
1624 load += cpu_rq(i)->load.weight;
1625
1626 } else {
1627 load = aggregate(tg->parent, sd)->load;
1628
1629 /*
1630 * shares is our weight in the parent's rq so
1631 * shares/parent->rq_weight gives our fraction of the load
1632 */
1633 load *= aggregate(tg, sd)->shares;
1634 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1635 }
1636
1637 aggregate(tg, sd)->load = load;
1638}
1639
1640static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1641
1642/*
1643 * Calculate and set the cpu's group shares.
1644 */
1645static void
1646__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1647 int tcpu)
1648{
1649 int boost = 0;
1650 unsigned long shares;
1651 unsigned long rq_weight;
1652
1653 if (!tg->se[tcpu])
1654 return;
1655
1656 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1657
1658 /*
1659 * If there are currently no tasks on the cpu pretend there is one of
1660 * average load so that when a new task gets to run here it will not
1661 * get delayed by group starvation.
1662 */
1663 if (!rq_weight) {
1664 boost = 1;
1665 rq_weight = NICE_0_LOAD;
1666 }
1667
1668 /*
1669 * \Sum shares * rq_weight
1670 * shares = -----------------------
1671 * \Sum rq_weight
1672 *
1673 */
1674 shares = aggregate(tg, sd)->shares * rq_weight;
1675 shares /= aggregate(tg, sd)->rq_weight + 1;
1676
1677 /*
1678 * record the actual number of shares, not the boosted amount.
1679 */
1680 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1681
1682 if (shares < MIN_SHARES)
1683 shares = MIN_SHARES;
1684
1685 __set_se_shares(tg->se[tcpu], shares);
1686}
1687
1688/*
1689 * Re-adjust the weights on the cpu the task came from and on the cpu the
1690 * task went to.
1691 */
1692static void
1693__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1694 int scpu, int dcpu)
1695{
1696 unsigned long shares;
1697
1698 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1699
1700 __update_group_shares_cpu(tg, sd, scpu);
1701 __update_group_shares_cpu(tg, sd, dcpu);
1702
1703 /*
1704 * ensure we never loose shares due to rounding errors in the
1705 * above redistribution.
1706 */
1707 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1708 if (shares)
1709 tg->cfs_rq[dcpu]->shares += shares;
1710}
1711
1712/*
1713 * Because changing a group's shares changes the weight of the super-group
1714 * we need to walk up the tree and change all shares until we hit the root.
1715 */
1716static void
1717move_group_shares(struct task_group *tg, struct sched_domain *sd,
1718 int scpu, int dcpu)
1719{
1720 while (tg) {
1721 __move_group_shares(tg, sd, scpu, dcpu);
1722 tg = tg->parent;
1723 }
1724}
1725
1726static
1727void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1728{
1729 unsigned long shares = aggregate(tg, sd)->shares;
1730 int i;
1731
1732 for_each_cpu_mask(i, sd->span) {
1733 struct rq *rq = cpu_rq(i);
1734 unsigned long flags;
1735
1736 spin_lock_irqsave(&rq->lock, flags);
1737 __update_group_shares_cpu(tg, sd, i);
1738 spin_unlock_irqrestore(&rq->lock, flags);
1739 }
1740
1741 aggregate_group_shares(tg, sd);
1742
1743 /*
1744 * ensure we never loose shares due to rounding errors in the
1745 * above redistribution.
1746 */
1747 shares -= aggregate(tg, sd)->shares;
1748 if (shares) {
1749 tg->cfs_rq[sd->first_cpu]->shares += shares;
1750 aggregate(tg, sd)->shares += shares;
1751 }
1752}
1753
1754/*
1755 * Calculate the accumulative weight and recursive load of each task group
1756 * while walking down the tree.
1757 */
1758static
1759void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1760{
1761 aggregate_group_weight(tg, sd);
1762 aggregate_group_shares(tg, sd);
1763 aggregate_group_load(tg, sd);
1764}
1765
1766/*
1767 * Rebalance the cpu shares while walking back up the tree.
1768 */
1769static
1770void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1771{
1772 aggregate_group_set_shares(tg, sd);
1773}
1774
1775static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1776
1777static void __init init_aggregate(void)
1778{
1779 int i;
1780
1781 for_each_possible_cpu(i)
1782 spin_lock_init(&per_cpu(aggregate_lock, i));
1783}
1784
1785static int get_aggregate(struct sched_domain *sd)
1786{
1787 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1788 return 0;
1789
1790 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1791 return 1;
1792}
1793
1794static void put_aggregate(struct sched_domain *sd)
1795{
1796 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1797}
1798
1799static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1800{
1801 cfs_rq->shares = shares;
1802}
1803
1804#else
1805
1806static inline void init_aggregate(void)
1807{
1808}
1809
1810static inline int get_aggregate(struct sched_domain *sd)
1811{
1812 return 0;
1813}
1814
1815static inline void put_aggregate(struct sched_domain *sd)
1816{
1817}
1818#endif
1819
1820#else /* CONFIG_SMP */
1821
1822#ifdef CONFIG_FAIR_GROUP_SCHED
1823static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1824{
1825}
1826#endif
1827
Gregory Haskinse7693a32008-01-25 21:08:09 +01001828#endif /* CONFIG_SMP */
1829
Ingo Molnardd41f592007-07-09 18:51:59 +02001830#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001831#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001832#include "sched_fair.c"
1833#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001834#ifdef CONFIG_SCHED_DEBUG
1835# include "sched_debug.c"
1836#endif
1837
1838#define sched_class_highest (&rt_sched_class)
1839
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001840static void inc_nr_running(struct rq *rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001841{
1842 rq->nr_running++;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001843}
1844
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001845static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001846{
1847 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001848}
1849
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001850static void set_load_weight(struct task_struct *p)
1851{
1852 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001853 p->se.load.weight = prio_to_weight[0] * 2;
1854 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1855 return;
1856 }
1857
1858 /*
1859 * SCHED_IDLE tasks get minimal weight:
1860 */
1861 if (p->policy == SCHED_IDLE) {
1862 p->se.load.weight = WEIGHT_IDLEPRIO;
1863 p->se.load.inv_weight = WMULT_IDLEPRIO;
1864 return;
1865 }
1866
1867 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1868 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001869}
1870
Ingo Molnar8159f872007-08-09 11:16:49 +02001871static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001872{
1873 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001874 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001875 p->se.on_rq = 1;
1876}
1877
Ingo Molnar69be72c2007-08-09 11:16:49 +02001878static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001879{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001880 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001881 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001882}
1883
1884/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001885 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001886 */
Ingo Molnar14531182007-07-09 18:51:59 +02001887static inline int __normal_prio(struct task_struct *p)
1888{
Ingo Molnardd41f592007-07-09 18:51:59 +02001889 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001890}
1891
1892/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001893 * Calculate the expected normal priority: i.e. priority
1894 * without taking RT-inheritance into account. Might be
1895 * boosted by interactivity modifiers. Changes upon fork,
1896 * setprio syscalls, and whenever the interactivity
1897 * estimator recalculates.
1898 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001899static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001900{
1901 int prio;
1902
Ingo Molnare05606d2007-07-09 18:51:59 +02001903 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001904 prio = MAX_RT_PRIO-1 - p->rt_priority;
1905 else
1906 prio = __normal_prio(p);
1907 return prio;
1908}
1909
1910/*
1911 * Calculate the current priority, i.e. the priority
1912 * taken into account by the scheduler. This value might
1913 * be boosted by RT tasks, or might be boosted by
1914 * interactivity modifiers. Will be RT if the task got
1915 * RT-boosted. If not then it returns p->normal_prio.
1916 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001917static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001918{
1919 p->normal_prio = normal_prio(p);
1920 /*
1921 * If we are RT tasks or we were boosted to RT priority,
1922 * keep the priority unchanged. Otherwise, update priority
1923 * to the normal priority:
1924 */
1925 if (!rt_prio(p->prio))
1926 return p->normal_prio;
1927 return p->prio;
1928}
1929
1930/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001931 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001933static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001935 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001936 rq->nr_uninterruptible--;
1937
Ingo Molnar8159f872007-08-09 11:16:49 +02001938 enqueue_task(rq, p, wakeup);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001939 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
1942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * deactivate_task - remove a task from the runqueue.
1944 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001945static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001947 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001948 rq->nr_uninterruptible++;
1949
Ingo Molnar69be72c2007-08-09 11:16:49 +02001950 dequeue_task(rq, p, sleep);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001951 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/**
1955 * task_curr - is this task currently executing on a CPU?
1956 * @p: the task in question.
1957 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001958inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 return cpu_curr(task_cpu(p)) == p;
1961}
1962
Peter Williams2dd73a42006-06-27 02:54:34 -07001963/* Used instead of source_load when we know the type == 0 */
1964unsigned long weighted_cpuload(const int cpu)
1965{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001966 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001967}
1968
1969static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1970{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001971 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001972#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001973 /*
1974 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1975 * successfuly executed on another CPU. We must ensure that updates of
1976 * per-task data have been completed by this moment.
1977 */
1978 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001979 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001980#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001981}
1982
Steven Rostedtcb469842008-01-25 21:08:22 +01001983static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1984 const struct sched_class *prev_class,
1985 int oldprio, int running)
1986{
1987 if (prev_class != p->sched_class) {
1988 if (prev_class->switched_from)
1989 prev_class->switched_from(rq, p, running);
1990 p->sched_class->switched_to(rq, p, running);
1991 } else
1992 p->sched_class->prio_changed(rq, p, oldprio, running);
1993}
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001996
Ingo Molnarcc367732007-10-15 17:00:18 +02001997/*
1998 * Is this task likely cache-hot:
1999 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002000static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002001task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2002{
2003 s64 delta;
2004
Ingo Molnarf540a602008-03-15 17:10:34 +01002005 /*
2006 * Buddy candidates are cache hot:
2007 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01002008 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01002009 return 1;
2010
Ingo Molnarcc367732007-10-15 17:00:18 +02002011 if (p->sched_class != &fair_sched_class)
2012 return 0;
2013
Ingo Molnar6bc16652007-10-15 17:00:18 +02002014 if (sysctl_sched_migration_cost == -1)
2015 return 1;
2016 if (sysctl_sched_migration_cost == 0)
2017 return 0;
2018
Ingo Molnarcc367732007-10-15 17:00:18 +02002019 delta = now - p->se.exec_start;
2020
2021 return delta < (s64)sysctl_sched_migration_cost;
2022}
2023
2024
Ingo Molnardd41f592007-07-09 18:51:59 +02002025void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002026{
Ingo Molnardd41f592007-07-09 18:51:59 +02002027 int old_cpu = task_cpu(p);
2028 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002029 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2030 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02002031 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002032
2033 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002034
2035#ifdef CONFIG_SCHEDSTATS
2036 if (p->se.wait_start)
2037 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02002038 if (p->se.sleep_start)
2039 p->se.sleep_start -= clock_offset;
2040 if (p->se.block_start)
2041 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02002042 if (old_cpu != new_cpu) {
2043 schedstat_inc(p, se.nr_migrations);
2044 if (task_hot(p, old_rq->clock, NULL))
2045 schedstat_inc(p, se.nr_forced2_migrations);
2046 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002047#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02002048 p->se.vruntime -= old_cfsrq->min_vruntime -
2049 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02002050
2051 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002052}
2053
Ingo Molnar70b97a72006-07-03 00:25:42 -07002054struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Ingo Molnar36c8b582006-07-03 00:25:41 -07002057 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int dest_cpu;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063/*
2064 * The task's runqueue lock must be held.
2065 * Returns true if you have to wait for migration thread.
2066 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002067static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002068migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002070 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /*
2073 * If the task is not on a runqueue (and not running), then
2074 * it is sufficient to simply update the task's cpu field.
2075 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002076 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 set_task_cpu(p, dest_cpu);
2078 return 0;
2079 }
2080
2081 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 req->task = p;
2083 req->dest_cpu = dest_cpu;
2084 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 1;
2087}
2088
2089/*
2090 * wait_task_inactive - wait for a thread to unschedule.
2091 *
2092 * The caller must ensure that the task *will* unschedule sometime soon,
2093 * else this function might spin for a *long* time. This function can't
2094 * be called with interrupts off, or it may introduce deadlock with
2095 * smp_call_function() if an IPI is sent by the same process we are
2096 * waiting to become inactive.
2097 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002098void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002101 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002102 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Andi Kleen3a5c3592007-10-15 17:00:14 +02002104 for (;;) {
2105 /*
2106 * We do the initial early heuristics without holding
2107 * any task-queue locks at all. We'll only try to get
2108 * the runqueue lock when things look like they will
2109 * work out!
2110 */
2111 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002112
Andi Kleen3a5c3592007-10-15 17:00:14 +02002113 /*
2114 * If the task is actively running on another CPU
2115 * still, just relax and busy-wait without holding
2116 * any locks.
2117 *
2118 * NOTE! Since we don't hold any locks, it's not
2119 * even sure that "rq" stays as the right runqueue!
2120 * But we don't care, since "task_running()" will
2121 * return false if the runqueue has changed and p
2122 * is actually now running somewhere else!
2123 */
2124 while (task_running(rq, p))
2125 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002126
Andi Kleen3a5c3592007-10-15 17:00:14 +02002127 /*
2128 * Ok, time to look more closely! We need the rq
2129 * lock now, to be *sure*. If we're wrong, we'll
2130 * just go back and repeat.
2131 */
2132 rq = task_rq_lock(p, &flags);
2133 running = task_running(rq, p);
2134 on_rq = p->se.on_rq;
2135 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002136
Andi Kleen3a5c3592007-10-15 17:00:14 +02002137 /*
2138 * Was it really running after all now that we
2139 * checked with the proper locks actually held?
2140 *
2141 * Oops. Go back and try again..
2142 */
2143 if (unlikely(running)) {
2144 cpu_relax();
2145 continue;
2146 }
2147
2148 /*
2149 * It's not enough that it's not actively running,
2150 * it must be off the runqueue _entirely_, and not
2151 * preempted!
2152 *
2153 * So if it wa still runnable (but just not actively
2154 * running right now), it's preempted, and we should
2155 * yield - it could be a while.
2156 */
2157 if (unlikely(on_rq)) {
2158 schedule_timeout_uninterruptible(1);
2159 continue;
2160 }
2161
2162 /*
2163 * Ahh, all good. It wasn't running, and it wasn't
2164 * runnable, which means that it will never become
2165 * running in the future either. We're all done!
2166 */
2167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/***
2172 * kick_process - kick a running thread to enter/exit the kernel
2173 * @p: the to-be-kicked thread
2174 *
2175 * Cause a process which is running on another CPU to enter
2176 * kernel-mode, without any delay. (to get signals handled.)
2177 *
2178 * NOTE: this function doesnt have to take the runqueue lock,
2179 * because all it wants to ensure is that the remote task enters
2180 * the kernel. If the IPI races and the task has been migrated
2181 * to another CPU then no harm is done and the purpose has been
2182 * achieved as well.
2183 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002184void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 int cpu;
2187
2188 preempt_disable();
2189 cpu = task_cpu(p);
2190 if ((cpu != smp_processor_id()) && task_curr(p))
2191 smp_send_reschedule(cpu);
2192 preempt_enable();
2193}
2194
2195/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002196 * Return a low guess at the load of a migration-source cpu weighted
2197 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 *
2199 * We want to under-estimate the load of migration sources, to
2200 * balance conservatively.
2201 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002202static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002203{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002204 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002205 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002206
Peter Williams2dd73a42006-06-27 02:54:34 -07002207 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002208 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002209
Ingo Molnardd41f592007-07-09 18:51:59 +02002210 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
2213/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002214 * Return a high guess at the load of a migration-target cpu weighted
2215 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002217static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002218{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002219 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002220 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002221
Peter Williams2dd73a42006-06-27 02:54:34 -07002222 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002223 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002224
Ingo Molnardd41f592007-07-09 18:51:59 +02002225 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002226}
2227
2228/*
2229 * Return the average load per task on the cpu's run queue
2230 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002231static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002232{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002233 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002234 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002235 unsigned long n = rq->nr_running;
2236
Ingo Molnardd41f592007-07-09 18:51:59 +02002237 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Nick Piggin147cbb42005-06-25 14:57:19 -07002240/*
2241 * find_idlest_group finds and returns the least busy CPU group within the
2242 * domain.
2243 */
2244static struct sched_group *
2245find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2246{
2247 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2248 unsigned long min_load = ULONG_MAX, this_load = 0;
2249 int load_idx = sd->forkexec_idx;
2250 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2251
2252 do {
2253 unsigned long load, avg_load;
2254 int local_group;
2255 int i;
2256
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002257 /* Skip over this group if it has no CPUs allowed */
2258 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002259 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002260
Nick Piggin147cbb42005-06-25 14:57:19 -07002261 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002262
2263 /* Tally up the load of all CPUs in the group */
2264 avg_load = 0;
2265
2266 for_each_cpu_mask(i, group->cpumask) {
2267 /* Bias balancing toward cpus of our domain */
2268 if (local_group)
2269 load = source_load(i, load_idx);
2270 else
2271 load = target_load(i, load_idx);
2272
2273 avg_load += load;
2274 }
2275
2276 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002277 avg_load = sg_div_cpu_power(group,
2278 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002279
2280 if (local_group) {
2281 this_load = avg_load;
2282 this = group;
2283 } else if (avg_load < min_load) {
2284 min_load = avg_load;
2285 idlest = group;
2286 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002287 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002288
2289 if (!idlest || 100*this_load < imbalance*min_load)
2290 return NULL;
2291 return idlest;
2292}
2293
2294/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002295 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002296 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002297static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002298find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2299 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002300{
2301 unsigned long load, min_load = ULONG_MAX;
2302 int idlest = -1;
2303 int i;
2304
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002305 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002306 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002307
Mike Travis7c16ec52008-04-04 18:11:11 -07002308 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002309 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002310
2311 if (load < min_load || (load == min_load && i == this_cpu)) {
2312 min_load = load;
2313 idlest = i;
2314 }
2315 }
2316
2317 return idlest;
2318}
2319
Nick Piggin476d1392005-06-25 14:57:29 -07002320/*
2321 * sched_balance_self: balance the current task (running on cpu) in domains
2322 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2323 * SD_BALANCE_EXEC.
2324 *
2325 * Balance, ie. select the least loaded group.
2326 *
2327 * Returns the target CPU number, or the same CPU if no balancing is needed.
2328 *
2329 * preempt must be disabled.
2330 */
2331static int sched_balance_self(int cpu, int flag)
2332{
2333 struct task_struct *t = current;
2334 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002335
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002336 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002337 /*
2338 * If power savings logic is enabled for a domain, stop there.
2339 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002340 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2341 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002342 if (tmp->flags & flag)
2343 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002344 }
Nick Piggin476d1392005-06-25 14:57:29 -07002345
2346 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002347 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002348 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002349 int new_cpu, weight;
2350
2351 if (!(sd->flags & flag)) {
2352 sd = sd->child;
2353 continue;
2354 }
Nick Piggin476d1392005-06-25 14:57:29 -07002355
2356 span = sd->span;
2357 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002358 if (!group) {
2359 sd = sd->child;
2360 continue;
2361 }
Nick Piggin476d1392005-06-25 14:57:29 -07002362
Mike Travis7c16ec52008-04-04 18:11:11 -07002363 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002364 if (new_cpu == -1 || new_cpu == cpu) {
2365 /* Now try balancing at a lower domain level of cpu */
2366 sd = sd->child;
2367 continue;
2368 }
Nick Piggin476d1392005-06-25 14:57:29 -07002369
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002370 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002371 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002372 sd = NULL;
2373 weight = cpus_weight(span);
2374 for_each_domain(cpu, tmp) {
2375 if (weight <= cpus_weight(tmp->span))
2376 break;
2377 if (tmp->flags & flag)
2378 sd = tmp;
2379 }
2380 /* while loop will break here if sd == NULL */
2381 }
2382
2383 return cpu;
2384}
2385
2386#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/***
2389 * try_to_wake_up - wake up a thread
2390 * @p: the to-be-woken-up thread
2391 * @state: the mask of task states that can be woken
2392 * @sync: do a synchronous wakeup?
2393 *
2394 * Put it on the run-queue if it's not already there. The "current"
2395 * thread is always on the run-queue (except when the actual
2396 * re-schedule is in progress), and as such you're allowed to do
2397 * the simpler "current->state = TASK_RUNNING" to mark yourself
2398 * runnable without the overhead of this.
2399 *
2400 * returns failure only if the task is already active.
2401 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002402static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Ingo Molnarcc367732007-10-15 17:00:18 +02002404 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 unsigned long flags;
2406 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002407 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Ingo Molnarb85d0662008-03-16 20:03:22 +01002409 if (!sched_feat(SYNC_WAKEUPS))
2410 sync = 0;
2411
Linus Torvalds04e2f172008-02-23 18:05:03 -08002412 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 rq = task_rq_lock(p, &flags);
2414 old_state = p->state;
2415 if (!(old_state & state))
2416 goto out;
2417
Ingo Molnardd41f592007-07-09 18:51:59 +02002418 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto out_running;
2420
2421 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002422 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 this_cpu = smp_processor_id();
2424
2425#ifdef CONFIG_SMP
2426 if (unlikely(task_running(rq, p)))
2427 goto out_activate;
2428
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002429 cpu = p->sched_class->select_task_rq(p, sync);
2430 if (cpu != orig_cpu) {
2431 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 task_rq_unlock(rq, &flags);
2433 /* might preempt at this point */
2434 rq = task_rq_lock(p, &flags);
2435 old_state = p->state;
2436 if (!(old_state & state))
2437 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002438 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto out_running;
2440
2441 this_cpu = smp_processor_id();
2442 cpu = task_cpu(p);
2443 }
2444
Gregory Haskinse7693a32008-01-25 21:08:09 +01002445#ifdef CONFIG_SCHEDSTATS
2446 schedstat_inc(rq, ttwu_count);
2447 if (cpu == this_cpu)
2448 schedstat_inc(rq, ttwu_local);
2449 else {
2450 struct sched_domain *sd;
2451 for_each_domain(this_cpu, sd) {
2452 if (cpu_isset(cpu, sd->span)) {
2453 schedstat_inc(sd, ttwu_wake_remote);
2454 break;
2455 }
2456 }
2457 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01002458#endif
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460out_activate:
2461#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002462 schedstat_inc(p, se.nr_wakeups);
2463 if (sync)
2464 schedstat_inc(p, se.nr_wakeups_sync);
2465 if (orig_cpu != cpu)
2466 schedstat_inc(p, se.nr_wakeups_migrate);
2467 if (cpu == this_cpu)
2468 schedstat_inc(p, se.nr_wakeups_local);
2469 else
2470 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002471 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002472 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 success = 1;
2474
2475out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002476 check_preempt_curr(rq, p);
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002479#ifdef CONFIG_SMP
2480 if (p->sched_class->task_wake_up)
2481 p->sched_class->task_wake_up(rq, p);
2482#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483out:
2484 task_rq_unlock(rq, &flags);
2485
2486 return success;
2487}
2488
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002489int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002491 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493EXPORT_SYMBOL(wake_up_process);
2494
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002495int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 return try_to_wake_up(p, state, 0);
2498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/*
2501 * Perform scheduler related setup for a newly forked process p.
2502 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002503 *
2504 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002506static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Ingo Molnardd41f592007-07-09 18:51:59 +02002508 p->se.exec_start = 0;
2509 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002510 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002511 p->se.last_wakeup = 0;
2512 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002513
2514#ifdef CONFIG_SCHEDSTATS
2515 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002516 p->se.sum_sleep_runtime = 0;
2517 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002518 p->se.block_start = 0;
2519 p->se.sleep_max = 0;
2520 p->se.block_max = 0;
2521 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002522 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002523 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002524#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002525
Peter Zijlstrafa717062008-01-25 21:08:27 +01002526 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002527 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002528
Avi Kivitye107be32007-07-26 13:40:43 +02002529#ifdef CONFIG_PREEMPT_NOTIFIERS
2530 INIT_HLIST_HEAD(&p->preempt_notifiers);
2531#endif
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /*
2534 * We mark the process as running here, but have not actually
2535 * inserted it onto the runqueue yet. This guarantees that
2536 * nobody will actually run it, and a signal or other external
2537 * event cannot wake it up and insert it on the runqueue either.
2538 */
2539 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002540}
2541
2542/*
2543 * fork()/clone()-time setup:
2544 */
2545void sched_fork(struct task_struct *p, int clone_flags)
2546{
2547 int cpu = get_cpu();
2548
2549 __sched_fork(p);
2550
2551#ifdef CONFIG_SMP
2552 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2553#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002554 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002555
2556 /*
2557 * Make sure we do not leak PI boosting priority to the child:
2558 */
2559 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002560 if (!rt_prio(p->prio))
2561 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002562
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002563#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002564 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002565 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002567#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002568 p->oncpu = 0;
2569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002571 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002572 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002574 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577/*
2578 * wake_up_new_task - wake up a newly created task for the first time.
2579 *
2580 * This function will do some initial scheduler statistics housekeeping
2581 * that must be done for every newly created context, then puts the task
2582 * on the runqueue and wakes it.
2583 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002584void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002587 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002591 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 p->prio = effective_prio(p);
2594
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002595 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002596 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002599 * Let the scheduling class do new task startup
2600 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002602 p->sched_class->task_new(rq, p);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02002603 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002605 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002606#ifdef CONFIG_SMP
2607 if (p->sched_class->task_wake_up)
2608 p->sched_class->task_wake_up(rq, p);
2609#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002610 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Avi Kivitye107be32007-07-26 13:40:43 +02002613#ifdef CONFIG_PREEMPT_NOTIFIERS
2614
2615/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002616 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2617 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002618 */
2619void preempt_notifier_register(struct preempt_notifier *notifier)
2620{
2621 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2622}
2623EXPORT_SYMBOL_GPL(preempt_notifier_register);
2624
2625/**
2626 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002627 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002628 *
2629 * This is safe to call from within a preemption notifier.
2630 */
2631void preempt_notifier_unregister(struct preempt_notifier *notifier)
2632{
2633 hlist_del(&notifier->link);
2634}
2635EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2636
2637static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2638{
2639 struct preempt_notifier *notifier;
2640 struct hlist_node *node;
2641
2642 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2643 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2644}
2645
2646static void
2647fire_sched_out_preempt_notifiers(struct task_struct *curr,
2648 struct task_struct *next)
2649{
2650 struct preempt_notifier *notifier;
2651 struct hlist_node *node;
2652
2653 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2654 notifier->ops->sched_out(notifier, next);
2655}
2656
2657#else
2658
2659static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2660{
2661}
2662
2663static void
2664fire_sched_out_preempt_notifiers(struct task_struct *curr,
2665 struct task_struct *next)
2666{
2667}
2668
2669#endif
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002672 * prepare_task_switch - prepare to switch tasks
2673 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002674 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002675 * @next: the task we are going to switch to.
2676 *
2677 * This is called with the rq lock held and interrupts off. It must
2678 * be paired with a subsequent finish_task_switch after the context
2679 * switch.
2680 *
2681 * prepare_task_switch sets up locking and calls architecture specific
2682 * hooks.
2683 */
Avi Kivitye107be32007-07-26 13:40:43 +02002684static inline void
2685prepare_task_switch(struct rq *rq, struct task_struct *prev,
2686 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002687{
Avi Kivitye107be32007-07-26 13:40:43 +02002688 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002689 prepare_lock_switch(rq, next);
2690 prepare_arch_switch(next);
2691}
2692
2693/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002695 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * @prev: the thread we just switched away from.
2697 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002698 * finish_task_switch must be called after the context switch, paired
2699 * with a prepare_task_switch call before the context switch.
2700 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2701 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *
2703 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002704 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * with the lock held can cause deadlocks; see schedule() for
2706 * details.)
2707 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002708static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 __releases(rq->lock)
2710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002712 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 rq->prev_mm = NULL;
2715
2716 /*
2717 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002718 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002719 * schedule one last time. The schedule call will never return, and
2720 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002721 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * still held, otherwise prev could be scheduled on another cpu, die
2723 * there before we look at prev->state, and then the reference would
2724 * be dropped twice.
2725 * Manfred Spraul <manfred@colorfullife.com>
2726 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002727 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002728 finish_arch_switch(prev);
2729 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002730#ifdef CONFIG_SMP
2731 if (current->sched_class->post_schedule)
2732 current->sched_class->post_schedule(rq);
2733#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002734
Avi Kivitye107be32007-07-26 13:40:43 +02002735 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (mm)
2737 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002738 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002739 /*
2740 * Remove function-return probe instances associated with this
2741 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002742 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002743 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
2748/**
2749 * schedule_tail - first thing a freshly forked thread must call.
2750 * @prev: the thread we just switched away from.
2751 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002752asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 __releases(rq->lock)
2754{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002755 struct rq *rq = this_rq();
2756
Nick Piggin4866cde2005-06-25 14:57:23 -07002757 finish_task_switch(rq, prev);
2758#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2759 /* In this case, finish_task_switch does not reenable preemption */
2760 preempt_enable();
2761#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002763 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
2766/*
2767 * context_switch - switch to the new MM and the new
2768 * thread's register state.
2769 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002770static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002771context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002772 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Ingo Molnardd41f592007-07-09 18:51:59 +02002774 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Avi Kivitye107be32007-07-26 13:40:43 +02002776 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002777 mm = next->mm;
2778 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002779 /*
2780 * For paravirt, this is coupled with an exit in switch_to to
2781 * combine the page table reload and the switch backend into
2782 * one hypercall.
2783 */
2784 arch_enter_lazy_cpu_mode();
2785
Ingo Molnardd41f592007-07-09 18:51:59 +02002786 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 next->active_mm = oldmm;
2788 atomic_inc(&oldmm->mm_count);
2789 enter_lazy_tlb(oldmm, next);
2790 } else
2791 switch_mm(oldmm, mm, next);
2792
Ingo Molnardd41f592007-07-09 18:51:59 +02002793 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 rq->prev_mm = oldmm;
2796 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002797 /*
2798 * Since the runqueue lock will be released by the next
2799 * task (which is an invalid locking op but in the case
2800 * of the scheduler it's an obvious special-case), so we
2801 * do an early lockdep release here:
2802 */
2803#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002804 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 /* Here we just switch the register state and the stack. */
2808 switch_to(prev, next, prev);
2809
Ingo Molnardd41f592007-07-09 18:51:59 +02002810 barrier();
2811 /*
2812 * this_rq must be evaluated again because prev may have moved
2813 * CPUs since it called schedule(), thus the 'rq' on its stack
2814 * frame will be invalid.
2815 */
2816 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
2819/*
2820 * nr_running, nr_uninterruptible and nr_context_switches:
2821 *
2822 * externally visible scheduler statistics: current number of runnable
2823 * threads, current number of uninterruptible-sleeping threads, total
2824 * number of context switches performed since bootup.
2825 */
2826unsigned long nr_running(void)
2827{
2828 unsigned long i, sum = 0;
2829
2830 for_each_online_cpu(i)
2831 sum += cpu_rq(i)->nr_running;
2832
2833 return sum;
2834}
2835
2836unsigned long nr_uninterruptible(void)
2837{
2838 unsigned long i, sum = 0;
2839
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002840 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 sum += cpu_rq(i)->nr_uninterruptible;
2842
2843 /*
2844 * Since we read the counters lockless, it might be slightly
2845 * inaccurate. Do not allow it to go below zero though:
2846 */
2847 if (unlikely((long)sum < 0))
2848 sum = 0;
2849
2850 return sum;
2851}
2852
2853unsigned long long nr_context_switches(void)
2854{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002855 int i;
2856 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002858 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 sum += cpu_rq(i)->nr_switches;
2860
2861 return sum;
2862}
2863
2864unsigned long nr_iowait(void)
2865{
2866 unsigned long i, sum = 0;
2867
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002868 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2870
2871 return sum;
2872}
2873
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002874unsigned long nr_active(void)
2875{
2876 unsigned long i, running = 0, uninterruptible = 0;
2877
2878 for_each_online_cpu(i) {
2879 running += cpu_rq(i)->nr_running;
2880 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2881 }
2882
2883 if (unlikely((long)uninterruptible < 0))
2884 uninterruptible = 0;
2885
2886 return running + uninterruptible;
2887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002890 * Update rq->cpu_load[] statistics. This function is usually called every
2891 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002892 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002893static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002894{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002895 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002896 int i, scale;
2897
2898 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002899
2900 /* Update our load: */
2901 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2902 unsigned long old_load, new_load;
2903
2904 /* scale is effectively 1 << i now, and >> i divides by scale */
2905
2906 old_load = this_rq->cpu_load[i];
2907 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002908 /*
2909 * Round up the averaging division if load is increasing. This
2910 * prevents us from getting stuck on 9 if the load is 10, for
2911 * example.
2912 */
2913 if (new_load > old_load)
2914 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002915 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2916 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002917}
2918
Ingo Molnardd41f592007-07-09 18:51:59 +02002919#ifdef CONFIG_SMP
2920
Ingo Molnar48f24c42006-07-03 00:25:40 -07002921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 * double_rq_lock - safely lock two runqueues
2923 *
2924 * Note this does not disable interrupts like task_rq_lock,
2925 * you need to do so manually before calling.
2926 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002927static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 __acquires(rq1->lock)
2929 __acquires(rq2->lock)
2930{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002931 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (rq1 == rq2) {
2933 spin_lock(&rq1->lock);
2934 __acquire(rq2->lock); /* Fake it out ;) */
2935 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002936 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 spin_lock(&rq1->lock);
2938 spin_lock(&rq2->lock);
2939 } else {
2940 spin_lock(&rq2->lock);
2941 spin_lock(&rq1->lock);
2942 }
2943 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002944 update_rq_clock(rq1);
2945 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
2948/*
2949 * double_rq_unlock - safely unlock two runqueues
2950 *
2951 * Note this does not restore interrupts like task_rq_unlock,
2952 * you need to do so manually after calling.
2953 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002954static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 __releases(rq1->lock)
2956 __releases(rq2->lock)
2957{
2958 spin_unlock(&rq1->lock);
2959 if (rq1 != rq2)
2960 spin_unlock(&rq2->lock);
2961 else
2962 __release(rq2->lock);
2963}
2964
2965/*
2966 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2967 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002968static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 __releases(this_rq->lock)
2970 __acquires(busiest->lock)
2971 __acquires(this_rq->lock)
2972{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002973 int ret = 0;
2974
Kirill Korotaev054b9102006-12-10 02:20:11 -08002975 if (unlikely(!irqs_disabled())) {
2976 /* printk() doesn't work good under rq->lock */
2977 spin_unlock(&this_rq->lock);
2978 BUG_ON(1);
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002981 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 spin_unlock(&this_rq->lock);
2983 spin_lock(&busiest->lock);
2984 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002985 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 } else
2987 spin_lock(&busiest->lock);
2988 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002989 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * If dest_cpu is allowed for this process, migrate the task to it.
2994 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002995 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 * the cpu_allowed mask is restored.
2997 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002998static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003000 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003002 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 rq = task_rq_lock(p, &flags);
3005 if (!cpu_isset(dest_cpu, p->cpus_allowed)
3006 || unlikely(cpu_is_offline(dest_cpu)))
3007 goto out;
3008
3009 /* force the process onto the specified CPU */
3010 if (migrate_task(p, dest_cpu, &req)) {
3011 /* Need to wait for migration thread (might exit: take ref). */
3012 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 get_task_struct(mt);
3015 task_rq_unlock(rq, &flags);
3016 wake_up_process(mt);
3017 put_task_struct(mt);
3018 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return;
3021 }
3022out:
3023 task_rq_unlock(rq, &flags);
3024}
3025
3026/*
Nick Piggin476d1392005-06-25 14:57:29 -07003027 * sched_exec - execve() is a valuable balancing opportunity, because at
3028 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
3030void sched_exec(void)
3031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003033 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003035 if (new_cpu != this_cpu)
3036 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/*
3040 * pull_task - move a task from a remote runqueue to the local runqueue.
3041 * Both runqueues must be locked.
3042 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003043static void pull_task(struct rq *src_rq, struct task_struct *p,
3044 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003046 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003048 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 /*
3050 * Note that idle threads have a prio of MAX_PRIO, for this test
3051 * to be always true for them.
3052 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003053 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056/*
3057 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3058 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003059static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003060int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003061 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003062 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 /*
3065 * We do not migrate tasks that are:
3066 * 1) running (obviously), or
3067 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3068 * 3) are cache-hot on their current CPU.
3069 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003070 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3071 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003073 }
Nick Piggin81026792005-06-25 14:57:07 -07003074 *all_pinned = 0;
3075
Ingo Molnarcc367732007-10-15 17:00:18 +02003076 if (task_running(rq, p)) {
3077 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003078 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Ingo Molnarda84d962007-10-15 17:00:18 +02003081 /*
3082 * Aggressive migration if:
3083 * 1) task is cache cold, or
3084 * 2) too many balance attempts have failed.
3085 */
3086
Ingo Molnar6bc16652007-10-15 17:00:18 +02003087 if (!task_hot(p, rq->clock, sd) ||
3088 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003089#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003090 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003091 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003092 schedstat_inc(p, se.nr_forced_migrations);
3093 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003094#endif
3095 return 1;
3096 }
3097
Ingo Molnarcc367732007-10-15 17:00:18 +02003098 if (task_hot(p, rq->clock, sd)) {
3099 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003100 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return 1;
3103}
3104
Peter Williamse1d14842007-10-24 18:23:51 +02003105static unsigned long
3106balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3107 unsigned long max_load_move, struct sched_domain *sd,
3108 enum cpu_idle_type idle, int *all_pinned,
3109 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003110{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003111 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003112 struct task_struct *p;
3113 long rem_load_move = max_load_move;
3114
Peter Williamse1d14842007-10-24 18:23:51 +02003115 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003116 goto out;
3117
3118 pinned = 1;
3119
3120 /*
3121 * Start the load-balancing iterator:
3122 */
3123 p = iterator->start(iterator->arg);
3124next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003125 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003126 goto out;
3127 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003128 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003129 * skip a task if it will be the highest priority task (i.e. smallest
3130 * prio value) on its new queue regardless of its load weight
3131 */
3132 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3133 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003134 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003135 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003136 p = iterator->next(iterator->arg);
3137 goto next;
3138 }
3139
3140 pull_task(busiest, p, this_rq, this_cpu);
3141 pulled++;
3142 rem_load_move -= p->se.load.weight;
3143
3144 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003145 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003146 */
Peter Williamse1d14842007-10-24 18:23:51 +02003147 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003148 if (p->prio < *this_best_prio)
3149 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003150 p = iterator->next(iterator->arg);
3151 goto next;
3152 }
3153out:
3154 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003155 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003156 * so we can safely collect pull_task() stats here rather than
3157 * inside pull_task().
3158 */
3159 schedstat_add(sd, lb_gained[idle], pulled);
3160
3161 if (all_pinned)
3162 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003163
3164 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003165}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167/*
Peter Williams43010652007-08-09 11:16:46 +02003168 * move_tasks tries to move up to max_load_move weighted load from busiest to
3169 * this_rq, as part of a balancing operation within domain "sd".
3170 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 *
3172 * Called with both runqueues locked.
3173 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003174static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003175 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003176 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003177 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003179 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003180 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003181 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Ingo Molnardd41f592007-07-09 18:51:59 +02003183 do {
Peter Williams43010652007-08-09 11:16:46 +02003184 total_load_moved +=
3185 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003186 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003187 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003188 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003189 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Peter Williams43010652007-08-09 11:16:46 +02003191 return total_load_moved > 0;
3192}
3193
Peter Williamse1d14842007-10-24 18:23:51 +02003194static int
3195iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3196 struct sched_domain *sd, enum cpu_idle_type idle,
3197 struct rq_iterator *iterator)
3198{
3199 struct task_struct *p = iterator->start(iterator->arg);
3200 int pinned = 0;
3201
3202 while (p) {
3203 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3204 pull_task(busiest, p, this_rq, this_cpu);
3205 /*
3206 * Right now, this is only the second place pull_task()
3207 * is called, so we can safely collect pull_task()
3208 * stats here rather than inside pull_task().
3209 */
3210 schedstat_inc(sd, lb_gained[idle]);
3211
3212 return 1;
3213 }
3214 p = iterator->next(iterator->arg);
3215 }
3216
3217 return 0;
3218}
3219
Peter Williams43010652007-08-09 11:16:46 +02003220/*
3221 * move_one_task tries to move exactly one task from busiest to this_rq, as
3222 * part of active balancing operations within "domain".
3223 * Returns 1 if successful and 0 otherwise.
3224 *
3225 * Called with both runqueues locked.
3226 */
3227static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3228 struct sched_domain *sd, enum cpu_idle_type idle)
3229{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003230 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003231
3232 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003233 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003234 return 1;
3235
3236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
3239/*
3240 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003241 * domain. It calculates and returns the amount of weighted load which
3242 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 */
3244static struct sched_group *
3245find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003246 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003247 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
3249 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3250 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003251 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003252 unsigned long busiest_load_per_task, busiest_nr_running;
3253 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003254 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003255#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3256 int power_savings_balance = 1;
3257 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3258 unsigned long min_nr_running = ULONG_MAX;
3259 struct sched_group *group_min = NULL, *group_leader = NULL;
3260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003263 busiest_load_per_task = busiest_nr_running = 0;
3264 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003265 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003266 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003267 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003268 load_idx = sd->newidle_idx;
3269 else
3270 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003273 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 int local_group;
3275 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003276 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003277 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003278 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 local_group = cpu_isset(this_cpu, group->cpumask);
3281
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003282 if (local_group)
3283 balance_cpu = first_cpu(group->cpumask);
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003286 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003287 max_cpu_load = 0;
3288 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003291 struct rq *rq;
3292
3293 if (!cpu_isset(i, *cpus))
3294 continue;
3295
3296 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003297
Suresh Siddha9439aab2007-07-19 21:28:35 +02003298 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003299 *sd_idle = 0;
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003302 if (local_group) {
3303 if (idle_cpu(i) && !first_idle_cpu) {
3304 first_idle_cpu = 1;
3305 balance_cpu = i;
3306 }
3307
Nick Piggina2000572006-02-10 01:51:02 -08003308 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003309 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003310 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003311 if (load > max_cpu_load)
3312 max_cpu_load = load;
3313 if (min_cpu_load > load)
3314 min_cpu_load = load;
3315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003318 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003319 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003322 /*
3323 * First idle cpu or the first cpu(busiest) in this sched group
3324 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003325 * domains. In the newly idle case, we will allow all the cpu's
3326 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003327 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003328 if (idle != CPU_NEWLY_IDLE && local_group &&
3329 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003330 *balance = 0;
3331 goto ret;
3332 }
3333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003335 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003338 avg_load = sg_div_cpu_power(group,
3339 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Ken Chen908a7c12007-10-17 16:55:11 +02003341 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3342 __group_imb = 1;
3343
Eric Dumazet5517d862007-05-08 00:32:57 -07003344 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (local_group) {
3347 this_load = avg_load;
3348 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003349 this_nr_running = sum_nr_running;
3350 this_load_per_task = sum_weighted_load;
3351 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003352 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 max_load = avg_load;
3354 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003355 busiest_nr_running = sum_nr_running;
3356 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003357 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003359
3360#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3361 /*
3362 * Busy processors will not participate in power savings
3363 * balance.
3364 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003365 if (idle == CPU_NOT_IDLE ||
3366 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3367 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003368
3369 /*
3370 * If the local group is idle or completely loaded
3371 * no need to do power savings balance at this domain
3372 */
3373 if (local_group && (this_nr_running >= group_capacity ||
3374 !this_nr_running))
3375 power_savings_balance = 0;
3376
Ingo Molnardd41f592007-07-09 18:51:59 +02003377 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003378 * If a group is already running at full capacity or idle,
3379 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003380 */
3381 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003382 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003383 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003384
Ingo Molnardd41f592007-07-09 18:51:59 +02003385 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003386 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003387 * This is the group from where we need to pick up the load
3388 * for saving power
3389 */
3390 if ((sum_nr_running < min_nr_running) ||
3391 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003392 first_cpu(group->cpumask) <
3393 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003394 group_min = group;
3395 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003396 min_load_per_task = sum_weighted_load /
3397 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003398 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003399
Ingo Molnardd41f592007-07-09 18:51:59 +02003400 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003401 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003402 * capacity but still has some space to pick up some load
3403 * from other group and save more power
3404 */
3405 if (sum_nr_running <= group_capacity - 1) {
3406 if (sum_nr_running > leader_nr_running ||
3407 (sum_nr_running == leader_nr_running &&
3408 first_cpu(group->cpumask) >
3409 first_cpu(group_leader->cpumask))) {
3410 group_leader = group;
3411 leader_nr_running = sum_nr_running;
3412 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003413 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003414group_next:
3415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 group = group->next;
3417 } while (group != sd->groups);
3418
Peter Williams2dd73a42006-06-27 02:54:34 -07003419 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 goto out_balanced;
3421
3422 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3423
3424 if (this_load >= avg_load ||
3425 100*max_load <= sd->imbalance_pct*this_load)
3426 goto out_balanced;
3427
Peter Williams2dd73a42006-06-27 02:54:34 -07003428 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003429 if (group_imb)
3430 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 /*
3433 * We're trying to get all the cpus to the average_load, so we don't
3434 * want to push ourselves above the average load, nor do we wish to
3435 * reduce the max loaded cpu below the average load, as either of these
3436 * actions would just result in more rebalancing later, and ping-pong
3437 * tasks around. Thus we look for the minimum possible imbalance.
3438 * Negative imbalances (*we* are more loaded than anyone else) will
3439 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003440 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 * appear as very large values with unsigned longs.
3442 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003443 if (max_load <= busiest_load_per_task)
3444 goto out_balanced;
3445
3446 /*
3447 * In the presence of smp nice balancing, certain scenarios can have
3448 * max load less than avg load(as we skip the groups at or below
3449 * its cpu_power, while calculating max_load..)
3450 */
3451 if (max_load < avg_load) {
3452 *imbalance = 0;
3453 goto small_imbalance;
3454 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003455
3456 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003457 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003460 *imbalance = min(max_pull * busiest->__cpu_power,
3461 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 / SCHED_LOAD_SCALE;
3463
Peter Williams2dd73a42006-06-27 02:54:34 -07003464 /*
3465 * if *imbalance is less than the average load per runnable task
3466 * there is no gaurantee that any tasks will be moved so we'll have
3467 * a think about bumping its value to force at least one task to be
3468 * moved
3469 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003470 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003471 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003472 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Peter Williams2dd73a42006-06-27 02:54:34 -07003474small_imbalance:
3475 pwr_move = pwr_now = 0;
3476 imbn = 2;
3477 if (this_nr_running) {
3478 this_load_per_task /= this_nr_running;
3479 if (busiest_load_per_task > this_load_per_task)
3480 imbn = 1;
3481 } else
3482 this_load_per_task = SCHED_LOAD_SCALE;
3483
Ingo Molnardd41f592007-07-09 18:51:59 +02003484 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3485 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003486 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return busiest;
3488 }
3489
3490 /*
3491 * OK, we don't have enough imbalance to justify moving tasks,
3492 * however we may be able to increase total CPU power used by
3493 * moving them.
3494 */
3495
Eric Dumazet5517d862007-05-08 00:32:57 -07003496 pwr_now += busiest->__cpu_power *
3497 min(busiest_load_per_task, max_load);
3498 pwr_now += this->__cpu_power *
3499 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 pwr_now /= SCHED_LOAD_SCALE;
3501
3502 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003503 tmp = sg_div_cpu_power(busiest,
3504 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003506 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003507 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003510 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003511 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003512 tmp = sg_div_cpu_power(this,
3513 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003515 tmp = sg_div_cpu_power(this,
3516 busiest_load_per_task * SCHED_LOAD_SCALE);
3517 pwr_move += this->__cpu_power *
3518 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 pwr_move /= SCHED_LOAD_SCALE;
3520
3521 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003522 if (pwr_move > pwr_now)
3523 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return busiest;
3527
3528out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003529#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003530 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003531 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003533 if (this == group_leader && group_leader != group_min) {
3534 *imbalance = min_load_per_task;
3535 return group_min;
3536 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003537#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003538ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 *imbalance = 0;
3540 return NULL;
3541}
3542
3543/*
3544 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3545 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003546static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003547find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003548 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003550 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003551 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 int i;
3553
3554 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003555 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003556
3557 if (!cpu_isset(i, *cpus))
3558 continue;
3559
Ingo Molnar48f24c42006-07-03 00:25:40 -07003560 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003561 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Ingo Molnardd41f592007-07-09 18:51:59 +02003563 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003564 continue;
3565
Ingo Molnardd41f592007-07-09 18:51:59 +02003566 if (wl > max_load) {
3567 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003568 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 }
3571
3572 return busiest;
3573}
3574
3575/*
Nick Piggin77391d72005-06-25 14:57:30 -07003576 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3577 * so long as it is large enough.
3578 */
3579#define MAX_PINNED_INTERVAL 512
3580
3581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3583 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003585static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003586 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003587 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Peter Williams43010652007-08-09 11:16:46 +02003589 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003592 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003593 unsigned long flags;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003594 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003595
Mike Travis7c16ec52008-04-04 18:11:11 -07003596 cpus_setall(*cpus);
3597
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003598 unlock_aggregate = get_aggregate(sd);
3599
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003600 /*
3601 * When power savings policy is enabled for the parent domain, idle
3602 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003603 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003604 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003605 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003606 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003607 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003608 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Ingo Molnar2d723762007-10-15 17:00:12 +02003610 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003612redo:
3613 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003614 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003615
Chen, Kenneth W06066712006-12-10 02:20:35 -08003616 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003617 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 if (!group) {
3620 schedstat_inc(sd, lb_nobusyg[idle]);
3621 goto out_balanced;
3622 }
3623
Mike Travis7c16ec52008-04-04 18:11:11 -07003624 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 if (!busiest) {
3626 schedstat_inc(sd, lb_nobusyq[idle]);
3627 goto out_balanced;
3628 }
3629
Nick Piggindb935db2005-06-25 14:57:11 -07003630 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 schedstat_add(sd, lb_imbalance[idle], imbalance);
3633
Peter Williams43010652007-08-09 11:16:46 +02003634 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if (busiest->nr_running > 1) {
3636 /*
3637 * Attempt to move tasks. If find_busiest_group has found
3638 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003639 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 * correctly treated as an imbalance.
3641 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003642 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003643 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003644 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003645 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003646 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003647 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003648
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003649 /*
3650 * some other cpu did the load balance for us.
3651 */
Peter Williams43010652007-08-09 11:16:46 +02003652 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003653 resched_cpu(this_cpu);
3654
Nick Piggin81026792005-06-25 14:57:07 -07003655 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003656 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003657 cpu_clear(cpu_of(busiest), *cpus);
3658 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003659 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003660 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Nick Piggin81026792005-06-25 14:57:07 -07003663
Peter Williams43010652007-08-09 11:16:46 +02003664 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 schedstat_inc(sd, lb_failed[idle]);
3666 sd->nr_balance_failed++;
3667
3668 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003670 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003671
3672 /* don't kick the migration_thread, if the curr
3673 * task on busiest cpu can't be moved to this_cpu
3674 */
3675 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003676 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003677 all_pinned = 1;
3678 goto out_one_pinned;
3679 }
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (!busiest->active_balance) {
3682 busiest->active_balance = 1;
3683 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003684 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003686 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003687 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 wake_up_process(busiest->migration_thread);
3689
3690 /*
3691 * We've kicked active balancing, reset the failure
3692 * counter.
3693 */
Nick Piggin39507452005-06-25 14:57:09 -07003694 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
Nick Piggin81026792005-06-25 14:57:07 -07003696 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 sd->nr_balance_failed = 0;
3698
Nick Piggin81026792005-06-25 14:57:07 -07003699 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 /* We were unbalanced, so reset the balancing interval */
3701 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003702 } else {
3703 /*
3704 * If we've begun active balancing, start to back off. This
3705 * case may not be covered by the all_pinned logic if there
3706 * is only 1 task on the busy runqueue (because we don't call
3707 * move_tasks).
3708 */
3709 if (sd->balance_interval < sd->max_interval)
3710 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 }
3712
Peter Williams43010652007-08-09 11:16:46 +02003713 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003714 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003715 ld_moved = -1;
3716
3717 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 schedstat_inc(sd, lb_balanced[idle]);
3721
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003722 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003723
3724out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003726 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3727 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 sd->balance_interval *= 2;
3729
Ingo Molnar48f24c42006-07-03 00:25:40 -07003730 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003731 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstra18d95a22008-04-19 19:45:00 +02003732 ld_moved = -1;
3733 else
3734 ld_moved = 0;
3735out:
3736 if (unlock_aggregate)
3737 put_aggregate(sd);
3738 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
3741/*
3742 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3743 * tasks if there is an imbalance.
3744 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003745 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 * this_rq is locked.
3747 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003748static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003749load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3750 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003753 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003755 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003756 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003757 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003758
3759 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003760
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003761 /*
3762 * When power savings policy is enabled for the parent domain, idle
3763 * sibling can pick up load irrespective of busy siblings. In this case,
3764 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003765 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003766 */
3767 if (sd->flags & SD_SHARE_CPUPOWER &&
3768 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003769 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Ingo Molnar2d723762007-10-15 17:00:12 +02003771 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003772redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003773 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003774 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003776 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003777 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
Mike Travis7c16ec52008-04-04 18:11:11 -07003780 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003781 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003782 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003783 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Nick Piggindb935db2005-06-25 14:57:11 -07003786 BUG_ON(busiest == this_rq);
3787
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003788 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003789
Peter Williams43010652007-08-09 11:16:46 +02003790 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003791 if (busiest->nr_running > 1) {
3792 /* Attempt to move tasks */
3793 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003794 /* this_rq->clock is already updated */
3795 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003796 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003797 imbalance, sd, CPU_NEWLY_IDLE,
3798 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003799 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003800
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003801 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003802 cpu_clear(cpu_of(busiest), *cpus);
3803 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003804 goto redo;
3805 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003806 }
3807
Peter Williams43010652007-08-09 11:16:46 +02003808 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003809 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003810 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3811 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003812 return -1;
3813 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003814 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Peter Williams43010652007-08-09 11:16:46 +02003816 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003817
3818out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003819 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003820 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003821 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003822 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003823 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003824
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
3828/*
3829 * idle_balance is called by schedule() if this_cpu is about to become
3830 * idle. Attempts to pull tasks from other CPUs.
3831 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003832static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
3834 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003835 int pulled_task = -1;
3836 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003837 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003840 unsigned long interval;
3841
3842 if (!(sd->flags & SD_LOAD_BALANCE))
3843 continue;
3844
3845 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003846 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003847 pulled_task = load_balance_newidle(this_cpu, this_rq,
3848 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003849
3850 interval = msecs_to_jiffies(sd->balance_interval);
3851 if (time_after(next_balance, sd->last_balance + interval))
3852 next_balance = sd->last_balance + interval;
3853 if (pulled_task)
3854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003856 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003857 /*
3858 * We are going idle. next_balance may be set based on
3859 * a busy processor. So reset next_balance.
3860 */
3861 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
3865/*
3866 * active_load_balance is run by migration threads. It pushes running tasks
3867 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3868 * running on each physical CPU where possible, and avoids physical /
3869 * logical imbalances.
3870 *
3871 * Called with busiest_rq locked.
3872 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003873static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
Nick Piggin39507452005-06-25 14:57:09 -07003875 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003876 struct sched_domain *sd;
3877 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003878
Ingo Molnar48f24c42006-07-03 00:25:40 -07003879 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003880 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003881 return;
3882
3883 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 /*
Nick Piggin39507452005-06-25 14:57:09 -07003886 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003887 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003888 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 */
Nick Piggin39507452005-06-25 14:57:09 -07003890 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Nick Piggin39507452005-06-25 14:57:09 -07003892 /* move a task from busiest_rq to target_rq */
3893 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003894 update_rq_clock(busiest_rq);
3895 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Nick Piggin39507452005-06-25 14:57:09 -07003897 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003898 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003899 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003900 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003901 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Ingo Molnar48f24c42006-07-03 00:25:40 -07003904 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003905 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Peter Williams43010652007-08-09 11:16:46 +02003907 if (move_one_task(target_rq, target_cpu, busiest_rq,
3908 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003909 schedstat_inc(sd, alb_pushed);
3910 else
3911 schedstat_inc(sd, alb_failed);
3912 }
Nick Piggin39507452005-06-25 14:57:09 -07003913 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003916#ifdef CONFIG_NO_HZ
3917static struct {
3918 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003919 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003920} nohz ____cacheline_aligned = {
3921 .load_balancer = ATOMIC_INIT(-1),
3922 .cpu_mask = CPU_MASK_NONE,
3923};
3924
Christoph Lameter7835b982006-12-10 02:20:22 -08003925/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003926 * This routine will try to nominate the ilb (idle load balancing)
3927 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3928 * load balancing on behalf of all those cpus. If all the cpus in the system
3929 * go into this tickless mode, then there will be no ilb owner (as there is
3930 * no need for one) and all the cpus will sleep till the next wakeup event
3931 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003932 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003933 * For the ilb owner, tick is not stopped. And this tick will be used
3934 * for idle load balancing. ilb owner will still be part of
3935 * nohz.cpu_mask..
3936 *
3937 * While stopping the tick, this cpu will become the ilb owner if there
3938 * is no other owner. And will be the owner till that cpu becomes busy
3939 * or if all cpus in the system stop their ticks at which point
3940 * there is no need for ilb owner.
3941 *
3942 * When the ilb owner becomes busy, it nominates another owner, during the
3943 * next busy scheduler_tick()
3944 */
3945int select_nohz_load_balancer(int stop_tick)
3946{
3947 int cpu = smp_processor_id();
3948
3949 if (stop_tick) {
3950 cpu_set(cpu, nohz.cpu_mask);
3951 cpu_rq(cpu)->in_nohz_recently = 1;
3952
3953 /*
3954 * If we are going offline and still the leader, give up!
3955 */
3956 if (cpu_is_offline(cpu) &&
3957 atomic_read(&nohz.load_balancer) == cpu) {
3958 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3959 BUG();
3960 return 0;
3961 }
3962
3963 /* time for ilb owner also to sleep */
3964 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3965 if (atomic_read(&nohz.load_balancer) == cpu)
3966 atomic_set(&nohz.load_balancer, -1);
3967 return 0;
3968 }
3969
3970 if (atomic_read(&nohz.load_balancer) == -1) {
3971 /* make me the ilb owner */
3972 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3973 return 1;
3974 } else if (atomic_read(&nohz.load_balancer) == cpu)
3975 return 1;
3976 } else {
3977 if (!cpu_isset(cpu, nohz.cpu_mask))
3978 return 0;
3979
3980 cpu_clear(cpu, nohz.cpu_mask);
3981
3982 if (atomic_read(&nohz.load_balancer) == cpu)
3983 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3984 BUG();
3985 }
3986 return 0;
3987}
3988#endif
3989
3990static DEFINE_SPINLOCK(balancing);
3991
3992/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003993 * It checks each scheduling domain to see if it is due to be balanced,
3994 * and initiates a balancing operation if so.
3995 *
3996 * Balancing parameters are set up in arch_init_sched_domains.
3997 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003998static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003999{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004000 int balance = 1;
4001 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004002 unsigned long interval;
4003 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004004 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004005 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004006 int update_next_balance = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07004007 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004009 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (!(sd->flags & SD_LOAD_BALANCE))
4011 continue;
4012
4013 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004014 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 interval *= sd->busy_factor;
4016
4017 /* scale ms to jiffies */
4018 interval = msecs_to_jiffies(interval);
4019 if (unlikely(!interval))
4020 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004021 if (interval > HZ*NR_CPUS/10)
4022 interval = HZ*NR_CPUS/10;
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Christoph Lameter08c183f2006-12-10 02:20:29 -08004025 if (sd->flags & SD_SERIALIZE) {
4026 if (!spin_trylock(&balancing))
4027 goto out;
4028 }
4029
Christoph Lameterc9819f42006-12-10 02:20:25 -08004030 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07004031 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004032 /*
4033 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004034 * longer idle, or one of our SMT siblings is
4035 * not idle.
4036 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004037 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004039 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08004041 if (sd->flags & SD_SERIALIZE)
4042 spin_unlock(&balancing);
4043out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004044 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004045 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004046 update_next_balance = 1;
4047 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004048
4049 /*
4050 * Stop the load balance at this level. There is another
4051 * CPU in our sched group which is doing load balancing more
4052 * actively.
4053 */
4054 if (!balance)
4055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004057
4058 /*
4059 * next_balance will be updated only when there is a need.
4060 * When the cpu is attached to null domain for ex, it will not be
4061 * updated.
4062 */
4063 if (likely(update_next_balance))
4064 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004065}
4066
4067/*
4068 * run_rebalance_domains is triggered when needed from the scheduler tick.
4069 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4070 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4071 */
4072static void run_rebalance_domains(struct softirq_action *h)
4073{
Ingo Molnardd41f592007-07-09 18:51:59 +02004074 int this_cpu = smp_processor_id();
4075 struct rq *this_rq = cpu_rq(this_cpu);
4076 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4077 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004078
Ingo Molnardd41f592007-07-09 18:51:59 +02004079 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004080
4081#ifdef CONFIG_NO_HZ
4082 /*
4083 * If this cpu is the owner for idle load balancing, then do the
4084 * balancing on behalf of the other idle cpus whose ticks are
4085 * stopped.
4086 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004087 if (this_rq->idle_at_tick &&
4088 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004089 cpumask_t cpus = nohz.cpu_mask;
4090 struct rq *rq;
4091 int balance_cpu;
4092
Ingo Molnardd41f592007-07-09 18:51:59 +02004093 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004094 for_each_cpu_mask(balance_cpu, cpus) {
4095 /*
4096 * If this cpu gets work to do, stop the load balancing
4097 * work being done for other cpus. Next load
4098 * balancing owner will pick it up.
4099 */
4100 if (need_resched())
4101 break;
4102
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004103 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004104
4105 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004106 if (time_after(this_rq->next_balance, rq->next_balance))
4107 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004108 }
4109 }
4110#endif
4111}
4112
4113/*
4114 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4115 *
4116 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4117 * idle load balancing owner or decide to stop the periodic load balancing,
4118 * if the whole system is idle.
4119 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004120static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004121{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004122#ifdef CONFIG_NO_HZ
4123 /*
4124 * If we were in the nohz mode recently and busy at the current
4125 * scheduler tick, then check if we need to nominate new idle
4126 * load balancer.
4127 */
4128 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4129 rq->in_nohz_recently = 0;
4130
4131 if (atomic_read(&nohz.load_balancer) == cpu) {
4132 cpu_clear(cpu, nohz.cpu_mask);
4133 atomic_set(&nohz.load_balancer, -1);
4134 }
4135
4136 if (atomic_read(&nohz.load_balancer) == -1) {
4137 /*
4138 * simple selection for now: Nominate the
4139 * first cpu in the nohz list to be the next
4140 * ilb owner.
4141 *
4142 * TBD: Traverse the sched domains and nominate
4143 * the nearest cpu in the nohz.cpu_mask.
4144 */
4145 int ilb = first_cpu(nohz.cpu_mask);
4146
Mike Travis434d53b2008-04-04 18:11:04 -07004147 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004148 resched_cpu(ilb);
4149 }
4150 }
4151
4152 /*
4153 * If this cpu is idle and doing idle load balancing for all the
4154 * cpus with ticks stopped, is it time for that to stop?
4155 */
4156 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4157 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4158 resched_cpu(cpu);
4159 return;
4160 }
4161
4162 /*
4163 * If this cpu is idle and the idle load balancing is done by
4164 * someone else, then no need raise the SCHED_SOFTIRQ
4165 */
4166 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4167 cpu_isset(cpu, nohz.cpu_mask))
4168 return;
4169#endif
4170 if (time_after_eq(jiffies, rq->next_balance))
4171 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
Ingo Molnardd41f592007-07-09 18:51:59 +02004173
4174#else /* CONFIG_SMP */
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176/*
4177 * on UP we do not need to balance between CPUs:
4178 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004179static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
4181}
Ingo Molnardd41f592007-07-09 18:51:59 +02004182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183#endif
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185DEFINE_PER_CPU(struct kernel_stat, kstat);
4186
4187EXPORT_PER_CPU_SYMBOL(kstat);
4188
4189/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004190 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4191 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004193unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004196 u64 ns, delta_exec;
4197 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004198
Ingo Molnar41b86e92007-07-09 18:51:58 +02004199 rq = task_rq_lock(p, &flags);
4200 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004201 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004202 update_rq_clock(rq);
4203 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004204 if ((s64)delta_exec > 0)
4205 ns += delta_exec;
4206 }
4207 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return ns;
4210}
4211
4212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 * Account user cpu time to a process.
4214 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 * @cputime: the cpu time spent in user space since the last update
4216 */
4217void account_user_time(struct task_struct *p, cputime_t cputime)
4218{
4219 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4220 cputime64_t tmp;
4221
4222 p->utime = cputime_add(p->utime, cputime);
4223
4224 /* Add user time to cpustat. */
4225 tmp = cputime_to_cputime64(cputime);
4226 if (TASK_NICE(p) > 0)
4227 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4228 else
4229 cpustat->user = cputime64_add(cpustat->user, tmp);
4230}
4231
4232/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004233 * Account guest cpu time to a process.
4234 * @p: the process that the cpu time gets accounted to
4235 * @cputime: the cpu time spent in virtual machine since the last update
4236 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004237static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004238{
4239 cputime64_t tmp;
4240 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4241
4242 tmp = cputime_to_cputime64(cputime);
4243
4244 p->utime = cputime_add(p->utime, cputime);
4245 p->gtime = cputime_add(p->gtime, cputime);
4246
4247 cpustat->user = cputime64_add(cpustat->user, tmp);
4248 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4249}
4250
4251/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004252 * Account scaled user cpu time to a process.
4253 * @p: the process that the cpu time gets accounted to
4254 * @cputime: the cpu time spent in user space since the last update
4255 */
4256void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4257{
4258 p->utimescaled = cputime_add(p->utimescaled, cputime);
4259}
4260
4261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 * Account system cpu time to a process.
4263 * @p: the process that the cpu time gets accounted to
4264 * @hardirq_offset: the offset to subtract from hardirq_count()
4265 * @cputime: the cpu time spent in kernel space since the last update
4266 */
4267void account_system_time(struct task_struct *p, int hardirq_offset,
4268 cputime_t cputime)
4269{
4270 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004271 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 cputime64_t tmp;
4273
Christian Borntraeger97783852007-11-15 20:57:39 +01004274 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
4275 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 p->stime = cputime_add(p->stime, cputime);
4278
4279 /* Add system time to cpustat. */
4280 tmp = cputime_to_cputime64(cputime);
4281 if (hardirq_count() - hardirq_offset)
4282 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4283 else if (softirq_count())
4284 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004285 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004287 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4289 else
4290 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4291 /* Account for system time used */
4292 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
4295/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004296 * Account scaled system cpu time to a process.
4297 * @p: the process that the cpu time gets accounted to
4298 * @hardirq_offset: the offset to subtract from hardirq_count()
4299 * @cputime: the cpu time spent in kernel space since the last update
4300 */
4301void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4302{
4303 p->stimescaled = cputime_add(p->stimescaled, cputime);
4304}
4305
4306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 * Account for involuntary wait time.
4308 * @p: the process from which the cpu time has been stolen
4309 * @steal: the cpu time spent in involuntary wait
4310 */
4311void account_steal_time(struct task_struct *p, cputime_t steal)
4312{
4313 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4314 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004315 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 if (p == rq->idle) {
4318 p->stime = cputime_add(p->stime, steal);
4319 if (atomic_read(&rq->nr_iowait) > 0)
4320 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4321 else
4322 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004323 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4325}
4326
Christoph Lameter7835b982006-12-10 02:20:22 -08004327/*
4328 * This function gets called by the timer code, with HZ frequency.
4329 * We call it with interrupts disabled.
4330 *
4331 * It also gets called by the fork code, when changing the parent's
4332 * timeslices.
4333 */
4334void scheduler_tick(void)
4335{
Christoph Lameter7835b982006-12-10 02:20:22 -08004336 int cpu = smp_processor_id();
4337 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02004339 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08004340
Ingo Molnardd41f592007-07-09 18:51:59 +02004341 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02004342 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02004343 /*
4344 * Let rq->clock advance by at least TICK_NSEC:
4345 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004346 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02004347 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01004348 rq->clock_underflows++;
4349 }
Ingo Molnar529c7722007-08-10 23:05:11 +02004350 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02004351 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004352 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004353 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004354 spin_unlock(&rq->lock);
4355
Christoph Lametere418e1c2006-12-10 02:20:23 -08004356#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004357 rq->idle_at_tick = idle_cpu(cpu);
4358 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4363
Srinivasa Ds43627582008-02-23 15:24:04 -08004364void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 /*
4367 * Underflow?
4368 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004369 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 preempt_count() += val;
4372 /*
4373 * Spinlock count overflowing soon?
4374 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004375 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4376 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377}
4378EXPORT_SYMBOL(add_preempt_count);
4379
Srinivasa Ds43627582008-02-23 15:24:04 -08004380void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 /*
4383 * Underflow?
4384 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004385 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4386 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 /*
4388 * Is the spinlock portion underflowing?
4389 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004390 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4391 !(preempt_count() & PREEMPT_MASK)))
4392 return;
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 preempt_count() -= val;
4395}
4396EXPORT_SYMBOL(sub_preempt_count);
4397
4398#endif
4399
4400/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004401 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004403static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
Satyam Sharma838225b2007-10-24 18:23:50 +02004405 struct pt_regs *regs = get_irq_regs();
4406
4407 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4408 prev->comm, prev->pid, preempt_count());
4409
Ingo Molnardd41f592007-07-09 18:51:59 +02004410 debug_show_held_locks(prev);
4411 if (irqs_disabled())
4412 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004413
4414 if (regs)
4415 show_regs(regs);
4416 else
4417 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Ingo Molnardd41f592007-07-09 18:51:59 +02004420/*
4421 * Various schedule()-time debugging checks and statistics:
4422 */
4423static inline void schedule_debug(struct task_struct *prev)
4424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004426 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 * schedule() atomically, we ignore that path for now.
4428 * Otherwise, whine if we are scheduling when we should not be.
4429 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004430 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
4431 __schedule_bug(prev);
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4434
Ingo Molnar2d723762007-10-15 17:00:12 +02004435 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004436#ifdef CONFIG_SCHEDSTATS
4437 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004438 schedstat_inc(this_rq(), bkl_count);
4439 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004440 }
4441#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004442}
4443
4444/*
4445 * Pick up the highest-prio task:
4446 */
4447static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004448pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004449{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004450 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004451 struct task_struct *p;
4452
4453 /*
4454 * Optimization: we know that if all tasks are in
4455 * the fair class we can call that function directly:
4456 */
4457 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004458 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004459 if (likely(p))
4460 return p;
4461 }
4462
4463 class = sched_class_highest;
4464 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004465 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004466 if (p)
4467 return p;
4468 /*
4469 * Will never be NULL as the idle class always
4470 * returns a non-NULL p:
4471 */
4472 class = class->next;
4473 }
4474}
4475
4476/*
4477 * schedule() is the main scheduler function.
4478 */
4479asmlinkage void __sched schedule(void)
4480{
4481 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004482 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004483 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02004484 int cpu;
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486need_resched:
4487 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004488 cpu = smp_processor_id();
4489 rq = cpu_rq(cpu);
4490 rcu_qsctr_inc(cpu);
4491 prev = rq->curr;
4492 switch_count = &prev->nivcsw;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 release_kernel_lock(prev);
4495need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Ingo Molnardd41f592007-07-09 18:51:59 +02004497 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004499 hrtick_clear(rq);
4500
Ingo Molnar1e819952007-10-15 17:00:13 +02004501 /*
4502 * Do the rq-clock update outside the rq lock:
4503 */
4504 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004505 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004506 spin_lock(&rq->lock);
4507 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Ingo Molnardd41f592007-07-09 18:51:59 +02004509 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4510 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004511 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004512 prev->state = TASK_RUNNING;
4513 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004514 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004515 }
4516 switch_count = &prev->nvcsw;
4517 }
4518
Steven Rostedt9a897c52008-01-25 21:08:22 +01004519#ifdef CONFIG_SMP
4520 if (prev->sched_class->pre_schedule)
4521 prev->sched_class->pre_schedule(rq, prev);
4522#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004523
Ingo Molnardd41f592007-07-09 18:51:59 +02004524 if (unlikely(!rq->nr_running))
4525 idle_balance(cpu, rq);
4526
Ingo Molnar31ee5292007-08-09 11:16:49 +02004527 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004528 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 rq->nr_switches++;
4534 rq->curr = next;
4535 ++*switch_count;
4536
Ingo Molnardd41f592007-07-09 18:51:59 +02004537 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004538 /*
4539 * the context switch might have flipped the stack from under
4540 * us, hence refresh the local variables.
4541 */
4542 cpu = smp_processor_id();
4543 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 } else
4545 spin_unlock_irq(&rq->lock);
4546
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004547 hrtick_set(rq);
4548
4549 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 preempt_enable_no_resched();
4553 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4554 goto need_resched;
4555}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556EXPORT_SYMBOL(schedule);
4557
4558#ifdef CONFIG_PREEMPT
4559/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004560 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004561 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 * occur there and call schedule directly.
4563 */
4564asmlinkage void __sched preempt_schedule(void)
4565{
4566 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 struct task_struct *task = current;
4568 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 /*
4571 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004572 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004574 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return;
4576
Andi Kleen3a5c3592007-10-15 17:00:14 +02004577 do {
4578 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Andi Kleen3a5c3592007-10-15 17:00:14 +02004580 /*
4581 * We keep the big kernel semaphore locked, but we
4582 * clear ->lock_depth so that schedule() doesnt
4583 * auto-release the semaphore:
4584 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004585 saved_lock_depth = task->lock_depth;
4586 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004587 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004588 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004589 sub_preempt_count(PREEMPT_ACTIVE);
4590
4591 /*
4592 * Check again in case we missed a preemption opportunity
4593 * between schedule and now.
4594 */
4595 barrier();
4596 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598EXPORT_SYMBOL(preempt_schedule);
4599
4600/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004601 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 * off of irq context.
4603 * Note, that this is called and return with irqs disabled. This will
4604 * protect us against recursive calling from irq.
4605 */
4606asmlinkage void __sched preempt_schedule_irq(void)
4607{
4608 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 struct task_struct *task = current;
4610 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004611
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004612 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 BUG_ON(ti->preempt_count || !irqs_disabled());
4614
Andi Kleen3a5c3592007-10-15 17:00:14 +02004615 do {
4616 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Andi Kleen3a5c3592007-10-15 17:00:14 +02004618 /*
4619 * We keep the big kernel semaphore locked, but we
4620 * clear ->lock_depth so that schedule() doesnt
4621 * auto-release the semaphore:
4622 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004623 saved_lock_depth = task->lock_depth;
4624 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004625 local_irq_enable();
4626 schedule();
4627 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004628 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004629 sub_preempt_count(PREEMPT_ACTIVE);
4630
4631 /*
4632 * Check again in case we missed a preemption opportunity
4633 * between schedule and now.
4634 */
4635 barrier();
4636 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639#endif /* CONFIG_PREEMPT */
4640
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004641int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4642 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004644 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646EXPORT_SYMBOL(default_wake_function);
4647
4648/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004649 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4650 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 * number) then we wake all the non-exclusive tasks and one exclusive task.
4652 *
4653 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004654 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4656 */
4657static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4658 int nr_exclusive, int sync, void *key)
4659{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004660 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004662 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004663 unsigned flags = curr->flags;
4664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004666 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 break;
4668 }
4669}
4670
4671/**
4672 * __wake_up - wake up threads blocked on a waitqueue.
4673 * @q: the waitqueue
4674 * @mode: which threads
4675 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004676 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004678void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004679 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
4681 unsigned long flags;
4682
4683 spin_lock_irqsave(&q->lock, flags);
4684 __wake_up_common(q, mode, nr_exclusive, 0, key);
4685 spin_unlock_irqrestore(&q->lock, flags);
4686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687EXPORT_SYMBOL(__wake_up);
4688
4689/*
4690 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4691 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004692void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
4694 __wake_up_common(q, mode, 1, 0, NULL);
4695}
4696
4697/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004698 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 * @q: the waitqueue
4700 * @mode: which threads
4701 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4702 *
4703 * The sync wakeup differs that the waker knows that it will schedule
4704 * away soon, so while the target thread will be woken up, it will not
4705 * be migrated to another CPU - ie. the two threads are 'synchronized'
4706 * with each other. This can prevent needless bouncing between CPUs.
4707 *
4708 * On UP it can prevent extra preemption.
4709 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004710void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004711__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
4713 unsigned long flags;
4714 int sync = 1;
4715
4716 if (unlikely(!q))
4717 return;
4718
4719 if (unlikely(!nr_exclusive))
4720 sync = 0;
4721
4722 spin_lock_irqsave(&q->lock, flags);
4723 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4724 spin_unlock_irqrestore(&q->lock, flags);
4725}
4726EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4727
Ingo Molnarb15136e2007-10-24 18:23:48 +02004728void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
4730 unsigned long flags;
4731
4732 spin_lock_irqsave(&x->wait.lock, flags);
4733 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004734 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 spin_unlock_irqrestore(&x->wait.lock, flags);
4736}
4737EXPORT_SYMBOL(complete);
4738
Ingo Molnarb15136e2007-10-24 18:23:48 +02004739void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 unsigned long flags;
4742
4743 spin_lock_irqsave(&x->wait.lock, flags);
4744 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004745 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 spin_unlock_irqrestore(&x->wait.lock, flags);
4747}
4748EXPORT_SYMBOL(complete_all);
4749
Andi Kleen8cbbe862007-10-15 17:00:14 +02004750static inline long __sched
4751do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (!x->done) {
4754 DECLARE_WAITQUEUE(wait, current);
4755
4756 wait.flags |= WQ_FLAG_EXCLUSIVE;
4757 __add_wait_queue_tail(&x->wait, &wait);
4758 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004759 if ((state == TASK_INTERRUPTIBLE &&
4760 signal_pending(current)) ||
4761 (state == TASK_KILLABLE &&
4762 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004763 __remove_wait_queue(&x->wait, &wait);
4764 return -ERESTARTSYS;
4765 }
4766 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004768 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004770 if (!timeout) {
4771 __remove_wait_queue(&x->wait, &wait);
4772 return timeout;
4773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 } while (!x->done);
4775 __remove_wait_queue(&x->wait, &wait);
4776 }
4777 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004778 return timeout;
4779}
4780
4781static long __sched
4782wait_for_common(struct completion *x, long timeout, int state)
4783{
4784 might_sleep();
4785
4786 spin_lock_irq(&x->wait.lock);
4787 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004789 return timeout;
4790}
4791
Ingo Molnarb15136e2007-10-24 18:23:48 +02004792void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004793{
4794 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796EXPORT_SYMBOL(wait_for_completion);
4797
Ingo Molnarb15136e2007-10-24 18:23:48 +02004798unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4800{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004801 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803EXPORT_SYMBOL(wait_for_completion_timeout);
4804
Andi Kleen8cbbe862007-10-15 17:00:14 +02004805int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Andi Kleen51e97992007-10-18 21:32:55 +02004807 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4808 if (t == -ERESTARTSYS)
4809 return t;
4810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
4812EXPORT_SYMBOL(wait_for_completion_interruptible);
4813
Ingo Molnarb15136e2007-10-24 18:23:48 +02004814unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815wait_for_completion_interruptible_timeout(struct completion *x,
4816 unsigned long timeout)
4817{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004818 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
4820EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4821
Matthew Wilcox009e5772007-12-06 12:29:54 -05004822int __sched wait_for_completion_killable(struct completion *x)
4823{
4824 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4825 if (t == -ERESTARTSYS)
4826 return t;
4827 return 0;
4828}
4829EXPORT_SYMBOL(wait_for_completion_killable);
4830
Andi Kleen8cbbe862007-10-15 17:00:14 +02004831static long __sched
4832sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004833{
4834 unsigned long flags;
4835 wait_queue_t wait;
4836
4837 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Andi Kleen8cbbe862007-10-15 17:00:14 +02004839 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Andi Kleen8cbbe862007-10-15 17:00:14 +02004841 spin_lock_irqsave(&q->lock, flags);
4842 __add_wait_queue(q, &wait);
4843 spin_unlock(&q->lock);
4844 timeout = schedule_timeout(timeout);
4845 spin_lock_irq(&q->lock);
4846 __remove_wait_queue(q, &wait);
4847 spin_unlock_irqrestore(&q->lock, flags);
4848
4849 return timeout;
4850}
4851
4852void __sched interruptible_sleep_on(wait_queue_head_t *q)
4853{
4854 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856EXPORT_SYMBOL(interruptible_sleep_on);
4857
Ingo Molnar0fec1712007-07-09 18:52:01 +02004858long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004859interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004861 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4864
Ingo Molnar0fec1712007-07-09 18:52:01 +02004865void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004867 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869EXPORT_SYMBOL(sleep_on);
4870
Ingo Molnar0fec1712007-07-09 18:52:01 +02004871long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004873 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875EXPORT_SYMBOL(sleep_on_timeout);
4876
Ingo Molnarb29739f2006-06-27 02:54:51 -07004877#ifdef CONFIG_RT_MUTEXES
4878
4879/*
4880 * rt_mutex_setprio - set the current priority of a task
4881 * @p: task
4882 * @prio: prio value (kernel-internal form)
4883 *
4884 * This function changes the 'effective' priority of a task. It does
4885 * not touch ->normal_prio like __setscheduler().
4886 *
4887 * Used by the rt_mutex code to implement priority inheritance logic.
4888 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004889void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004890{
4891 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004892 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004893 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004894 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004895
4896 BUG_ON(prio < 0 || prio > MAX_PRIO);
4897
4898 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004899 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004900
Andrew Mortond5f9f942007-05-08 20:27:06 -07004901 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004902 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004903 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004904 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004905 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004906 if (running)
4907 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004908
4909 if (rt_prio(prio))
4910 p->sched_class = &rt_sched_class;
4911 else
4912 p->sched_class = &fair_sched_class;
4913
Ingo Molnarb29739f2006-06-27 02:54:51 -07004914 p->prio = prio;
4915
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004916 if (running)
4917 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004918 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004919 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004920
4921 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004922 }
4923 task_rq_unlock(rq, &flags);
4924}
4925
4926#endif
4927
Ingo Molnar36c8b582006-07-03 00:25:41 -07004928void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
Ingo Molnardd41f592007-07-09 18:51:59 +02004930 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004932 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4935 return;
4936 /*
4937 * We have to be careful, if called from sys_setpriority(),
4938 * the task might be in the middle of scheduling on another CPU.
4939 */
4940 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004941 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 /*
4943 * The RT priorities are set via sched_setscheduler(), but we still
4944 * allow the 'normal' nice value to be set - but as expected
4945 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004946 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004948 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 p->static_prio = NICE_TO_PRIO(nice);
4950 goto out_unlock;
4951 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004952 on_rq = p->se.on_rq;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02004953 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004954 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004957 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004958 old_prio = p->prio;
4959 p->prio = effective_prio(p);
4960 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Ingo Molnardd41f592007-07-09 18:51:59 +02004962 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004963 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004965 * If the task increased its priority or is running and
4966 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004968 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 resched_task(rq->curr);
4970 }
4971out_unlock:
4972 task_rq_unlock(rq, &flags);
4973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974EXPORT_SYMBOL(set_user_nice);
4975
Matt Mackalle43379f2005-05-01 08:59:00 -07004976/*
4977 * can_nice - check if a task can reduce its nice value
4978 * @p: task
4979 * @nice: nice value
4980 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004981int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004982{
Matt Mackall024f4742005-08-18 11:24:19 -07004983 /* convert nice value [19,-20] to rlimit style value [1,40] */
4984 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004985
Matt Mackalle43379f2005-05-01 08:59:00 -07004986 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4987 capable(CAP_SYS_NICE));
4988}
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990#ifdef __ARCH_WANT_SYS_NICE
4991
4992/*
4993 * sys_nice - change the priority of the current process.
4994 * @increment: priority increment
4995 *
4996 * sys_setpriority is a more generic, but much slower function that
4997 * does similar things.
4998 */
4999asmlinkage long sys_nice(int increment)
5000{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005001 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 /*
5004 * Setpriority might change our priority at the same moment.
5005 * We don't have to worry. Conceptually one call occurs first
5006 * and we have a single winner.
5007 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005008 if (increment < -40)
5009 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 if (increment > 40)
5011 increment = 40;
5012
5013 nice = PRIO_TO_NICE(current->static_prio) + increment;
5014 if (nice < -20)
5015 nice = -20;
5016 if (nice > 19)
5017 nice = 19;
5018
Matt Mackalle43379f2005-05-01 08:59:00 -07005019 if (increment < 0 && !can_nice(current, nice))
5020 return -EPERM;
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 retval = security_task_setnice(current, nice);
5023 if (retval)
5024 return retval;
5025
5026 set_user_nice(current, nice);
5027 return 0;
5028}
5029
5030#endif
5031
5032/**
5033 * task_prio - return the priority value of a given task.
5034 * @p: the task in question.
5035 *
5036 * This is the priority value as seen by users in /proc.
5037 * RT tasks are offset by -200. Normal tasks are centered
5038 * around 0, value goes from -16 to +15.
5039 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005040int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
5042 return p->prio - MAX_RT_PRIO;
5043}
5044
5045/**
5046 * task_nice - return the nice value of a given task.
5047 * @p: the task in question.
5048 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005049int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050{
5051 return TASK_NICE(p);
5052}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005053EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055/**
5056 * idle_cpu - is a given cpu idle currently?
5057 * @cpu: the processor in question.
5058 */
5059int idle_cpu(int cpu)
5060{
5061 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5062}
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064/**
5065 * idle_task - return the idle task for a given cpu.
5066 * @cpu: the processor in question.
5067 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005068struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
5070 return cpu_rq(cpu)->idle;
5071}
5072
5073/**
5074 * find_process_by_pid - find a process with a matching PID value.
5075 * @pid: the pid in question.
5076 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005077static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005079 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080}
5081
5082/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005083static void
5084__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
Ingo Molnardd41f592007-07-09 18:51:59 +02005086 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005089 switch (p->policy) {
5090 case SCHED_NORMAL:
5091 case SCHED_BATCH:
5092 case SCHED_IDLE:
5093 p->sched_class = &fair_sched_class;
5094 break;
5095 case SCHED_FIFO:
5096 case SCHED_RR:
5097 p->sched_class = &rt_sched_class;
5098 break;
5099 }
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005102 p->normal_prio = normal_prio(p);
5103 /* we are holding p->pi_lock already */
5104 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005105 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
5108/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005109 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 * @p: the task in question.
5111 * @policy: new policy.
5112 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005113 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005114 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005116int sched_setscheduler(struct task_struct *p, int policy,
5117 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005119 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005121 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005122 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Steven Rostedt66e53932006-06-27 02:54:44 -07005124 /* may grab non-irq protected spin_locks */
5125 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126recheck:
5127 /* double check policy once rq lock held */
5128 if (policy < 0)
5129 policy = oldpolicy = p->policy;
5130 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005131 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5132 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005133 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 /*
5135 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005136 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5137 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 */
5139 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005140 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005141 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005143 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return -EINVAL;
5145
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005146 /*
5147 * Allow unprivileged RT tasks to decrease priority:
5148 */
5149 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005150 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005151 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005152
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005153 if (!lock_task_sighand(p, &flags))
5154 return -ESRCH;
5155 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5156 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005157
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005158 /* can't set/change the rt policy */
5159 if (policy != p->policy && !rlim_rtprio)
5160 return -EPERM;
5161
5162 /* can't increase priority */
5163 if (param->sched_priority > p->rt_priority &&
5164 param->sched_priority > rlim_rtprio)
5165 return -EPERM;
5166 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005167 /*
5168 * Like positive nice levels, dont allow tasks to
5169 * move out of SCHED_IDLE either:
5170 */
5171 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5172 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005173
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005174 /* can't change other user's priorities */
5175 if ((current->euid != p->euid) &&
5176 (current->euid != p->uid))
5177 return -EPERM;
5178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005180#ifdef CONFIG_RT_GROUP_SCHED
5181 /*
5182 * Do not allow realtime tasks into groups that have no runtime
5183 * assigned.
5184 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005185 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005186 return -EPERM;
5187#endif
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 retval = security_task_setscheduler(p, policy, param);
5190 if (retval)
5191 return retval;
5192 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005193 * make sure no PI-waiters arrive (or leave) while we are
5194 * changing the priority of the task:
5195 */
5196 spin_lock_irqsave(&p->pi_lock, flags);
5197 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 * To be able to change p->policy safely, the apropriate
5199 * runqueue lock must be held.
5200 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005201 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 /* recheck policy now with rq lock held */
5203 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5204 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005205 __task_rq_unlock(rq);
5206 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 goto recheck;
5208 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005209 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005210 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005211 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005212 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005213 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005214 if (running)
5215 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005218 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005219
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005220 if (running)
5221 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005222 if (on_rq) {
5223 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005224
5225 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005227 __task_rq_unlock(rq);
5228 spin_unlock_irqrestore(&p->pi_lock, flags);
5229
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005230 rt_mutex_adjust_pi(p);
5231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 return 0;
5233}
5234EXPORT_SYMBOL_GPL(sched_setscheduler);
5235
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005236static int
5237do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 struct sched_param lparam;
5240 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005241 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 if (!param || pid < 0)
5244 return -EINVAL;
5245 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5246 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005247
5248 rcu_read_lock();
5249 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005251 if (p != NULL)
5252 retval = sched_setscheduler(p, policy, &lparam);
5253 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return retval;
5256}
5257
5258/**
5259 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5260 * @pid: the pid in question.
5261 * @policy: new policy.
5262 * @param: structure containing the new RT priority.
5263 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005264asmlinkage long
5265sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
Jason Baronc21761f2006-01-18 17:43:03 -08005267 /* negative values for policy are not valid */
5268 if (policy < 0)
5269 return -EINVAL;
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 return do_sched_setscheduler(pid, policy, param);
5272}
5273
5274/**
5275 * sys_sched_setparam - set/change the RT priority of a thread
5276 * @pid: the pid in question.
5277 * @param: structure containing the new RT priority.
5278 */
5279asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5280{
5281 return do_sched_setscheduler(pid, -1, param);
5282}
5283
5284/**
5285 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5286 * @pid: the pid in question.
5287 */
5288asmlinkage long sys_sched_getscheduler(pid_t pid)
5289{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005290 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005291 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 retval = -ESRCH;
5297 read_lock(&tasklist_lock);
5298 p = find_process_by_pid(pid);
5299 if (p) {
5300 retval = security_task_getscheduler(p);
5301 if (!retval)
5302 retval = p->policy;
5303 }
5304 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 return retval;
5306}
5307
5308/**
5309 * sys_sched_getscheduler - get the RT priority of a thread
5310 * @pid: the pid in question.
5311 * @param: structure containing the RT priority.
5312 */
5313asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5314{
5315 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005316 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005317 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
5319 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005320 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
5322 read_lock(&tasklist_lock);
5323 p = find_process_by_pid(pid);
5324 retval = -ESRCH;
5325 if (!p)
5326 goto out_unlock;
5327
5328 retval = security_task_getscheduler(p);
5329 if (retval)
5330 goto out_unlock;
5331
5332 lp.sched_priority = p->rt_priority;
5333 read_unlock(&tasklist_lock);
5334
5335 /*
5336 * This one might sleep, we cannot do it with a spinlock held ...
5337 */
5338 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 return retval;
5341
5342out_unlock:
5343 read_unlock(&tasklist_lock);
5344 return retval;
5345}
5346
Mike Travisb53e9212008-04-04 18:11:08 -07005347long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005350 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005351 struct task_struct *p;
5352 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005354 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 read_lock(&tasklist_lock);
5356
5357 p = find_process_by_pid(pid);
5358 if (!p) {
5359 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005360 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 return -ESRCH;
5362 }
5363
5364 /*
5365 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005366 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 * usage count and then drop tasklist_lock.
5368 */
5369 get_task_struct(p);
5370 read_unlock(&tasklist_lock);
5371
5372 retval = -EPERM;
5373 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5374 !capable(CAP_SYS_NICE))
5375 goto out_unlock;
5376
David Quigleye7834f82006-06-23 02:03:59 -07005377 retval = security_task_setscheduler(p, 0, NULL);
5378 if (retval)
5379 goto out_unlock;
5380
Mike Travisf9a86fc2008-04-04 18:11:07 -07005381 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005383 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005384 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Paul Menage8707d8b2007-10-18 23:40:22 -07005386 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005387 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005388 if (!cpus_subset(new_mask, cpus_allowed)) {
5389 /*
5390 * We must have raced with a concurrent cpuset
5391 * update. Just reset the cpus_allowed to the
5392 * cpuset's cpus_allowed
5393 */
5394 new_mask = cpus_allowed;
5395 goto again;
5396 }
5397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398out_unlock:
5399 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005400 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 return retval;
5402}
5403
5404static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5405 cpumask_t *new_mask)
5406{
5407 if (len < sizeof(cpumask_t)) {
5408 memset(new_mask, 0, sizeof(cpumask_t));
5409 } else if (len > sizeof(cpumask_t)) {
5410 len = sizeof(cpumask_t);
5411 }
5412 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5413}
5414
5415/**
5416 * sys_sched_setaffinity - set the cpu affinity of a process
5417 * @pid: pid of the process
5418 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5419 * @user_mask_ptr: user-space pointer to the new cpu mask
5420 */
5421asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5422 unsigned long __user *user_mask_ptr)
5423{
5424 cpumask_t new_mask;
5425 int retval;
5426
5427 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5428 if (retval)
5429 return retval;
5430
Mike Travisb53e9212008-04-04 18:11:08 -07005431 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
5434/*
5435 * Represents all cpu's present in the system
5436 * In systems capable of hotplug, this map could dynamically grow
5437 * as new cpu's are detected in the system via any platform specific
5438 * method, such as ACPI for e.g.
5439 */
5440
Andi Kleen4cef0c62006-01-11 22:44:57 +01005441cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442EXPORT_SYMBOL(cpu_present_map);
5443
5444#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01005445cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005446EXPORT_SYMBOL(cpu_online_map);
5447
Andi Kleen4cef0c62006-01-11 22:44:57 +01005448cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07005449EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450#endif
5451
5452long sched_getaffinity(pid_t pid, cpumask_t *mask)
5453{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005454 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005457 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 read_lock(&tasklist_lock);
5459
5460 retval = -ESRCH;
5461 p = find_process_by_pid(pid);
5462 if (!p)
5463 goto out_unlock;
5464
David Quigleye7834f82006-06-23 02:03:59 -07005465 retval = security_task_getscheduler(p);
5466 if (retval)
5467 goto out_unlock;
5468
Jack Steiner2f7016d2006-02-01 03:05:18 -08005469 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471out_unlock:
5472 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005473 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Ulrich Drepper9531b622007-08-09 11:16:46 +02005475 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
5478/**
5479 * sys_sched_getaffinity - get the cpu affinity of a process
5480 * @pid: pid of the process
5481 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5482 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5483 */
5484asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5485 unsigned long __user *user_mask_ptr)
5486{
5487 int ret;
5488 cpumask_t mask;
5489
5490 if (len < sizeof(cpumask_t))
5491 return -EINVAL;
5492
5493 ret = sched_getaffinity(pid, &mask);
5494 if (ret < 0)
5495 return ret;
5496
5497 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5498 return -EFAULT;
5499
5500 return sizeof(cpumask_t);
5501}
5502
5503/**
5504 * sys_sched_yield - yield the current processor to other threads.
5505 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005506 * This function yields the current CPU to other tasks. If there are no
5507 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 */
5509asmlinkage long sys_sched_yield(void)
5510{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005511 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Ingo Molnar2d723762007-10-15 17:00:12 +02005513 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005514 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
5516 /*
5517 * Since we are going to call schedule() anyway, there's
5518 * no need to preempt or enable interrupts:
5519 */
5520 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005521 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 _raw_spin_unlock(&rq->lock);
5523 preempt_enable_no_resched();
5524
5525 schedule();
5526
5527 return 0;
5528}
5529
Andrew Mortone7b38402006-06-30 01:56:00 -07005530static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005532#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5533 __might_sleep(__FILE__, __LINE__);
5534#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005535 /*
5536 * The BKS might be reacquired before we have dropped
5537 * PREEMPT_ACTIVE, which could trigger a second
5538 * cond_resched() call.
5539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 do {
5541 add_preempt_count(PREEMPT_ACTIVE);
5542 schedule();
5543 sub_preempt_count(PREEMPT_ACTIVE);
5544 } while (need_resched());
5545}
5546
Herbert Xu02b67cc32008-01-25 21:08:28 +01005547#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5548int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
Ingo Molnar94142322006-12-29 16:48:13 -08005550 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5551 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 __cond_resched();
5553 return 1;
5554 }
5555 return 0;
5556}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005557EXPORT_SYMBOL(_cond_resched);
5558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560/*
5561 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5562 * call schedule, and on return reacquire the lock.
5563 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005564 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 * operations here to prevent schedule() from being called twice (once via
5566 * spin_unlock(), once by hand).
5567 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005568int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
Nick Piggin95c354f2008-01-30 13:31:20 +01005570 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005571 int ret = 0;
5572
Nick Piggin95c354f2008-01-30 13:31:20 +01005573 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005575 if (resched && need_resched())
5576 __cond_resched();
5577 else
5578 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005579 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005582 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584EXPORT_SYMBOL(cond_resched_lock);
5585
5586int __sched cond_resched_softirq(void)
5587{
5588 BUG_ON(!in_softirq());
5589
Ingo Molnar94142322006-12-29 16:48:13 -08005590 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005591 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 __cond_resched();
5593 local_bh_disable();
5594 return 1;
5595 }
5596 return 0;
5597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598EXPORT_SYMBOL(cond_resched_softirq);
5599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600/**
5601 * yield - yield the current processor to other threads.
5602 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005603 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 * thread runnable and calls sys_sched_yield().
5605 */
5606void __sched yield(void)
5607{
5608 set_current_state(TASK_RUNNING);
5609 sys_sched_yield();
5610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611EXPORT_SYMBOL(yield);
5612
5613/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005614 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 * that process accounting knows that this is a task in IO wait state.
5616 *
5617 * But don't do that if it is a deliberate, throttling IO wait (this task
5618 * has set its backing_dev_info: the queue against which it should throttle)
5619 */
5620void __sched io_schedule(void)
5621{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005622 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005624 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 atomic_inc(&rq->nr_iowait);
5626 schedule();
5627 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005628 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630EXPORT_SYMBOL(io_schedule);
5631
5632long __sched io_schedule_timeout(long timeout)
5633{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005634 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 long ret;
5636
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005637 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 atomic_inc(&rq->nr_iowait);
5639 ret = schedule_timeout(timeout);
5640 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005641 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 return ret;
5643}
5644
5645/**
5646 * sys_sched_get_priority_max - return maximum RT priority.
5647 * @policy: scheduling class.
5648 *
5649 * this syscall returns the maximum rt_priority that can be used
5650 * by a given scheduling class.
5651 */
5652asmlinkage long sys_sched_get_priority_max(int policy)
5653{
5654 int ret = -EINVAL;
5655
5656 switch (policy) {
5657 case SCHED_FIFO:
5658 case SCHED_RR:
5659 ret = MAX_USER_RT_PRIO-1;
5660 break;
5661 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005662 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005663 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 ret = 0;
5665 break;
5666 }
5667 return ret;
5668}
5669
5670/**
5671 * sys_sched_get_priority_min - return minimum RT priority.
5672 * @policy: scheduling class.
5673 *
5674 * this syscall returns the minimum rt_priority that can be used
5675 * by a given scheduling class.
5676 */
5677asmlinkage long sys_sched_get_priority_min(int policy)
5678{
5679 int ret = -EINVAL;
5680
5681 switch (policy) {
5682 case SCHED_FIFO:
5683 case SCHED_RR:
5684 ret = 1;
5685 break;
5686 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005687 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005688 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 ret = 0;
5690 }
5691 return ret;
5692}
5693
5694/**
5695 * sys_sched_rr_get_interval - return the default timeslice of a process.
5696 * @pid: pid of the process.
5697 * @interval: userspace pointer to the timeslice value.
5698 *
5699 * this syscall writes the default timeslice value of a given process
5700 * into the user-space timespec buffer. A value of '0' means infinity.
5701 */
5702asmlinkage
5703long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5704{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005705 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005706 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005707 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005711 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713 retval = -ESRCH;
5714 read_lock(&tasklist_lock);
5715 p = find_process_by_pid(pid);
5716 if (!p)
5717 goto out_unlock;
5718
5719 retval = security_task_getscheduler(p);
5720 if (retval)
5721 goto out_unlock;
5722
Ingo Molnar77034932007-12-04 17:04:39 +01005723 /*
5724 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5725 * tasks that are on an otherwise idle runqueue:
5726 */
5727 time_slice = 0;
5728 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005729 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005730 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005731 struct sched_entity *se = &p->se;
5732 unsigned long flags;
5733 struct rq *rq;
5734
5735 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005736 if (rq->cfs.load.weight)
5737 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005738 task_rq_unlock(rq, &flags);
5739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005741 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745out_unlock:
5746 read_unlock(&tasklist_lock);
5747 return retval;
5748}
5749
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005750static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005751
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005752void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005755 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005758 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005759 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005760#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005762 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005764 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765#else
5766 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005767 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005769 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770#endif
5771#ifdef CONFIG_DEBUG_STACK_USAGE
5772 {
Al Viro10ebffd2005-11-13 16:06:56 -08005773 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 while (!*n)
5775 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005776 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 }
5778#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005779 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005780 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005782 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783}
5784
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005785void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005787 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Ingo Molnar4bd77322007-07-11 21:21:47 +02005789#if BITS_PER_LONG == 32
5790 printk(KERN_INFO
5791 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005793 printk(KERN_INFO
5794 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795#endif
5796 read_lock(&tasklist_lock);
5797 do_each_thread(g, p) {
5798 /*
5799 * reset the NMI-timeout, listing all files on a slow
5800 * console might take alot of time:
5801 */
5802 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005803 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005804 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 } while_each_thread(g, p);
5806
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005807 touch_all_softlockup_watchdogs();
5808
Ingo Molnardd41f592007-07-09 18:51:59 +02005809#ifdef CONFIG_SCHED_DEBUG
5810 sysrq_sched_debug_show();
5811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005813 /*
5814 * Only show locks if all tasks are dumped:
5815 */
5816 if (state_filter == -1)
5817 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818}
5819
Ingo Molnar1df21052007-07-09 18:51:58 +02005820void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5821{
Ingo Molnardd41f592007-07-09 18:51:59 +02005822 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005823}
5824
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005825/**
5826 * init_idle - set up an idle thread for a given CPU
5827 * @idle: task in question
5828 * @cpu: cpu the idle task belongs to
5829 *
5830 * NOTE: this function does not set the idle thread's NEED_RESCHED
5831 * flag, to make booting more robust.
5832 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005833void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005835 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 unsigned long flags;
5837
Ingo Molnardd41f592007-07-09 18:51:59 +02005838 __sched_fork(idle);
5839 idle->se.exec_start = sched_clock();
5840
Ingo Molnarb29739f2006-06-27 02:54:51 -07005841 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005843 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 spin_lock_irqsave(&rq->lock, flags);
5846 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005847#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5848 idle->oncpu = 1;
5849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 spin_unlock_irqrestore(&rq->lock, flags);
5851
5852 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005853 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005854
Ingo Molnardd41f592007-07-09 18:51:59 +02005855 /*
5856 * The idle tasks have their own, simple scheduling class:
5857 */
5858 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859}
5860
5861/*
5862 * In a system that switches off the HZ timer nohz_cpu_mask
5863 * indicates which cpus entered this state. This is used
5864 * in the rcu update to wait only for active cpus. For system
5865 * which do not switch off the HZ timer nohz_cpu_mask should
5866 * always be CPU_MASK_NONE.
5867 */
5868cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5869
Ingo Molnar19978ca2007-11-09 22:39:38 +01005870/*
5871 * Increase the granularity value when there are more CPUs,
5872 * because with more CPUs the 'effective latency' as visible
5873 * to users decreases. But the relationship is not linear,
5874 * so pick a second-best guess by going with the log2 of the
5875 * number of CPUs.
5876 *
5877 * This idea comes from the SD scheduler of Con Kolivas:
5878 */
5879static inline void sched_init_granularity(void)
5880{
5881 unsigned int factor = 1 + ilog2(num_online_cpus());
5882 const unsigned long limit = 200000000;
5883
5884 sysctl_sched_min_granularity *= factor;
5885 if (sysctl_sched_min_granularity > limit)
5886 sysctl_sched_min_granularity = limit;
5887
5888 sysctl_sched_latency *= factor;
5889 if (sysctl_sched_latency > limit)
5890 sysctl_sched_latency = limit;
5891
5892 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005893}
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895#ifdef CONFIG_SMP
5896/*
5897 * This is how migration works:
5898 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005899 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 * runqueue and wake up that CPU's migration thread.
5901 * 2) we down() the locked semaphore => thread blocks.
5902 * 3) migration thread wakes up (implicitly it forces the migrated
5903 * thread off the CPU)
5904 * 4) it gets the migration request and checks whether the migrated
5905 * task is still in the wrong runqueue.
5906 * 5) if it's in the wrong runqueue then the migration thread removes
5907 * it and puts it into the right queue.
5908 * 6) migration thread up()s the semaphore.
5909 * 7) we wake up and the migration is done.
5910 */
5911
5912/*
5913 * Change a given task's CPU affinity. Migrate the thread to a
5914 * proper CPU and schedule it away if the CPU it's executing on
5915 * is removed from the allowed bitmask.
5916 *
5917 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005918 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 * call is not atomic; no spinlocks may be held.
5920 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005921int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005923 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005925 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005926 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
5928 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005929 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 ret = -EINVAL;
5931 goto out;
5932 }
5933
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005934 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005935 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005936 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005937 p->cpus_allowed = *new_mask;
5938 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005939 }
5940
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005942 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 goto out;
5944
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005945 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 /* Need help from migration thread: drop lock and wait. */
5947 task_rq_unlock(rq, &flags);
5948 wake_up_process(rq->migration_thread);
5949 wait_for_completion(&req.done);
5950 tlb_migrate_finish(p->mm);
5951 return 0;
5952 }
5953out:
5954 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 return ret;
5957}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005958EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005961 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 * this because either it can't run here any more (set_cpus_allowed()
5963 * away from this CPU, or CPU going down), or because we're
5964 * attempting to rebalance this task on exec (sched_exec).
5965 *
5966 * So we race with normal scheduler movements, but that's OK, as long
5967 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005968 *
5969 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005971static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005973 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005974 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 rq_src = cpu_rq(src_cpu);
5980 rq_dest = cpu_rq(dest_cpu);
5981
5982 double_rq_lock(rq_src, rq_dest);
5983 /* Already moved. */
5984 if (task_cpu(p) != src_cpu)
5985 goto out;
5986 /* Affinity changed (again). */
5987 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5988 goto out;
5989
Ingo Molnardd41f592007-07-09 18:51:59 +02005990 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005991 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005992 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005995 if (on_rq) {
5996 activate_task(rq_dest, p, 0);
5997 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005999 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000out:
6001 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006002 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003}
6004
6005/*
6006 * migration_thread - this is a highprio system thread that performs
6007 * thread migration by bumping thread off CPU then 'pushing' onto
6008 * another runqueue.
6009 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006010static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006013 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 rq = cpu_rq(cpu);
6016 BUG_ON(rq->migration_thread != current);
6017
6018 set_current_state(TASK_INTERRUPTIBLE);
6019 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006020 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 spin_lock_irq(&rq->lock);
6024
6025 if (cpu_is_offline(cpu)) {
6026 spin_unlock_irq(&rq->lock);
6027 goto wait_to_die;
6028 }
6029
6030 if (rq->active_balance) {
6031 active_load_balance(rq, cpu);
6032 rq->active_balance = 0;
6033 }
6034
6035 head = &rq->migration_queue;
6036
6037 if (list_empty(head)) {
6038 spin_unlock_irq(&rq->lock);
6039 schedule();
6040 set_current_state(TASK_INTERRUPTIBLE);
6041 continue;
6042 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006043 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 list_del_init(head->next);
6045
Nick Piggin674311d2005-06-25 14:57:27 -07006046 spin_unlock(&rq->lock);
6047 __migrate_task(req->task, cpu, req->dest_cpu);
6048 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050 complete(&req->done);
6051 }
6052 __set_current_state(TASK_RUNNING);
6053 return 0;
6054
6055wait_to_die:
6056 /* Wait for kthread_stop */
6057 set_current_state(TASK_INTERRUPTIBLE);
6058 while (!kthread_should_stop()) {
6059 schedule();
6060 set_current_state(TASK_INTERRUPTIBLE);
6061 }
6062 __set_current_state(TASK_RUNNING);
6063 return 0;
6064}
6065
6066#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006067
6068static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6069{
6070 int ret;
6071
6072 local_irq_disable();
6073 ret = __migrate_task(p, src_cpu, dest_cpu);
6074 local_irq_enable();
6075 return ret;
6076}
6077
Kirill Korotaev054b9102006-12-10 02:20:11 -08006078/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006079 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006080 * NOTE: interrupts should be disabled by the caller
6081 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006082static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006084 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006086 struct rq *rq;
6087 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
Andi Kleen3a5c3592007-10-15 17:00:14 +02006089 do {
6090 /* On same node? */
6091 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6092 cpus_and(mask, mask, p->cpus_allowed);
6093 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Andi Kleen3a5c3592007-10-15 17:00:14 +02006095 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006096 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006097 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
Andi Kleen3a5c3592007-10-15 17:00:14 +02006099 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006100 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006101 cpumask_t cpus_allowed;
6102
6103 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006104 /*
6105 * Try to stay on the same cpuset, where the
6106 * current cpuset may be a subset of all cpus.
6107 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006108 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006109 * called within calls to cpuset_lock/cpuset_unlock.
6110 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006111 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006112 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006113 dest_cpu = any_online_cpu(p->cpus_allowed);
6114 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
Andi Kleen3a5c3592007-10-15 17:00:14 +02006116 /*
6117 * Don't tell them about moving exiting tasks or
6118 * kernel threads (both mm NULL), since they never
6119 * leave kernel.
6120 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006121 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006122 printk(KERN_INFO "process %d (%s) no "
6123 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006124 task_pid_nr(p), p->comm, dead_cpu);
6125 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006126 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006127 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128}
6129
6130/*
6131 * While a dead CPU has no uninterruptible tasks queued at this point,
6132 * it might still have a nonzero ->nr_uninterruptible counter, because
6133 * for performance reasons the counter is not stricly tracking tasks to
6134 * their home CPUs. So we just add the counter to another CPU's counter,
6135 * to keep the global sum constant after CPU-down:
6136 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006137static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138{
Mike Travis7c16ec52008-04-04 18:11:11 -07006139 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 unsigned long flags;
6141
6142 local_irq_save(flags);
6143 double_rq_lock(rq_src, rq_dest);
6144 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6145 rq_src->nr_uninterruptible = 0;
6146 double_rq_unlock(rq_src, rq_dest);
6147 local_irq_restore(flags);
6148}
6149
6150/* Run through task list and migrate tasks from the dead cpu. */
6151static void migrate_live_tasks(int src_cpu)
6152{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006153 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006155 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Ingo Molnar48f24c42006-07-03 00:25:40 -07006157 do_each_thread(t, p) {
6158 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 continue;
6160
Ingo Molnar48f24c42006-07-03 00:25:40 -07006161 if (task_cpu(p) == src_cpu)
6162 move_task_off_dead_cpu(src_cpu, p);
6163 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006165 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166}
6167
Ingo Molnardd41f592007-07-09 18:51:59 +02006168/*
6169 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006170 * It does so by boosting its priority to highest possible.
6171 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 */
6173void sched_idle_next(void)
6174{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006175 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006176 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 struct task_struct *p = rq->idle;
6178 unsigned long flags;
6179
6180 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006181 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
Ingo Molnar48f24c42006-07-03 00:25:40 -07006183 /*
6184 * Strictly not necessary since rest of the CPUs are stopped by now
6185 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 */
6187 spin_lock_irqsave(&rq->lock, flags);
6188
Ingo Molnardd41f592007-07-09 18:51:59 +02006189 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006190
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006191 update_rq_clock(rq);
6192 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
6194 spin_unlock_irqrestore(&rq->lock, flags);
6195}
6196
Ingo Molnar48f24c42006-07-03 00:25:40 -07006197/*
6198 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 * offline.
6200 */
6201void idle_task_exit(void)
6202{
6203 struct mm_struct *mm = current->active_mm;
6204
6205 BUG_ON(cpu_online(smp_processor_id()));
6206
6207 if (mm != &init_mm)
6208 switch_mm(mm, &init_mm, current);
6209 mmdrop(mm);
6210}
6211
Kirill Korotaev054b9102006-12-10 02:20:11 -08006212/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006213static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006215 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
6217 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006218 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
6220 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006221 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Ingo Molnar48f24c42006-07-03 00:25:40 -07006223 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 /*
6226 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006227 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 * fine.
6229 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006230 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006231 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006232 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Ingo Molnar48f24c42006-07-03 00:25:40 -07006234 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235}
6236
6237/* release_task() removes task from tasklist, so we won't find dead tasks. */
6238static void migrate_dead_tasks(unsigned int dead_cpu)
6239{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006240 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006241 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Ingo Molnardd41f592007-07-09 18:51:59 +02006243 for ( ; ; ) {
6244 if (!rq->nr_running)
6245 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006246 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006247 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006248 if (!next)
6249 break;
6250 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 }
6253}
6254#endif /* CONFIG_HOTPLUG_CPU */
6255
Nick Piggine692ab52007-07-26 13:40:43 +02006256#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6257
6258static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006259 {
6260 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006261 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006262 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006263 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006264};
6265
6266static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006267 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006268 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006269 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006270 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006271 .child = sd_ctl_dir,
6272 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006273 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006274};
6275
6276static struct ctl_table *sd_alloc_ctl_entry(int n)
6277{
6278 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006279 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006280
Nick Piggine692ab52007-07-26 13:40:43 +02006281 return entry;
6282}
6283
Milton Miller6382bc92007-10-15 17:00:19 +02006284static void sd_free_ctl_entry(struct ctl_table **tablep)
6285{
Milton Millercd7900762007-10-17 16:55:11 +02006286 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006287
Milton Millercd7900762007-10-17 16:55:11 +02006288 /*
6289 * In the intermediate directories, both the child directory and
6290 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006291 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006292 * static strings and all have proc handlers.
6293 */
6294 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006295 if (entry->child)
6296 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006297 if (entry->proc_handler == NULL)
6298 kfree(entry->procname);
6299 }
Milton Miller6382bc92007-10-15 17:00:19 +02006300
6301 kfree(*tablep);
6302 *tablep = NULL;
6303}
6304
Nick Piggine692ab52007-07-26 13:40:43 +02006305static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006306set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006307 const char *procname, void *data, int maxlen,
6308 mode_t mode, proc_handler *proc_handler)
6309{
Nick Piggine692ab52007-07-26 13:40:43 +02006310 entry->procname = procname;
6311 entry->data = data;
6312 entry->maxlen = maxlen;
6313 entry->mode = mode;
6314 entry->proc_handler = proc_handler;
6315}
6316
6317static struct ctl_table *
6318sd_alloc_ctl_domain_table(struct sched_domain *sd)
6319{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006320 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006321
Milton Millerad1cdc12007-10-15 17:00:19 +02006322 if (table == NULL)
6323 return NULL;
6324
Alexey Dobriyane0361852007-08-09 11:16:46 +02006325 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006326 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006327 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006328 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006329 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006330 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006331 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006332 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006333 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006334 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006335 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006336 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006337 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006338 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006339 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006340 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006341 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006342 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006343 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006344 &sd->cache_nice_tries,
6345 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006346 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006347 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006348 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006349
6350 return table;
6351}
6352
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006353static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006354{
6355 struct ctl_table *entry, *table;
6356 struct sched_domain *sd;
6357 int domain_num = 0, i;
6358 char buf[32];
6359
6360 for_each_domain(cpu, sd)
6361 domain_num++;
6362 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006363 if (table == NULL)
6364 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006365
6366 i = 0;
6367 for_each_domain(cpu, sd) {
6368 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006369 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006370 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006371 entry->child = sd_alloc_ctl_domain_table(sd);
6372 entry++;
6373 i++;
6374 }
6375 return table;
6376}
6377
6378static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006379static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006380{
6381 int i, cpu_num = num_online_cpus();
6382 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6383 char buf[32];
6384
Milton Miller73785472007-10-24 18:23:48 +02006385 WARN_ON(sd_ctl_dir[0].child);
6386 sd_ctl_dir[0].child = entry;
6387
Milton Millerad1cdc12007-10-15 17:00:19 +02006388 if (entry == NULL)
6389 return;
6390
Milton Miller97b6ea72007-10-15 17:00:19 +02006391 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006392 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006393 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006394 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006395 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006396 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006397 }
Milton Miller73785472007-10-24 18:23:48 +02006398
6399 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006400 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6401}
Milton Miller6382bc92007-10-15 17:00:19 +02006402
Milton Miller73785472007-10-24 18:23:48 +02006403/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006404static void unregister_sched_domain_sysctl(void)
6405{
Milton Miller73785472007-10-24 18:23:48 +02006406 if (sd_sysctl_header)
6407 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006408 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006409 if (sd_ctl_dir[0].child)
6410 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006411}
Nick Piggine692ab52007-07-26 13:40:43 +02006412#else
Milton Miller6382bc92007-10-15 17:00:19 +02006413static void register_sched_domain_sysctl(void)
6414{
6415}
6416static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006417{
6418}
6419#endif
6420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421/*
6422 * migration_call - callback that gets triggered when a CPU is added.
6423 * Here we can start up the necessary migration thread for the new CPU.
6424 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006425static int __cpuinit
6426migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006429 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006431 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006436 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006437 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 if (IS_ERR(p))
6439 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 kthread_bind(p, cpu);
6441 /* Must be high prio: stop_machine expects to yield to it. */
6442 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006443 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 task_rq_unlock(rq, &flags);
6445 cpu_rq(cpu)->migration_thread = p;
6446 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006447
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006449 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006450 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006452
6453 /* Update our root-domain */
6454 rq = cpu_rq(cpu);
6455 spin_lock_irqsave(&rq->lock, flags);
6456 if (rq->rd) {
6457 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6458 cpu_set(cpu, rq->rd->online);
6459 }
6460 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006462
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463#ifdef CONFIG_HOTPLUG_CPU
6464 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006465 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006466 if (!cpu_rq(cpu)->migration_thread)
6467 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006468 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006469 kthread_bind(cpu_rq(cpu)->migration_thread,
6470 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 kthread_stop(cpu_rq(cpu)->migration_thread);
6472 cpu_rq(cpu)->migration_thread = NULL;
6473 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006474
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006476 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006477 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 migrate_live_tasks(cpu);
6479 rq = cpu_rq(cpu);
6480 kthread_stop(rq->migration_thread);
6481 rq->migration_thread = NULL;
6482 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006483 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006484 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006485 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006487 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6488 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006490 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006491 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 migrate_nr_uninterruptible(rq);
6493 BUG_ON(rq->nr_running != 0);
6494
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006495 /*
6496 * No need to migrate the tasks: it was best-effort if
6497 * they didn't take sched_hotcpu_mutex. Just wake up
6498 * the requestors.
6499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 spin_lock_irq(&rq->lock);
6501 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006502 struct migration_req *req;
6503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006505 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 list_del_init(&req->list);
6507 complete(&req->done);
6508 }
6509 spin_unlock_irq(&rq->lock);
6510 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006511
Gregory Haskins08f503b2008-03-10 17:59:11 -04006512 case CPU_DYING:
6513 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006514 /* Update our root-domain */
6515 rq = cpu_rq(cpu);
6516 spin_lock_irqsave(&rq->lock, flags);
6517 if (rq->rd) {
6518 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6519 cpu_clear(cpu, rq->rd->online);
6520 }
6521 spin_unlock_irqrestore(&rq->lock, flags);
6522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523#endif
6524 }
6525 return NOTIFY_OK;
6526}
6527
6528/* Register at highest priority so that task migration (migrate_all_tasks)
6529 * happens before everything else.
6530 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006531static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 .notifier_call = migration_call,
6533 .priority = 10
6534};
6535
Adrian Bunke6fe6642007-11-09 22:39:39 +01006536void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537{
6538 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006539 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006540
6541 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006542 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6543 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6545 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}
6547#endif
6548
6549#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006550
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006551#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006552
Mike Travis7c16ec52008-04-04 18:11:11 -07006553static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6554 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006555{
6556 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006557 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006558
Mike Travis434d53b2008-04-04 18:11:04 -07006559 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006560 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006561
6562 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6563
6564 if (!(sd->flags & SD_LOAD_BALANCE)) {
6565 printk("does not load-balance\n");
6566 if (sd->parent)
6567 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6568 " has parent");
6569 return -1;
6570 }
6571
6572 printk(KERN_CONT "span %s\n", str);
6573
6574 if (!cpu_isset(cpu, sd->span)) {
6575 printk(KERN_ERR "ERROR: domain->span does not contain "
6576 "CPU%d\n", cpu);
6577 }
6578 if (!cpu_isset(cpu, group->cpumask)) {
6579 printk(KERN_ERR "ERROR: domain->groups does not contain"
6580 " CPU%d\n", cpu);
6581 }
6582
6583 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6584 do {
6585 if (!group) {
6586 printk("\n");
6587 printk(KERN_ERR "ERROR: group is NULL\n");
6588 break;
6589 }
6590
6591 if (!group->__cpu_power) {
6592 printk(KERN_CONT "\n");
6593 printk(KERN_ERR "ERROR: domain->cpu_power not "
6594 "set\n");
6595 break;
6596 }
6597
6598 if (!cpus_weight(group->cpumask)) {
6599 printk(KERN_CONT "\n");
6600 printk(KERN_ERR "ERROR: empty group\n");
6601 break;
6602 }
6603
Mike Travis7c16ec52008-04-04 18:11:11 -07006604 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006605 printk(KERN_CONT "\n");
6606 printk(KERN_ERR "ERROR: repeated CPUs\n");
6607 break;
6608 }
6609
Mike Travis7c16ec52008-04-04 18:11:11 -07006610 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006611
Mike Travis434d53b2008-04-04 18:11:04 -07006612 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006613 printk(KERN_CONT " %s", str);
6614
6615 group = group->next;
6616 } while (group != sd->groups);
6617 printk(KERN_CONT "\n");
6618
Mike Travis7c16ec52008-04-04 18:11:11 -07006619 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006620 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6621
Mike Travis7c16ec52008-04-04 18:11:11 -07006622 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006623 printk(KERN_ERR "ERROR: parent span is not a superset "
6624 "of domain->span\n");
6625 return 0;
6626}
6627
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628static void sched_domain_debug(struct sched_domain *sd, int cpu)
6629{
Mike Travis7c16ec52008-04-04 18:11:11 -07006630 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 int level = 0;
6632
Nick Piggin41c7ce92005-06-25 14:57:24 -07006633 if (!sd) {
6634 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6635 return;
6636 }
6637
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6639
Mike Travis7c16ec52008-04-04 18:11:11 -07006640 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6641 if (!groupmask) {
6642 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6643 return;
6644 }
6645
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006646 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006647 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 level++;
6650 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006651 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006652 break;
6653 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006654 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655}
6656#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006657# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658#endif
6659
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006660static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006661{
6662 if (cpus_weight(sd->span) == 1)
6663 return 1;
6664
6665 /* Following flags need at least 2 groups */
6666 if (sd->flags & (SD_LOAD_BALANCE |
6667 SD_BALANCE_NEWIDLE |
6668 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006669 SD_BALANCE_EXEC |
6670 SD_SHARE_CPUPOWER |
6671 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006672 if (sd->groups != sd->groups->next)
6673 return 0;
6674 }
6675
6676 /* Following flags don't use groups */
6677 if (sd->flags & (SD_WAKE_IDLE |
6678 SD_WAKE_AFFINE |
6679 SD_WAKE_BALANCE))
6680 return 0;
6681
6682 return 1;
6683}
6684
Ingo Molnar48f24c42006-07-03 00:25:40 -07006685static int
6686sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006687{
6688 unsigned long cflags = sd->flags, pflags = parent->flags;
6689
6690 if (sd_degenerate(parent))
6691 return 1;
6692
6693 if (!cpus_equal(sd->span, parent->span))
6694 return 0;
6695
6696 /* Does parent contain flags not in child? */
6697 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6698 if (cflags & SD_WAKE_AFFINE)
6699 pflags &= ~SD_WAKE_BALANCE;
6700 /* Flags needing groups don't count if only 1 group in parent */
6701 if (parent->groups == parent->groups->next) {
6702 pflags &= ~(SD_LOAD_BALANCE |
6703 SD_BALANCE_NEWIDLE |
6704 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006705 SD_BALANCE_EXEC |
6706 SD_SHARE_CPUPOWER |
6707 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006708 }
6709 if (~cflags & pflags)
6710 return 0;
6711
6712 return 1;
6713}
6714
Gregory Haskins57d885f2008-01-25 21:08:18 +01006715static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6716{
6717 unsigned long flags;
6718 const struct sched_class *class;
6719
6720 spin_lock_irqsave(&rq->lock, flags);
6721
6722 if (rq->rd) {
6723 struct root_domain *old_rd = rq->rd;
6724
Ingo Molnar0eab9142008-01-25 21:08:19 +01006725 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006726 if (class->leave_domain)
6727 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006728 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006729
Gregory Haskinsdc938522008-01-25 21:08:26 +01006730 cpu_clear(rq->cpu, old_rd->span);
6731 cpu_clear(rq->cpu, old_rd->online);
6732
Gregory Haskins57d885f2008-01-25 21:08:18 +01006733 if (atomic_dec_and_test(&old_rd->refcount))
6734 kfree(old_rd);
6735 }
6736
6737 atomic_inc(&rd->refcount);
6738 rq->rd = rd;
6739
Gregory Haskinsdc938522008-01-25 21:08:26 +01006740 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006741 if (cpu_isset(rq->cpu, cpu_online_map))
6742 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006743
Ingo Molnar0eab9142008-01-25 21:08:19 +01006744 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006745 if (class->join_domain)
6746 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006747 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006748
6749 spin_unlock_irqrestore(&rq->lock, flags);
6750}
6751
Gregory Haskinsdc938522008-01-25 21:08:26 +01006752static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006753{
6754 memset(rd, 0, sizeof(*rd));
6755
Gregory Haskinsdc938522008-01-25 21:08:26 +01006756 cpus_clear(rd->span);
6757 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006758}
6759
6760static void init_defrootdomain(void)
6761{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006762 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006763 atomic_set(&def_root_domain.refcount, 1);
6764}
6765
Gregory Haskinsdc938522008-01-25 21:08:26 +01006766static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006767{
6768 struct root_domain *rd;
6769
6770 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6771 if (!rd)
6772 return NULL;
6773
Gregory Haskinsdc938522008-01-25 21:08:26 +01006774 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006775
6776 return rd;
6777}
6778
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006780 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 * hold the hotplug lock.
6782 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006783static void
6784cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006786 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006787 struct sched_domain *tmp;
6788
6789 /* Remove the sched domains which do not contribute to scheduling. */
6790 for (tmp = sd; tmp; tmp = tmp->parent) {
6791 struct sched_domain *parent = tmp->parent;
6792 if (!parent)
6793 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006794 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006795 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006796 if (parent->parent)
6797 parent->parent->child = tmp;
6798 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006799 }
6800
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006801 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006802 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006803 if (sd)
6804 sd->child = NULL;
6805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
6807 sched_domain_debug(sd, cpu);
6808
Gregory Haskins57d885f2008-01-25 21:08:18 +01006809 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006810 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811}
6812
6813/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006814static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
6816/* Setup the mask of cpus configured for isolated domains */
6817static int __init isolated_cpu_setup(char *str)
6818{
6819 int ints[NR_CPUS], i;
6820
6821 str = get_options(str, ARRAY_SIZE(ints), ints);
6822 cpus_clear(cpu_isolated_map);
6823 for (i = 1; i <= ints[0]; i++)
6824 if (ints[i] < NR_CPUS)
6825 cpu_set(ints[i], cpu_isolated_map);
6826 return 1;
6827}
6828
Ingo Molnar8927f492007-10-15 17:00:13 +02006829__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
6831/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006832 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6833 * to a function which identifies what group(along with sched group) a CPU
6834 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6835 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 *
6837 * init_sched_build_groups will build a circular linked list of the groups
6838 * covered by the given span, and will set each group's ->cpumask correctly,
6839 * and ->cpu_power to 0.
6840 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006841static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006842init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006843 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006844 struct sched_group **sg,
6845 cpumask_t *tmpmask),
6846 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847{
6848 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 int i;
6850
Mike Travis7c16ec52008-04-04 18:11:11 -07006851 cpus_clear(*covered);
6852
6853 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006854 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006855 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 int j;
6857
Mike Travis7c16ec52008-04-04 18:11:11 -07006858 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 continue;
6860
Mike Travis7c16ec52008-04-04 18:11:11 -07006861 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006862 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Mike Travis7c16ec52008-04-04 18:11:11 -07006864 for_each_cpu_mask(j, *span) {
6865 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 continue;
6867
Mike Travis7c16ec52008-04-04 18:11:11 -07006868 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 cpu_set(j, sg->cpumask);
6870 }
6871 if (!first)
6872 first = sg;
6873 if (last)
6874 last->next = sg;
6875 last = sg;
6876 }
6877 last->next = first;
6878}
6879
John Hawkes9c1cfda2005-09-06 15:18:14 -07006880#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
John Hawkes9c1cfda2005-09-06 15:18:14 -07006882#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006883
John Hawkes9c1cfda2005-09-06 15:18:14 -07006884/**
6885 * find_next_best_node - find the next node to include in a sched_domain
6886 * @node: node whose sched_domain we're building
6887 * @used_nodes: nodes already in the sched_domain
6888 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006889 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006890 * finds the closest node not already in the @used_nodes map.
6891 *
6892 * Should use nodemask_t.
6893 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006894static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006895{
6896 int i, n, val, min_val, best_node = 0;
6897
6898 min_val = INT_MAX;
6899
6900 for (i = 0; i < MAX_NUMNODES; i++) {
6901 /* Start at @node */
6902 n = (node + i) % MAX_NUMNODES;
6903
6904 if (!nr_cpus_node(n))
6905 continue;
6906
6907 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006908 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006909 continue;
6910
6911 /* Simple min distance search */
6912 val = node_distance(node, n);
6913
6914 if (val < min_val) {
6915 min_val = val;
6916 best_node = n;
6917 }
6918 }
6919
Mike Travisc5f59f02008-04-04 18:11:10 -07006920 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006921 return best_node;
6922}
6923
6924/**
6925 * sched_domain_node_span - get a cpumask for a node's sched_domain
6926 * @node: node whose cpumask we're constructing
John Hawkes9c1cfda2005-09-06 15:18:14 -07006927 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006928 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006929 * should be one that prevents unnecessary balancing, but also spreads tasks
6930 * out optimally.
6931 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006932static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006933{
Mike Travisc5f59f02008-04-04 18:11:10 -07006934 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006935 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006936 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006937
Mike Travis4bdbaad32008-04-15 16:35:52 -07006938 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006939 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006940
Mike Travis4bdbaad32008-04-15 16:35:52 -07006941 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006942 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006943
6944 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006945 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006946
Mike Travisc5f59f02008-04-04 18:11:10 -07006947 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006948 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006949 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006950}
6951#endif
6952
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006953int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006954
John Hawkes9c1cfda2005-09-06 15:18:14 -07006955/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006956 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958#ifdef CONFIG_SCHED_SMT
6959static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006960static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006961
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006962static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006963cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6964 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006966 if (sg)
6967 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 return cpu;
6969}
6970#endif
6971
Ingo Molnar48f24c42006-07-03 00:25:40 -07006972/*
6973 * multi-core sched-domains:
6974 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006975#ifdef CONFIG_SCHED_MC
6976static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006977static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006978#endif
6979
6980#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006981static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006982cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6983 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006984{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006985 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006986
6987 *mask = per_cpu(cpu_sibling_map, cpu);
6988 cpus_and(*mask, *mask, *cpu_map);
6989 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006990 if (sg)
6991 *sg = &per_cpu(sched_group_core, group);
6992 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006993}
6994#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006995static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006996cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6997 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006998{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006999 if (sg)
7000 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007001 return cpu;
7002}
7003#endif
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007006static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007007
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007008static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007009cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7010 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007012 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007013#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007014 *mask = cpu_coregroup_map(cpu);
7015 cpus_and(*mask, *mask, *cpu_map);
7016 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007017#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007018 *mask = per_cpu(cpu_sibling_map, cpu);
7019 cpus_and(*mask, *mask, *cpu_map);
7020 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007022 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007024 if (sg)
7025 *sg = &per_cpu(sched_group_phys, group);
7026 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027}
7028
7029#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007030/*
7031 * The init_sched_build_groups can't handle what we want to do with node
7032 * groups, so roll our own. Now each node has its own list of groups which
7033 * gets dynamically allocated.
7034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007036static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007037
7038static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007039static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007040
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007041static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007042 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007044 int group;
7045
Mike Travis7c16ec52008-04-04 18:11:11 -07007046 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7047 cpus_and(*nodemask, *nodemask, *cpu_map);
7048 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007049
7050 if (sg)
7051 *sg = &per_cpu(sched_group_allnodes, group);
7052 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007054
Siddha, Suresh B08069032006-03-27 01:15:23 -08007055static void init_numa_sched_groups_power(struct sched_group *group_head)
7056{
7057 struct sched_group *sg = group_head;
7058 int j;
7059
7060 if (!sg)
7061 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007062 do {
7063 for_each_cpu_mask(j, sg->cpumask) {
7064 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007065
Andi Kleen3a5c3592007-10-15 17:00:14 +02007066 sd = &per_cpu(phys_domains, j);
7067 if (j != first_cpu(sd->groups->cpumask)) {
7068 /*
7069 * Only add "power" once for each
7070 * physical package.
7071 */
7072 continue;
7073 }
7074
7075 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007076 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007077 sg = sg->next;
7078 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080#endif
7081
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007082#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007083/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007084static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007085{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007086 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007087
7088 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007089 struct sched_group **sched_group_nodes
7090 = sched_group_nodes_bycpu[cpu];
7091
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007092 if (!sched_group_nodes)
7093 continue;
7094
7095 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007096 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7097
Mike Travis7c16ec52008-04-04 18:11:11 -07007098 *nodemask = node_to_cpumask(i);
7099 cpus_and(*nodemask, *nodemask, *cpu_map);
7100 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007101 continue;
7102
7103 if (sg == NULL)
7104 continue;
7105 sg = sg->next;
7106next_sg:
7107 oldsg = sg;
7108 sg = sg->next;
7109 kfree(oldsg);
7110 if (oldsg != sched_group_nodes[i])
7111 goto next_sg;
7112 }
7113 kfree(sched_group_nodes);
7114 sched_group_nodes_bycpu[cpu] = NULL;
7115 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007116}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007117#else
Mike Travis7c16ec52008-04-04 18:11:11 -07007118static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007119{
7120}
7121#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007124 * Initialize sched groups cpu_power.
7125 *
7126 * cpu_power indicates the capacity of sched group, which is used while
7127 * distributing the load between different sched groups in a sched domain.
7128 * Typically cpu_power for all the groups in a sched domain will be same unless
7129 * there are asymmetries in the topology. If there are asymmetries, group
7130 * having more cpu_power will pickup more load compared to the group having
7131 * less cpu_power.
7132 *
7133 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7134 * the maximum number of tasks a group can handle in the presence of other idle
7135 * or lightly loaded groups in the same sched domain.
7136 */
7137static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7138{
7139 struct sched_domain *child;
7140 struct sched_group *group;
7141
7142 WARN_ON(!sd || !sd->groups);
7143
7144 if (cpu != first_cpu(sd->groups->cpumask))
7145 return;
7146
7147 child = sd->child;
7148
Eric Dumazet5517d862007-05-08 00:32:57 -07007149 sd->groups->__cpu_power = 0;
7150
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007151 /*
7152 * For perf policy, if the groups in child domain share resources
7153 * (for example cores sharing some portions of the cache hierarchy
7154 * or SMT), then set this domain groups cpu_power such that each group
7155 * can handle only one task, when there are other idle groups in the
7156 * same sched domain.
7157 */
7158 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7159 (child->flags &
7160 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007161 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007162 return;
7163 }
7164
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007165 /*
7166 * add cpu_power of each child group to this groups cpu_power
7167 */
7168 group = child->groups;
7169 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007170 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007171 group = group->next;
7172 } while (group != child->groups);
7173}
7174
7175/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007176 * Initializers for schedule domains
7177 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7178 */
7179
7180#define SD_INIT(sd, type) sd_init_##type(sd)
7181#define SD_INIT_FUNC(type) \
7182static noinline void sd_init_##type(struct sched_domain *sd) \
7183{ \
7184 memset(sd, 0, sizeof(*sd)); \
7185 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007186 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007187}
7188
7189SD_INIT_FUNC(CPU)
7190#ifdef CONFIG_NUMA
7191 SD_INIT_FUNC(ALLNODES)
7192 SD_INIT_FUNC(NODE)
7193#endif
7194#ifdef CONFIG_SCHED_SMT
7195 SD_INIT_FUNC(SIBLING)
7196#endif
7197#ifdef CONFIG_SCHED_MC
7198 SD_INIT_FUNC(MC)
7199#endif
7200
7201/*
7202 * To minimize stack usage kmalloc room for cpumasks and share the
7203 * space as the usage in build_sched_domains() dictates. Used only
7204 * if the amount of space is significant.
7205 */
7206struct allmasks {
7207 cpumask_t tmpmask; /* make this one first */
7208 union {
7209 cpumask_t nodemask;
7210 cpumask_t this_sibling_map;
7211 cpumask_t this_core_map;
7212 };
7213 cpumask_t send_covered;
7214
7215#ifdef CONFIG_NUMA
7216 cpumask_t domainspan;
7217 cpumask_t covered;
7218 cpumask_t notcovered;
7219#endif
7220};
7221
7222#if NR_CPUS > 128
7223#define SCHED_CPUMASK_ALLOC 1
7224#define SCHED_CPUMASK_FREE(v) kfree(v)
7225#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7226#else
7227#define SCHED_CPUMASK_ALLOC 0
7228#define SCHED_CPUMASK_FREE(v)
7229#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7230#endif
7231
7232#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7233 ((unsigned long)(a) + offsetof(struct allmasks, v))
7234
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007235static int default_relax_domain_level = -1;
7236
7237static int __init setup_relax_domain_level(char *str)
7238{
7239 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7240 return 1;
7241}
7242__setup("relax_domain_level=", setup_relax_domain_level);
7243
7244static void set_domain_attribute(struct sched_domain *sd,
7245 struct sched_domain_attr *attr)
7246{
7247 int request;
7248
7249 if (!attr || attr->relax_domain_level < 0) {
7250 if (default_relax_domain_level < 0)
7251 return;
7252 else
7253 request = default_relax_domain_level;
7254 } else
7255 request = attr->relax_domain_level;
7256 if (request < sd->level) {
7257 /* turn off idle balance on this domain */
7258 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7259 } else {
7260 /* turn on idle balance on this domain */
7261 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7262 }
7263}
7264
Mike Travis7c16ec52008-04-04 18:11:11 -07007265/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007266 * Build sched domains for a given set of cpus and attach the sched domains
7267 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007269static int __build_sched_domains(const cpumask_t *cpu_map,
7270 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
7272 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007273 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007274 SCHED_CPUMASK_DECLARE(allmasks);
7275 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007276#ifdef CONFIG_NUMA
7277 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007278 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007279
7280 /*
7281 * Allocate the per-node list of sched groups
7282 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007283 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007284 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007285 if (!sched_group_nodes) {
7286 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007287 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007288 }
John Hawkesd1b55132005-09-06 15:18:14 -07007289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Gregory Haskinsdc938522008-01-25 21:08:26 +01007291 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007292 if (!rd) {
7293 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007294#ifdef CONFIG_NUMA
7295 kfree(sched_group_nodes);
7296#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007297 return -ENOMEM;
7298 }
7299
Mike Travis7c16ec52008-04-04 18:11:11 -07007300#if SCHED_CPUMASK_ALLOC
7301 /* get space for all scratch cpumask variables */
7302 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7303 if (!allmasks) {
7304 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7305 kfree(rd);
7306#ifdef CONFIG_NUMA
7307 kfree(sched_group_nodes);
7308#endif
7309 return -ENOMEM;
7310 }
7311#endif
7312 tmpmask = (cpumask_t *)allmasks;
7313
7314
7315#ifdef CONFIG_NUMA
7316 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7317#endif
7318
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007320 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007322 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007324 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
Mike Travis7c16ec52008-04-04 18:11:11 -07007326 *nodemask = node_to_cpumask(cpu_to_node(i));
7327 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
7329#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007330 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007331 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007332 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007333 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007334 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007335 sd->span = *cpu_map;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007336 sd->first_cpu = first_cpu(sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07007337 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007338 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007339 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007340 } else
7341 p = NULL;
7342
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007344 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007345 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007346 sched_domain_node_span(cpu_to_node(i), &sd->span);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007347 sd->first_cpu = first_cpu(sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007348 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007349 if (p)
7350 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007351 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352#endif
7353
7354 p = sd;
7355 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007356 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007357 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007358 sd->span = *nodemask;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007359 sd->first_cpu = first_cpu(sd->span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007361 if (p)
7362 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007363 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007365#ifdef CONFIG_SCHED_MC
7366 p = sd;
7367 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007368 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007369 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007370 sd->span = cpu_coregroup_map(i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007371 sd->first_cpu = first_cpu(sd->span);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007372 cpus_and(sd->span, sd->span, *cpu_map);
7373 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007374 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007375 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007376#endif
7377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378#ifdef CONFIG_SCHED_SMT
7379 p = sd;
7380 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007381 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007382 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007383 sd->span = per_cpu(cpu_sibling_map, i);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02007384 sd->first_cpu = first_cpu(sd->span);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007385 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007387 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007388 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389#endif
7390 }
7391
7392#ifdef CONFIG_SCHED_SMT
7393 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007394 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007395 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7396 SCHED_CPUMASK_VAR(send_covered, allmasks);
7397
7398 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7399 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7400 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 continue;
7402
Ingo Molnardd41f592007-07-09 18:51:59 +02007403 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007404 &cpu_to_cpu_group,
7405 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 }
7407#endif
7408
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007409#ifdef CONFIG_SCHED_MC
7410 /* Set up multi-core groups */
7411 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007412 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7413 SCHED_CPUMASK_VAR(send_covered, allmasks);
7414
7415 *this_core_map = cpu_coregroup_map(i);
7416 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7417 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007418 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007419
Ingo Molnardd41f592007-07-09 18:51:59 +02007420 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007421 &cpu_to_core_group,
7422 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007423 }
7424#endif
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 /* Set up physical groups */
7427 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007428 SCHED_CPUMASK_VAR(nodemask, allmasks);
7429 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430
Mike Travis7c16ec52008-04-04 18:11:11 -07007431 *nodemask = node_to_cpumask(i);
7432 cpus_and(*nodemask, *nodemask, *cpu_map);
7433 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 continue;
7435
Mike Travis7c16ec52008-04-04 18:11:11 -07007436 init_sched_build_groups(nodemask, cpu_map,
7437 &cpu_to_phys_group,
7438 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 }
7440
7441#ifdef CONFIG_NUMA
7442 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007443 if (sd_allnodes) {
7444 SCHED_CPUMASK_VAR(send_covered, allmasks);
7445
7446 init_sched_build_groups(cpu_map, cpu_map,
7447 &cpu_to_allnodes_group,
7448 send_covered, tmpmask);
7449 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007450
7451 for (i = 0; i < MAX_NUMNODES; i++) {
7452 /* Set up node groups */
7453 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007454 SCHED_CPUMASK_VAR(nodemask, allmasks);
7455 SCHED_CPUMASK_VAR(domainspan, allmasks);
7456 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007457 int j;
7458
Mike Travis7c16ec52008-04-04 18:11:11 -07007459 *nodemask = node_to_cpumask(i);
7460 cpus_clear(*covered);
7461
7462 cpus_and(*nodemask, *nodemask, *cpu_map);
7463 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007464 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007465 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007466 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007467
Mike Travis4bdbaad32008-04-15 16:35:52 -07007468 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007469 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007470
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007471 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007472 if (!sg) {
7473 printk(KERN_WARNING "Can not alloc domain group for "
7474 "node %d\n", i);
7475 goto error;
7476 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007477 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007478 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007479 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007480
John Hawkes9c1cfda2005-09-06 15:18:14 -07007481 sd = &per_cpu(node_domains, j);
7482 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007483 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007484 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007485 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007486 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007487 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007488 prev = sg;
7489
7490 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007491 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007492 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007493 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007494
Mike Travis7c16ec52008-04-04 18:11:11 -07007495 cpus_complement(*notcovered, *covered);
7496 cpus_and(*tmpmask, *notcovered, *cpu_map);
7497 cpus_and(*tmpmask, *tmpmask, *domainspan);
7498 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007499 break;
7500
Mike Travis7c16ec52008-04-04 18:11:11 -07007501 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7502 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007503 continue;
7504
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007505 sg = kmalloc_node(sizeof(struct sched_group),
7506 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007507 if (!sg) {
7508 printk(KERN_WARNING
7509 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007510 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007511 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007512 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007513 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007514 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007515 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007516 prev->next = sg;
7517 prev = sg;
7518 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520#endif
7521
7522 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007523#ifdef CONFIG_SCHED_SMT
7524 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007525 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7526
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007527 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007528 }
7529#endif
7530#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007531 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007532 struct sched_domain *sd = &per_cpu(core_domains, i);
7533
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007534 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007535 }
7536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007538 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007539 struct sched_domain *sd = &per_cpu(phys_domains, i);
7540
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007541 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 }
7543
John Hawkes9c1cfda2005-09-06 15:18:14 -07007544#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007545 for (i = 0; i < MAX_NUMNODES; i++)
7546 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007547
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007548 if (sd_allnodes) {
7549 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007550
Mike Travis7c16ec52008-04-04 18:11:11 -07007551 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7552 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007553 init_numa_sched_groups_power(sg);
7554 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007555#endif
7556
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007558 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 struct sched_domain *sd;
7560#ifdef CONFIG_SCHED_SMT
7561 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007562#elif defined(CONFIG_SCHED_MC)
7563 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564#else
7565 sd = &per_cpu(phys_domains, i);
7566#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007567 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007569
Mike Travis7c16ec52008-04-04 18:11:11 -07007570 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007571 return 0;
7572
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007573#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007574error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007575 free_sched_groups(cpu_map, tmpmask);
7576 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007577 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579}
Paul Jackson029190c2007-10-18 23:40:20 -07007580
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007581static int build_sched_domains(const cpumask_t *cpu_map)
7582{
7583 return __build_sched_domains(cpu_map, NULL);
7584}
7585
Paul Jackson029190c2007-10-18 23:40:20 -07007586static cpumask_t *doms_cur; /* current sched domains */
7587static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007588static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7589 in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007590
7591/*
7592 * Special case: If a kmalloc of a doms_cur partition (array of
7593 * cpumask_t) fails, then fallback to a single sched domain,
7594 * as determined by the single cpumask_t fallback_doms.
7595 */
7596static cpumask_t fallback_doms;
7597
Heiko Carstens22e52b02008-03-12 18:31:59 +01007598void __attribute__((weak)) arch_update_cpu_topology(void)
7599{
7600}
7601
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007602/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007603 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007604 * For now this just excludes isolated cpus, but could be used to
7605 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007606 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007607static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007608{
Milton Miller73785472007-10-24 18:23:48 +02007609 int err;
7610
Heiko Carstens22e52b02008-03-12 18:31:59 +01007611 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007612 ndoms_cur = 1;
7613 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7614 if (!doms_cur)
7615 doms_cur = &fallback_doms;
7616 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007617 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007618 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007619 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007620
7621 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007622}
7623
Mike Travis7c16ec52008-04-04 18:11:11 -07007624static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7625 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626{
Mike Travis7c16ec52008-04-04 18:11:11 -07007627 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007630/*
7631 * Detach sched domains from a group of cpus specified in cpu_map
7632 * These cpus will now be attached to the NULL domain
7633 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007634static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007635{
Mike Travis7c16ec52008-04-04 18:11:11 -07007636 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007637 int i;
7638
Milton Miller6382bc92007-10-15 17:00:19 +02007639 unregister_sched_domain_sysctl();
7640
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007641 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007642 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007643 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007644 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007645}
7646
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007647/* handle null as "default" */
7648static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7649 struct sched_domain_attr *new, int idx_new)
7650{
7651 struct sched_domain_attr tmp;
7652
7653 /* fast path */
7654 if (!new && !cur)
7655 return 1;
7656
7657 tmp = SD_ATTR_INIT;
7658 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7659 new ? (new + idx_new) : &tmp,
7660 sizeof(struct sched_domain_attr));
7661}
7662
Paul Jackson029190c2007-10-18 23:40:20 -07007663/*
7664 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007665 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007666 * doms_new[] to the current sched domain partitioning, doms_cur[].
7667 * It destroys each deleted domain and builds each new domain.
7668 *
7669 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007670 * The masks don't intersect (don't overlap.) We should setup one
7671 * sched domain for each mask. CPUs not in any of the cpumasks will
7672 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007673 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7674 * it as it is.
7675 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007676 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7677 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007678 * failed the kmalloc call, then it can pass in doms_new == NULL,
7679 * and partition_sched_domains() will fallback to the single partition
7680 * 'fallback_doms'.
7681 *
7682 * Call with hotplug lock held
7683 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007684void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7685 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007686{
7687 int i, j;
7688
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007689 lock_doms_cur();
7690
Milton Miller73785472007-10-24 18:23:48 +02007691 /* always unregister in case we don't destroy any domains */
7692 unregister_sched_domain_sysctl();
7693
Paul Jackson029190c2007-10-18 23:40:20 -07007694 if (doms_new == NULL) {
7695 ndoms_new = 1;
7696 doms_new = &fallback_doms;
7697 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007698 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007699 }
7700
7701 /* Destroy deleted domains */
7702 for (i = 0; i < ndoms_cur; i++) {
7703 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007704 if (cpus_equal(doms_cur[i], doms_new[j])
7705 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007706 goto match1;
7707 }
7708 /* no match - a current sched domain not in new doms_new[] */
7709 detach_destroy_domains(doms_cur + i);
7710match1:
7711 ;
7712 }
7713
7714 /* Build new domains */
7715 for (i = 0; i < ndoms_new; i++) {
7716 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007717 if (cpus_equal(doms_new[i], doms_cur[j])
7718 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007719 goto match2;
7720 }
7721 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007722 __build_sched_domains(doms_new + i,
7723 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007724match2:
7725 ;
7726 }
7727
7728 /* Remember the new sched domains */
7729 if (doms_cur != &fallback_doms)
7730 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007731 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007732 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007733 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007734 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007735
7736 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007737
7738 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007739}
7740
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007741#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007742int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007743{
7744 int err;
7745
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007746 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007747 detach_destroy_domains(&cpu_online_map);
7748 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007749 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007750
7751 return err;
7752}
7753
7754static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7755{
7756 int ret;
7757
7758 if (buf[0] != '0' && buf[0] != '1')
7759 return -EINVAL;
7760
7761 if (smt)
7762 sched_smt_power_savings = (buf[0] == '1');
7763 else
7764 sched_mc_power_savings = (buf[0] == '1');
7765
7766 ret = arch_reinit_sched_domains();
7767
7768 return ret ? ret : count;
7769}
7770
Adrian Bunk6707de002007-08-12 18:08:19 +02007771#ifdef CONFIG_SCHED_MC
7772static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7773{
7774 return sprintf(page, "%u\n", sched_mc_power_savings);
7775}
7776static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7777 const char *buf, size_t count)
7778{
7779 return sched_power_savings_store(buf, count, 0);
7780}
7781static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7782 sched_mc_power_savings_store);
7783#endif
7784
7785#ifdef CONFIG_SCHED_SMT
7786static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7787{
7788 return sprintf(page, "%u\n", sched_smt_power_savings);
7789}
7790static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7791 const char *buf, size_t count)
7792{
7793 return sched_power_savings_store(buf, count, 1);
7794}
7795static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7796 sched_smt_power_savings_store);
7797#endif
7798
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007799int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7800{
7801 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007802
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007803#ifdef CONFIG_SCHED_SMT
7804 if (smt_capable())
7805 err = sysfs_create_file(&cls->kset.kobj,
7806 &attr_sched_smt_power_savings.attr);
7807#endif
7808#ifdef CONFIG_SCHED_MC
7809 if (!err && mc_capable())
7810 err = sysfs_create_file(&cls->kset.kobj,
7811 &attr_sched_mc_power_savings.attr);
7812#endif
7813 return err;
7814}
7815#endif
7816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007818 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007820 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 * which will prevent rebalancing while the sched domains are recalculated.
7822 */
7823static int update_sched_domains(struct notifier_block *nfb,
7824 unsigned long action, void *hcpu)
7825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 switch (action) {
7827 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007828 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007830 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007831 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 return NOTIFY_OK;
7833
7834 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007835 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007837 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007839 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007841 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 /*
7843 * Fall through and re-initialise the domains.
7844 */
7845 break;
7846 default:
7847 return NOTIFY_DONE;
7848 }
7849
7850 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007851 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852
7853 return NOTIFY_OK;
7854}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855
7856void __init sched_init_smp(void)
7857{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007858 cpumask_t non_isolated_cpus;
7859
Mike Travis434d53b2008-04-04 18:11:04 -07007860#if defined(CONFIG_NUMA)
7861 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7862 GFP_KERNEL);
7863 BUG_ON(sched_group_nodes_bycpu == NULL);
7864#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007865 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007866 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007867 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007868 if (cpus_empty(non_isolated_cpus))
7869 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007870 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 /* XXX: Theoretical race here - CPU may be hotplugged now */
7872 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007873
7874 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007875 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007876 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007877 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878}
7879#else
7880void __init sched_init_smp(void)
7881{
Mike Travis434d53b2008-04-04 18:11:04 -07007882#if defined(CONFIG_NUMA)
7883 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7884 GFP_KERNEL);
7885 BUG_ON(sched_group_nodes_bycpu == NULL);
7886#endif
Ingo Molnar19978ca2007-11-09 22:39:38 +01007887 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888}
7889#endif /* CONFIG_SMP */
7890
7891int in_sched_functions(unsigned long addr)
7892{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 return in_lock_functions(addr) ||
7894 (addr >= (unsigned long)__sched_text_start
7895 && addr < (unsigned long)__sched_text_end);
7896}
7897
Alexey Dobriyana9957442007-10-15 17:00:13 +02007898static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007899{
7900 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007901#ifdef CONFIG_FAIR_GROUP_SCHED
7902 cfs_rq->rq = rq;
7903#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007904 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007905}
7906
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007907static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7908{
7909 struct rt_prio_array *array;
7910 int i;
7911
7912 array = &rt_rq->active;
7913 for (i = 0; i < MAX_RT_PRIO; i++) {
7914 INIT_LIST_HEAD(array->queue + i);
7915 __clear_bit(i, array->bitmap);
7916 }
7917 /* delimiter for bitsearch: */
7918 __set_bit(MAX_RT_PRIO, array->bitmap);
7919
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007920#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007921 rt_rq->highest_prio = MAX_RT_PRIO;
7922#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007923#ifdef CONFIG_SMP
7924 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007925 rt_rq->overloaded = 0;
7926#endif
7927
7928 rt_rq->rt_time = 0;
7929 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007930 rt_rq->rt_runtime = 0;
7931 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007932
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007933#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007934 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007935 rt_rq->rq = rq;
7936#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007937}
7938
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007939#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007940static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7941 struct sched_entity *se, int cpu, int add,
7942 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007943{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007944 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007945 tg->cfs_rq[cpu] = cfs_rq;
7946 init_cfs_rq(cfs_rq, rq);
7947 cfs_rq->tg = tg;
7948 if (add)
7949 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7950
7951 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007952 /* se could be NULL for init_task_group */
7953 if (!se)
7954 return;
7955
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007956 if (!parent)
7957 se->cfs_rq = &rq->cfs;
7958 else
7959 se->cfs_rq = parent->my_q;
7960
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007961 se->my_q = cfs_rq;
7962 se->load.weight = tg->shares;
7963 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007964 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007965}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007966#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007967
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007968#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007969static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7970 struct sched_rt_entity *rt_se, int cpu, int add,
7971 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007972{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007973 struct rq *rq = cpu_rq(cpu);
7974
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007975 tg->rt_rq[cpu] = rt_rq;
7976 init_rt_rq(rt_rq, rq);
7977 rt_rq->tg = tg;
7978 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007979 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007980 if (add)
7981 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7982
7983 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007984 if (!rt_se)
7985 return;
7986
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007987 if (!parent)
7988 rt_se->rt_rq = &rq->rt;
7989 else
7990 rt_se->rt_rq = parent->my_q;
7991
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007992 rt_se->rt_rq = &rq->rt;
7993 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007994 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007995 INIT_LIST_HEAD(&rt_se->run_list);
7996}
7997#endif
7998
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999void __init sched_init(void)
8000{
Ingo Molnardd41f592007-07-09 18:51:59 +02008001 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008002 unsigned long alloc_size = 0, ptr;
8003
8004#ifdef CONFIG_FAIR_GROUP_SCHED
8005 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8006#endif
8007#ifdef CONFIG_RT_GROUP_SCHED
8008 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8009#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008010#ifdef CONFIG_USER_SCHED
8011 alloc_size *= 2;
8012#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008013 /*
8014 * As sched_init() is called before page_alloc is setup,
8015 * we use alloc_bootmem().
8016 */
8017 if (alloc_size) {
8018 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
8019
8020#ifdef CONFIG_FAIR_GROUP_SCHED
8021 init_task_group.se = (struct sched_entity **)ptr;
8022 ptr += nr_cpu_ids * sizeof(void **);
8023
8024 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8025 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008026
8027#ifdef CONFIG_USER_SCHED
8028 root_task_group.se = (struct sched_entity **)ptr;
8029 ptr += nr_cpu_ids * sizeof(void **);
8030
8031 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8032 ptr += nr_cpu_ids * sizeof(void **);
8033#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008034#endif
8035#ifdef CONFIG_RT_GROUP_SCHED
8036 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8037 ptr += nr_cpu_ids * sizeof(void **);
8038
8039 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008040 ptr += nr_cpu_ids * sizeof(void **);
8041
8042#ifdef CONFIG_USER_SCHED
8043 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8044 ptr += nr_cpu_ids * sizeof(void **);
8045
8046 root_task_group.rt_rq = (struct rt_rq **)ptr;
8047 ptr += nr_cpu_ids * sizeof(void **);
8048#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008049#endif
8050 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008051
Gregory Haskins57d885f2008-01-25 21:08:18 +01008052#ifdef CONFIG_SMP
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008053 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008054 init_defrootdomain();
8055#endif
8056
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008057 init_rt_bandwidth(&def_rt_bandwidth,
8058 global_rt_period(), global_rt_runtime());
8059
8060#ifdef CONFIG_RT_GROUP_SCHED
8061 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8062 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008063#ifdef CONFIG_USER_SCHED
8064 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8065 global_rt_period(), RUNTIME_INF);
8066#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008067#endif
8068
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008069#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008070 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008071 INIT_LIST_HEAD(&init_task_group.children);
8072
8073#ifdef CONFIG_USER_SCHED
8074 INIT_LIST_HEAD(&root_task_group.children);
8075 init_task_group.parent = &root_task_group;
8076 list_add(&init_task_group.siblings, &root_task_group.children);
8077#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008078#endif
8079
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008080 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008081 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082
8083 rq = cpu_rq(i);
8084 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008085 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008086 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008087 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02008088 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02008089 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008090 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008091#ifdef CONFIG_FAIR_GROUP_SCHED
8092 init_task_group.shares = init_task_group_load;
8093 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008094#ifdef CONFIG_CGROUP_SCHED
8095 /*
8096 * How much cpu bandwidth does init_task_group get?
8097 *
8098 * In case of task-groups formed thr' the cgroup filesystem, it
8099 * gets 100% of the cpu resources in the system. This overall
8100 * system cpu resource is divided among the tasks of
8101 * init_task_group and its child task-groups in a fair manner,
8102 * based on each entity's (task or task-group's) weight
8103 * (se->load.weight).
8104 *
8105 * In other words, if init_task_group has 10 tasks of weight
8106 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8107 * then A0's share of the cpu resource is:
8108 *
8109 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8110 *
8111 * We achieve this by letting init_task_group's tasks sit
8112 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8113 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008114 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008115#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008116 root_task_group.shares = NICE_0_LOAD;
8117 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008118 /*
8119 * In case of task-groups formed thr' the user id of tasks,
8120 * init_task_group represents tasks belonging to root user.
8121 * Hence it forms a sibling of all subsequent groups formed.
8122 * In this case, init_task_group gets only a fraction of overall
8123 * system cpu resource, based on the weight assigned to root
8124 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8125 * by letting tasks of init_task_group sit in a separate cfs_rq
8126 * (init_cfs_rq) and having one entity represent this group of
8127 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8128 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008129 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008130 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008131 &per_cpu(init_sched_entity, i), i, 1,
8132 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008133
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008134#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008135#endif /* CONFIG_FAIR_GROUP_SCHED */
8136
8137 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008138#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008139 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008140#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008141 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008142#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008143 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008144 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008145 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008146 &per_cpu(init_sched_rt_entity, i), i, 1,
8147 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008148#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150
Ingo Molnardd41f592007-07-09 18:51:59 +02008151 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8152 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008154 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008155 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008157 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008159 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 rq->migration_thread = NULL;
8161 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008162 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008164 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 }
8167
Peter Williams2dd73a42006-06-27 02:54:34 -07008168 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008169
Avi Kivitye107be32007-07-26 13:40:43 +02008170#ifdef CONFIG_PREEMPT_NOTIFIERS
8171 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8172#endif
8173
Christoph Lameterc9819f42006-12-10 02:20:25 -08008174#ifdef CONFIG_SMP
8175 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8176#endif
8177
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008178#ifdef CONFIG_RT_MUTEXES
8179 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8180#endif
8181
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 /*
8183 * The boot idle thread does lazy MMU switching as well:
8184 */
8185 atomic_inc(&init_mm.mm_count);
8186 enter_lazy_tlb(&init_mm, current);
8187
8188 /*
8189 * Make us the idle thread. Technically, schedule() should not be
8190 * called from this thread, however somewhere below it might be,
8191 * but because we are the idle thread, we just pick up running again
8192 * when this runqueue becomes "idle".
8193 */
8194 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008195 /*
8196 * During early bootup we pretend to be a normal task:
8197 */
8198 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008199
8200 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201}
8202
8203#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8204void __might_sleep(char *file, int line)
8205{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008206#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 static unsigned long prev_jiffy; /* ratelimiting */
8208
8209 if ((in_atomic() || irqs_disabled()) &&
8210 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8211 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8212 return;
8213 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008214 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 " context at %s:%d\n", file, line);
8216 printk("in_atomic():%d, irqs_disabled():%d\n",
8217 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008218 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008219 if (irqs_disabled())
8220 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 dump_stack();
8222 }
8223#endif
8224}
8225EXPORT_SYMBOL(__might_sleep);
8226#endif
8227
8228#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008229static void normalize_task(struct rq *rq, struct task_struct *p)
8230{
8231 int on_rq;
8232 update_rq_clock(rq);
8233 on_rq = p->se.on_rq;
8234 if (on_rq)
8235 deactivate_task(rq, p, 0);
8236 __setscheduler(rq, p, SCHED_NORMAL, 0);
8237 if (on_rq) {
8238 activate_task(rq, p, 0);
8239 resched_task(rq->curr);
8240 }
8241}
8242
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243void normalize_rt_tasks(void)
8244{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008245 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008247 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008249 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008250 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008251 /*
8252 * Only normalize user tasks:
8253 */
8254 if (!p->mm)
8255 continue;
8256
Ingo Molnardd41f592007-07-09 18:51:59 +02008257 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008258#ifdef CONFIG_SCHEDSTATS
8259 p->se.wait_start = 0;
8260 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008261 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008262#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008263 task_rq(p)->clock = 0;
8264
8265 if (!rt_task(p)) {
8266 /*
8267 * Renice negative nice level userspace
8268 * tasks back to 0:
8269 */
8270 if (TASK_NICE(p) < 0 && p->mm)
8271 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008275 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008276 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
Ingo Molnar178be792007-10-15 17:00:18 +02008278 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008279
Ingo Molnarb29739f2006-06-27 02:54:51 -07008280 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008281 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008282 } while_each_thread(g, p);
8283
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008284 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285}
8286
8287#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008288
8289#ifdef CONFIG_IA64
8290/*
8291 * These functions are only useful for the IA64 MCA handling.
8292 *
8293 * They can only be called when the whole system has been
8294 * stopped - every CPU needs to be quiescent, and no scheduling
8295 * activity can take place. Using them for anything else would
8296 * be a serious bug, and as a result, they aren't even visible
8297 * under any other configuration.
8298 */
8299
8300/**
8301 * curr_task - return the current task for a given cpu.
8302 * @cpu: the processor in question.
8303 *
8304 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8305 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008306struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008307{
8308 return cpu_curr(cpu);
8309}
8310
8311/**
8312 * set_curr_task - set the current task for a given cpu.
8313 * @cpu: the processor in question.
8314 * @p: the task pointer to set.
8315 *
8316 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008317 * are serviced on a separate stack. It allows the architecture to switch the
8318 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008319 * must be called with all CPU's synchronized, and interrupts disabled, the
8320 * and caller must save the original value of the current task (see
8321 * curr_task() above) and restore that value before reenabling interrupts and
8322 * re-starting the system.
8323 *
8324 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8325 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008326void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008327{
8328 cpu_curr(cpu) = p;
8329}
8330
8331#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008332
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008333#ifdef CONFIG_FAIR_GROUP_SCHED
8334static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008335{
8336 int i;
8337
8338 for_each_possible_cpu(i) {
8339 if (tg->cfs_rq)
8340 kfree(tg->cfs_rq[i]);
8341 if (tg->se)
8342 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008343 }
8344
8345 kfree(tg->cfs_rq);
8346 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008347}
8348
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008349static
8350int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008351{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008352 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008353 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008354 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008355 int i;
8356
Mike Travis434d53b2008-04-04 18:11:04 -07008357 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008358 if (!tg->cfs_rq)
8359 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008360 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008361 if (!tg->se)
8362 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008363
8364 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008365
8366 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008367 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008368
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008369 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8370 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008371 if (!cfs_rq)
8372 goto err;
8373
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008374 se = kmalloc_node(sizeof(struct sched_entity),
8375 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008376 if (!se)
8377 goto err;
8378
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008379 parent_se = parent ? parent->se[i] : NULL;
8380 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008381 }
8382
8383 return 1;
8384
8385 err:
8386 return 0;
8387}
8388
8389static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8390{
8391 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8392 &cpu_rq(cpu)->leaf_cfs_rq_list);
8393}
8394
8395static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8396{
8397 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8398}
8399#else
8400static inline void free_fair_sched_group(struct task_group *tg)
8401{
8402}
8403
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008404static inline
8405int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008406{
8407 return 1;
8408}
8409
8410static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8411{
8412}
8413
8414static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8415{
8416}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008417#endif
8418
8419#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008420static void free_rt_sched_group(struct task_group *tg)
8421{
8422 int i;
8423
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008424 destroy_rt_bandwidth(&tg->rt_bandwidth);
8425
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008426 for_each_possible_cpu(i) {
8427 if (tg->rt_rq)
8428 kfree(tg->rt_rq[i]);
8429 if (tg->rt_se)
8430 kfree(tg->rt_se[i]);
8431 }
8432
8433 kfree(tg->rt_rq);
8434 kfree(tg->rt_se);
8435}
8436
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008437static
8438int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008439{
8440 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008441 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008442 struct rq *rq;
8443 int i;
8444
Mike Travis434d53b2008-04-04 18:11:04 -07008445 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008446 if (!tg->rt_rq)
8447 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008448 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008449 if (!tg->rt_se)
8450 goto err;
8451
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008452 init_rt_bandwidth(&tg->rt_bandwidth,
8453 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008454
8455 for_each_possible_cpu(i) {
8456 rq = cpu_rq(i);
8457
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008458 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8459 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8460 if (!rt_rq)
8461 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008462
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008463 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8464 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8465 if (!rt_se)
8466 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008467
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008468 parent_se = parent ? parent->rt_se[i] : NULL;
8469 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008470 }
8471
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008472 return 1;
8473
8474 err:
8475 return 0;
8476}
8477
8478static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8479{
8480 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8481 &cpu_rq(cpu)->leaf_rt_rq_list);
8482}
8483
8484static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8485{
8486 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8487}
8488#else
8489static inline void free_rt_sched_group(struct task_group *tg)
8490{
8491}
8492
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008493static inline
8494int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008495{
8496 return 1;
8497}
8498
8499static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8500{
8501}
8502
8503static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8504{
8505}
8506#endif
8507
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008508#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008509static void free_sched_group(struct task_group *tg)
8510{
8511 free_fair_sched_group(tg);
8512 free_rt_sched_group(tg);
8513 kfree(tg);
8514}
8515
8516/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008517struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008518{
8519 struct task_group *tg;
8520 unsigned long flags;
8521 int i;
8522
8523 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8524 if (!tg)
8525 return ERR_PTR(-ENOMEM);
8526
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008527 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008528 goto err;
8529
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008530 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008531 goto err;
8532
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008533 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008534 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008535 register_fair_sched_group(tg, i);
8536 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008537 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008538 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008539
8540 WARN_ON(!parent); /* root should already exist */
8541
8542 tg->parent = parent;
8543 list_add_rcu(&tg->siblings, &parent->children);
8544 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008545 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008546
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008547 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008548
8549err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008550 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008551 return ERR_PTR(-ENOMEM);
8552}
8553
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008554/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008555static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008556{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008557 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008558 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008559}
8560
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008561/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008562void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008563{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008564 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008565 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008566
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008567 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008568 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008569 unregister_fair_sched_group(tg, i);
8570 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008571 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008572 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008573 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008574 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008575
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008576 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008577 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008578}
8579
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008580/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008581 * The caller of this function should have put the task in its new group
8582 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8583 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008584 */
8585void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008586{
8587 int on_rq, running;
8588 unsigned long flags;
8589 struct rq *rq;
8590
8591 rq = task_rq_lock(tsk, &flags);
8592
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008593 update_rq_clock(rq);
8594
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008595 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008596 on_rq = tsk->se.on_rq;
8597
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008598 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008599 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008600 if (unlikely(running))
8601 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008602
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008603 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008604
Peter Zijlstra810b3812008-02-29 15:21:01 -05008605#ifdef CONFIG_FAIR_GROUP_SCHED
8606 if (tsk->sched_class->moved_group)
8607 tsk->sched_class->moved_group(tsk);
8608#endif
8609
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008610 if (unlikely(running))
8611 tsk->sched_class->set_curr_task(rq);
8612 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008613 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008614
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008615 task_rq_unlock(rq, &flags);
8616}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008617#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008618
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008619#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008620static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008621{
8622 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008623 int on_rq;
8624
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008625 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008626 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008627 dequeue_entity(cfs_rq, se, 0);
8628
8629 se->load.weight = shares;
8630 se->load.inv_weight = div64_64((1ULL<<32), shares);
8631
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008632 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008633 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008634}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008635
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008636static void set_se_shares(struct sched_entity *se, unsigned long shares)
8637{
8638 struct cfs_rq *cfs_rq = se->cfs_rq;
8639 struct rq *rq = cfs_rq->rq;
8640 unsigned long flags;
8641
8642 spin_lock_irqsave(&rq->lock, flags);
8643 __set_se_shares(se, shares);
8644 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008645}
8646
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008647static DEFINE_MUTEX(shares_mutex);
8648
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008649int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650{
8651 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008652 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008653
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008654 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008655 * We can't change the weight of the root cgroup.
8656 */
8657 if (!tg->se[0])
8658 return -EINVAL;
8659
8660 /*
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008661 * A weight of 0 or 1 can cause arithmetics problems.
8662 * (The default weight is 1024 - so there's no practical
8663 * limitation from this.)
8664 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008665 if (shares < MIN_SHARES)
8666 shares = MIN_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008667
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008668 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008669 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008670 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008671
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008672 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008673 for_each_possible_cpu(i)
8674 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008675 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008676 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008677
8678 /* wait for any ongoing reference to this group to finish */
8679 synchronize_sched();
8680
8681 /*
8682 * Now we are free to modify the group's share on each cpu
8683 * w/o tripping rebalance_share or load_balance_fair.
8684 */
8685 tg->shares = shares;
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008686 for_each_possible_cpu(i) {
8687 /*
8688 * force a rebalance
8689 */
8690 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8691 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8692 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008693
8694 /*
8695 * Enable load balance activity on this group, by inserting it back on
8696 * each cpu's rq->leaf_cfs_rq_list.
8697 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008698 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008699 for_each_possible_cpu(i)
8700 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008701 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008702 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008703done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008704 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008705 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008706}
8707
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008708unsigned long sched_group_shares(struct task_group *tg)
8709{
8710 return tg->shares;
8711}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008712#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008713
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008714#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008715/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008716 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008717 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008718static DEFINE_MUTEX(rt_constraints_mutex);
8719
8720static unsigned long to_ratio(u64 period, u64 runtime)
8721{
8722 if (runtime == RUNTIME_INF)
8723 return 1ULL << 16;
8724
Peter Zijlstra2692a242008-02-27 12:00:46 +01008725 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008726}
8727
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008728#ifdef CONFIG_CGROUP_SCHED
8729static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8730{
8731 struct task_group *tgi, *parent = tg->parent;
8732 unsigned long total = 0;
8733
8734 if (!parent) {
8735 if (global_rt_period() < period)
8736 return 0;
8737
8738 return to_ratio(period, runtime) <
8739 to_ratio(global_rt_period(), global_rt_runtime());
8740 }
8741
8742 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8743 return 0;
8744
8745 rcu_read_lock();
8746 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8747 if (tgi == tg)
8748 continue;
8749
8750 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8751 tgi->rt_bandwidth.rt_runtime);
8752 }
8753 rcu_read_unlock();
8754
8755 return total + to_ratio(period, runtime) <
8756 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8757 parent->rt_bandwidth.rt_runtime);
8758}
8759#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008760static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008761{
8762 struct task_group *tgi;
8763 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008764 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008765 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008766
8767 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008768 list_for_each_entry_rcu(tgi, &task_groups, list) {
8769 if (tgi == tg)
8770 continue;
8771
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008772 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8773 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008774 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008775 rcu_read_unlock();
8776
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008777 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008778}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008779#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008780
Dhaval Giani521f1a242008-02-28 15:21:56 +05308781/* Must be called with tasklist_lock held */
8782static inline int tg_has_rt_tasks(struct task_group *tg)
8783{
8784 struct task_struct *g, *p;
8785 do_each_thread(g, p) {
8786 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8787 return 1;
8788 } while_each_thread(g, p);
8789 return 0;
8790}
8791
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008792static int tg_set_bandwidth(struct task_group *tg,
8793 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008794{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008795 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008796
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008797 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308798 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008799 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308800 err = -EBUSY;
8801 goto unlock;
8802 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008803 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8804 err = -EINVAL;
8805 goto unlock;
8806 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008807
8808 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008809 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8810 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008811
8812 for_each_possible_cpu(i) {
8813 struct rt_rq *rt_rq = tg->rt_rq[i];
8814
8815 spin_lock(&rt_rq->rt_runtime_lock);
8816 rt_rq->rt_runtime = rt_runtime;
8817 spin_unlock(&rt_rq->rt_runtime_lock);
8818 }
8819 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008820 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308821 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008822 mutex_unlock(&rt_constraints_mutex);
8823
8824 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008825}
8826
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008827int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8828{
8829 u64 rt_runtime, rt_period;
8830
8831 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8832 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8833 if (rt_runtime_us < 0)
8834 rt_runtime = RUNTIME_INF;
8835
8836 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8837}
8838
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008839long sched_group_rt_runtime(struct task_group *tg)
8840{
8841 u64 rt_runtime_us;
8842
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008843 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008844 return -1;
8845
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008846 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008847 do_div(rt_runtime_us, NSEC_PER_USEC);
8848 return rt_runtime_us;
8849}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008850
8851int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8852{
8853 u64 rt_runtime, rt_period;
8854
8855 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8856 rt_runtime = tg->rt_bandwidth.rt_runtime;
8857
8858 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8859}
8860
8861long sched_group_rt_period(struct task_group *tg)
8862{
8863 u64 rt_period_us;
8864
8865 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8866 do_div(rt_period_us, NSEC_PER_USEC);
8867 return rt_period_us;
8868}
8869
8870static int sched_rt_global_constraints(void)
8871{
8872 int ret = 0;
8873
8874 mutex_lock(&rt_constraints_mutex);
8875 if (!__rt_schedulable(NULL, 1, 0))
8876 ret = -EINVAL;
8877 mutex_unlock(&rt_constraints_mutex);
8878
8879 return ret;
8880}
8881#else
8882static int sched_rt_global_constraints(void)
8883{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008884 unsigned long flags;
8885 int i;
8886
8887 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8888 for_each_possible_cpu(i) {
8889 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8890
8891 spin_lock(&rt_rq->rt_runtime_lock);
8892 rt_rq->rt_runtime = global_rt_runtime();
8893 spin_unlock(&rt_rq->rt_runtime_lock);
8894 }
8895 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8896
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008897 return 0;
8898}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008899#endif
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008900
8901int sched_rt_handler(struct ctl_table *table, int write,
8902 struct file *filp, void __user *buffer, size_t *lenp,
8903 loff_t *ppos)
8904{
8905 int ret;
8906 int old_period, old_runtime;
8907 static DEFINE_MUTEX(mutex);
8908
8909 mutex_lock(&mutex);
8910 old_period = sysctl_sched_rt_period;
8911 old_runtime = sysctl_sched_rt_runtime;
8912
8913 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8914
8915 if (!ret && write) {
8916 ret = sched_rt_global_constraints();
8917 if (ret) {
8918 sysctl_sched_rt_period = old_period;
8919 sysctl_sched_rt_runtime = old_runtime;
8920 } else {
8921 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8922 def_rt_bandwidth.rt_period =
8923 ns_to_ktime(global_rt_period());
8924 }
8925 }
8926 mutex_unlock(&mutex);
8927
8928 return ret;
8929}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008930
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008931#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008932
8933/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008934static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008935{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008936 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8937 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008938}
8939
8940static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008941cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008942{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008943 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008944
Paul Menage2b01dfe2007-10-24 18:23:50 +02008945 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008946 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008947 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008948 return &init_task_group.css;
8949 }
8950
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008951 parent = cgroup_tg(cgrp->parent);
8952 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008953 if (IS_ERR(tg))
8954 return ERR_PTR(-ENOMEM);
8955
8956 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008957 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008958
8959 return &tg->css;
8960}
8961
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008962static void
8963cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008964{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008965 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008966
8967 sched_destroy_group(tg);
8968}
8969
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008970static int
8971cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8972 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008973{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008974#ifdef CONFIG_RT_GROUP_SCHED
8975 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008976 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008977 return -EINVAL;
8978#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008979 /* We don't support RT-tasks being in separate groups */
8980 if (tsk->sched_class != &fair_sched_class)
8981 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008982#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008983
8984 return 0;
8985}
8986
8987static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008988cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008989 struct cgroup *old_cont, struct task_struct *tsk)
8990{
8991 sched_move_task(tsk);
8992}
8993
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008994#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008995static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8996 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008997{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008998 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008999}
9000
Paul Menage2b01dfe2007-10-24 18:23:50 +02009001static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009002{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009003 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009004
9005 return (u64) tg->shares;
9006}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009007#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009008
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009009#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009010static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009011 struct file *file,
9012 const char __user *userbuf,
9013 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009014{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009015 char buffer[64];
9016 int retval = 0;
9017 s64 val;
9018 char *end;
9019
9020 if (!nbytes)
9021 return -EINVAL;
9022 if (nbytes >= sizeof(buffer))
9023 return -E2BIG;
9024 if (copy_from_user(buffer, userbuf, nbytes))
9025 return -EFAULT;
9026
9027 buffer[nbytes] = 0; /* nul-terminate */
9028
9029 /* strip newline if necessary */
9030 if (nbytes && (buffer[nbytes-1] == '\n'))
9031 buffer[nbytes-1] = 0;
9032 val = simple_strtoll(buffer, &end, 0);
9033 if (*end)
9034 return -EINVAL;
9035
9036 /* Pass to subsystem */
9037 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
9038 if (!retval)
9039 retval = nbytes;
9040 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009041}
9042
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009043static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
9044 struct file *file,
9045 char __user *buf, size_t nbytes,
9046 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009047{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009048 char tmp[64];
9049 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
9050 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009051
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009052 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009053}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009054
9055static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9056 u64 rt_period_us)
9057{
9058 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9059}
9060
9061static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9062{
9063 return sched_group_rt_period(cgroup_tg(cgrp));
9064}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009065#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009066
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009067static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009068#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009069 {
9070 .name = "shares",
9071 .read_uint = cpu_shares_read_uint,
9072 .write_uint = cpu_shares_write_uint,
9073 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009074#endif
9075#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009076 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009077 .name = "rt_runtime_us",
9078 .read = cpu_rt_runtime_read,
9079 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009080 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009081 {
9082 .name = "rt_period_us",
9083 .read_uint = cpu_rt_period_read_uint,
9084 .write_uint = cpu_rt_period_write_uint,
9085 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009086#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009087};
9088
9089static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9090{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009091 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009092}
9093
9094struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009095 .name = "cpu",
9096 .create = cpu_cgroup_create,
9097 .destroy = cpu_cgroup_destroy,
9098 .can_attach = cpu_cgroup_can_attach,
9099 .attach = cpu_cgroup_attach,
9100 .populate = cpu_cgroup_populate,
9101 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009102 .early_init = 1,
9103};
9104
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009105#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009106
9107#ifdef CONFIG_CGROUP_CPUACCT
9108
9109/*
9110 * CPU accounting code for task groups.
9111 *
9112 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9113 * (balbir@in.ibm.com).
9114 */
9115
9116/* track cpu usage of a group of tasks */
9117struct cpuacct {
9118 struct cgroup_subsys_state css;
9119 /* cpuusage holds pointer to a u64-type object on every cpu */
9120 u64 *cpuusage;
9121};
9122
9123struct cgroup_subsys cpuacct_subsys;
9124
9125/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309126static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009127{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309128 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009129 struct cpuacct, css);
9130}
9131
9132/* return cpu accounting group to which this task belongs */
9133static inline struct cpuacct *task_ca(struct task_struct *tsk)
9134{
9135 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9136 struct cpuacct, css);
9137}
9138
9139/* create a new cpu accounting group */
9140static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309141 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009142{
9143 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9144
9145 if (!ca)
9146 return ERR_PTR(-ENOMEM);
9147
9148 ca->cpuusage = alloc_percpu(u64);
9149 if (!ca->cpuusage) {
9150 kfree(ca);
9151 return ERR_PTR(-ENOMEM);
9152 }
9153
9154 return &ca->css;
9155}
9156
9157/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009158static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309159cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009160{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309161 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009162
9163 free_percpu(ca->cpuusage);
9164 kfree(ca);
9165}
9166
9167/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309168static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009169{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309170 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009171 u64 totalcpuusage = 0;
9172 int i;
9173
9174 for_each_possible_cpu(i) {
9175 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9176
9177 /*
9178 * Take rq->lock to make 64-bit addition safe on 32-bit
9179 * platforms.
9180 */
9181 spin_lock_irq(&cpu_rq(i)->lock);
9182 totalcpuusage += *cpuusage;
9183 spin_unlock_irq(&cpu_rq(i)->lock);
9184 }
9185
9186 return totalcpuusage;
9187}
9188
Dhaval Giani0297b802008-02-29 10:02:44 +05309189static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9190 u64 reset)
9191{
9192 struct cpuacct *ca = cgroup_ca(cgrp);
9193 int err = 0;
9194 int i;
9195
9196 if (reset) {
9197 err = -EINVAL;
9198 goto out;
9199 }
9200
9201 for_each_possible_cpu(i) {
9202 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9203
9204 spin_lock_irq(&cpu_rq(i)->lock);
9205 *cpuusage = 0;
9206 spin_unlock_irq(&cpu_rq(i)->lock);
9207 }
9208out:
9209 return err;
9210}
9211
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009212static struct cftype files[] = {
9213 {
9214 .name = "usage",
9215 .read_uint = cpuusage_read,
Dhaval Giani0297b802008-02-29 10:02:44 +05309216 .write_uint = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009217 },
9218};
9219
Dhaval Giani32cd7562008-02-29 10:02:43 +05309220static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009221{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309222 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009223}
9224
9225/*
9226 * charge this task's execution time to its accounting group.
9227 *
9228 * called with rq->lock held.
9229 */
9230static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9231{
9232 struct cpuacct *ca;
9233
9234 if (!cpuacct_subsys.active)
9235 return;
9236
9237 ca = task_ca(tsk);
9238 if (ca) {
9239 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9240
9241 *cpuusage += cputime;
9242 }
9243}
9244
9245struct cgroup_subsys cpuacct_subsys = {
9246 .name = "cpuacct",
9247 .create = cpuacct_create,
9248 .destroy = cpuacct_destroy,
9249 .populate = cpuacct_populate,
9250 .subsys_id = cpuacct_subsys_id,
9251};
9252#endif /* CONFIG_CGROUP_CPUACCT */